This file contains 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
package com.dlmu.agent.test; | |
/** | |
* Created by fupan on 16-4-2. | |
*/ | |
public class HelloWorld { | |
public static void main(String[] args) { | |
System.out.println("hello, world."); | |
} |
This file contains 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
hey,hey,hey |
This file contains 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
package com.dlmu.agent; | |
import java.lang.instrument.Instrumentation; | |
/** | |
* Created by fupan on 16-4-2. | |
*/ | |
public class AgentTest { | |
private static Instrumentation inst = null; |
This file contains 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
private static Instrumentation instrumentation() { | |
ClassLoader mainAppLoader = ClassLoader.getSystemClassLoader(); | |
try { | |
Class<?> javaAgentClass = mainAppLoader.loadClass("com.dlmu.agent.AgentTest"); | |
Method method = javaAgentClass.getDeclaredMethod("instrumentation", new Class[0]); | |
return (Instrumentation) method.invoke(null, new Object[0]); | |
} catch (Throwable e) { | |
System.out.println("can not get agent class" + e.getStackTrace()); | |
return null; | |
} |
This file contains 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
private static void addCode(Instrumentation instrumentation) { | |
Class[] allLoadedClasses = instrumentation.getAllLoadedClasses(); | |
System.out.println("all load classes length:" + allLoadedClasses.length); | |
System.out.println("object size:" + instrumentation.getObjectSize(new Object())); | |
instrumentation.addTransformer(new ClassFileTransformer() { | |
@Override | |
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { | |
if (classBeingRedefined != null) return null; | |
System.out.println("class transform " + className); | |
return classfileBuffer; |
This file contains 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
private String jarFilePath = "/home/fupan/IdeaProjects/agentTest/agent-jar/target/agent-jar-1.0-SNAPSHOT.jar"; | |
private static void attachAgent() { | |
TestAgent testAgent = new TestAgent(); | |
testAgent.loadAgent(); | |
} | |
void loadAgent() { | |
VirtualMachine vm; |
This file contains 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
/** | |
* Registers the supplied transformer. | |
* <P> | |
* Same as <code>addTransformer(transformer, false)</code>. | |
* | |
* @param transformer the transformer to register | |
* @throws java.lang.NullPointerException if passed a <code>null</code> transformer | |
* @see #addTransformer(ClassFileTransformer,boolean) | |
*/ | |
void |
This file contains 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
package com.dlmu.agent.server; | |
import java.lang.instrument.ClassFileTransformer; | |
import java.lang.instrument.IllegalClassFormatException; | |
import java.security.ProtectionDomain; | |
/** | |
* Created by fupan on 16-4-9. | |
*/ | |
public class SimpleClassFileTransformer implements ClassFileTransformer { |
This file contains 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
package com.dlmu.agent.server; | |
import net.bytebuddy.agent.builder.AgentBuilder; | |
import net.bytebuddy.description.type.TypeDescription; | |
import net.bytebuddy.dynamic.DynamicType; | |
import net.bytebuddy.implementation.MethodDelegation; | |
import net.bytebuddy.implementation.SuperMethodCall; | |
import net.bytebuddy.matcher.ElementMatchers; | |
import java.lang.instrument.ClassFileTransformer; |
This file contains 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
package com.dlmu.agent.server; | |
import net.bytebuddy.implementation.bind.annotation.Origin; | |
import java.lang.reflect.Method; | |
/** | |
* Created by fupan on 16-4-9. | |
*/ | |
public class DelagateLogging { |
OlderNewer