Skip to content

Commit dfc949d

Browse files
author
jossc
committed
1.添加基础算法
1 parent 4594e48 commit dfc949d

11 files changed

Lines changed: 209 additions & 0 deletions

File tree

.idea/compiler.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agent/pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>basics.code</artifactId>
7+
<groupId>com.cz</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<packaging>jar</packaging>
13+
14+
<artifactId>agent</artifactId>
15+
16+
<build>
17+
<finalName>my-javaagent</finalName>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-jar-plugin</artifactId>
22+
<configuration>
23+
<archive>
24+
<manifestEntries>
25+
<Agent-Class>com.cz.agent.AgentMain</Agent-Class>
26+
<Premain-Class>com.cz.agent.AgentPreMain</Premain-Class>
27+
<Can-Redefine-Classes>true</Can-Redefine-Classes>
28+
<Can-Retransform-Classes>true</Can-Retransform-Classes>
29+
</manifestEntries>
30+
</archive>
31+
</configuration>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<configuration>
37+
<source>8</source>
38+
<target>8</target>
39+
</configuration>
40+
</plugin>
41+
42+
</plugins>
43+
</build>
44+
45+
46+
47+
</project>

agent/src/main/java/Test1.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @ClassName Test1
3+
* @Author chenzhuo
4+
* @Version 1.0
5+
* @Date 2019-09-04 22:22
6+
**/
7+
public class Test1 {
8+
public static void main(String[] args) {
9+
System.out.println("in test01 main");
10+
}
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.cz.agent;
2+
3+
import java.lang.instrument.Instrumentation;
4+
5+
/**
6+
* @ClassName com.cz.AgentMain
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-09-04 22:17
10+
**/
11+
public class AgentMain {
12+
13+
public static void agentmain(String agentArgs, Instrumentation inst) {
14+
System.out.println("agentmain called: " + agentArgs);
15+
/**
16+
* 传入什么就返回什么
17+
* 不修改class 二进制文件
18+
*/
19+
inst.addTransformer((loader, className, classBeingRedefined,
20+
protectionDomain, classfileBuffer) -> {
21+
System.out.println("agentmain load Class :" + className);
22+
return classfileBuffer;
23+
},true);
24+
}
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.cz.agent;
2+
3+
import java.io.IOException;
4+
import java.lang.instrument.ClassFileTransformer;
5+
import java.lang.instrument.IllegalClassFormatException;
6+
import java.lang.instrument.Instrumentation;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.security.ProtectionDomain;
11+
12+
/**
13+
* @ClassName AgentPreMain
14+
* @Author chenzhuo
15+
* @Version 1.0
16+
* @Date 2019-09-04 22:20
17+
**/
18+
public class AgentPreMain {
19+
20+
public static class ClassLoggerTransformer implements ClassFileTransformer {
21+
22+
@Override
23+
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
24+
System.out.println("transform: " + className);
25+
Path path = Paths.get("/tmp/" + className.replaceAll("/", ".") + ".class");
26+
try {
27+
Files.write(path, classfileBuffer);
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}
31+
return classfileBuffer;
32+
}
33+
}
34+
35+
public static void premain(String agentArgument, Instrumentation instrumentation) {
36+
ClassFileTransformer classFileTransformer = new ClassLoggerTransformer();
37+
instrumentation.addTransformer(classFileTransformer, true);
38+
}
39+
}

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
</plugin>
2121
</plugins>
2222
</build>
23+
<modules>
24+
<module>agent</module>
25+
</modules>
2326

2427

2528
<dependencies>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* 基础算法
3+
*/
4+
package com.algorithm;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.concurrent.map;
2+
3+
import java.util.concurrent.ConcurrentHashMap;
4+
5+
/**
6+
* @ClassName ConcurrentHashMapLearn
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-08-20 22:40
10+
**/
11+
public class ConcurrentHashMapLearn {
12+
13+
public static void main(String[] args) {
14+
ConcurrentHashMap<String, String> concurrentHashMap
15+
= new ConcurrentHashMap(16);
16+
concurrentHashMap.put("hhhh", "cccc");
17+
System.err.println(concurrentHashMap.entrySet());
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.jvm.bytecode;
2+
3+
import java.lang.reflect.Method;
4+
5+
/**
6+
* @ClassName ReflectionTest
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-08-28 22:25
10+
**/
11+
public class ReflectionTest {
12+
13+
private static int count = 0;
14+
15+
public static void foo(){
16+
new Exception("test#" + (count++)).printStackTrace();
17+
}
18+
19+
public static void main(String[] args) throws Exception {
20+
Class<?> clazz= Class.forName("com.jvm.bytecode.ReflectionTest");
21+
Method method = clazz.getMethod("foo");
22+
for (int i = 0; i < 20; i++) {
23+
method.invoke(null);
24+
}
25+
26+
}
27+
28+
}

0 commit comments

Comments
 (0)