-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertiesClassFileTransformer.java
84 lines (69 loc) · 2.67 KB
/
PropertiesClassFileTransformer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Copyright (c) 2005 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/
package instr;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import java.util.Properties;
import javassist.CtClass;
import javassist.CtMethod;
/**
* PropertiesClassFileTransformer. (regex matching version, different from original)
*
* @author <a href="mailto:vavivavi@yahoo.co.jp">Naohide Sano</a> (nsano)
* @version 0.00 051215 nsano initial version <br>
*/
class PropertiesClassFileTransformer implements ClassFileTransformer {
/** */
public byte[] transform(ClassLoader loader,
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer)
throws IllegalClassFormatException {
if (className.matches(clazz)) {
System.err.println("PropertiesClassFileTransformer::transform: class: " + className);
try {
ByteArrayInputStream stream = new ByteArrayInputStream(classfileBuffer);
CtClass ctClass = GenericInstrumentation.getClassPool().makeClass(stream);
CtMethod ctMethod = ctClass.getDeclaredMethod(method);
System.err.println("PropertiesClassFileTransformer::transform: method: " + method);
ctMethod.insertBefore(instruction);
return ctClass.toBytecode();
} catch (Exception e) {
e.printStackTrace(System.err);
throw (IllegalClassFormatException) new IllegalClassFormatException().initCause(e);
}
} else {
return null;
}
}
/** */
static String instruction;
/** */
static String clazz;
/** */
static String method;
/* */
static {
try {
Properties props = new Properties();
props.load(GenericInstrumentation.class.getResourceAsStream("/PropertiesClassFileTransformer.properties"));
instruction = props.getProperty("instruction");
clazz = props.getProperty("class");
method = props.getProperty("method");
System.err.println("* PropertiesClassFileTransformer class will modify: ");
System.err.println(" class: " + clazz);
System.err.println(" method: " + method);
System.err.println(" instruction: " + instruction);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
/* */