Tag: bug


String class broken in latest JDK

September 30th, 2010 — 11:32am

Java bug 6935535:

  1. public class Test {
  2.   public static void main(String args[]) {
  3.  
  4.     String str = "1111111111111xx1111111111111xx1x";
  5.     str = str.substring(0, 31);
  6.     String query = "1111111111111xx1x";
  7.     int idx = str.indexOf(query);
  8.     System.out.println("IndexOf(" + query + ") = " + idx + " in " + str);
  9.   }
  10. }
javac Test.java

# Java interpreter
java Test
IndexOf(1111111111111xx1x) = -1 in 1111111111111xx1111111111111xx1

# BUG !! When HotSpot compiler is on
java -Xcompile Test
IndexOf(1111111111111xx1x) = 15 in 1111111111111xx1111111111111xx1

Seen on both Sun JDK 1.6.0_20 and OpenJDK 1.6.0_18

Update: This bug is fixed in Sun JDK 1.6.0_22

Comment » | Software

Nasty bug with generics and introspection

April 19th, 2010 — 5:15pm
public class Test {
  1.  
  2.   public static interface Foo {
  3.     public T getX();
  4.   }
  5.  
  6.   public static class Bar implements Foo {
  7.     public String getX() {
  8.       return "Hello World";
  9.     }
  10.   }
  11.  
  12.   public static void main(String[] args) {
  13.     Method[] methods = Bar.class.getDeclaredMethods();
  14.  
  15.     for (Method method : methods) {
  16.       System.out.println(method.getReturnType().toString()+" "+method.getName());
  17.     }
  18.   }
  19. }

Will return :

class java.lang.String getX
  1. class java.lang.Object getX

A) It’s illegal in Java to have two methods with the same signature returning different types.

B) The order in which these methods are returned would be completely random. For example, this can cause BeanUtils.copyProperties(..) to intermittently fail to copy some bean properties. BeanUtils would take the first get method returned, find that the return type is not matching corresponding set method and skip it.

The bug is present in both Java 5 and 6. There are several bugs filed around this problem. For example: 6422403 and 6528714. The bad news is that this is not going to be fixed until Java 7.

Update:
This is not a bug but a feature. One of the methods returned by introspection is synthetic. Unfortuntely, code written for Java prior to 1.5 like Commons BeanUtils do not recognize this and will break in described situation.

Comment » | Software

Back to top