Java Code Examples for java.lang.instrument.Instrumentation#removeTransformer()
The following examples show how to use
java.lang.instrument.Instrumentation#removeTransformer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainAgent.java From COLA with GNU Lesser General Public License v2.1 | 6 votes |
public static void agentmain(String agentArgs, Instrumentation inst) throws ClassNotFoundException, UnmodifiableClassException, InterruptedException { final ColaTransformer transformer = new ColaTransformer(convert(agentArgs)); if(!isLock(transformer)){ return; } try { inst.addTransformer(transformer, true); Set<Class<?>> oriClassSet = searchClass(inst, transformer.getArgs().getClassName(), 1000); final Class<?>[] classArray = new Class<?>[oriClassSet.size()]; System.arraycopy(oriClassSet.toArray(), 0, classArray, 0, oriClassSet.size()); inst.retransformClasses(classArray); }finally { inst.removeTransformer(transformer); } System.out.println("agentmain DONE"); }
Example 2
Source File: InstrumentationUtils.java From arthas with Apache License 2.0 | 6 votes |
public static void retransformClasses(Instrumentation inst, ClassFileTransformer transformer, Set<Class<?>> classes) { try { inst.addTransformer(transformer, true); for (Class<?> clazz : classes) { try { inst.retransformClasses(clazz); } catch (Throwable e) { String errorMsg = "retransformClasses class error, name: " + clazz.getName(); if (ClassUtils.isLambdaClass(clazz) && e instanceof VerifyError) { errorMsg += ", Please ignore lambda class VerifyError: https://github.com/alibaba/arthas/issues/675"; } logger.error(errorMsg, e); } } } finally { inst.removeTransformer(transformer); } }
Example 3
Source File: LambdaTransformBootloader.java From pinpoint with Apache License 2.0 | 6 votes |
private void retransform(Instrumentation instrumentation) { final String lambdaMetaFactoryName = "java.lang.invoke.InnerClassLambdaMetafactory"; try { final Class<?> lamdbaFactoryClazz = Class.forName(lambdaMetaFactoryName, false, null); logger.info("retransformClasses:{}", lamdbaFactoryClazz); final ClassFileTransformer classFileTransfomrer = new InnerClassLambdaMetafactoryTransformer(); instrumentation.addTransformer(classFileTransfomrer, true); try { instrumentation.retransformClasses(lamdbaFactoryClazz); } finally { instrumentation.removeTransformer(classFileTransfomrer); } } catch (Exception e) { logger.warn("retransform fail class:{}", lambdaMetaFactoryName, e); } }
Example 4
Source File: Assembler.java From tomee with Apache License 2.0 | 6 votes |
@Override public void destroy(final String unitId) { final List<ClassFileTransformer> transformers = this.transformers.remove(unitId); if (transformers != null) { final Instrumentation instrumentation = Agent.getInstrumentation(); if (instrumentation != null) { for (final ClassFileTransformer transformer : transformers) { instrumentation.removeTransformer(transformer); } } else { final Assembler assembler = SystemInstance.get().getComponent(Assembler.class); if (assembler != null) { assembler.logger.info("assembler.noAgent"); }else { System.err.println("destroy: Assembler not initialized: JAVA AGENT NOT INSTALLED"); } } } }
Example 5
Source File: Enhancer.java From bistoury with GNU General Public License v3.0 | 5 votes |
public static void enhance(Instrumentation inst, ClassFileTransformer transformer, Set<Class<?>> classes) throws UnmodifiableClassException { try { inst.addTransformer(transformer, true); int size = classes.size(); Class<?>[] classArray = new Class<?>[size]; arraycopy(classes.toArray(), 0, classArray, 0, size); if (classArray.length > 0) { inst.retransformClasses(classArray); } } finally { inst.removeTransformer(transformer); } }
Example 6
Source File: Enhancer.java From arthas with Apache License 2.0 | 5 votes |
private static void enhance(Instrumentation inst, ClassFileTransformer transformer, Set<Class<?>> classes) throws UnmodifiableClassException { try { inst.addTransformer(transformer, true); int size = classes.size(); Class<?>[] classArray = new Class<?>[size]; arraycopy(classes.toArray(), 0, classArray, 0, size); if (classArray.length > 0) { inst.retransformClasses(classArray); } } finally { inst.removeTransformer(transformer); } }
Example 7
Source File: ClassReloadingStrategy.java From byte-buddy with Apache License 2.0 | 5 votes |
@Override protected void apply(Instrumentation instrumentation, Map<Class<?>, ClassDefinition> classDefinitions) throws UnmodifiableClassException { ClassRedefinitionTransformer classRedefinitionTransformer = new ClassRedefinitionTransformer(classDefinitions); synchronized (this) { DISPATCHER.addTransformer(instrumentation, classRedefinitionTransformer, REDEFINE_CLASSES); try { DISPATCHER.retransformClasses(instrumentation, classDefinitions.keySet().toArray(new Class<?>[0])); } finally { instrumentation.removeTransformer(classRedefinitionTransformer); } } classRedefinitionTransformer.assertTransformation(); }
Example 8
Source File: ClassReloadingStrategy.java From byte-buddy with Apache License 2.0 | 5 votes |
@Override public void reset(Instrumentation instrumentation, ClassFileLocator classFileLocator, List<Class<?>> types) throws UnmodifiableClassException, ClassNotFoundException { for (Class<?> type : types) { if (!DISPATCHER.isModifiableClass(instrumentation, type)) { throw new IllegalArgumentException("Cannot modify type: " + type); } } DISPATCHER.addTransformer(instrumentation, ClassResettingTransformer.INSTANCE, REDEFINE_CLASSES); try { DISPATCHER.retransformClasses(instrumentation, types.toArray(new Class<?>[0])); } finally { instrumentation.removeTransformer(ClassResettingTransformer.INSTANCE); } }
Example 9
Source File: Enhancer.java From bistoury with GNU General Public License v3.0 | 4 votes |
/** * 对象增强 * * @param inst inst * @param adviceId 通知ID * @param isTracing 可跟踪方法调用 * @param skipJDKTrace 是否忽略对JDK内部方法的跟踪 * @param classNameMatcher 类名匹配 * @param methodNameMatcher 方法名匹配 * @return 增强影响范围 * @throws UnmodifiableClassException 增强失败 */ public static synchronized EnhancerAffect enhance( final Instrumentation inst, final int adviceId, final boolean isTracing, final boolean skipJDKTrace, final Matcher classNameMatcher, final Matcher methodNameMatcher) throws UnmodifiableClassException { final EnhancerAffect affect = new EnhancerAffect(); // 获取需要增强的类集合 final Set<Class<?>> enhanceClassSet = GlobalOptions.isDisableSubClass ? SearchUtils.searchClass(inst, classNameMatcher) : SearchUtils.searchSubClass(inst, SearchUtils.searchClass(inst, classNameMatcher)); // 过滤掉无法被增强的类 filter(enhanceClassSet); // 构建增强器 final Enhancer enhancer = new Enhancer(adviceId, isTracing, skipJDKTrace, enhanceClassSet, methodNameMatcher, affect); try { inst.addTransformer(enhancer, true); // 批量增强 if (GlobalOptions.isBatchReTransform) { final int size = enhanceClassSet.size(); final Class<?>[] classArray = new Class<?>[size]; arraycopy(enhanceClassSet.toArray(), 0, classArray, 0, size); if (classArray.length > 0) { inst.retransformClasses(classArray); logger.info("Success to batch transform classes: " + Arrays.toString(classArray)); } } else { // for each 增强 for (Class<?> clazz : enhanceClassSet) { try { inst.retransformClasses(clazz); logger.info("Success to transform class: " + clazz); } catch (Throwable t) { logger.warn("retransform {} failed.", clazz, t); if (t instanceof UnmodifiableClassException) { throw (UnmodifiableClassException) t; } else if (t instanceof RuntimeException) { throw (RuntimeException) t; } else { throw new RuntimeException(t); } } } } } finally { inst.removeTransformer(enhancer); } return affect; }