narkisr / genericsfaction

Making Java Generics meta data available on runtime

This URL has Read+Write access

narkisr (author)
Sat Jul 04 07:46:42 -0700 2009
commit  de677bdf7bc95ac4ea47df4e1e92efe4c2c71372
tree    e36e256f303c7d20a563abca0729ee4310bb2b17
parent  56b6fc08ec541357ebb48420f67ce734ea42df89
README.textile

Type erasure removes all type meta data from Java’s byte code, this means that the following is valid in runtime:

public static void addTo(List list, Object value){
    list.add(value);
}

public static void main(String [] args){
List list = new ArrayList();
addTo(list,“not legel”);// there is no way to validated this on runtime
}

This project attempts to make types meta data available in runtime by enriching the AST before the source code gets compiled.

At the moment there are two types of enrichments:

  1. @GenMeta annotation which is added on method input parameters:
public void twoTypeParamsMet(@GenMeta(stringRep = "((Object ()) (Object ()))", classes = { Object.class, Object.class }) Map<Object, Object> map) {
   // method code
}
  1. Proxy meta which is added on initialization expressions (annotation on local variable are not retained) :
 public static void main(String [] args) {
   List<Integer> list = (List <Integer>)MetaProxy.newInstance(new ArrayList<Integer>(),"(Integer ())",new Class []{Integer.class});
 }

Note that meta is added only for resolvable type (no wild card types).

Usage TBD ..