diff options
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. * |