diff options
Diffstat (limited to 'src/test/javassist')
-rw-r--r-- | src/test/javassist/JvstTest.java | 7 | ||||
-rw-r--r-- | src/test/javassist/JvstTest2.java | 3 | ||||
-rw-r--r-- | src/test/javassist/JvstTest3.java | 8 | ||||
-rw-r--r-- | src/test/javassist/JvstTest4.java | 3 | ||||
-rw-r--r-- | src/test/javassist/JvstTest5.java | 4 | ||||
-rw-r--r-- | src/test/javassist/SetterTest.java | 226 | ||||
-rw-r--r-- | src/test/javassist/bytecode/InsertGap0.java | 4 | ||||
-rw-r--r-- | src/test/javassist/tools/CallbackTest.java | 3 |
8 files changed, 133 insertions, 125 deletions
diff --git a/src/test/javassist/JvstTest.java b/src/test/javassist/JvstTest.java index e2f9211f..aab0b907 100644 --- a/src/test/javassist/JvstTest.java +++ b/src/test/javassist/JvstTest.java @@ -1,6 +1,8 @@ package javassist; import junit.framework.*; +import test1.DefineClassCapability; + import java.io.File; import java.io.FileInputStream; import java.io.InputStream; @@ -743,8 +745,8 @@ public class JvstTest extends JvstTestRoot { ctInterface.stopPruning(true); ctInterface.writeFile(); - ctInterface.toClass(); - targetCtClass.toClass(); + ctInterface.toClass(DefineClassCapability.class); + targetCtClass.toClass(DefineClassCapability.class); } public void testDispatch() throws Exception { @@ -1161,6 +1163,7 @@ public class JvstTest extends JvstTestRoot { suite.addTestSuite(javassist.SetterTest.class); suite.addTestSuite(javassist.bytecode.InsertGap0.class); suite.addTestSuite(javassist.tools.reflect.LoaderTest.class); + suite.addTestSuite(javassist.tools.CallbackTest.class); suite.addTestSuite(testproxy.ProxyTester.class); suite.addTestSuite(testproxy.ProxyFactoryPerformanceTest.class); // remove? suite.addTestSuite(javassist.proxyfactory.ProxyFactoryTest.class); diff --git a/src/test/javassist/JvstTest2.java b/src/test/javassist/JvstTest2.java index 117560c0..0ea4571b 100644 --- a/src/test/javassist/JvstTest2.java +++ b/src/test/javassist/JvstTest2.java @@ -9,6 +9,7 @@ import org.junit.runners.MethodSorters; import java.lang.reflect.Method; import javassist.expr.*; +import test2.DefineClassCapability; @SuppressWarnings({"rawtypes","unused"}) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @@ -709,7 +710,7 @@ public class JvstTest2 extends JvstTestRoot { public void testToClass() throws Exception { ClassPool cp = ClassPool.getDefault(); CtClass cc = cp.makeClass("test2.ToClassTest"); - Class c = cc.toClass(); + Class c = cc.toClass(DefineClassCapability.class); assertEquals(getClass().getClassLoader(), c.getClassLoader()); } diff --git a/src/test/javassist/JvstTest3.java b/src/test/javassist/JvstTest3.java index 47131a93..46f06b16 100644 --- a/src/test/javassist/JvstTest3.java +++ b/src/test/javassist/JvstTest3.java @@ -340,7 +340,7 @@ public class JvstTest3 extends JvstTestRoot { System.out.println("Num Annotation : " +ans.length); // c.debugWriteFile(); - Class newclass = c.toClass(); + Class newclass = c.toClass(DefineClassCapability.class); java.lang.annotation.Annotation[] anns = newclass.getAnnotations(); System.out.println("Num NewClass Annotation : " +anns.length); assertEquals(ans.length, anns.length); @@ -737,7 +737,7 @@ public class JvstTest3 extends JvstTestRoot { CtMethod m2 = cc2.getDeclaredMethod("getX"); copyAnnotations(m1, m2); cc2.getClassFile(); - Class clazz = cc2.toClass(); + Class clazz = cc2.toClass(DefineClassCapability.class); java.lang.reflect.Method m = clazz.getDeclaredMethod("getX", new Class[0]); assertEquals(1, m.getAnnotations().length); test3.VisibleAnno a = m.getAnnotation(test3.VisibleAnno.class); @@ -790,7 +790,7 @@ public class JvstTest3 extends JvstTestRoot { cc.addField(fobj, CtField.Initializer.constant("bar")); cc.writeFile(); - Class clazz = cc.toClass(); + Class clazz = cc.toClass(DefineClassCapability.class); assertEquals(2L, clazz.getField("j").getLong(null)); assertEquals(3, clazz.getField("i").getInt(null)); assertEquals(4, clazz.getField("s").getShort(null)); @@ -1108,7 +1108,7 @@ public class JvstTest3 extends JvstTestRoot { sb.append("}"); ctc.addMethod(CtNewMethod.make(sb.toString(), ctc)); ctc.debugWriteFile(); - ctc.toClass().getConstructor().newInstance(); + ctc.toClass(DefineClassCapability.class).getConstructor().newInstance(); } // JIRA-83 diff --git a/src/test/javassist/JvstTest4.java b/src/test/javassist/JvstTest4.java index 187abb3d..7c8c7457 100644 --- a/src/test/javassist/JvstTest4.java +++ b/src/test/javassist/JvstTest4.java @@ -1054,7 +1054,7 @@ public class JvstTest4 extends JvstTestRoot { addDeadCode(newClass, "public boolean evaluate7(){ return !true; }"); newClass.debugWriteFile(); - Class<?> cClass = newClass.toClass(); + Class<?> cClass = newClass.toClass(test4.DefineClassCapability.class); Object o = cClass.getConstructor().newInstance(); java.lang.reflect.Method m = cClass.getMethod("evaluate"); m.invoke(o); @@ -1112,6 +1112,7 @@ public class JvstTest4 extends JvstTestRoot { attr.setAnnotation(a); m.getMethodInfo().addAttribute(attr); cc.writeFile(); + anno.toClass(test4.DefineClassCapability.class); Class<?> rc = ((java.lang.annotation.Annotation)m.getAnnotations()[0]).annotationType(); assertEquals(anno.getName(), rc.getName()); } diff --git a/src/test/javassist/JvstTest5.java b/src/test/javassist/JvstTest5.java index ec8ff66a..c5eff4d1 100644 --- a/src/test/javassist/JvstTest5.java +++ b/src/test/javassist/JvstTest5.java @@ -223,7 +223,7 @@ public class JvstTest5 extends JvstTestRoot { "}"); System.out.println(src); badClass.addMethod(CtMethod.make(src, badClass)); - Class clazzz = badClass.toClass(); + Class clazzz = badClass.toClass(Class.forName("DefineClassCapability")); Object obj = clazzz.getConstructor().newInstance(); // <-- falls here } @@ -451,6 +451,6 @@ public class JvstTest5 extends JvstTestRoot { public void testNestHostAttributeCopy() throws Exception { CtClass cc = sloader.get("test5.NestHost2$Foo"); cc.getClassFile().compact(); - cc.toClass(); + cc.toClass(test5.DefineClassCapability.class); } } diff --git a/src/test/javassist/SetterTest.java b/src/test/javassist/SetterTest.java index 44eec140..a76b2a30 100644 --- a/src/test/javassist/SetterTest.java +++ b/src/test/javassist/SetterTest.java @@ -1,112 +1,114 @@ -package javassist;
-import java.lang.reflect.Method;
-
-import junit.framework.TestCase;
-
-@SuppressWarnings({"rawtypes","unchecked"})
-public class SetterTest extends TestCase {
-
- ClassPool pool;
-
- public SetterTest(String name) {
- super(name);
- }
-
- protected void setUp() throws Exception {
- super.setUp();
- pool = ClassPool.getDefault();
- }
-
- /**
- * Tests a getter only on a field without a Modifier.
- *
- * @throws Exception
- */
- public void testFieldGetter() throws Exception {
- CtClass clazz = pool.makeClass("HasFieldGetter");
- clazz.setSuperclass(pool.get("java.lang.Object"));
- CtField field = new CtField(CtClass.booleanType, "broken", clazz);
- clazz.addField(field, "true");
- clazz.addMethod(CtNewMethod.getter("isBroken", field));
- Class _class = clazz.toClass();
-
- Object object = _class.getConstructor().newInstance();
- check(_class, object, true);
- }
-
- /**
- * Tests a getter and a setter on a field without a Modifier.
- *
- * @throws Exception
- */
- public void testFieldGetterSetter() throws Exception {
- CtClass clazz = pool.makeClass("HasFieldGetterSetter");
- clazz.setSuperclass(pool.get("java.lang.Object"));
- CtField field = new CtField(CtClass.booleanType, "broken", clazz);
- clazz.addField(field, "true");
- clazz.addMethod(CtNewMethod.getter("isBroken", field));
- clazz.addMethod(CtNewMethod.setter("setBroken", field));
- Class _class = clazz.toClass();
-
- Object object = _class.getConstructor().newInstance();
-
- set(_class, object, false);
- check(_class, object, false);
- }
-
- /**
- * Tests a getter only on a field with Modifier.STATIC.
- *
- * @throws Exception
- */
- public void testStaticFieldGetter() throws Exception {
- CtClass clazz = pool.makeClass("HasStaticFieldGetter");
- clazz.setSuperclass(pool.get("java.lang.Object"));
- CtField field = new CtField(CtClass.booleanType, "broken", clazz);
- field.setModifiers(Modifier.STATIC);
- clazz.addField(field, "true");
- clazz.addMethod(CtNewMethod.getter("isBroken", field));
- Class _class = clazz.toClass();
-
- Object object = _class.getConstructor().newInstance();
- check(_class, object, true);
- }
-
- /**
- * Tests a getter and setter on a field with Modifier.STATIC.
- *
- * @throws Exception
- */
- public void testStaticFieldGetterSetter() throws Exception {
- CtClass clazz = pool.makeClass("HasStaticFieldGetterSetter");
- clazz.setSuperclass(pool.get("java.lang.Object"));
- CtField field = new CtField(CtClass.booleanType, "broken", clazz);
- field.setModifiers(Modifier.STATIC);
- clazz.addField(field, "true");
- clazz.addMethod(CtNewMethod.getter("isBroken", field));
- clazz.addMethod(CtNewMethod.setter("setBroken", field));
- Class _class = clazz.toClass();
-
- Object object = _class.getConstructor().newInstance();
-
- set(_class, object, false);
- check(_class, object, false);
- }
-
- private void check(Class _class, Object object, boolean shouldBe)
- throws Exception
- {
- Method method = _class.getMethod("isBroken", new Class[] {});
- Boolean result = (Boolean) method.invoke(object, new Object[] {});
- assertEquals("boolean is wrong value",
- shouldBe, result.booleanValue());
- }
-
- private void set(Class _class, Object object, boolean willBe)
- throws Exception
- {
- Method method = _class.getMethod("setBroken",
- new Class[] {Boolean.TYPE});
- method.invoke(object, new Object[] { Boolean.valueOf(willBe)});
- }
-}
+package javassist; +import java.lang.reflect.Method; + +import junit.framework.TestCase; + +@SuppressWarnings({"rawtypes","unchecked"}) +public class SetterTest extends TestCase { + + ClassPool pool; + Class<?> capability; + + public SetterTest(String name) { + super(name); + } + + protected void setUp() throws Exception { + super.setUp(); + pool = ClassPool.getDefault(); + capability = Class.forName("DefineClassCapability"); + } + + /** + * Tests a getter only on a field without a Modifier. + * + * @throws Exception + */ + public void testFieldGetter() throws Exception { + CtClass clazz = pool.makeClass("HasFieldGetter"); + clazz.setSuperclass(pool.get("java.lang.Object")); + CtField field = new CtField(CtClass.booleanType, "broken", clazz); + clazz.addField(field, "true"); + clazz.addMethod(CtNewMethod.getter("isBroken", field)); + Class _class = clazz.toClass(capability); + + Object object = _class.getConstructor().newInstance(); + check(_class, object, true); + } + + /** + * Tests a getter and a setter on a field without a Modifier. + * + * @throws Exception + */ + public void testFieldGetterSetter() throws Exception { + CtClass clazz = pool.makeClass("HasFieldGetterSetter"); + clazz.setSuperclass(pool.get("java.lang.Object")); + CtField field = new CtField(CtClass.booleanType, "broken", clazz); + clazz.addField(field, "true"); + clazz.addMethod(CtNewMethod.getter("isBroken", field)); + clazz.addMethod(CtNewMethod.setter("setBroken", field)); + Class _class = clazz.toClass(capability); + + Object object = _class.getConstructor().newInstance(); + + set(_class, object, false); + check(_class, object, false); + } + + /** + * Tests a getter only on a field with Modifier.STATIC. + * + * @throws Exception + */ + public void testStaticFieldGetter() throws Exception { + CtClass clazz = pool.makeClass("HasStaticFieldGetter"); + clazz.setSuperclass(pool.get("java.lang.Object")); + CtField field = new CtField(CtClass.booleanType, "broken", clazz); + field.setModifiers(Modifier.STATIC); + clazz.addField(field, "true"); + clazz.addMethod(CtNewMethod.getter("isBroken", field)); + Class _class = clazz.toClass(capability); + + Object object = _class.getConstructor().newInstance(); + check(_class, object, true); + } + + /** + * Tests a getter and setter on a field with Modifier.STATIC. + * + * @throws Exception + */ + public void testStaticFieldGetterSetter() throws Exception { + CtClass clazz = pool.makeClass("HasStaticFieldGetterSetter"); + clazz.setSuperclass(pool.get("java.lang.Object")); + CtField field = new CtField(CtClass.booleanType, "broken", clazz); + field.setModifiers(Modifier.STATIC); + clazz.addField(field, "true"); + clazz.addMethod(CtNewMethod.getter("isBroken", field)); + clazz.addMethod(CtNewMethod.setter("setBroken", field)); + Class _class = clazz.toClass(capability); + + Object object = _class.getConstructor().newInstance(); + + set(_class, object, false); + check(_class, object, false); + } + + private void check(Class _class, Object object, boolean shouldBe) + throws Exception + { + Method method = _class.getMethod("isBroken", new Class[] {}); + Boolean result = (Boolean) method.invoke(object, new Object[] {}); + assertEquals("boolean is wrong value", + shouldBe, result.booleanValue()); + } + + private void set(Class _class, Object object, boolean willBe) + throws Exception + { + Method method = _class.getMethod("setBroken", + new Class[] {Boolean.TYPE}); + method.invoke(object, new Object[] { Boolean.valueOf(willBe)}); + } +} diff --git a/src/test/javassist/bytecode/InsertGap0.java b/src/test/javassist/bytecode/InsertGap0.java index 824b9305..425f12d7 100644 --- a/src/test/javassist/bytecode/InsertGap0.java +++ b/src/test/javassist/bytecode/InsertGap0.java @@ -175,7 +175,7 @@ public final class InsertGap0 extends JvstTestRoot { cc.addField(new CtField(CtClass.intType, "i", cc), "++counter"); boolean p = cc.stopPruning(true); cc.writeFile(); - Class c = cc.toClass(); + Class c = cc.toClass(ClassFile.class); cc.stopPruning(p); Object obj = c.getConstructor().newInstance(); @@ -194,7 +194,7 @@ public final class InsertGap0 extends JvstTestRoot { cc.addField(new CtField(CtClass.intType, "i", cc), "++counter"); boolean p = cc.stopPruning(true); cc.writeFile(); - Class c = cc.toClass(); + Class c = cc.toClass(ClassFile.class); cc.stopPruning(p); Object obj = c.getConstructor().newInstance(); diff --git a/src/test/javassist/tools/CallbackTest.java b/src/test/javassist/tools/CallbackTest.java index 96632bda..f5861ceb 100644 --- a/src/test/javassist/tools/CallbackTest.java +++ b/src/test/javassist/tools/CallbackTest.java @@ -3,6 +3,7 @@ package javassist.tools; import javassist.*; import junit.framework.TestCase; import test.javassist.tools.DummyClass; +import test.javassist.tools.DefineClassCapability; import static javassist.tools.Callback.*; @@ -34,7 +35,7 @@ public class CallbackTest extends TestCase { }); // Change class and invoke method; - classToChange.toClass(); + classToChange.toClass(DefineClassCapability.class); new DummyClass().dummyMethod(); } } |