12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124 |
- package javassist;
-
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.HashSet;
-
- import org.junit.FixMethodOrder;
- import org.junit.runners.MethodSorters;
-
- import javassist.bytecode.*;
- import javassist.bytecode.annotation.Annotation;
- import javassist.expr.*;
-
- @SuppressWarnings({"rawtypes","unchecked","unused"})
- @FixMethodOrder(MethodSorters.NAME_ASCENDING)
- public class JvstTest4 extends JvstTestRoot {
- public JvstTest4(String name) {
- super(name);
- }
-
- public void testInsertLocalVars() throws Exception {
- CtClass cc = sloader.get("test4.LocalVars");
-
- CtMethod m1 = cc.getDeclaredMethod("run");
- m1.getMethodInfo().getCodeAttribute().insertLocalVar(2, 20);
- m1.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile());
- CtMethod m2 = cc.getDeclaredMethod("run2");
- m2.getMethodInfo().getCodeAttribute().insertLocalVar(2, 0x101);
- m2.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile());
-
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(10, invoke(obj, "run"));
- assertEquals(10, invoke(obj, "run2"));
- }
-
- public void testCodeConv() throws Exception {
- CtClass cc = sloader.get("test4.CodeConv");
- CtMethod m1 = cc.getDeclaredMethod("m1");
- CtMethod m2 = cc.getDeclaredMethod("m2");
- CtMethod m3 = cc.getDeclaredMethod("m3");
- CodeConverter conv = new CodeConverter();
- conv.insertAfterMethod(m1, m3);
- conv.insertBeforeMethod(m2, m3);
- cc.instrument(conv);
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(111033, invoke(obj, "run"));
- }
-
- public void testCodeConv2() throws Exception {
- CtClass cc = sloader.get("test4.CodeConv2");
- CtField f = cc.getDeclaredField("field");
- CtField f2 = cc.getDeclaredField("sf");
- CtMethod run = cc.getDeclaredMethod("run");
- CodeConverter conv = new CodeConverter();
- conv.replaceFieldRead(f, cc, "read");
- conv.replaceFieldWrite(f, cc, "write");
- conv.replaceFieldRead(f2, cc, "read");
- conv.replaceFieldWrite(f2, cc, "write");
- run.instrument(conv);
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(14001600, invoke(obj, "run"));
- }
-
- public void testInsGap() throws Exception {
- CtClass cc = sloader.get("test4.GapSwitch");
- ExprEditor ed = new ExprEditor() {
- public void edit(MethodCall c) throws CannotCompileException {
- c.replace("{ value++; $_ = $proceed($$); }");
- }
- };
-
- CtMethod m1 = cc.getDeclaredMethod("run");
- m1.instrument(ed);
- CtMethod m2 = cc.getDeclaredMethod("run2");
- m2.instrument(ed);
-
- final CtMethod m3 = cc.getDeclaredMethod("run3");
- m3.instrument(new ExprEditor() {
- public void edit(MethodCall c) throws CannotCompileException {
- CodeIterator it = m3.getMethodInfo().getCodeAttribute().iterator();
- try {
- it.insertGap(c.indexOfBytecode(), 5000);
- } catch (BadBytecode e) {
- throw new CannotCompileException(e);
- }
- }
- });
- m3.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile());
-
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(1010, invoke(obj, "run"));
- assertEquals(1100, invoke(obj, "run2"));
- assertEquals(12222, invoke(obj, "run3"));
- }
-
- public void testAnnotationCheck() throws Exception {
- CtClass cc = sloader.get("test4.Anno");
- CtMethod m1 = cc.getDeclaredMethod("foo");
- CtField f = cc.getDeclaredField("value");
-
- assertTrue(cc.hasAnnotation(test4.Anno1.class));
- assertFalse(cc.hasAnnotation(java.lang.annotation.Documented.class));
-
- assertTrue(cc.hasAnnotation(test4.Anno1.class.getName()));
- assertFalse(cc.hasAnnotation(java.lang.annotation.Documented.class.getName()));
-
- assertEquals("empty", ((test4.Anno1)cc.getAnnotation(test4.Anno1.class)).value());
- assertNull(cc.getAnnotation(Deprecated.class));
-
- assertTrue(m1.hasAnnotation(test4.Anno1.class));
- assertFalse(m1.hasAnnotation(java.lang.annotation.Documented.class));
- assertTrue(m1.getAnnotation(test4.Anno1.class) != null);
- assertNull(m1.getAnnotation(Deprecated.class));
-
- assertTrue(f.hasAnnotation(test4.Anno1.class));
- assertFalse(f.hasAnnotation(java.lang.annotation.Documented.class));
- assertTrue(f.getAnnotation(test4.Anno1.class) != null);
- assertNull(f.getAnnotation(Deprecated.class));
- }
-
- public void testRename() throws Exception {
- CtClass cc = sloader.get("test4.Rename");
- cc.setName("test4.Rename2");
- cc.rebuildClassFile();
- cc.writeFile();
- CtClass cc2 = sloader.get("test4.IRename");
- cc2.replaceClassName("test4.Rename", "test4.Rename2");
- cc2.rebuildClassFile();
- cc2.writeFile();
- Object obj = make(cc.getName());
- assertEquals("test4.Rename2", obj.getClass().getName());
- assertEquals(14, invoke(obj, "run"));
- }
-
- public void testRename2() throws Exception {
- CtClass cc = sloader.get("test4.Signature");
- cc.setName("test4.Sig");
- cc.rebuildClassFile();
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(3, invoke(obj, "run"));
- }
-
- public void testJIRA93() throws Exception {
- ClassPool cp = ClassPool.getDefault();
- CtClass cc = sloader.getCtClass("test4.JIRA93");
- CtMethod m = cc.getDeclaredMethod("foo");
-
- m.addLocalVariable("bar", CtClass.longType);
- // The original bug report includes the next line.
- // But this is not a bug.
- //m.insertAfter("bar;", true);
- // Instead, the following code is OK.
- m.insertBefore("bar = 0;");
- m.insertAfter("bar;", false);
-
- cc.writeFile();
- Object obj = make(cc.getName());
- }
-
- public void testNewRemover() throws Exception {
- CtClass cc = sloader.get("test4.NewRemover");
- CtMethod mth = cc.getDeclaredMethod("make");
- mth.getMethodInfo().rebuildStackMap(cc.getClassPool());
- mth.getMethodInfo().rebuildStackMapForME(cc.getClassPool());
- //cc.debugWriteFile("debug");
- CodeConverter conv = new CodeConverter();
- conv.replaceNew(cc, cc, "make2");
- mth.instrument(conv);
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(10, invoke(obj, "run"));
- }
-
- public void testClassFileWriter() throws Exception {
- ClassFileWriter cfw = new ClassFileWriter(ClassFile.JAVA_4, 0);
- ClassFileWriter.ConstPoolWriter cpw = cfw.getConstPool();
-
- ClassFileWriter.FieldWriter fw = cfw.getFieldWriter();
- fw.add(AccessFlag.PUBLIC, "value", "J", null);
- fw.add(AccessFlag.PROTECTED | AccessFlag.STATIC, "value2", "Ljava/lang/String;", null);
-
- ClassFileWriter.MethodWriter mw = cfw.getMethodWriter();
-
- mw.begin(AccessFlag.PUBLIC, MethodInfo.nameInit, "()V", null, null);
- assertEquals(0, mw.size());
- mw.add(Opcode.ALOAD_0);
- assertEquals(1, mw.size());
- mw.addInvoke(Opcode.INVOKESPECIAL, "java/lang/Object", MethodInfo.nameInit, "()V");
- mw.add(Opcode.RETURN);
- mw.codeEnd(1, 1);
- mw.end(null, null);
-
- mw.begin(AccessFlag.PUBLIC, "move", "(II)V", null, null);
- assertEquals(0, mw.size());
- mw.add(Opcode.ALOAD_0);
- mw.addInvoke(Opcode.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
- assertEquals(4, mw.size());
- mw.add(Opcode.POP);
- mw.add(Opcode.RETURN);
- mw.add(Opcode.POP);
- mw.add(Opcode.RETURN);
- mw.codeEnd(1, 3);
- mw.addCatch(0, 4, 6, cpw.addClassInfo("java/lang/Exception"));
- mw.addCatch(0, 4, 6, cpw.addClassInfo("java/lang/Throwable"));
- mw.end(null, null);
-
- String[] exceptions = { "java/lang/Exception", "java/lang/NullPointerException" };
- mw.begin(AccessFlag.PUBLIC, "move2", "()V", exceptions, null);
- mw.add(Opcode.RETURN);
- mw.codeEnd(0, 1);
- StackMapTable.Writer stack = new StackMapTable.Writer(32);
- stack.sameFrame(1);
- mw.end(stack, null);
-
- mw.begin(AccessFlag.PUBLIC, "foo", "()I", null, null);
- mw.add(Opcode.ICONST_2);
- mw.add(Opcode.IRETURN);
- mw.codeEnd(1, 1);
- mw.end(null, null);
-
- byte[] out = cfw.end(AccessFlag.PUBLIC, cpw.addClassInfo("test4/WrittenFile"),
- cpw.addClassInfo("java/lang/Object"),
- null, null);
- FileOutputStream fos = new FileOutputStream("test4/WrittenFile.class");
- fos.write(out);
- fos.close();
- Object obj = make("test4.WrittenFile");
- assertNotNull(obj);
- assertEquals(2, invoke(obj, "foo"));
- }
-
- public void testClassFileWriter2() throws Exception {
- ClassFileWriter cfw = new ClassFileWriter(ClassFile.JAVA_4, 0);
- ClassFileWriter.ConstPoolWriter cpw = cfw.getConstPool();
-
- ClassFileWriter.FieldWriter fw = cfw.getFieldWriter();
- fw.add(AccessFlag.PUBLIC | AccessFlag.STATIC, "value", "I", null);
-
- ClassFileWriter.MethodWriter mw = cfw.getMethodWriter();
-
- mw.begin(AccessFlag.PUBLIC, MethodInfo.nameInit, "()V", null, null);
- mw.add(Opcode.ALOAD_0);
- mw.addInvoke(Opcode.INVOKESPECIAL, "java/lang/Object", MethodInfo.nameInit, "()V");
- mw.add(Opcode.RETURN);
- mw.codeEnd(1, 1);
- mw.end(null, null);
-
- String[] exceptions = { "java/lang/Exception" };
- mw.begin(AccessFlag.PUBLIC | AccessFlag.ABSTRACT, "move", "(II)V", exceptions, null);
- mw.end(null, null);
-
- int thisClass = cpw.addClassInfo("test4/WrittenFile2");
- int superClass = cpw.addClassInfo("java/lang/Object");
-
- cfw.end(new DataOutputStream(new FileOutputStream("test4/WrittenFile2.class")),
- AccessFlag.PUBLIC | AccessFlag.ABSTRACT, thisClass, superClass,
- null, null);
-
- File f = new File("test4/WrittenFile2.class");
- byte[] file = new byte[(int)f.length()];
- FileInputStream fis = new FileInputStream(f);
- fis.read(file);
- fis.close();
-
- byte[] out = cfw.end(AccessFlag.PUBLIC | AccessFlag.ABSTRACT, thisClass,
- superClass, null, null);
-
- assertEquals(out.length, file.length);
- for (int i = 0; i < out.length; i++)
- assertEquals(out[i], file[i]);
-
- CtClass sub = dloader.makeClass("test4.WrittenFile2sub", dloader.get("test4.WrittenFile2"));
- sub.addMethod(CtMethod.make("public void move(int i, int j) {}", sub));
- sub.addMethod(CtMethod.make("public int foo() { move(0, 1); return 1; }", sub));
- sub.writeFile();
- Object obj = make("test4.WrittenFile2sub");
- assertEquals(1, invoke(obj, "foo"));
- }
-
- public void testClassFileWriter3() throws Exception {
- ClassFileWriter cfw = new ClassFileWriter(ClassFile.JAVA_4, 0);
- ClassFileWriter.ConstPoolWriter cpw = cfw.getConstPool();
- int superClass = cpw.addClassInfo("java/lang/Object");
-
- final int syntheticTag = cpw.addUtf8Info("Synthetic");
- ClassFileWriter.AttributeWriter attribute = new ClassFileWriter.AttributeWriter() {
- public void write(DataOutputStream out) throws java.io.IOException {
- out.writeShort(syntheticTag);
- out.writeInt(0);
- }
-
- public int size() {
- return 1;
- }
- };
-
- ClassFileWriter.FieldWriter fw = cfw.getFieldWriter();
- fw.add(AccessFlag.PUBLIC, "value", "J", null);
- fw.add(AccessFlag.PROTECTED | AccessFlag.STATIC, "value2", "Ljava/lang/String;", attribute);
-
- ClassFileWriter.MethodWriter mw = cfw.getMethodWriter();
-
- mw.begin(AccessFlag.PUBLIC, MethodInfo.nameInit, "()V", null, attribute);
- mw.add(Opcode.ALOAD_0);
- mw.add(Opcode.INVOKESPECIAL);
- mw.add16(cpw.addMethodrefInfo(superClass, cpw.addNameAndTypeInfo(MethodInfo.nameInit, "()V")));
- // mw.addInvoke(Opcode.INVOKESPECIAL, "java/lang/Object", MethodInfo.nameInit, "()V");
- mw.add(Opcode.RETURN);
- mw.codeEnd(1, 1);
- mw.end(null, null);
-
- mw.begin(AccessFlag.PUBLIC, "foo", "()I", null, attribute);
- mw.add(Opcode.ICONST_2);
- mw.add(Opcode.IRETURN);
- mw.codeEnd(1, 1);
- mw.end(null, null);
-
- int thisClass = cpw.addClassInfo("test4/WrittenFile3");
- cfw.end(new DataOutputStream(new FileOutputStream("test4/WrittenFile3.class")),
- AccessFlag.PUBLIC, thisClass, superClass,
- null, attribute);
-
- File f = new File("test4/WrittenFile3.class");
- byte[] file = new byte[(int)f.length()];
- FileInputStream fis = new FileInputStream(f);
- fis.read(file);
- fis.close();
-
- byte[] out = cfw.end(AccessFlag.PUBLIC, thisClass, superClass,
- null, attribute);
-
- assertEquals(out.length, file.length);
- for (int i = 0; i < out.length; i++)
- assertEquals(out[i], file[i]);
-
- Object obj = make("test4.WrittenFile3");
- assertNotNull(obj);
- assertEquals(2, invoke(obj, "foo"));
- }
-
- public void testCtArray() throws Exception {
- CtClass cc = sloader.get("int");
- assertEquals(Modifier.FINAL | Modifier.PUBLIC, cc.getModifiers());
- cc = sloader.get("int[]");
- assertEquals(Modifier.FINAL | Modifier.PUBLIC, cc.getModifiers());
- cc = sloader.get("java.lang.String[]");
- assertEquals(Modifier.FINAL | Modifier.PUBLIC, cc.getModifiers());
- CtClass[] intfs = cc.getInterfaces();
- assertEquals(Cloneable.class.getName(), intfs[0].getName());
- assertEquals(java.io.Serializable.class.getName(), intfs[1].getName());
- cc = sloader.get("test4.CtArrayTest[]");
- assertEquals(Modifier.FINAL | Modifier.PUBLIC, cc.getModifiers());
- }
-
- public void testAnalysisType() throws Exception {
- testAnalysisType2(sloader.get("int[]"), 1);
- testAnalysisType2(sloader.get("java.lang.String[][]"), 2);
- sloader.makeClass("A");
- testAnalysisType2(sloader.getCtClass("A"), 0);
- testAnalysisType2(sloader.getCtClass("A[]"), 1);
- testAnalysisType2(sloader.getCtClass("A[][]"), 2);
- }
-
- private void testAnalysisType2(CtClass cc, int size) throws Exception {
- javassist.bytecode.analysis.Type t = javassist.bytecode.analysis.Type.get(cc);
- assertEquals(cc.getName(), size, t.getDimensions());
- }
-
- public void testArrayType() throws Exception {
- CtClass at = sloader.get("java.lang.Object[]");
- CtClass[] intfs = at.getInterfaces();
- assertEquals(intfs.length, 2);
- assertEquals(intfs[0].getName(), java.lang.Cloneable.class.getName());
- assertEquals(intfs[1].getName(), java.io.Serializable.class.getName());
-
- assertTrue(at.subtypeOf(sloader.get(java.lang.Object.class.getName())));
- assertTrue(at.subtypeOf(intfs[0]));
- assertTrue(at.subtypeOf(intfs[1]));
- assertTrue(at.subtypeOf(intfs[1]));
- CtClass subt = sloader.get(java.text.CharacterIterator.class.getName());
- assertFalse(at.subtypeOf(subt));
- }
-
- public void testGetFieldDesc() throws Exception {
- CtClass cc = sloader.get("test4.GetFieldDesc");
- cc.getDeclaredField("f", "I");
- cc.getField("s", "Ljava/lang/String;");
- CtClass cc2 = sloader.get("test4.GetFieldDescSub");
- assertEquals(cc2.getField("s", "Ljava/lang/String;").getDeclaringClass().getName(),
- "test4.GetFieldDesc");
- assertEquals(cc2.getField("s", "I").getDeclaringClass().getName(),
- "test4.GetFieldDescSub");
- }
-
- public void testMakeMethod() throws CannotCompileException {
- CtClass ctClass = sloader.makeClass("test4.MakeMethod2");
- CtNewMethod.make("public String foox(){return test4.MakeMethod.foo();}", ctClass);
- CtNewMethod.make("public String foo(){return test4.MakeMethod.foo();}", ctClass);
- }
-
- public void testVarArgs() throws Exception {
- CtClass cc = sloader.get("test4.VarArgs");
- CtMethod m = CtMethod.make("public int foo(int i, String[] args) { return args.length; }", cc);
- m.setModifiers(m.getModifiers() | Modifier.VARARGS);
- cc.addMethod(m);
- m = CtMethod.make("public int run() { return goo(7, new int[] { 1, 2, 3 }); }", cc);
- cc.addMethod(m);
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(3, invoke(obj, "run"));
- }
-
- public void testGetAllRef() throws Exception {
- CtClass cc = sloader.get("test4.GetAllRef");
- ClassFile cf = cc.getClassFile();
- AttributeInfo ainfo
- = cf.getAttribute(AnnotationsAttribute.visibleTag);
- ClassMap map = new ClassMap();
- map.put("test4.GetAllRefAnno", "test4.GetAllRefAnno2");
- map.put("test4.GetAllRefEnum", "test4.GetAllRefEnum2");
- map.put("java.lang.String", "java.lang.StringBuilder");
- cf.addAttribute(ainfo.copy(cf.getConstPool(), map));
- cc.writeFile();
- cc.detach();
-
- cc = dloader.get(cc.getName());
- test4.GetAllRefAnno2 anno
- = (test4.GetAllRefAnno2)cc.getAnnotation(test4.GetAllRefAnno2.class);
- assertEquals(test4.GetAllRefEnum2.A, anno.getA());
- assertEquals(StringBuilder.class, anno.getC());
- }
-
- public void testGetAllRefB() throws Exception {
- CtClass cc = sloader.get("test4.GetAllRefB");
- ClassMap map = new ClassMap();
- map.put("test4.GetAllRefAnno", "test4.GetAllRefAnno2");
- map.put("test4.GetAllRefEnum", "test4.GetAllRefEnum2");
- map.put("java.lang.String", "java.lang.StringBuilder");
- cc.replaceClassName(map);
- //cc.replaceClassName("test4.GetAllRefAnno", "test4.GetAllRefAnno2");
- cc.writeFile();
-
- cc = dloader.get(cc.getName());
- test4.GetAllRefAnno2 anno
- = (test4.GetAllRefAnno2)cc.getAnnotation(test4.GetAllRefAnno2.class);
- assertEquals(test4.GetAllRefEnum2.A, anno.getA());
- assertEquals(StringBuilder.class, anno.getC());
- /*
- AnnotationsAttribute aainfo = (AnnotationsAttribute)
- cc.getClassFile().getAttribute(AnnotationsAttribute.visibleTag);
- Annotation[] a = aainfo.getAnnotations();
- System.err.println(a[0].getTypeName());
- System.err.println(a[0]);
- */
- }
-
- public void testGetAllRefC() throws Exception {
- CtClass cc = sloader.get("test4.GetAllRefC");
- HashSet set = new HashSet();
- set.add("java.lang.Object");
- set.add("java.lang.String");
- set.add("test4.GetAllRefC");
- set.add("test4.GetAllRefAnno");
- set.add("test4.GetAllRefEnum");
- set.add("test4.GetAllRefAnnoC");
- set.add("test4.GetAllRefAnnoC2");
- set.add("test4.GetAllRefAnnoC3");
- set.add("test4.GetAllRefAnnoC4");
- java.util.Collection<String> refs
- = (java.util.Collection<String>)cc.getRefClasses();
- assertEquals(set.size(), refs.size());
- for (String s: refs) {
- assertTrue(set.contains(s));
- }
- }
-
- public void testGetAllRefInner() throws Exception {
- HashSet set = new HashSet();
- set.add("java.lang.Object");
- set.add("test4.GetAllRefInnerTest");
- set.add("test4.GetAllRefInnerTest$1");
- set.add("test4.GetAllRefInnerTest$2");
- CtClass cc = sloader.get("test4.GetAllRefInnerTest");
- int size = 0;
- for (Object s: cc.getRefClasses()) {
- assertTrue((String)s, set.contains(s));
- ++size;
- }
-
- assertEquals(set.size(), size);
- }
-
- public void testNestedClass() throws Exception {
- CtClass cc = sloader.get("test4.NestedClass$1");
- CtClass[] tab = cc.getNestedClasses();
- assertEquals(1, tab.length);
- assertEquals("test4.NestedClass$1$1", tab[0].getName());
-
- cc = sloader.get("test4.NestedClass$1$1");
- tab = cc.getNestedClasses();
- assertEquals(0, tab.length);
-
- cc = sloader.get("test4.NestedClass");
- tab = cc.getNestedClasses();
- for (CtClass c: tab) {
- System.err.println(c.getName());
- }
-
- // Eclipse compiler sets tab.length to 4 but javac sets to 3.
- assertTrue(tab.length == 4 || tab.length == 3);
- for (CtClass c: tab) {
- String name = c.getName();
- assertTrue(name.equals("test4.NestedClass$N")
- || name.equals("test4.NestedClass$S")
- || name.equals("test4.NestedClass$1")
- || name.equals("test4.NestedClass$1In"));
- }
-
- cc = sloader.get("test4.NestedClass$1In");
- tab = cc.getNestedClasses();
- assertEquals(0, tab.length);
- }
-
- public void testGetClasses() throws Exception {
- CtClass cc = sloader.get("test4.NestedClass");
- CtClass[] tab = cc.getDeclaredClasses();
-
- // Eclipse compiler sets tab.length to 4 but javac sets to 3.
- assertTrue(tab.length == 4 || tab.length == 3);
- for (CtClass c: tab) {
- String name = c.getName();
- assertTrue(name.equals("test4.NestedClass$N")
- || name.equals("test4.NestedClass$S")
- || name.equals("test4.NestedClass$1")
- || name.equals("test4.NestedClass$1In"));
- }
-
- cc = sloader.get("test4.NestedClass$1In");
- tab = cc.getDeclaredClasses();
- assertEquals(0, tab.length);
- }
-
- public void testImportPac() throws Exception {
- CtClass cc = sloader.makeClass("test4.TestImpP");
- sloader.importPackage("test4.NewImportPac");
- try {
- cc.addMethod(CtNewMethod.make(
- "public int foo(){ " +
- " ImportPac obj = new ImportPac();" +
- " return obj.getClass().getName().length(); }", cc));
- fail("ImportPac was found");
- }
- catch (CannotCompileException e) {}
-
- cc.addMethod(CtNewMethod.make(
- "public int bar(){ " +
- " NewImportPac obj = new NewImportPac();" +
- " return obj.getClass().getName().length(); }", cc));
- sloader.clearImportedPackages();
- }
-
- public void testLength() throws Exception {
- CtClass cc = sloader.makeClass("test4.LengthTest");
- cc.addMethod(CtNewMethod.make(
- "public int len(String s){ " +
- " return s.length(); }", cc));
- cc.addField(CtField.make("int length = 100;", cc));
- cc.addConstructor(CtNewConstructor.defaultConstructor(cc));
- cc.addMethod(CtNewMethod.make(
- "public int run(){ " +
- " test4.LengthTest t = new test4.LengthTest();" +
- " return len(\"foo\") + t.length + test4.length.m(); }", cc));
- try {
- cc.addMethod(CtNewMethod.make(
- "public int run(){ " +
- " return test4no.length.m(); }", cc));
- fail("test4no was found!");
- }
- catch (CannotCompileException e) {
- System.out.println(e);
- }
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(110, invoke(obj, "run"));
- }
-
- public void testAaload() throws Exception {
- CtClass clazz = sloader.get("test4.Aaload");
- CtMethod method = clazz.getMethod("narf", "()V");
- method.instrument(new ExprEditor() {
- @Override
- public void edit(MethodCall call) throws CannotCompileException {
- String name = call.getMethodName();
- if (name.equals("addActionListener"))
- call.replace("$0." + name + "($$);");
- }
- });
- }
-
- @SuppressWarnings("deprecation")
- public void testMakePackage() throws Exception {
- if (ClassFile.MAJOR_VERSION >= ClassFile.JAVA_9) {
- ClassPool pool = ClassPool.getDefault();
- try {
- pool.makePackage(pool.getClassLoader(), "test4.pack2");
- fail("makePackage() ran");
- }
- catch (CannotCompileException e) {}
- }
- }
-
- @SuppressWarnings("deprecation")
- public void testPackage() throws Throwable { // JASSIST-147
- String packageName = "test4.pack";
- ClassPool pool = ClassPool.getDefault();
- try {
- pool.makePackage(pool.getClassLoader(), packageName);
- pool.makePackage(pool.getClassLoader(), packageName);
- }
- catch (CannotCompileException e) {
- if (ClassFile.MAJOR_VERSION >= ClassFile.JAVA_9)
- return; // makePackage() does not work in Java 9.
- }
-
- CtClass ctcl = pool.makeClass("test4.pack.Clazz");
- Class cl = ctcl.toClass();
- Object obj = cl.getConstructor().newInstance();
- assertEquals(packageName, obj.getClass().getPackage().getName());
- }
-
- public static final String BASE_PATH = "../../";
- public static final String JAVASSIST_JAR = BASE_PATH + "javassist.jar";
- public static final String CLASSES_FOLDER = BASE_PATH + "build/classes";
- public static final String TEST_CLASSES_FOLDER = BASE_PATH + "build/test-classes";
-
- public static class Inner1 {
- public static int get() {
- return 0;
- }
- }
-
- public void testJIRA150() throws Exception {
- ClassPool pool = new ClassPool(true);
- for(int paths=0; paths<50; paths++) {
- pool.appendClassPath(JAVASSIST_JAR);
- pool.appendClassPath(CLASSES_FOLDER);
- pool.appendClassPath(TEST_CLASSES_FOLDER);
- }
- CtClass cc = pool.get("Jassist150$Inner1");
- CtMethod ccGet = cc.getDeclaredMethod("get");
- long startTime = System.currentTimeMillis();
- for(int replace=0; replace<1000; replace++) {
- ccGet.setBody(
- "{ int n1 = java.lang.Integer#valueOf(1); " +
- " int n2 = java.lang.Integer#valueOf(2); " +
- " int n3 = java.lang.Integer#valueOf(3); " +
- " int n4 = java.lang.Integer#valueOf(4); " +
- " int n5 = java.lang.Integer#valueOf(5); " +
- " return n1+n2+n3+n4+n5; }");
- }
- long endTime = System.currentTimeMillis();
- for(int replace=0; replace<1000; replace++) {
- ccGet.setBody(
- "{ int n1 = java.lang.Integer.valueOf(1); " +
- " int n2 = java.lang.Integer.valueOf(2); " +
- " int n3 = java.lang.Integer.valueOf(3); " +
- " int n4 = java.lang.Integer.valueOf(4); " +
- " int n5 = java.lang.Integer.valueOf(5); " +
- " return n1+n2+n3+n4+n5; }");
- }
- long endTime2 = System.currentTimeMillis();
- for(int replace=0; replace<1000; replace++) {
- ccGet.setBody(
- "{ int n1 = Integer.valueOf(1); " +
- " int n2 = Integer.valueOf(2); " +
- " int n3 = Integer.valueOf(3); " +
- " int n4 = Integer.valueOf(4); " +
- " int n5 = Integer.valueOf(5); " +
- " return n1+n2+n3+n4+n5; }");
- }
- long endTime3 = System.currentTimeMillis();
- long t1 = endTime - startTime;
- long t2 = endTime2 - endTime;
- long t3 = endTime3 - endTime2;
- System.out.println("JIRA150: " + t1 + ", " + t2 + ", " + t3);
- assertTrue("performance test (the next try may succeed): " + t2 + " < 6 * " + t1,
- t2 < t1 * 6);
- assertTrue("performance test (the next try may succeed): " + t3 + " < 3 * " + t1,
- t3 < t1 * 3);
- }
-
- 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);
- for(int paths=0; paths<50; paths++) {
- pool.appendClassPath(JAVASSIST_JAR);
- pool.appendClassPath(CLASSES_FOLDER);
- pool.appendClassPath(TEST_CLASSES_FOLDER);
- }
- CtClass cc = pool.get("Jassist150$Inner1");
- CtMethod ccGet = cc.getDeclaredMethod("get");
- for(int replace=0; replace < 5; replace++) {
- ccGet.setBody(
- "{ int n1 = java.lang.Integer#valueOf(1); " +
- " int n2 = java.lang.Integer#valueOf(2); " +
- " int n3 = java.lang.Integer#valueOf(3); " +
- " int n4 = java.lang.Integer#valueOf(4); " +
- " int n5 = java.lang.Integer#valueOf(5); " +
- " return n1+n2+n3+n4+n5; }");
- }
- pool = null;
- }
-
- // try to run garbage collection.
- int[][] mem = new int[100][];
- int[] large;
- for (int i = 0; i < 100; i++) {
- large = new int[1000000];
- large[large.length - 2] = 9 + i;
- mem[i] = large;
- }
- System.gc();
- System.gc();
- int size = javassist.compiler.MemberResolver.getInvalidMapSize();
- System.out.println("JIRA150b " + size + " " + mem[mem.length - 1][mem[0].length - 2]);
- // Now this seems obsolete.
- // assertTrue("JIRA150b size: " + origSize + " " + size, size < origSize + N);
- }
-
- public void testJIRA152() throws Exception {
- CtClass cc = sloader.get("test4.JIRA152");
- CtMethod mth = cc.getDeclaredMethod("buildColumnOverride");
- //CtMethod mth = cc.getDeclaredMethod("tested");
- mth.instrument(new ExprEditor() {
- public void edit(MethodCall c) throws CannotCompileException {
- c.replace("try{ $_ = $proceed($$); } catch (Throwable t) { throw t; }");
- }
- });
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(1, invoke(obj, "test"));
- }
-
- public void testJIRA151() {
- // try it using classloader of TestDescForName Desc.useContextClassLoader = false;
- assertTrue(javassist.runtime.Desc.getClazz("[Ljava.lang.String;") != null);
- //Thread.currentThread().setContextClassLoader(TestDescForName.class.getClassLoader());
- boolean old = javassist.runtime.Desc.useContextClassLoader;
- javassist.runtime.Desc.useContextClassLoader = true;
- assertTrue(javassist.runtime.Desc.getClazz("[Ljava.lang.String;") != null);
- javassist.runtime.Desc.useContextClassLoader = old;
- }
-
- public void testJIRA166() throws Exception {
- CtClass cc = sloader.get("test4.JIRA166");
- cc.instrument(new ExprEditor() {
- public void edit(FieldAccess fa) throws CannotCompileException {
- if (fa.isReader() && fa.getFieldName().equals("length"))
- fa.replace("length = self().length + \"!\"; $_ = ($r) this.length.substring(1);");
- }
- });
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(1, invoke(obj, "run"));
- }
-
- public void testGenericSignature() throws Exception {
- CtClass cc = sloader.makeClass("test4.GenSig");
- CtClass objClass = sloader.get(CtClass.javaLangObject);
- SignatureAttribute.ClassSignature cs
- = new SignatureAttribute.ClassSignature(
- new SignatureAttribute.TypeParameter[] {
- new SignatureAttribute.TypeParameter("T") });
- cc.setGenericSignature(cs.encode()); // <T:Ljava/lang/Object;>Ljava/lang/Object;
-
- CtField f = new CtField(objClass, "value", cc);
- SignatureAttribute.TypeVariable tvar = new SignatureAttribute.TypeVariable("T");
- f.setGenericSignature(tvar.encode()); // TT;
- cc.addField(f);
-
- CtMethod m = CtNewMethod.make("public Object get(){return value;}", cc);
- SignatureAttribute.MethodSignature ms
- = new SignatureAttribute.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);
- SignatureAttribute.MethodSignature ms2
- = new SignatureAttribute.MethodSignature(null, new SignatureAttribute.Type[] { tvar },
- new SignatureAttribute.BaseType("void"), null);
- m2.setGenericSignature(ms2.encode()); // (TT;)V;
- cc.addMethod(m2);
-
- cc.writeFile();
- Object obj = make(cc.getName());
- Class clazz = obj.getClass();
- assertEquals("T", clazz.getTypeParameters()[0].getName());
- assertEquals("T", ((java.lang.reflect.TypeVariable)clazz.getDeclaredField("value").getGenericType()).getName());
- java.lang.reflect.Method rm = clazz.getDeclaredMethod("get", new Class[0]);
- assertEquals("T", ((java.lang.reflect.TypeVariable)rm.getGenericReturnType()).getName());
- java.lang.reflect.Method rm2 = clazz.getDeclaredMethod("set", new Class[] { Object.class });
- assertEquals("T", ((java.lang.reflect.TypeVariable)rm2.getGenericParameterTypes()[0]).getName());
- }
-
- public void testJIRA171() throws Exception {
- SignatureAttribute.MethodSignature ms
- = SignatureAttribute.toMethodSignature("(Ljava/lang/Object;Lorg/apache/hadoop/io/Text;"
- + "Lorg/apache/hadoop/mapreduce/Mapper<Ljava/lang/Object;Lorg/apache/hadoop/io/Text;"
- + "Lorg/apache/hadoop/io/Text;Lorg/apache/hadoop/io/IntWritable;>.Context;)V");
- String s = ms.toString();
- System.out.println(s);
- assertEquals("<> (java.lang.Object, org.apache.hadoop.io.Text, "
- + "org.apache.hadoop.mapreduce.Mapper<java.lang.Object, org.apache.hadoop.io.Text, "
- + "org.apache.hadoop.io.Text, org.apache.hadoop.io.IntWritable>.Context) void", s);
- }
-
- public void testAfter() throws Exception {
- CtClass cc = sloader.get("test4.AfterTest");
- CtMethod m1 = cc.getDeclaredMethod("m1");
- m1.insertAfter("print();");
- CtMethod m2 = cc.getDeclaredMethod("m2");
- m2.insertAfter("print();");
- CtMethod m3 = cc.getDeclaredMethod("m3");
- m3.insertAfter("print();");
- CtMethod m4 = cc.getDeclaredMethod("m4");
- m4.insertAfter("print();");
-
- CtMethod mm1 = cc.getDeclaredMethod("mm1");
- mm1.insertAfter("print();", true);
- CtMethod mm2 = cc.getDeclaredMethod("mm2");
- mm2.insertAfter("print();", true);
- CtMethod mm3 = cc.getDeclaredMethod("mm3");
- mm3.insertAfter("print();", true);
- CtMethod mm4 = cc.getDeclaredMethod("mm4");
- mm4.insertAfter("print();", true);
-
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(131, invoke(obj, "test1"));
- assertEquals(112, invoke(obj, "test2"));
- assertEquals(10, invoke(obj, "test3"));
- assertEquals(100, invoke(obj, "test4"));
-
- assertEquals(131, invoke(obj, "test11"));
- assertEquals(112, invoke(obj, "test22"));
- assertEquals(10, invoke(obj, "test33"));
- assertEquals(100, invoke(obj, "test44"));
- }
-
- public void testJIRA186() throws Exception {
- CtClass cc = sloader.get("test4.JIRA186");
- cc.getDeclaredMethod("test").insertBefore("{" +
- " java.util.List l = new java.util.ArrayList();" +
- " l.add(this.toString());" +
- "}");
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(1, invoke(obj, "test"));
- }
-
- // JASSIST-190
- public void testMultipleCatch() throws Exception {
- CtClass cc = sloader.get("test4.MultiCatch");
- CtMethod m1 = cc.getDeclaredMethod("m1");
- m1.insertAfter("print();");
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(12, invoke(obj, "test1"));
- }
-
- // JASSIST-185
- public void testLocalVariableTypeTable() throws Exception {
- CtClass cc = sloader.get("test4.Lvtt");
- CtMethod m = cc.getDeclaredMethod("run");
- m.addParameter(CtClass.intType);
- cc.writeFile();
- Object obj = make(cc.getName());
- }
-
- // JASSISt-181
- public void testAnnotationWithGenerics() throws Exception {
- CtClass cc0 = sloader.get("test4.JIRA181b");
- CtField field0 = cc0.getField("aField");
- String s0 = field0.getAnnotation(test4.JIRA181b.Condition.class).toString();
- assertEquals("@test4.JIRA181b$Condition(condition=java.lang.String.class)", s0);
- CtField field01 = cc0.getField("aField2");
- String s01 = field01.getAnnotation(test4.JIRA181b.Condition.class).toString();
- assertEquals("@test4.JIRA181b$Condition(condition=void.class)", s01);
-
- CtClass cc = sloader.get("test4.JIRA181");
- CtField field = cc.getField("aField");
- String s = field.getAnnotation(test4.JIRA181.Condition.class).toString();
- if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_9)
- assertEquals("@test4.JIRA181$Condition(condition=test4.JIRA181<T>.B.class)", s);
- else
- assertEquals("@test4.JIRA181$Condition(condition=test4.JIRA181.B.class)", s);
-
- CtField field2 = cc.getField("aField2");
- String s2 = field2.getAnnotation(test4.JIRA181.Condition2.class).toString();
- if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_9)
- assertEquals("@test4.JIRA181$Condition2(condition=test4.JIRA181<T>.B[].class)", s2);
- else
- assertEquals("@test4.JIRA181$Condition2(condition=test4.JIRA181.B[].class)", s2);
- }
-
- public void testJIRA195() throws Exception {
- CtClass cc = sloader.get("test4.JIRA195");
- CtMethod mth = cc.getDeclaredMethod("test");
- mth.getMethodInfo().rebuildStackMap(cc.getClassPool());
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(4, invoke(obj, "run"));
- }
-
- public void testJIRA188() throws Exception {
- CtClass cc = sloader.makeClass("test4.JIRA188");
- CtField f = new CtField(CtClass.intType, "f", cc);
- f.setModifiers(Modifier.PRIVATE);
- cc.addField(f);
- cc.addMethod(CtNewMethod.make(
- "public int getf(test4.JIRA188 p){ return p.f; }", cc));
- cc.detach();
- // System.gc();
- try {
- cc = sloader.get("test4.JIRA188");
- fail("test4.JIRA188 found");
- }
- catch (NotFoundException e) {}
- cc = sloader.makeClass("test4.JIRA188");
- cc.addField(new CtField(CtClass.intType, "g", cc));
- cc.addMethod(CtNewMethod.make(
- "public int getf(test4.JIRA188 p){ return p.g; }", cc));
- }
-
- public void testJIRA158() throws Exception {
- CtClass cc = sloader.get("test4.JIRA158");
- cc.addMethod(CtMethod.make("public int run() { return obj.foo(jj, dd) + obj.bar(j, d); }", cc));
- cc.writeFile();
- Object obj = make(cc.getName());
- assertEquals(15, invoke(obj, "run"));
- }
-
- public void testJIRA207() throws Exception {
- CtClass cc = sloader.get("test4.JIRA207");
- CtMethod cm = cc.getDeclaredMethod("foo");
- cm.insertBefore("throw new Exception();");
-
- CtMethod cm2 = cc.getDeclaredMethod("run2");
- cm2.insertBefore("throw new Exception();");
- cc.writeFile();
- Object obj = make(cc.getName());
- try {
- invoke(obj, "run2");
- fail("run2");
- }
- catch (Exception e) {}
- }
-
- public void testJIRA212() throws Exception {
- CtClass cc = sloader.get("test4.JIRA212");
- for (final CtBehavior behavior : cc.getDeclaredBehaviors()) {
- behavior.instrument(new ExprEditor() {
- public void edit(FieldAccess fieldAccess) throws CannotCompileException {
- String fieldName = fieldAccess.getFieldName().substring(0,1).toUpperCase() + fieldAccess.getFieldName().substring(1);
- if (fieldAccess.isReader()) {
- // Rewrite read access
- fieldAccess.replace("$_ = $0.get" + fieldName + "();");
- } else if (fieldAccess.isWriter()) {
- // Rewrite write access
- fieldAccess.replace("$0.set" + fieldName + "($1);");
- }
- }});
- }
- cc.writeFile();
- Object obj = make(cc.getName());
- }
-
- public void testWhileTrueKO() throws Exception {
- final ClassPool pool = ClassPool.getDefault();
- final CtClass cc = pool.makeClass("test4.TestWhileTrueKO");
- String source = "public void testWhile() { while(true) { break; } }";
- cc.addMethod(CtMethod.make(source, cc));
- cc.writeFile();
- make(cc.getName());
- }
-
- public void testWhileTrueOK() throws Exception {
- final ClassPool pool = ClassPool.getDefault();
- final CtClass cc = pool.makeClass("test4.TestWhileTrueOK");
- String source = "public void testWhile() { while(0==0) { break; }}";
- cc.addMethod(CtMethod.make(source, cc));
- cc.writeFile();
- make(cc.getName());
- }
-
- // JIRA JASSIST-224
- public void testMethodParameters() throws Exception {
- Class rc = test4.MethodParamTest.class;
- java.lang.reflect.Method m = rc.getDeclaredMethods()[0];
- java.lang.reflect.Parameter[] params = m.getParameters();
- assertEquals("i", params[0].getName());
- assertEquals("s", params[1].getName());
-
- CtClass cc = sloader.get("test4.MethodParamTest");
- ClassFile cf = cc.getClassFile2();
- ConstPool cp = cf.getConstPool();
- MethodInfo minfo = cf.getMethod("test");
- MethodParametersAttribute attr
- = (MethodParametersAttribute)minfo.getAttribute(MethodParametersAttribute.tag);
- assertEquals(2, attr.size());
- assertEquals("i", cp.getUtf8Info(attr.name(0)));
- assertEquals("s", cp.getUtf8Info(attr.name(1)));
- assertEquals("i", attr.parameterName(0));
- assertEquals("s", attr.parameterName(1));
-
- attr = (MethodParametersAttribute)attr.copy(cp, null);
- assertEquals(2, attr.size());
- assertEquals("i", cp.getUtf8Info(attr.name(0)));
- assertEquals("s", cp.getUtf8Info(attr.name(1)));
- assertEquals("i", attr.parameterName(0));
- assertEquals("s", attr.parameterName(1));
- }
-
- // JIRA JASSIST-220
- public void testStaticInterfaceMethods() throws Exception {
- CtClass cc = sloader.get("test4.JIRA220");
-
- cc.getMethod("foo", "()V").instrument(new ExprEditor() {
- @Override
- public void edit(MethodCall m) throws CannotCompileException {
- try {
- m.getClassName();
- } catch (Exception e) {
- fail(e.getMessage());
- }
- }
- });
- }
-
- // JIRA-230
- public void testDeadcode() throws Exception {
- CtClass newClass = sloader.makeClass("test4.TestDeadcode");
- addDeadCode(newClass, "public void evaluate(){if (false) {int i = 0;}}");
- addDeadCode(newClass, "public void evaluate2(){if (false == true) {int i = 0;}}");
- addDeadCode(newClass, "public void evaluate3(){if (true) {} else {} int i = 0; if (false) {} else {} i++; }");
- addDeadCode(newClass, "public void evaluate4(){ for (;false;); int i = false ? 1 : 0; while (true) { return; }}");
- addDeadCode(newClass, "public void evaluate5(){ boolean b = !false; b = false && b; b = true && true;"
- + " b = true || b; b = b || false; }");
- addDeadCode(newClass, "public boolean evaluate6(){ return !false; }");
- addDeadCode(newClass, "public boolean evaluate7(){ return !true; }");
-
- newClass.debugWriteFile();
- Class<?> cClass = newClass.toClass(test4.DefineClassCapability.class);
- Object o = cClass.getConstructor().newInstance();
- java.lang.reflect.Method m = cClass.getMethod("evaluate");
- m.invoke(o);
- m = cClass.getMethod("evaluate2");
- m.invoke(o);
- m = cClass.getMethod("evaluate3");
- m.invoke(o);
- m = cClass.getMethod("evaluate4");
- m.invoke(o);
- m = cClass.getMethod("evaluate6");
- assertTrue((boolean)m.invoke(o));
- m = cClass.getMethod("evaluate7");
- assertFalse((boolean)m.invoke(o));
- m = cClass.getMethod("evaluate5");
- m.invoke(o);
- }
-
- private void addDeadCode(CtClass cc, String meth) throws Exception {
- CtMethod m = CtNewMethod.make(meth, cc);
- cc.addMethod(m);
- }
-
- public void testAnnArg() throws Exception {
- CtClass cc = sloader.get("test4.AnnoArg");
- CtMethod m = cc.getDeclaredMethod("foo");
- test4.AnnoArg.AnnoArgAt a = (test4.AnnoArg.AnnoArgAt)m.getAnnotations()[0];
- assertEquals("test4.AnnoArg$B", a.value().getName());
- System.out.println(a.value().getName());
- }
-
- public void testDeclaredMethods() throws Exception {
- CtClass cc = sloader.get("test4.DeclMethodsList");
- CtMethod[] meth = cc.getDeclaredMethods("foo");
- assertEquals(2, meth.length);
- assertEquals("()V", meth[0].getSignature());
- assertEquals("(I)I", meth[1].getSignature());
- meth = cc.getDeclaredMethods("bar");
- assertEquals(1, meth.length);
- assertEquals("()V", meth[0].getSignature());
- meth = cc.getDeclaredMethods("baz");
- assertEquals(0, meth.length);
- }
-
- public void testAnnotationLoader() throws Exception {
- CtClass anno = sloader.makeAnnotation("test4.AnnoLoadAnno");
- anno.debugWriteFile();
- CtClass cc = sloader.get("test4.AnnoLoad");
- CtMethod m = cc.getDeclaredMethod("foo");
- ClassFile cf = cc.getClassFile();
- ConstPool cp = cf.getConstPool();
- AnnotationsAttribute attr
- = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
- Annotation a = new Annotation(anno.getName(), cp);
- a.addMemberValue("value", new javassist.bytecode.annotation.StringMemberValue("file/path", cp));
- 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());
- }
- }
|