diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2012-06-17 15:01:27 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2012-06-17 15:01:27 +0000 |
commit | e2cfbeaed7be298c93251f6f9af480cdba84f68f (patch) | |
tree | 8b49af3c3a31cde77e252c5327900fbb0d628505 /src/main/javassist/CtClass.java | |
parent | 08deb0e6bc17f84f6bda03a29a7c2d3a8610a1ef (diff) | |
download | javassist-e2cfbeaed7be298c93251f6f9af480cdba84f68f.tar.gz javassist-e2cfbeaed7be298c93251f6f9af480cdba84f68f.zip |
implemented JASSIST-170
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@639 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist/CtClass.java')
-rw-r--r-- | src/main/javassist/CtClass.java | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 7f115d0d..bcf61e41 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -29,6 +29,7 @@ import java.util.Collection; import javassist.bytecode.ClassFile; import javassist.bytecode.Descriptor; import javassist.bytecode.Opcode; +import javassist.bytecode.SignatureAttribute; import javassist.expr.ExprEditor; /* Note: @@ -388,6 +389,89 @@ public abstract class CtClass { } /** + * Returns the generic signature of the class. + * + * <p>The generics of Java is implemented by the erasure technique. + * After compilation, all type parameters are dropped off from the + * main part of a class file. However, for reflection, the type + * parameters are encoded into generic signatures and attached + * to a class file. + * + * @return null if the generic signature is not included. + * @see javassist.bytecode.SignatureAttribute#toClassSignature(String) + * @see CtMember#getGenericSignature() + * @since 3.17 + */ + public String getGenericSignature() { return null; } + + /** + * Sets the generic signature of the class. + * + * <p>The generics of Java is implemented by the erasure technique. + * After compilation, all type parameters are dropped off from the + * main part of a class file. However, for reflection, the type + * parameters must be encoded into generic signatures and attached + * to a class file. + * + * <p>For example, + * + * <pre>class List<T> { + * T value; + * T get() { return value; } + * void set(T v) { value = v; } + * } + * </pre> + * + * <p>this class is generated by the following code: + * + * <pre> + * ClassPool pool = ClassPool.getDefault(); + * CtClass cc = pool.makeClass("List"); + * CtClass objectClass = pool.get(CtClass.javaLangObject); + * ClassSignature cs = new ClassSignature( + * new TypeParameter[] { new TypeParameter("T") }); + * cc.setGenericSignature(cs.encode()); // <T:Ljava/lang/Object;>Ljava/lang/Object; + * + * CtField f = new CtField(objClass, "value", cc); + * TypeVariable tvar = new TypeVariable("T"); + * f.setGenericSignature(tvar.encode()); // TT; + * cc.addField(f); + * + * CtMethod m = CtNewMethod.make("public Object get(){return value;}", cc); + * MethodSignature ms = new MethodSignature(null, null, tvar, null); + * m.setGenericSignature(ms.encode()); // ()TT; + * cc.addMethod(m); + * + * CtMethod m2 = CtNewMethod.make("public void set(Object v){value = v;}", cc); + * MethodSignature ms2 = new MethodSignature(null, new Type[] { tvar }, + * new BaseType("void"), null); + * m2.setGenericSignature(ms2.encode()); // (TT;)V; + * cc.addMethod(m2); + * + * cc.writeFile(); + * </pre> + * + * <p>The generated class file is equivalent to the following: + * + * <pre>class List { + * Object value; + * Object get() { return value; } + * void set(Object v) { value = v; } + * }</pre> + * + * <p>but it includes generic signatures for the class, the field, + * and the methods so that the type variable <code>T</code> can be + * accessible through reflection. + * + * @param sig a generic signature. + * @see javassist.bytecode.SignatureAttribute.ClassSignature#encode() + * @see javassist.bytecode.SignatureAttribute.MethodSignature#encode() + * @see CtMember#setGenericSignature(String) + * @since 3.17 + */ + public void setGenericSignature(String sig) { checkModify(); } + + /** * Substitutes <code>newName</code> for all occurrences of a class * name <code>oldName</code> in the class file. * |