public void testCodeGen2() throws Exception { CtClass cc = sloader.makeClass("test2.CodeGen2"); CtMethod m1 = CtNewMethod.make( "public int test() {" + " int len;" + " String s = \"foo\" + \"bar\" + 3;" + " System.out.println(s); len = s.length();" + " len = -3 + len; len = len - (7 - 2 + -1);" + " int k = 3; len += ~k - ~3;" + " return len; }", cc); cc.addMethod(m1); CtMethod m2 = CtNewMethod.make( "public int test2() {" + " double d = 0.2 - -0.1;" + " d += (0.2 + 0.3) * 1.0;" + " return (int)(d * 10); }", cc); cc.addMethod(m2); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(0, invoke(obj, "test")); assertEquals(8, invoke(obj, "test2")); }
public void testDollarClassInStaticMethod() throws Exception { CtClass cc = sloader.makeClass("test5.DollarClass"); CtMethod m = CtNewMethod.make("public static int run(){ return $class.getName().length(); }", cc); cc.addMethod(m); m = CtNewMethod.make("public int run2(){ return $class.getName().length(); }", cc); cc.addMethod(m); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(cc.getName().length(), invoke(obj, "run")); assertEquals(cc.getName().length(), invoke(obj, "run2")); }
public void testJIRA250() throws Exception { CtClass cc = sloader.makeClass("test5.JIRA250", sloader.get("test5.JIRA250Super")); cc.addMethod( CtNewMethod.make( " public test5.JIRA250Bar getBar() {" + " return super.getBar();\n" + " }\n", cc)); cc.addMethod(CtNewMethod.make("public int run() { getBar(); return 1; }", cc)); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(1, invoke(obj, "run")); }
public void testNullArg() throws Exception { CtClass cc = sloader.makeClass("test2.NullArgTest"); CtMethod m1 = CtNewMethod.make( "public Object foo(Object[] obj, int idx) throws Throwable {" + " return null; }", cc); cc.addMethod(m1); CtMethod m2 = CtNewMethod.make("public void bar() { this.foo(null, 0); }", cc); cc.addMethod(m2); CtMethod m3 = CtNewMethod.make("public void bar2() { this.foo((Object[])null, 0); }", cc); cc.addMethod(m3); cc.writeFile(); }
public void testSuperDefaultMethodCall() throws Exception { CtClass cc = sloader.get("test5.DefaultMethod"); CtMethod m = CtNewMethod.make("public int run(){ return test5.DefaultMethodIntf.super.foo(); }", cc); cc.addMethod(m); m = CtNewMethod.make("public int run2(){ return test5.DefaultMethodIntf.baz(); }", cc); cc.addMethod(m); m = CtNewMethod.make("public int run3(){ return test5.DefaultMethodIntf.super.baz(); }", cc); cc.addMethod(m); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(1, invoke(obj, "run")); assertEquals(10, invoke(obj, "run2")); assertEquals(10, invoke(obj, "run3")); }
private static TemplateRenderer tryToCompile(String source, Map<String, String> expressions) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException { ClassPool cp = ClassPool.getDefault(); CtClass sup = cp.get(Object.class.getCanonicalName()); CtClass cls = cp.makeClass("RapidoidTemplate" + ID_GEN.incrementAndGet(), sup); cls.addInterface(cp.get(TemplateRenderer.class.getCanonicalName())); cls.addConstructor(CtNewConstructor.defaultConstructor(cls)); for (Map.Entry<String, String> expr : expressions.entrySet()) { String fld = "private static final org.rapidoid.render.retriever.ValueRetriever %s = org.rapidoid.render.retriever.Retriever.of(%s);"; String retrieverId = retrieverId(expr.getKey()); String prop = expr.getValue(); String field = U.frmt(fld, retrieverId, prop); cls.addField(CtField.make(field, cls)); } CtClass[] params = {cp.get(RenderCtx.class.getCanonicalName())}; CtClass clsVoid = cp.get(void.class.getCanonicalName()); cls.addMethod( CtNewMethod.make(Modifier.PUBLIC, clsVoid, "render", params, new CtClass[0], source, cls)); return (TemplateRenderer) cls.toClass().newInstance(); }
public void testJIRA246() throws Exception { CtClass ctClass = sloader.makeClass("test5.JIRA246Test"); ctClass.addInterface(sloader.get(test5.JIRA246.Test.class.getName())); String methodBody = "public void test() { defaultMethod(); }"; CtMethod ctMethod = CtMethod.make(methodBody, ctClass); ctClass.addMethod(ctMethod); }
public void testBadClass() throws Exception { CtClass badClass = ClassPool.getDefault().makeClass("badClass"); String src = String.join( System.getProperty("line.separator"), "public void eval () {", " if (true) {", " double t=0;", " } else {", " double t=0;", " }", " for (int i=0; i < 2; i++) {", " int a=0;", " int b=0;", " int c=0;", " int d=0;", " if (true) {", " int e = 0;", " }", " }", "}"); System.out.println(src); badClass.addMethod(CtMethod.make(src, badClass)); Class clazzz = badClass.toClass(); Object obj = clazzz.getConstructor().newInstance(); // <-- falls here }
public void testCodeGen() throws Exception { CtClass cc = sloader.get("test2.CodeGen"); CtMethod m1 = cc.getDeclaredMethod("run"); m1.insertBefore( "{ double d = true ? 1 : 0.1; " + " d = d > 0.5 ? 0.0 : - 1.0; " + " System.out.println(d); " + " String s = \"foo\"; " + " s = 1 + 2 + s + \"bar\"; " + " s += \"poi\" + 3 + seven() + seven(\":\" + ' '); " + " s += .14; " + " msg = s; " + " System.out.println(s); }"); // recursive type check is done if $proceed is used. CtMethod m2 = CtNewMethod.make( "public int test() {" + " String s = $proceed(\"int\" + (3 + 0.14)) + '.'; " + " System.out.println(s); return s.length(); }", cc, "this", "seven"); cc.addMethod(m2); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(19, invoke(obj, "run")); assertEquals(9, invoke(obj, "test")); }
public T createJavassistProxy( LoadBalancer loadBalance, ConcurrentMap<String, T> map, Class ifaces) throws Exception { Class<?>[] interfaces = ifaces.getInterfaces(); if (interfaces.length == 1) { ClassPool mPool = new ClassPool(true); CtClass ctClass = mPool.get(interfaces[0].getName()); // 新建代理类 CtClass mCtc = mPool.makeClass(ifaces.getName() + "$JavassistProxy"); mCtc.setSuperclass(ctClass); for (CtMethod method : ctClass.getDeclaredMethods()) { System.out.println(method.getName()); // CtMethod m = new CtMethod(method.getReturnType(), // ,method.getParameterTypes(), mCtc); // cc.addMethod(m); // m.setBody("{ x += $1; }"); mCtc.addMethod(method); // method.setBody(""); } // mCtc.debugWriteFile("/home/liguojun"); return null; } else { return null; } }
private void enhanceJPACallback(CtClass ctClass, CtMethod method, Class anno) throws Exception { if (method.hasAnnotation(anno)) { CtMethod ctMethod = CtMethod.make( format( "public void {}() {\n" + " net.csdn.jpa.context.JPAContext jpaContext = getJPAConfig().reInitJPAContext();\n" + " try {\n" + " {}();\n" + " getJPAConfig().getJPAContext().closeTx(false);\n" + " } catch (Exception e) {\n" + " getJPAConfig().getJPAContext().closeTx(true);\n" + " } finally {\n" + " getJPAConfig().setJPAContext(jpaContext);\n" + " }\n" + " }", "$_" + method.getName(), method.getName()), ctClass); ctClass.addMethod(ctMethod); AnnotationsAttribute annotationsAttribute = EnhancerHelper.getAnnotations(ctMethod); EnhancerHelper.createAnnotation(annotationsAttribute, callback_classes.get(anno)); } }
public void testArrayLen() throws Exception { CtClass cc = sloader.get("test2.ArrayLenTest"); cc.addMethod(CtNewMethod.make("public int foo(){ return this.length; }", cc)); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(1, invoke(obj, "foo")); }
public void testMethodInInner2() throws Exception { CtClass inner = sloader.get("test2.Nested3$Inner"); CtClass outer = sloader.get("test2.Nested3"); String src = "public int f() {" + " int k = 0;" + " test2.Nested3 n = new test2.Nested3(3);" + " k += n.geti();" + " n = new test2.Nested3();" + " k += n.geti();" + " n = new test2.Nested3(\"foo\");" + " k += n.geti();" + " return k; }"; outer.stopPruning(true); outer.writeFile(); try { CtMethod m = CtNewMethod.make(src, inner); fail(); } catch (RuntimeException e) { } outer.defrost(); CtMethod m = CtNewMethod.make(src, inner); inner.addMethod(m); inner.writeFile(); outer.writeFile(); Object iobj = make(inner.getName()); assertEquals(6, invoke(iobj, "f")); }
public void testAddMethod() throws Exception { CtClass cc = sloader.get("test2.AddMethod"); CtMethod m = CtNewMethod.make("public void f() { return 1; }", cc); try { cc.addMethod(m); fail(); } catch (CannotCompileException e) { } CtMethod m2 = CtNewMethod.make("public void f(int i, int j) { return 1; }", cc); cc.addMethod(m2); try { cc.addField(new CtField(CtClass.longType, "f", cc)); fail(); } catch (CannotCompileException e) { } }
public void onWrite(ClassPool pool, String className) throws NotFoundException, CannotCompileException { CtClass cc = pool.get(className); try { if (isPersistent(className)) { CtClass base = cc.getSuperclass(); CtConstructor cons = new CtConstructor(constructorParams, cc); if (base.subclassOf(persistent) || base == object) { cons.setBody(null); cc.addConstructor(cons); if (base == object) { cc.setSuperclass(persistent); } } else { if (!isPersistent(base.getName())) { throw new NotFoundException( "Base class " + base.getName() + " was not declared as persistent"); } cons.setBody("super($0);"); cc.addConstructor(cons); } preprocessMethods(cc, true, true); if (base == persistent || base == object) { CtMethod m = new CtMethod(isRecursive, cc, null); m.setBody("return false;"); cc.addMethod(m); addSerializeMethods(cc, false); } else if (base.subtypeOf(serializable)) { addSerializeMethods(cc, true); } if ((cc.getModifiers() & Modifier.PRIVATE) == 0) { CtClass f = pool.makeClass(className + "LoadFactory"); f.addInterface(factory); CtMethod c = new CtMethod(create, f, null); c.setBody("return new " + className + "($1);"); f.addMethod(c); CtNewConstructor.defaultConstructor(f); } } else { preprocessMethods( cc, cc.subtypeOf(persistent) && cc != persistent, !className.startsWith("org.nachodb")); } } catch (Exception x) { x.printStackTrace(); } }
public void testMakeStaticMethod() throws Exception { CtClass cc = sloader.makeClass("test2.MakeStaticMethod"); CtMethod m = CtNewMethod.make( Modifier.PUBLIC | Modifier.STATIC, CtClass.intType, "create", new CtClass[] {CtClass.intType}, null, "{ return $1; }", cc); cc.addMethod(m); cc.addMethod(CtNewMethod.make("public int test() { return create(13); }", cc)); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(13, invoke(obj, "test")); }
public void testObjectSuper() throws Exception { CtClass cc = sloader.get("java.lang.Object"); try { cc.addMethod(CtNewMethod.make("public int foo(){ return super.hashCode(); }", cc)); fail("could access the super of java.lang.Object"); } catch (CannotCompileException e) { } }
public void testSuperInterface() throws Exception { CtClass cc = sloader.makeClass("test2.SuperInterface3"); CtClass cc2 = sloader.get("test2.SuperInterface2"); cc.addInterface(cc2); cc.addField(new CtField(cc2, "inner", cc)); CtMethod m = CtNewMethod.make("public int getAge() { return inner.getAge(); }", cc); cc.addMethod(m); cc.writeFile(); }
public void testArrayLength() throws Exception { CtClass cc = sloader.makeClass("test2.ArrayLength"); CtMethod m2 = CtNewMethod.make( "public int f() { String[] s = new String[3]; " + "return s.length; }", cc); cc.addMethod(m2); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(3, invoke(obj, "f")); }
public void testJIRA248() throws Exception { CtClass cc = sloader.get("test5.JIRA248"); String methodBody = "public int run() { return foo() + super.foo() + super.bar() + test5.JIRA248Intf2.super.baz(); }"; CtMethod ctMethod = CtMethod.make(methodBody, cc); cc.addMethod(ctMethod); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(40271, invoke(obj, "run")); }
public void testMethodInInner() throws Exception { CtClass inner = sloader.get("test2.Nested2$Inner"); CtClass outer = sloader.get("test2.Nested2"); String src = "public int f(test2.Nested2 n) {" + " n.i = 1; n.i++; n.i += 2; return n.i; }"; outer.writeFile(); try { CtMethod m = CtNewMethod.make(src, inner); fail(); } catch (RuntimeException e) { } outer.defrost(); CtMethod m = CtNewMethod.make(src, inner); inner.addMethod(m); src = "public int g(test2.Nested2 n) {" + " n.d = 1.0; n.d++; n.d += 2.0;" + " return n.d == 4.0 ? 7 : 8; }"; m = CtNewMethod.make(src, inner); inner.addMethod(m); src = "public int h(test2.Nested2 n) {" + " n.s = \"poi\";" + "return n.s.length() + f(n) + g(n); }"; m = CtNewMethod.make(src, inner); inner.addMethod(m); inner.writeFile(); outer.writeFile(); Object nobj = make(outer.getName()); Object iobj = make(inner.getName()); Method mth = iobj.getClass().getMethod("h", new Class[] {nobj.getClass()}); Object resobj = mth.invoke(iobj, new Object[] {nobj}); int res = ((Integer) resobj).intValue(); assertEquals(14, res); }
public void testStaticMember2() throws Exception { CtClass cc = sloader.get("test2.StaticMember2"); cc.addMethod( CtNewMethod.make( "public int run() {" + " return test2.StaticMember2.k + test2.StaticMember2.seven()" + " + (test2.StaticMember2.f + f)" + " + test2.StaticMember2.f + f; }", cc)); cc.addMethod( CtNewMethod.make( "public int run1() {" + " long j = 1L;" + " return (int)(j + (test2.StaticMember2.fj + fj)" + " + test2.StaticMember2.fj + fj); }", cc)); cc.addMethod( CtNewMethod.make( "public int run2() {" + " double x = 1.0;" + " double d = x + test2.StaticMember2.fd + fd" + " + (test2.StaticMember2.fd + fd);" + " return (int)(d * 10); }", cc)); cc.addMethod( CtNewMethod.make( "public int run3() {" + " return (test2.StaticMember2.fb & fb) ? 1 : 0; }", cc)); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(54, invoke(obj, "run")); assertEquals(53, invoke(obj, "run1")); assertEquals(958, invoke(obj, "run2")); assertEquals(0, invoke(obj, "run3")); }
public void testArrayInit() throws Exception { CtClass cc = sloader.makeClass("test2.ArrayInit"); cc.addMethod( CtNewMethod.make( "public int foo(){ " + " int[] i = new int[] { 1, 2 };" + " double[] d = new double[] { 3.0, 4.0 };" + " String[] s = new String[] { \"foo\", \"12345\" };" + " return i[0] + (int)d[0] + s[1].length(); }", cc)); cc.addMethod( CtNewMethod.make( "public int bar(){ " + " int[] i = { 1, 2.0 };" + " double[] d = { 3.0, 4 };" + " String[] s = { \"foo\", \"12345\" };" + " return i[0] + (int)d[0] + s[1].length(); }", cc)); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(9, invoke(obj, "foo")); assertEquals(9, invoke(obj, "bar")); }
public void testBrennan() throws Exception { CtClass cc = sloader.get("test2.Brennan"); cc.addMethod( CtNewMethod.make( "public int foo(){" + " java.text.SimpleDateFormat df;" + " if((df = (java.text.SimpleDateFormat)format) == null)" + " df = new java.text.SimpleDateFormat(\"yyyyMMdd\");" + " return 1;}", cc)); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(1, invoke(obj, "foo")); }
private void testDotClass(String cname, boolean java5) throws Exception { CtClass cc = sloader.makeClass(cname); if (java5) cc.getClassFile2().setVersionToJava5(); CtMethod m = CtNewMethod.make( "public String getclass() {" + " return int.class.getName() + int[].class.getName()" + " + String.class.getName() + String[].class.getName()" + " + java.lang.Object.class.getName()" + " + java.util.Vector.class.getName(); }", cc); cc.addMethod(m); CtMethod m2 = CtNewMethod.make( "public int test() {" + " String s = getclass(); System.out.println(s);" + " return s.length(); }", cc); cc.addMethod(m2); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(72, invoke(obj, "test")); }
public void testInner() throws Exception { ClassPool pool = ClassPool.getDefault(); String classname = "test2.Inner"; CtClass target = pool.get(classname); String src = "public void sampleMethod() throws Exception {" + "java.util.Properties props = new java.util.Properties();" + "java.rmi.activation.ActivationGroupDesc.CommandEnvironment ace " + " = null;" + "java.rmi.activation.ActivationGroupDesc agd " + " = new java.rmi.activation.ActivationGroupDesc(props,ace);}"; CtMethod newmethod = CtNewMethod.make(src, target); target.addMethod(newmethod); String src2 = "public java.lang.Character.Subset sampleMethod2() {" + " java.lang.Character.Subset s " + " = Character.UnicodeBlock.HIRAGANA; " + " return s; }"; CtMethod newmethod2 = CtNewMethod.make(src2, target); target.addMethod(newmethod2); target.writeFile(); }
public void testInheritance() throws Exception { ClassPool pool = ClassPool.getDefault(); String classname = "test2.Inherit"; CtClass target = pool.get(classname); String src = "public void sampleMethod() {" + " test2.Inherit i = new test2.Inherit();" + " test2.Inherit2 i2 = i;" + " test2.Inherit3 i3 = i;" + " i3.foo2(); i3.foo2(); i2.foo1(); }"; CtMethod newmethod = CtNewMethod.make(src, target); target.addMethod(newmethod); target.writeFile(); }
public void testIncOp() throws Exception { CtClass target = sloader.makeClass("test2.IncOp"); String src = "public int sample() {" + " int ia[] = new int[50];" + " ia[0] = 1;" + " int v = ++(ia[0]);" + " return v; }"; CtMethod newmethod = CtNewMethod.make(src, target); target.addMethod(newmethod); target.writeFile(); Object obj = make(target.getName()); assertEquals(2, invoke(obj, "sample")); }
private void changeMethod(CtClass ctClass, CtMethod ctMethod) throws CannotCompileException { // basically your before-advice... ctMethod.insertBefore("System.out.println(\"started method at \" + new java.util.Date());"); // basically your after-advice... ctMethod.insertAfter("System.out.println(\"ended method at \" + new java.util.Date());"); // basically your around-advice... String methodName = ctMethod.getName(); String proxyName = methodName + "_$proxy"; CtMethod proxy = CtNewMethod.copy(ctMethod, proxyName, ctClass, null); ctMethod.setName(ctMethod.getName() + "_orig"); proxy.setBody( "{ System.out.println(\"hoot!\"); return $proceed($$);}", "this", ctMethod.getName()); proxy.setName(methodName); ctClass.addMethod(proxy); }
private void testDotClass4(String cname, boolean java5) throws Exception { CtClass cc = sloader.makeClass(cname); if (java5) cc.getClassFile2().setVersionToJava5(); CtMethod m = CtNewMethod.make( "public int test() {" + " String s = Object.class.getName()" + " + Object[].class.getName();" + " return s.length(); }", cc); cc.addMethod(m); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(35, invoke(obj, "test")); }