Created
June 12, 2015 12:25
-
-
Save lsharada/b3cc7b4a726791cbaec8 to your computer and use it in GitHub Desktop.
java assist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Method; | |
import javassist.*; | |
import java.security.*; | |
import java.lang.instrument.*; | |
class Hello { | |
public void say(int a) { | |
System.out.println("Hello"+a); | |
} | |
public static Object newInstance() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
} | |
class foo { | |
public static void main(String args[]) { | |
foo f = new foo(); | |
f.doIt(); | |
} | |
void doIt() { | |
Object paramsObj[] = {}; | |
try { | |
System.out.println("ran doIt() method"); | |
ClassPool pool = ClassPool.getDefault(); | |
pool.insertClassPath(new ClassClassPath(this.getClass())); | |
CtClass cc = pool.makeClass("Point"); | |
CtClass cc1 = pool.get("Point"); | |
cc1.setName("Pair"); | |
ClassPool cp = ClassPool.getDefault(); | |
CtClass cc2 = cp.get("Hello"); | |
CtMethod m = cc2.getDeclaredMethod("say"); | |
CtMethod newmethod = CtNewMethod.make("public void testPrint(int a) { System.out.println(\"test ok\"+a); }",cc2); | |
cc2.addMethod(newmethod); | |
cc2.writeFile(); | |
System.out.println("testing ct"+m); | |
CtMethod m1 = cc2.getDeclaredMethod("testPrint"); | |
System.out.println("testing ct -- new method added"+m1); | |
byte[] myBytes = null; | |
for(Method me: cc2.toClass().getDeclaredMethods()){ //test print, ok | |
Hello h = new Hello(); | |
Object result = null; | |
System.out.println("declared methods"+me.getName()); | |
if((me.getName()).equals("testPrint")) | |
{ | |
result = me.invoke(h,10); | |
System.out.println("declared methods exec:"+result.toString()); | |
} | |
} | |
Thread.sleep(5000); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Method; | |
import javassist.*; | |
import java.security.*; | |
import java.lang.instrument.*; | |
class Hello { | |
public void say(int a) { | |
System.out.println("Hello"+a); | |
} | |
public static Object newInstance() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
} | |
class foo { | |
public static void main(String args[]) { | |
foo f = new foo(); | |
f.doIt(); | |
} | |
void doIt() { | |
Object paramsObj[] = {}; | |
try { | |
System.out.println("ran doIt() method"); | |
ClassPool pool = ClassPool.getDefault(); | |
pool.insertClassPath(new ClassClassPath(this.getClass())); | |
CtClass cc = pool.makeClass("Point"); | |
CtClass cc1 = pool.get("Point"); | |
cc1.setName("Pair"); | |
ClassPool cp = ClassPool.getDefault(); | |
CtClass cc2 = cp.get("Hello"); | |
CtMethod m = cc2.getDeclaredMethod("say"); | |
CtMethod newmethod = CtNewMethod.make("public void testPrint(int a) { System.out.println(\"test ok\"+a); }",cc2); | |
cc2.addMethod(newmethod); | |
cc2.writeFile(); | |
System.out.println("testing ct"+m); | |
CtMethod m1 = cc2.getDeclaredMethod("testPrint"); | |
System.out.println("testing ct -- new method added"+m1); | |
byte[] myBytes = null; | |
for(Method me: cc2.toClass().getDeclaredMethods()){ //test print, ok | |
Hello h = new Hello(); | |
Object result = null; | |
System.out.println("declared methods"+me.getName()); | |
if((me.getName()).equals("testPrint")) | |
{ | |
result = me.invoke(h,10); | |
System.out.println("declared methods exec:"+result.toString()); | |
} | |
} | |
Thread.sleep(5000); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment