本文整理匯總了Java中java.lang.instrument.Instrumentation.addTransformer方法的典型用法代碼示例。如果您正苦於以下問題:Java Instrumentation.addTransformer方法的具體用法?Java Instrumentation.addTransformer怎麽用?Java Instrumentation.addTransformer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.instrument.Instrumentation
的用法示例。
在下文中一共展示了Instrumentation.addTransformer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initTransformer
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
/**
* 初始化類字節碼的轉換器
*
* @param inst 用於管理字節碼轉換器
*/
private static void initTransformer(Instrumentation inst) throws UnmodifiableClassException {
LinkedList<Class> retransformClasses = new LinkedList<Class>();
CustomClassTransformer customClassTransformer = new CustomClassTransformer();
inst.addTransformer(customClassTransformer, true);
Class[] loadedClasses = inst.getAllLoadedClasses();
for (Class clazz : loadedClasses) {
for (final AbstractClassHook hook : customClassTransformer.getHooks()) {
if (hook.isClassMatched(clazz.getName().replace(".", "/"))) {
if (inst.isModifiableClass(clazz) && !clazz.getName().startsWith("java.lang.invoke.LambdaForm")) {
retransformClasses.add(clazz);
}
}
}
}
// hook已經加載的類
Class[] classes = new Class[retransformClasses.size()];
retransformClasses.toArray(classes);
if (classes.length > 0) {
inst.retransformClasses(classes);
}
}
示例2: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(String args, Instrumentation instrumentation) {
if (!instrumentation.isRetransformClassesSupported()) {
System.out.println("Class retransformation is not supported.");
return;
}
System.out.println("Calling lambda to ensure that lambda forms were created");
Agent.lambda.run();
System.out.println("Registering class file transformer");
instrumentation.addTransformer(new Agent());
for (Class c : instrumentation.getAllLoadedClasses()) {
if (c.getName().contains("LambdaForm") &&
instrumentation.isModifiableClass(c)) {
System.out.format("We've found a modifiable lambda form: %s%n", c.getName());
try {
instrumentation.retransformClasses(c);
} catch (UnmodifiableClassException e) {
throw new AssertionError("Modification of modifiable class " +
"caused UnmodifiableClassException", e);
}
}
}
}
示例3: agentmain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {
if (inst.isRedefineClassesSupported() && inst.isRetransformClassesSupported()) {
inst.addTransformer(new FooTransformer(), true);
Class<?>[] allClasses = inst.getAllLoadedClasses();
for (int i = 0; i < allClasses.length; i++) {
Class<?> c = allClasses[i];
if (c == Foo.class) {
inst.retransformClasses(new Class<?>[]{c});
}
}
}
}
示例4: startUp
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static synchronized void startUp(){
if(!isLoaded){
try {
loadAgent();
Instrumentation inst = instrumentation();
inst.addTransformer(new CodeTransformer(), true);
isLoaded = true;
}catch (Throwable e){
logger.warn("start agentTool failed",e);
}
}
}
示例5: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(String agentArgs, Instrumentation inst) throws Exception {
LoggingTransformer t = new LoggingTransformer();
inst.addTransformer(t, true);
{
Class demoClass = Class.forName("RedefineLeak$Tester");
for (int i = 0; i < 10000; i++) {
inst.retransformClasses(demoClass);
}
}
System.gc();
}
示例6: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(final String agentArgs,
final Instrumentation inst) throws Exception {
System.out.printf("Starting %s\n",
MetricsCollectionJavassistAgent.class.getSimpleName());
inst.addTransformer(new MetricsCollectionTransformer());
}
示例7: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(String args, Instrumentation instrumentation) throws ClassNotFoundException { // magic
instrumentation.addTransformer(new TailRecClassFileTransformer());
}
示例8: agentmain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception {
Agent transformer = new Agent();
instrumentation.addTransformer(transformer, true);
redefine(agentArgs, instrumentation, Test.class);
}
示例9: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(String args, Instrumentation instr){
System.out.format("[%s] [logging agent]\n", Thread.currentThread().getName());
instr.addTransformer(new ClassLoadingLogger());
}
示例10: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(String agentArgs, Instrumentation inst) {
final SimpleClassTransformer transformer = new SimpleClassTransformer();
inst.addTransformer(transformer);
}
示例11: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(String agentArgs, Instrumentation inst) {
inst.addTransformer(new DummyAgent(), false);
}
示例12: make
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void make(final AgentArgs agentArgs, Instrumentation instrumentation) {
try {
if (!AgentArgs.valida(agentArgs)) {
throw new InterceptorException("JavaAgent參數設置錯誤");
}
ConnectorMatcher<Type> readBeforeMatcher;
switch (agentArgs.getNameMatcherRule()) {
case NAME_ALL:
readBeforeMatcher = TypeMatchers.any();
break;
case NAME_START_WITH:
readBeforeMatcher = TypeMatchers.nameStartsWith(agentArgs.getMatchRule());
break;
case NAME_END_WITH:
readBeforeMatcher = TypeMatchers.nameEndsWith(agentArgs.getMatchRule());
break;
case NAMED:
readBeforeMatcher = TypeMatchers.named(agentArgs.getMatchRule());
break;
case NAME_CONTAINS:
readBeforeMatcher = TypeMatchers.nameContains(agentArgs.getMatchRule());
break;
default:
throw new InterceptorException("JavaAgent必須設置正確的代理實現方式.");
}
ConnectorMatcher<ClassNode> readAfterMather = ClassMatchers.not(ClassMatchers.isInterface());
ConnectorMatcher<MethodNode> methodMather = MethodMatchers.not(MethodMatchers.isAbstract())
.and(MethodMatchers.not(MethodMatchers.isConstructor()))
.and(MethodMatchers.not(MethodMatchers.isTypeInitializer()))
.and(MethodMatchers.not(MethodMatchers.named("main")))
.and(MethodMatchers.withAnnotation(ForExample.class)
.or(MethodMatchers.withAnnotation(ForExample1.class)));
Interceptor interceptor = new ExampleInterceptor(
readBeforeMatcher,
readAfterMather,
methodMather);
String interceptorId = Interceptor.registerInterceptor(interceptor);
instrumentation.addTransformer((loader, className, classBeingRedefined, protectionDomain, classfileBuffer) -> {
if (!isAncestor(TransformerMaker.class.getClassLoader(), loader) && !isTarget(readBeforeMatcher,className)) {
return classfileBuffer;
}
return AccessController.doPrivileged((PrivilegedAction<byte[]>) () -> ClassModifier.modify(className, classfileBuffer, interceptorId));
});
} catch (Throwable th) {
th.printStackTrace(System.err);
}
}
示例13: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(final String agentArgs, final Instrumentation inst) {
System.out.println("premain...");
instrumentation = inst;
inst.addTransformer(new SleepTransformer());
}
示例14: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(String args, Instrumentation inst)
{
inst.addTransformer(new Transformer(args));
}
示例15: premain
import java.lang.instrument.Instrumentation; //導入方法依賴的package包/類
public static void premain(String agentArguments, Instrumentation instrumentation) {
log("premain() is called, arguments = " + agentArguments);
classesToTransform = agentArguments.split(",");
instrumentation.addTransformer(new SimpleTransformer(), /*canRetransform=*/true);
}