Reflection


The reflection package has been retrofitted to support providing parameter information for parameterized types and methods. For example, you can send the message getTypeParameters to a class in order to retrieve an array of TypeParameter objects; each TypeParameter object provides you with enough information to be able to reconstruct the type parameters.

To support these changes, Sun altered the Java byte code specification. Class files now store additional information about type parameters. Most significant, the Class class has been modified to be a parameterized type, Class<T>. The following assignment works:

 Class<String> klass = String.class; 

If you're interested in how you might use this, take a look at the source for the CheckedCollection class. It's a static inner class of java.util.Collections.

The reflection modifications provide you with information on the declaration of parameterized types and methods. What you will not get from reflection is information about the binding of a type variable. If you bind an ArrayList to a String, that information is not known to the ArrayList object because of the erasure scheme. Thus reflection has no way of providing it to you. It would be nice to be able to code

 public class MultiHashMap<K,V> {    ...    public Class<V> getKeyType() {       return V.class; // this will not work!    } } 

but it just won't work.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net