Infer Generic Type Arguments", eclipse will do rest for you:Ant:Generic:Enum:Mystery bug from autoboxing/unboxing:繼續學..."> Infer Generic Type Arguments", eclipse will do rest for you:Ant:Generic:Enum:Mystery bug from autoboxing/unboxing:繼續學..."> Infer Generic Type Arguments", eclipse will do rest for you:Ant:Generic:Enum:Mystery bug from autoboxing/unboxing:繼續學...">

08 August 2005

雜記...

Eclipse:

  • Enable jdk 5 compliance mode: window -> preferences -> java -> compiler -> Compiler compliance Level -> 5.0
  • refactoring old code to generic:
    //Old code:
    List l = new ArrayList();
    for (Iterator iter = l.iterator(); iter.hasNext();) {
      String element = (String) iter.next();
    }
    
    //change to:
    List<String> l = new ArrayList();
    for (Iterator iter = l.iterator(); iter.hasNext();) {
      String element = (String) iter.next();
    }
    

    then select eclipse menu: "Refactor -> Infer Generic Type Arguments", eclipse will do rest for you:

    List<String> l = new ArrayList<String>();
    for (Iterator<String> iter = l.iterator(); iter.hasNext();) {
       String element = iter.next();
    }
    

Ant:

  • Enable jdk5 mode for javac task:
      <javac source="1.5" >
         ...
    

Generic:

  • Erasure -- Generic type information will be "erased" after compile (ByteCode does not contain any type information, hence we can not do reflection on generic type)
  • Due to above reason, we can not cast generic type:
    List<Integer> ints = new LinkedList<Integer>();
    List<Number> nums = ints; // compile error.
    
  • Thus, we need to use wildcard <?> to support more types, however, <?> is a read-only type:
    List<?> list = new ArrayList(); // image ? is a "unknown" type 
    list.get() ; // return unknown type, here is Object 
    list.add("1"); // compile error, add() method can not process unknown type.
    
  • Considering wildcard <?> is a kind of concrete type, just like <Integer>,<String>...etc. It's not type for declaration
  • static variable is not allowed to declared with generic type
    //compile error, because static variable is shared for all instances. But every instance
    //has its own generic type.
    private static List<T> staticList = new ArrayList<T>();
    

Enum:

  • Enum can not do subclass, but can implement interface.
  • Always use .name() instead of .toString() to determine Enum.
  • Mapping Enum as a value type in Hibernate: Enum CustomUserType
  • Hibernate Customed Enum Type with join-table (many-to-many for Enum)
    <typedef name="Gender" class="GenericEnumUserType">
       <param name="enumClass">Gender</param>
    </typedef>
    
    <set name="genders" table="room_genders" >
        <key column="room_id" />
        <element type="Gender" column="gender"  />
    </set>
    

Mystery bug from autoboxing/unboxing:

  • Null
    Integer i = null ;
    int j = i ; //throw NullPointException
    
  • Object Identity
    Integer i1 = 256 ;
    Integer i2 = 256 ;
    assertFalse(i1 == i2) ; // not always true
    
    Integer i3 = 90 ;
    Integer i4 = 90 ;
    assertTrue(i3 == i4) ; // sun jdk implementation reuse Integer instance within -128~127
    

繼續學...


回響

可以用 Tag <I>、<B>,程式碼請用 <PRE>