commit 4ef7145148

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.seele</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

@ -0,0 +1,29 @@
package com.seele;
public class Main {
public int print(int i) {
System.out.println("i: " + i);
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
return i + 2;
}
public void run(){
int i = new Trans().getNumber();
while (i < 1000){
try{
i = print(i);
}catch (Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Main main = new Main();
main.run();
}
}

@ -0,0 +1,7 @@
package com.seele;
public class Trans {
public int getNumber() {
return 1;
}
}

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.seele</groupId>
<artifactId>simple_agent</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<!-- <manifest>-->
<!-- <addClasspath>true</addClasspath>-->
<!-- </manifest>-->
<manifestEntries>
<Built-By>neo</Built-By>
<Premain-Class>com.seele.SimpleAgent</Premain-Class>
<Agent-Class>com.seele.SimpleAgent</Agent-Class>
<!-- <Main-Class>com.janetfilter.core.Launcher</Main-Class>-->
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<!-- <Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>-->
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,16 @@
package com.seele;
import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import java.io.IOException;
public class AttachMain {
public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
VirtualMachine vm = VirtualMachine.attach("15168");
vm.loadAgent("D:\\Code\\learn\\java_agent\\simple_agent\\target\\simple_agent-1.0-jar-with-dependencies.jar");
}
}

@ -0,0 +1,28 @@
package com.seele;
import java.lang.instrument.Instrumentation;
public class SimpleAgent {
/**
* jvm
*
* @param agentArgs
* @param inst
*/
public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("premain");
inst.addTransformer(new Transformer());
}
/**
* attach
*
* @param agentArgs
* @param inst
*/
public static void agentmain(String agentArgs, Instrumentation inst) {
System.out.println("agentmain");
}
}

@ -0,0 +1,46 @@
package com.seele;
import java.io.*;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
public class Transformer implements ClassFileTransformer {
private static String classNumberReturn2 = "D:\\Code\\learn\\java_agent\\simple_agent\\src\\main\\java\\com\\seele\\Trans.class";
private static byte[] getBytesFromFile(String filename) {
try {
File file = new File(filename);
InputStream is = new FileInputStream(file);
long length = file.length();
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file." + filename);
}
is.close();
return bytes;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public byte[] transform(ClassLoader loader, String className,
Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)
throws IllegalClassFormatException {
if (!className.equals("com/seele/Trans")) {
return ClassFileTransformer.super.transform(loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
}
return getBytesFromFile(classNumberReturn2);
}
}
Loading…
Cancel
Save