Skip to content

Commit

Permalink
Refactor .gitignore and MyTransformer.java
Browse files Browse the repository at this point in the history
- Refactor .gitignore to include Java and IntelliJ IDEA generated files, and miscellaneous files.
- Update MyTransformer.java to intercept the return value of the getAge() method in the Example class.
  • Loading branch information
Vghxv committed Sep 20, 2024
1 parent 7044911 commit 45a15a3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# Compiled source
**/out/
**.jar
# Java
**.jar
# Intellij idea generated files
.idea
*.iml
# misc
*.zip
27 changes: 21 additions & 6 deletions agent/src/myagent/MyTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,28 @@ public byte[] transform(ClassLoader loader,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws IllegalClassFormatException {
if (className.equals("theapp/HelloWorld")) {
if (className.equals("theapp/Example")) {
try {
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.makeClass(
new ByteArrayInputStream(classfileBuffer)
);
CtMethod m = cc.getDeclaredMethod("main");
m.setBody("{ System.out.println(\"Bye, Bye! ๐Ÿ–๏ธ\");}");
CtClass cc = cp.makeClass(new ByteArrayInputStream(classfileBuffer));
CtMethod getAgeMethod = cc.getDeclaredMethod("getAge");

// Add code to intercept the return value
getAgeMethod.insertBefore("{ System.out.println(\"Intercepted getAge()\"); }");
getAgeMethod.insertAfter("{ System.out.println(\"Return value: \" + $_); }");
// CtMethod m = cc.getDeclaredMethod("main");
// m.insertBefore("{ System.out.println(\"Hello, World! ๐ŸŒ\");}");
// m.insertAfter("{ System.out.println(\"Bye, Bye! ๐Ÿ–๏ธ๐Ÿ–๏ธ\");}");
// classfileBuffer = cc.toBytecode();

// CtMethod myFunction = cc.getDeclaredMethod("myFunction");
// myFunction.insertBefore("{ System.out.println(\"Calling myFunction with third = \" + $3 ); }");
// myFunction.insertBefore("{ System.out.println(\"Calling myFunction with second = \" + $2 ); }");
// myFunction.insertBefore("{ System.out.println(\"Calling myFunction with first = \" + $1 ); }");
//
// // Insert code after the original method call
// myFunction.insertAfter("{ System.out.println(\"Result: \" + $_); }");

classfileBuffer = cc.toBytecode();
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -37,6 +51,7 @@ public byte[] transform(ClassLoader loader,
e.printStackTrace();
}
}
System.out.println(className);
return classfileBuffer;
}
}
17 changes: 17 additions & 0 deletions helloworldapp/src/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
package theapp;
class Example{
private final int age;
public Example(int age){
this.age = age;
}

public int getAge(){
return age;
}
}
class HelloWorld {

public static String myFunction(int x, int y, String z) {
return String.valueOf(x + y) + "_" + z;
}

public static void main(String []args) {
System.out.println("Hello World! ๐Ÿ‘€");
Example example = new Example(25);
int age = example.getAge();
System.out.println("Hi my Age is " + age);
}
}

0 comments on commit 45a15a3

Please sign in to comment.