From 721ab4e11e031a4667623cbdec9c24502a13d905 Mon Sep 17 00:00:00 2001 From: chiba Date: Sun, 17 Jun 2012 15:57:19 +0000 Subject: implemented JASSIST-170 git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@640 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- .../javassist/bytecode/SignatureAttribute.java | 32 ++++++++++++++-------- src/test/javassist/bytecode/BytecodeTest.java | 28 +++++++++++++++++++ 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/main/javassist/bytecode/SignatureAttribute.java b/src/main/javassist/bytecode/SignatureAttribute.java index 9e6921c5..4ebb457d 100644 --- a/src/main/javassist/bytecode/SignatureAttribute.java +++ b/src/main/javassist/bytecode/SignatureAttribute.java @@ -182,14 +182,14 @@ public class SignatureAttribute extends AttributeInfo { /** * Constructs a class signature. * - * @param p type parameters. - * @param s the super class. - * @param i the interface types. + * @param params type parameters. + * @param superClass the super class. + * @param interfaces the interface types. */ - public ClassSignature(TypeParameter[] p, ClassType s, ClassType[] i) { - params = p == null ? new TypeParameter[0] : p; - superClass = s == null ? ClassType.OBJECT : s; - interfaces = i == null ? new ClassType[0] : i; + public ClassSignature(TypeParameter[] params, ClassType superClass, ClassType[] interfaces) { + this.params = params == null ? new TypeParameter[0] : params; + this.superClass = superClass == null ? ClassType.OBJECT : superClass; + this.interfaces = interfaces == null ? new ClassType[0] : interfaces; } /** @@ -273,13 +273,13 @@ public class SignatureAttribute extends AttributeInfo { * to represent void or nothing. * * @param tp type parameters. - * @param p parameter types. - * @param ret a return type. + * @param params parameter types. + * @param ret a return type, or null if the return type is void. * @param ex exception types. */ - public MethodSignature(TypeParameter[] tp, Type[] p, Type ret, ObjectType[] ex) { + public MethodSignature(TypeParameter[] tp, Type[] params, Type ret, ObjectType[] ex) { typeParams = tp == null ? new TypeParameter[0] : tp; - params = p == null ? new Type[0] : p; + this.params = params == null ? new Type[0] : params; retType = ret == null ? new BaseType("void") : ret; exceptions = ex == null ? new ObjectType[0] : ex; } @@ -688,6 +688,16 @@ public class SignatureAttribute extends AttributeInfo { arguments = args; } + /** + * Constructs a ClassType. It represents + * the name of a non-nested class. + * + * @param className a fully qualified class name. + */ + public ClassType(String className) { + this(className, null); + } + /** * Returns the class name. */ diff --git a/src/test/javassist/bytecode/BytecodeTest.java b/src/test/javassist/bytecode/BytecodeTest.java index 4b88852b..c31e3965 100644 --- a/src/test/javassist/bytecode/BytecodeTest.java +++ b/src/test/javassist/bytecode/BytecodeTest.java @@ -5,6 +5,7 @@ import java.lang.reflect.Method; import junit.framework.*; import javassist.*; import javassist.bytecode.annotation.*; +import javassist.bytecode.SignatureAttribute.*; public class BytecodeTest extends TestCase { public static final String PATH = JvstTest.PATH; @@ -488,6 +489,33 @@ public class BytecodeTest extends TestCase { assertEquals(result, r); } + public void testSignatureEncode() throws Exception { + BaseType bt = new BaseType("int"); + TypeVariable tv = new TypeVariable("S"); + ArrayType at = new ArrayType(1, tv); + ClassType ct1 = new ClassType("test.Foo"); + TypeArgument ta = new TypeArgument(); + TypeArgument ta2 = new TypeArgument(ct1); + TypeArgument ta3 = TypeArgument.subclassOf(ct1); + ClassType ct2 = new ClassType("test.Foo", new TypeArgument[] { ta, ta2, ta3 }); + ClassType ct3 = new ClassType("test.Bar"); + ClassType ct4 = new ClassType("test.Bar", new TypeArgument[] { ta }); + NestedClassType ct5 = new NestedClassType(ct4, "Baz", new TypeArgument[] { ta }); + TypeParameter tp1 = new TypeParameter("U"); + TypeParameter tp2 = new TypeParameter("V", ct1, new ObjectType[] { ct3 }); + ClassSignature cs = new ClassSignature(new TypeParameter[] { tp1 }, + ct1, + new ClassType[] { ct2 }); + MethodSignature ms = new MethodSignature(new TypeParameter[] { tp1, tp2 }, + new Type[] { bt, at, ct5 }, ct3, + new ObjectType[] { ct1, tv }); + + assertEquals("Ltest/Foo;Ltest/Foo<*Ltest/Foo;+Ltest/Foo;>;", + cs.encode()); + assertEquals("(I[TS;Ltest/Bar<*>$Baz<*>;)Ltest/Bar;^Ltest/Foo;^TS;", + ms.encode()); + } + public void testModifiers() throws Exception { CtClass c = loader.get("test3.Mods"); c.setModifiers(Modifier.PROTECTED); -- cgit v1.2.3