당신이 트릭을 수행하는 자바 에이전트를 사용할 수 있습니다
에이전트는 매우 간단합니다 :
import java.lang.instrument.Instrumentation;
import java.lang.instrument.ClassFileTransformer;
public class ClassDumpAgent
{
/**
* This method is called before the application’s main-method is called, when
* this agent is specified to the Java VM.
**/
public static void premain(String agentArgs, Instrumentation inst)
{
ClassFileTransformer trans = new ClassDumpTransformer();
inst.addTransformer(trans);
}
}
ClassFileTransformer를 : 그것은 바이트 코드에 접근 할 수있는 수준의 변압기를, 등록 즉 단순히 사용되는 파일 시스템에 대한 바이트 코드로 바이트 배열 덤프된다
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
public class ClassDumpTransformer implements ClassFileTransformer
{
private File rootFolder = new File("C:\\temp\\dump");
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer)
throws IllegalClassFormatException
{
File dumpFile = new File(rootFolder,className+".class");
dumpFile.getParentFile().mkdirs();
try {
FileOutputStream out = new FileOutputStream(dumpFile);
try {
out.write(classfileBuffer);
} finally {
out.close();
}
} catch (IOException e) {
throw new IllegalClassFormatException(e.getMessage());
}
return classfileBuffer;
}
}
이 클래스 덤핑 에이전트 패키지를, 당신은 두 개의 클래스를 JAR 필요가 있고 포함 이 에이전트에 대한 MANIFEST.MF : 솔루션에 대한
java -javaagent:cldumpagent.jar MyApplication
일부 발언 :
-
이 같은 명령 줄을 사용하여이 에이전트로 응용 프로그램을 실행하려면 클래스는 하드 코딩 된 폴더 (C : \ TEMP \ DUMP)에 덤프됩니다. 은이를 변경하려고 할 수 있습니다.
- 변압기는 모두 클래스 (JDK 런타임 포함)를 덤프합니다. 덤프되는 패키지를 필터링 할 수 있습니다.
- 리버스 엔지니어링에주의하십시오. 일부 국가에서는 이것이 불법으로 간주 될 수 있습니다.
베리
사람이 아이디어가? –
'ClassLoader'가 에이전트가 명령 행에 지정되었을 때 작동을 거부한다면, 나중에 에이전트를 붙이기 만하면됩니다. 'ClassLoader'는'Instrumentation'을 좋아하는지 여부를 묻지 않으므로 그것을 막을 수 없습니다. – Holger