diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2012-10-04 07:37:39 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2012-10-04 07:37:39 +0000 |
commit | 5a4853ee87743b72a5a0c1486f3af4ce04ae8774 (patch) | |
tree | b90b93aa0e098efc2fd3a0df4a2c1cd6ee07e8f5 /src/test/javassist | |
parent | c3329e418ee0a8eea563ace2fd483630527a1080 (diff) | |
download | javassist-5a4853ee87743b72a5a0c1486f3af4ce04ae8774.tar.gz javassist-5a4853ee87743b72a5a0c1486f3af4ce04ae8774.zip |
fixed JASSIST-174
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@669 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/test/javassist')
-rw-r--r-- | src/test/javassist/JvstTest4.java | 11 | ||||
-rw-r--r-- | src/test/javassist/bytecode/BytecodeTest.java | 46 | ||||
-rw-r--r-- | src/test/javassist/bytecode/StackMapTest.java | 165 |
3 files changed, 221 insertions, 1 deletions
diff --git a/src/test/javassist/JvstTest4.java b/src/test/javassist/JvstTest4.java index 3ff6927d..76f139bd 100644 --- a/src/test/javassist/JvstTest4.java +++ b/src/test/javassist/JvstTest4.java @@ -662,6 +662,7 @@ public class JvstTest4 extends JvstTestRoot { } public void testJIRA150b() throws Exception { + int origSize = javassist.compiler.MemberResolver.getInvalidMapSize(); int N = 100; for (int k = 0; k < N; k++) { ClassPool pool = new ClassPool(true); @@ -681,12 +682,20 @@ public class JvstTest4 extends JvstTestRoot { " int n5 = java.lang.Integer#valueOf(5); " + " return n1+n2+n3+n4+n5; }"); } + pool = null; + } + + // try to run garbage collection. + int[] large; + for (int i = 0; i < 100; i++) { + large = new int[1000000]; + large[large.length - 2] = 9; } System.gc(); System.gc(); int size = javassist.compiler.MemberResolver.getInvalidMapSize(); System.out.println("JIRA150b " + size); - assertTrue("JIRA150b size: " + size, size < N - 10); + assertTrue("JIRA150b size: " + origSize + " " + size, size < origSize + N); } public void testJIRA152() throws Exception { diff --git a/src/test/javassist/bytecode/BytecodeTest.java b/src/test/javassist/bytecode/BytecodeTest.java index c31e3965..e37529a0 100644 --- a/src/test/javassist/bytecode/BytecodeTest.java +++ b/src/test/javassist/bytecode/BytecodeTest.java @@ -779,6 +779,52 @@ public class BytecodeTest extends TestCase { assertEquals("[Ltest.Bar2;", cp.getClassInfo(n8)); } + public void testInvokeDynamic() throws Exception { + CtClass cc = loader.get("test4.InvokeDyn"); + ClassFile cf = cc.getClassFile(); + ConstPool cp = cf.getConstPool(); + + Bytecode code = new Bytecode(cp, 0, 1); + code.addAload(0); + code.addIconst(9); + code.addLdc("nine"); + code.addInvokedynamic(0, "call", "(ILjava/lang/String;)I"); + code.addOpcode(Opcode.SWAP); + code.addOpcode(Opcode.POP); + code.addOpcode(Opcode.IRETURN); + + MethodInfo minfo = new MethodInfo(cp, "test", "()I"); + minfo.setCodeAttribute(code.toCodeAttribute()); + minfo.setAccessFlags(AccessFlag.PUBLIC); + minfo.rebuildStackMapIf6(loader, cf); + cf.addMethod(minfo); + + cf.addMethod(new MethodInfo(cp, "test2", minfo, null)); + int mtIndex = cp.addMethodTypeInfo(cp.addUtf8Info("(I)V")); + + String desc + = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)" + + "Ljava/lang/invoke/CallSite;"; + int mri = cp.addMethodrefInfo(cp.addClassInfo(cc.getName()), "boot", desc); + int mhi = cp.addMethodHandleInfo(ConstPool.REF_invokeStatic, mri); + int[] args = new int[0]; + BootstrapMethodsAttribute.BootstrapMethod[] bms + = new BootstrapMethodsAttribute.BootstrapMethod[1]; + bms[0] = new BootstrapMethodsAttribute.BootstrapMethod(mhi, args); + + cf.addAttribute(new BootstrapMethodsAttribute(cp, bms)); + cc.writeFile(); + Object obj = make(cc.getName()); + assertEquals(9, invoke(obj, "test")); + + ClassPool cp2 = new ClassPool(); + cp2.appendClassPath("."); + CtClass cc2 = cp2.get(cc.getName()); + assertEquals("test4.InvokeDyn", cc2.getClassFile().getName()); + ConstPool cPool2 = cc2.getClassFile().getConstPool(); + assertEquals("(I)V", cPool2.getUtf8Info(cPool2.getMethodTypeInfo(mtIndex))); + } + public static void main(String[] args) { // junit.textui.TestRunner.run(suite()); junit.awtui.TestRunner.main(new String[] { diff --git a/src/test/javassist/bytecode/StackMapTest.java b/src/test/javassist/bytecode/StackMapTest.java index c7a77cb8..4b9339d7 100644 --- a/src/test/javassist/bytecode/StackMapTest.java +++ b/src/test/javassist/bytecode/StackMapTest.java @@ -423,6 +423,171 @@ public class StackMapTest extends TestCase { } } + public void testSwitchCase3() throws Exception { + CtClass cc = loader.get("javassist.bytecode.StackMapTest$T7c"); + StringBuffer sbuf = new StringBuffer("String s;"); + for (int i = 0; i < 130; i++) + sbuf.append("s =\"" + i + "\";"); + + cc.getDeclaredMethod("foo").insertBefore(sbuf.toString()); + cc.getDeclaredMethod("test2").setBody(loader.get("javassist.bytecode.StackMapTest$T8c").getDeclaredMethod("test2"), null); + + cc.writeFile(); + Object t1 = make(cc.getName()); + assertEquals(100, invoke(t1, "test")); + } + + public static class T7c { + int value = 1; + T7b t7; + public static T7b make2() { return null; } + public static void print(String s) {} + public int foo() { return 1; } + public int test() { return test2(10); } + public int test2(int k) { return 0; } + } + + public static class T8c { + public int test2(int k) { + int jj = 50; + if (k > 0) + k += "fooo".length(); + + int j = 50; + loop: for (int i = 0; i < 10; i++) { + int jjj = 1; + switch (i) { + case 0: + k++; + { int poi = 0; } + break; + case 9: + break loop; + case 7: + k += jjj; + break; + } + } + return j + jj; + } + } + + public void testSwitchCase4() throws Exception { + CtClass cc = loader.get("javassist.bytecode.StackMapTest$T8e"); + StringBuffer sbuf = new StringBuffer("String s;"); + for (int i = 0; i < 130; i++) + sbuf.append("s =\"" + i + "\";"); + + cc.getDeclaredMethod("foo").insertBefore(sbuf.toString()); + CtClass orig = loader.get("javassist.bytecode.StackMapTest$T8d"); + CtMethod origM = orig.getDeclaredMethod("test2"); + writeLdcw(origM); + cc.getDeclaredMethod("test2").setBody(origM, null); + + orig.writeFile(); + cc.writeFile(); + Object t1 = make(cc.getName()); + assertEquals(100, invoke(t1, "test")); + } + + private void writeLdcw(CtMethod method) { + CodeAttribute ca = method.getMethodInfo().getCodeAttribute(); + int index = ca.getConstPool().addStringInfo("ldcw"); + CodeIterator ci = ca.iterator(); + ci.writeByte(Opcode.LDC_W, 14); + ci.write16bit(index, 15); + ci.writeByte(Opcode.LDC_W, 43); + ci.write16bit(index, 44); + } + + public static class T8e { + static T8dd helper() { return new T8dd(); } + int integer() { return 9; } + boolean integer2(String s) { return true; } + static void print(String s) {} + private void print2(String s) {} + private Object func(String s) { return null; } + I8d func2(String s) { return null; } + static String ldcw() { return "k"; } + static boolean debug = false; + void log(String p1,String p2,String p3,Long p4, Object p5, Object p6) {} + + public int foo() { return 1; } + public int test() { + try { + return test2("", 10) ? 1 : 0; + } catch (Exception e) {} + return 100; + } + public boolean test2(String s, int k) throws Exception { return false; } + } + + public static interface I8d { + String foo(String s, int i); + } + + public static class T8dd { + java.util.List foo(String s) { return new java.util.ArrayList(); } + } + public static class T8d { + static T8dd helper() { return new T8dd(); } + int integer() { return 9; } + boolean integer2(String s) { return true; } + static void print(String s) {} + private void print2(String s) {} + private Object func(String s) { return null; } + I8d func2(String s) { return null; } + static String ldcw() { return "k"; } + static boolean debug = false; + void log(String p1,String p2,String p3,Long p4, Object p5, Object p6) {} + + public boolean test2(String s, int i) throws Exception { + String k = null; + Object k2 = null; + boolean v5 = true; + if (!debug) + print(ldcw()); + + Object k3 = this.func(s); + if (k3 == null) + throw new Exception(new StringBuilder().append(ldcw()).append(s).append(",").toString()); + + String v7 = k3.toString(); + k2 = this.func2(v7); + if (k2 != null) { // 82: + if (k2 instanceof I8d) { + I8d k5 = (I8d)k2; + k = k5.foo(s, i); + } + } + + java.util.List list = helper().foo(v7); // local 8 + for (int v9 = 0; v9 < list.size(); v9++) { + boolean v10 = true; + T8d v11 = (T8d)list.get(v9); + switch (v11.integer()) { + case 1: + break; + case 2: + v10 = this.integer2(s); + v5 &= v10; + break; + default : + throw new Exception(new StringBuilder().append("ldc 189").append(v11.integer()).append("ldc 169").toString()); + } + } + + if (v5) // 246: + this.print2(s); + if (v5) + this.log(ldcw(), v7, s, Long.valueOf(new Integer(i).longValue()), k, null); + else // 290: + this.log(ldcw(), v7, s, Long.valueOf(new Integer(i).longValue()), k, null); + + return v5; + } + } + public void tstCtClassType() throws Exception { ClassPool cp = ClassPool.getDefault(); CtClass cc = cp.get("javassist.CtClassType"); |