code manipulation : 바이트 코드 변경하기(javaagent)

This commit is contained in:
haerong22
2021-02-18 22:16:32 +09:00
parent edd21fce02
commit 41890753df
3 changed files with 84 additions and 4 deletions

View File

@@ -0,0 +1,53 @@
<?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>org.example</groupId>
<artifactId>JavaAgent</artifactId>
<version>1.0-SNAPSHOT</version>
<name>JavaAgent</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.10.20</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
<key>value</key>
<Premain-Class>org.example.MasulsaAgent</Premain-Class>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@@ -0,0 +1,16 @@
package org.example;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.instrument.Instrumentation;
public class MasulsaAgent {
public static void premain(String agentArgs, Instrumentation inst) {
new AgentBuilder.Default()
.type(ElementMatchers.any())
.transform((builder, typeDescription, classLoader, javaModule) -> builder.method(ElementMatchers.named("pullOut")).intercept(FixedValue.value("Rabbit!!"))).installOn(inst);
}
}

View File

@@ -1,8 +1,10 @@
package org.example; package org.example;
import net.bytebuddy.ByteBuddy; import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.implementation.FixedValue; import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers; import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.pool.TypePool;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -11,20 +13,29 @@ public class Masulsa {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(new Moja().pullOut());
/* ============================================================= */
// ClassLoader classLoader = Masulsa.class.getClassLoader();
// TypePool typePool = TypePool.Default.of(classLoader);
//
// /** // /**
// * redefine() : Moja 클래스를 재정의 // * redefine() : Moja 클래스를 재정의
// * method() : Moja 클래스의 pullOut 메소드의 값을 Rabbit 으로 재정의 // * method() : Moja 클래스의 pullOut 메소드의 값을 Rabbit 으로 재정의
// * make().saveIn() : 재정의 후 지정한 위치에 저장 ( Moja.class 가 생성되는 파일 위치 지정 ) // * make().saveIn() : 재정의 후 지정한 위치에 저장 ( Moja.class 가 생성되는 파일 위치 지정 )
// */ // */
// try { // try {
// new ByteBuddy().redefine(Moja.class) // new ByteBuddy().redefine(typePool.describe("org.example.Moja").resolve(),
// ClassFileLocator.ForClassLoader.of(classLoader))
// .method(ElementMatchers.named("pullOut")).intercept(FixedValue.value("Rabbit!!")) // .method(ElementMatchers.named("pullOut")).intercept(FixedValue.value("Rabbit!!"))
// .make().saveIn(new File("C:\\Users\\Woojin\\Desktop\\study\\Study\\code-manipulation\\target\\classes\\")); // .make().saveIn(new File("C:\\Users\\Woojin\\Desktop\\study\\Study\\code-manipulation\\target\\classes\\"));
// } catch (IOException e) { // } catch (IOException e) {
// e.printStackTrace(); // e.printStackTrace();
// } // }
//
// 같이 실행하면 new Moja()에서 먼저 클래스가 로딩이 되기 때문에 위의 코드 재정의가 되지 않는다. // System.out.println(new Moja().pullOut());
System.out.println(new Moja().pullOut());
} }
} }