Skip to content

Commit

Permalink
implemented JASSIST-170
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@639 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
  • Loading branch information
chiba committed Jun 17, 2012
1 parent 08deb0e commit e2cfbea
Show file tree
Hide file tree
Showing 10 changed files with 546 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ <h2>Changes</h2>
<p>-version 3.17
<ul>
<li>OSGi bundle info is now included in the jar file.
<li>JIRA JASSIST-166, 168 have been fixed.
<li>JIRA JASSIST-166, 168, 170 have been fixed.
</ul>

<p>-version 3.16.1 on March 6, 2012
Expand Down
36 changes: 30 additions & 6 deletions src/main/javassist/CtBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,19 +314,43 @@ CtClass getReturnType0() throws NotFoundException {
*
* <p>Note that the returned string is not the type signature
* contained in the <code>SignatureAttirbute</code>. It is
* a descriptor. To obtain a type signature, call the following
* methods:
*
* <ul><pre>getMethodInfo().getAttribute(SignatureAttribute.tag)
* </pre></ul>
* a descriptor.
*
* @see javassist.bytecode.Descriptor
* @see javassist.bytecode.SignatureAttribute
* @see #getGenericSignature()
*/
public String getSignature() {
return methodInfo.getDescriptor();
}

/**
* Returns the generic signature of the method.
* It represents parameter types including type variables.
*
* @see SignatureAttribute#toMethodSignature(String)
* @since 3.17
*/
public String getGenericSignature() {
SignatureAttribute sa
= (SignatureAttribute)methodInfo.getAttribute(SignatureAttribute.tag);
return sa == null ? null : sa.getSignature();
}

/**
* Set the generic signature of the method.
* It represents parameter types including type variables.
* See {@link javassist.CtClass#setGenericSignature(String)}
* for a code sample.
*
* @param sig a new generic signature.
* @see javassist.bytecode.SignatureAttribute.MethodSignature#encode()
* @since 3.17
*/
public void setGenericSignature(String sig) {
declaringClass.checkModify();
methodInfo.addAttribute(new SignatureAttribute(methodInfo.getConstPool(), sig));
}

/**
* Obtains exceptions that this method/constructor may throw.
*
Expand Down
84 changes: 84 additions & 0 deletions src/main/javassist/CtClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javassist.bytecode.ClassFile;
import javassist.bytecode.Descriptor;
import javassist.bytecode.Opcode;
import javassist.bytecode.SignatureAttribute;
import javassist.expr.ExprEditor;

/* Note:
Expand Down Expand Up @@ -387,6 +388,89 @@ public void setName(String name) {
qualifiedName = name;
}

/**
* 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()); // &lt;T:Ljava/lang/Object;&gt;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.
Expand Down
13 changes: 13 additions & 0 deletions src/main/javassist/CtClassType.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import javassist.bytecode.InnerClassesAttribute;
import javassist.bytecode.MethodInfo;
import javassist.bytecode.ParameterAnnotationsAttribute;
import javassist.bytecode.SignatureAttribute;
import javassist.bytecode.annotation.Annotation;
import javassist.compiler.AccessorMaker;
import javassist.compiler.CompileError;
Expand Down Expand Up @@ -336,6 +337,18 @@ public void setName(String name) throws RuntimeException {
classPool.classNameChanged(oldname, this);
}

public String getGenericSignature() {
SignatureAttribute sa
= (SignatureAttribute)getClassFile2().getAttribute(SignatureAttribute.tag);
return sa == null ? null : sa.getSignature();
}

public void setGenericSignature(String sig) {
ClassFile cf = getClassFile();
SignatureAttribute sa = new SignatureAttribute(cf.getConstPool(), sig);
cf.addAttribute(sa);
}

public void replaceClassName(ClassMap classnames)
throws RuntimeException
{
Expand Down
36 changes: 30 additions & 6 deletions src/main/javassist/CtField.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,43 @@ private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundExce
*
* <p>Note that the returned string is not the type signature
* contained in the <code>SignatureAttirbute</code>. It is
* a descriptor. To obtain a type signature, call the following
* methods:
*
* <ul><pre>getFieldInfo().getAttribute(SignatureAttribute.tag)
* </pre></ul>
* a descriptor.
*
* @see javassist.bytecode.Descriptor
* @see javassist.bytecode.SignatureAttribute
* @see #getGenericSignature()
*/
public String getSignature() {
return fieldInfo.getDescriptor();
}

/**
* Returns the generic signature of the field.
* It represents a type including type variables.
*
* @see SignatureAttribute#toFieldSignature(String)
* @since 3.17
*/
public String getGenericSignature() {
SignatureAttribute sa
= (SignatureAttribute)fieldInfo.getAttribute(SignatureAttribute.tag);
return sa == null ? null : sa.getSignature();
}

/**
* Set the generic signature of the field.
* It represents a type including type variables.
* See {@link javassist.CtClass#setGenericSignature(String)}
* for a code sample.
*
* @param sig a new generic signature.
* @see javassist.bytecode.SignatureAttribute.ObjectType#encode()
* @since 3.17
*/
public void setGenericSignature(String sig) {
declaringClass.checkModify();
fieldInfo.addAttribute(new SignatureAttribute(fieldInfo.getConstPool(), sig));
}

/**
* Returns the type of the field.
*/
Expand Down
23 changes: 23 additions & 0 deletions src/main/javassist/CtMember.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public Object[] getAnnotations()
public String getSignature() { return null; }
public void setAttribute(String name, byte[] data) {}
public void setModifiers(int mod) {}
public String getGenericSignature() { return null; }
public void setGenericSignature(String sig) {}

private CtMember methodTail;
private CtMember consTail; // constructor tail
Expand Down Expand Up @@ -269,6 +271,27 @@ else if (Modifier.isPrivate(mod))
*/
public abstract String getSignature();

/**
* Returns the generic signature of the member.
*
* @see javassist.bytecode.SignatureAttribute#toFieldSignature(String)
* @see javassist.bytecode.SignatureAttribute#toMethodSignature(String)
* @see CtClass#getGenericSignature()
* @since 3.17
*/
public abstract String getGenericSignature();

/**
* Sets the generic signature of the member.
*
* @param sig a new generic signature.
* @see javassist.bytecode.SignatureAttribute.ObjectType#encode()
* @see javassist.bytecode.SignatureAttribute.MethodSignature#encode()
* @see CtClass#setGenericSignature(String)
* @since 3.17
*/
public abstract void setGenericSignature(String sig);

/**
* Obtains a user-defined attribute with the given name.
* If that attribute is not found in the class file, this
Expand Down
Loading

0 comments on commit e2cfbea

Please sign in to comment.