java.lang.instrument.ClassFileTransformer Java Examples
The following examples show how to use
java.lang.instrument.ClassFileTransformer.
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: WebappClassLoaderBase.java From Tomcat8-Source-Read with MIT License | 6 votes |
/** * Adds the specified class file transformer to this class loader. The * transformer will then be able to modify the bytecode of any classes * loaded by this class loader after the invocation of this method. * * @param transformer The transformer to add to the class loader */ @Override public void addTransformer(ClassFileTransformer transformer) { if (transformer == null) { throw new IllegalArgumentException(sm.getString( "webappClassLoader.addTransformer.illegalArgument", getContextName())); } if (this.transformers.contains(transformer)) { // if the same instance of this transformer was already added, bail out log.warn(sm.getString("webappClassLoader.addTransformer.duplicate", transformer, getContextName())); return; } this.transformers.add(transformer); log.info(sm.getString("webappClassLoader.addTransformer", transformer, getContextName())); }
Example #2
Source File: InstrumentationImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
public synchronized void addTransformer(ClassFileTransformer transformer, boolean canRetransform) { if (transformer == null) { throw new NullPointerException("null passed as 'transformer' in addTransformer"); } if (canRetransform) { if (!isRetransformClassesSupported()) { throw new UnsupportedOperationException( "adding retransformable transformers is not supported in this environment"); } if (mRetransfomableTransformerManager == null) { mRetransfomableTransformerManager = new TransformerManager(true); } mRetransfomableTransformerManager.addTransformer(transformer); if (mRetransfomableTransformerManager.getTransformerCount() == 1) { setHasRetransformableTransformers(mNativeAgent, true); } } else { mTransformerManager.addTransformer(transformer); } }
Example #3
Source File: MapReduceInputFormatTracerTest.java From garmadon with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce public void InputFormatTracer_should_intercept_InputFormat_direct_implementor() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); //Install tracer ClassFileTransformer classFileTransformer = new MapReduceTracer.InputFormatTracer().installOnByteBuddyAgent(); try { //Call InputFormat Class<?> type = classLoader.loadClass(MapReduceInputFormatTestClasses.OneLevelHierarchy.class.getName()); invokeRecordReader(type); invokeListInputSplits(type); //Verify mock interaction verify(eventHandler, times(2)).accept(any(Long.class), argument.capture()); DataAccessEventProtos.PathEvent pathEvent = DataAccessEventProtos.PathEvent .newBuilder() .setPath(inputPath) .setType(PathType.INPUT.name()) .build(); assertEquals(pathEvent, argument.getValue()); } finally { ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer); } }
Example #4
Source File: InstrumentationImpl.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
public synchronized void addTransformer(ClassFileTransformer transformer, boolean canRetransform) { if (transformer == null) { throw new NullPointerException("null passed as 'transformer' in addTransformer"); } if (canRetransform) { if (!isRetransformClassesSupported()) { throw new UnsupportedOperationException( "adding retransformable transformers is not supported in this environment"); } if (mRetransfomableTransformerManager == null) { mRetransfomableTransformerManager = new TransformerManager(true); } mRetransfomableTransformerManager.addTransformer(transformer); if (mRetransfomableTransformerManager.getTransformerCount() == 1) { setHasRetransformableTransformers(mNativeAgent, true); } } else { mTransformerManager.addTransformer(transformer); } }
Example #5
Source File: ReflectiveLoadTimeWeaver.java From spring-analysis-note with MIT License | 6 votes |
/** * Create a new SimpleLoadTimeWeaver for the given class loader. * @param classLoader the {@code ClassLoader} to delegate to for * weaving (<i>must</i> support the required weaving methods). * @throws IllegalStateException if the supplied {@code ClassLoader} * does not support the required weaving methods */ public ReflectiveLoadTimeWeaver(@Nullable ClassLoader classLoader) { Assert.notNull(classLoader, "ClassLoader must not be null"); this.classLoader = classLoader; Method addTransformerMethod = ClassUtils.getMethodIfAvailable( this.classLoader.getClass(), ADD_TRANSFORMER_METHOD_NAME, ClassFileTransformer.class); if (addTransformerMethod == null) { throw new IllegalStateException( "ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide an " + "'addTransformer(ClassFileTransformer)' method."); } this.addTransformerMethod = addTransformerMethod; Method getThrowawayClassLoaderMethod = ClassUtils.getMethodIfAvailable( this.classLoader.getClass(), GET_THROWAWAY_CLASS_LOADER_METHOD_NAME); // getThrowawayClassLoader method is optional if (getThrowawayClassLoaderMethod == null) { if (logger.isDebugEnabled()) { logger.debug("The ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide a " + "'getThrowawayClassLoader()' method; SimpleThrowawayClassLoader will be used instead."); } } this.getThrowawayClassLoaderMethod = getThrowawayClassLoaderMethod; }
Example #6
Source File: InstrumentationImpl.java From hottub with GNU General Public License v2.0 | 6 votes |
public synchronized void setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) { if (!isNativeMethodPrefixSupported()) { throw new UnsupportedOperationException( "setNativeMethodPrefix is not supported in this environment"); } if (transformer == null) { throw new NullPointerException( "null passed as 'transformer' in setNativeMethodPrefix"); } TransformerManager mgr = findTransformerManager(transformer); if (mgr == null) { throw new IllegalArgumentException( "transformer not registered in setNativeMethodPrefix"); } mgr.setNativeMethodPrefix(transformer, prefix); String[] prefixes = mgr.getNativeMethodPrefixes(); setNativeMethodPrefixes(mNativeAgent, prefixes, mgr.isRetransformable()); }
Example #7
Source File: InstrumentationImpl.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
public synchronized void setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) { if (!isNativeMethodPrefixSupported()) { throw new UnsupportedOperationException( "setNativeMethodPrefix is not supported in this environment"); } if (transformer == null) { throw new NullPointerException( "null passed as 'transformer' in setNativeMethodPrefix"); } TransformerManager mgr = findTransformerManager(transformer); if (mgr == null) { throw new IllegalArgumentException( "transformer not registered in setNativeMethodPrefix"); } mgr.setNativeMethodPrefix(transformer, prefix); String[] prefixes = mgr.getNativeMethodPrefixes(); setNativeMethodPrefixes(mNativeAgent, prefixes, mgr.isRetransformable()); }
Example #8
Source File: InstrumentationImpl.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public synchronized void addTransformer(ClassFileTransformer transformer, boolean canRetransform) { if (transformer == null) { throw new NullPointerException("null passed as 'transformer' in addTransformer"); } if (canRetransform) { if (!isRetransformClassesSupported()) { throw new UnsupportedOperationException( "adding retransformable transformers is not supported in this environment"); } if (mRetransfomableTransformerManager == null) { mRetransfomableTransformerManager = new TransformerManager(true); } mRetransfomableTransformerManager.addTransformer(transformer); if (mRetransfomableTransformerManager.getTransformerCount() == 1) { setHasRetransformableTransformers(mNativeAgent, true); } } else { mTransformerManager.addTransformer(transformer); } }
Example #9
Source File: InstrumentationImpl.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
public synchronized void addTransformer(ClassFileTransformer transformer, boolean canRetransform) { if (transformer == null) { throw new NullPointerException("null passed as 'transformer' in addTransformer"); } if (canRetransform) { if (!isRetransformClassesSupported()) { throw new UnsupportedOperationException( "adding retransformable transformers is not supported in this environment"); } if (mRetransfomableTransformerManager == null) { mRetransfomableTransformerManager = new TransformerManager(true); } mRetransfomableTransformerManager.addTransformer(transformer); if (mRetransfomableTransformerManager.getTransformerCount() == 1) { setHasRetransformableTransformers(mNativeAgent, true); } } else { mTransformerManager.addTransformer(transformer); } }
Example #10
Source File: InstrumentationImpl.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public synchronized void setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) { if (!isNativeMethodPrefixSupported()) { throw new UnsupportedOperationException( "setNativeMethodPrefix is not supported in this environment"); } if (transformer == null) { throw new NullPointerException( "null passed as 'transformer' in setNativeMethodPrefix"); } TransformerManager mgr = findTransformerManager(transformer); if (mgr == null) { throw new IllegalArgumentException( "transformer not registered in setNativeMethodPrefix"); } mgr.setNativeMethodPrefix(transformer, prefix); String[] prefixes = mgr.getNativeMethodPrefixes(); setNativeMethodPrefixes(mNativeAgent, prefixes, mgr.isRetransformable()); }
Example #11
Source File: WebappClassLoaderBase.java From tomcatsrc with Apache License 2.0 | 6 votes |
/** * Adds the specified class file transformer to this class loader. The * transformer will then be able to modify the bytecode of any classes * loaded by this class loader after the invocation of this method. * * @param transformer The transformer to add to the class loader */ @Override public void addTransformer(ClassFileTransformer transformer) { if (transformer == null) { throw new IllegalArgumentException(sm.getString( "webappClassLoader.addTransformer.illegalArgument", getContextName())); } if (this.transformers.contains(transformer)) { // if the same instance of this transformer was already added, bail out log.warn(sm.getString("webappClassLoader.addTransformer.duplicate", transformer, getContextName())); return; } this.transformers.add(transformer); log.info(sm.getString("webappClassLoader.addTransformer", transformer, getContextName())); }
Example #12
Source File: EnableLoadTimeWeavingTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void enableLTW_withAjWeavingEnabled() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(EnableLTWConfig_withAjWeavingEnabled.class); ctx.refresh(); LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class); verify(loadTimeWeaver).addTransformer(isA(ClassFileTransformer.class)); }
Example #13
Source File: NS4Agent.java From ns4_gear_watchdog with Apache License 2.0 | 5 votes |
/** * The agent class must implement a public static premain method similar in principle to the main application entry point. * After the Java Virtual Machine (JVM) has initialized, * each premain method will be called in the order the agents were specified, * then the real application main method will be called. **/ public static void premain(String agentArgs, Instrumentation inst) { System.out.println("NS4Agent.premain() was called."); /* Provides services that allow Java programming language agents to instrument programs running on the JVM.*/ _inst = inst; /* ClassFileTransformer : An agent provides an implementation of this interface in order to transform class files.*/ ClassFileTransformer trans = new AgentTransformer(); System.out.println("Adding a NS4Agent instance to the JVM."); /*Registers the supplied transformer.*/ _inst.addTransformer(trans); }
Example #14
Source File: DefaultMonitor.java From bistoury with GNU General Public License v3.0 | 5 votes |
private boolean instrument(String source, ResolvedSourceLocation location, final int line) throws UnmodifiableClassException, ClassNotFoundException { ClassFileTransformer transformer = new MonitorClassFileTransformer(classFileBuffer, source, location, line); try { Class<?> clazz = instrumentInfo.signatureToClass(location.getClassSignature()); inst.addTransformer(transformer, true); inst.retransformClasses(clazz); instrumentInfo.addTransformedClasses(clazz); return true; } finally { inst.removeTransformer(transformer); } }
Example #15
Source File: TransformerManager.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
public synchronized void addTransformer( ClassFileTransformer transformer) { TransformerInfo[] oldList = mTransformerList; TransformerInfo[] newList = new TransformerInfo[oldList.length + 1]; System.arraycopy( oldList, 0, newList, 0, oldList.length); newList[oldList.length] = new TransformerInfo(transformer); mTransformerList = newList; }
Example #16
Source File: JBossLoadTimeWeaver.java From java-technology-stack with MIT License | 5 votes |
@Override public void addTransformer(ClassFileTransformer transformer) { try { this.addTransformer.invoke(this.delegatingTransformer, transformer); } catch (Throwable ex) { throw new IllegalStateException("Could not add transformer on JBoss ClassLoader: " + this.classLoader, ex); } }
Example #17
Source File: InstrumentationImpl.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private TransformerManager findTransformerManager(ClassFileTransformer transformer) { if (mTransformerManager.includesTransformer(transformer)) { return mTransformerManager; } if (mRetransfomableTransformerManager != null && mRetransfomableTransformerManager.includesTransformer(transformer)) { return mRetransfomableTransformerManager; } return null; }
Example #18
Source File: InstrumentationLoadTimeWeaver.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public void addTransformer(ClassFileTransformer transformer) { Assert.notNull(transformer, "Transformer must not be null"); FilteringClassFileTransformer actualTransformer = new FilteringClassFileTransformer(transformer, this.classLoader); synchronized (this.transformers) { if (this.instrumentation == null) { throw new IllegalStateException( "Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation."); } this.instrumentation.addTransformer(actualTransformer); this.transformers.add(actualTransformer); } }
Example #19
Source File: GlassFishLoadTimeWeaver.java From spring-analysis-note with MIT License | 5 votes |
/** * Create a new instance of the {@link GlassFishLoadTimeWeaver} class using * the supplied {@link ClassLoader}. * @param classLoader the {@code ClassLoader} to delegate to for weaving */ public GlassFishLoadTimeWeaver(@Nullable ClassLoader classLoader) { Assert.notNull(classLoader, "ClassLoader must not be null"); Class<?> instrumentableLoaderClass; try { instrumentableLoaderClass = classLoader.loadClass(INSTRUMENTABLE_LOADER_CLASS_NAME); this.addTransformerMethod = instrumentableLoaderClass.getMethod("addTransformer", ClassFileTransformer.class); this.copyMethod = instrumentableLoaderClass.getMethod("copy"); } catch (Throwable ex) { throw new IllegalStateException( "Could not initialize GlassFishLoadTimeWeaver because GlassFish API classes are not available", ex); } ClassLoader clazzLoader = null; // Detect transformation-aware ClassLoader by traversing the hierarchy // (as in GlassFish, Spring can be loaded by the WebappClassLoader). for (ClassLoader cl = classLoader; cl != null && clazzLoader == null; cl = cl.getParent()) { if (instrumentableLoaderClass.isInstance(cl)) { clazzLoader = cl; } } if (clazzLoader == null) { throw new IllegalArgumentException(classLoader + " and its parents are not suitable ClassLoaders: A [" + instrumentableLoaderClass.getName() + "] implementation is required."); } this.classLoader = clazzLoader; }
Example #20
Source File: TransformerManager.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
boolean setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) { TransformerInfo[] transformerList = getSnapshotTransformerList(); for ( int x = 0; x < transformerList.length; x++ ) { TransformerInfo transformerInfo = transformerList[x]; ClassFileTransformer aTransformer = transformerInfo.transformer(); if ( aTransformer == transformer ) { transformerInfo.setPrefix(prefix); return true; } } return false; }
Example #21
Source File: TransformerManager.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
boolean setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) { TransformerInfo[] transformerList = getSnapshotTransformerList(); for ( int x = 0; x < transformerList.length; x++ ) { TransformerInfo transformerInfo = transformerList[x]; ClassFileTransformer aTransformer = transformerInfo.transformer(); if ( aTransformer == transformer ) { transformerInfo.setPrefix(prefix); return true; } } return false; }
Example #22
Source File: InstrumentationImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public synchronized boolean removeTransformer(ClassFileTransformer transformer) { if (transformer == null) { throw new NullPointerException("null passed as 'transformer' in removeTransformer"); } TransformerManager mgr = findTransformerManager(transformer); if (mgr != null) { mgr.removeTransformer(transformer); if (mgr.isRetransformable() && mgr.getTransformerCount() == 0) { setHasRetransformableTransformers(mNativeAgent, false); } return true; } return false; }
Example #23
Source File: TransformerManager.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
synchronized boolean includesTransformer(ClassFileTransformer transformer) { for (TransformerInfo info : mTransformerList) { if ( info.transformer() == transformer ) { return true; } } return false; }
Example #24
Source File: InstrumentationImpl.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public synchronized boolean removeTransformer(ClassFileTransformer transformer) { if (transformer == null) { throw new NullPointerException("null passed as 'transformer' in removeTransformer"); } TransformerManager mgr = findTransformerManager(transformer); if (mgr != null) { mgr.removeTransformer(transformer); if (mgr.isRetransformable() && mgr.getTransformerCount() == 0) { setHasRetransformableTransformers(mNativeAgent, false); } return true; } return false; }
Example #25
Source File: ContainerResourceMonitoringTracerTest.java From garmadon with Apache License 2.0 | 5 votes |
@Test @AgentAttachmentRule.Enforce public void ContainerResourceMonitoringModule_should_attach_to_recordCpuUsage() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); final Header[] header = new Header[1]; final Object[] event = new Object[1]; TriConsumer<Long, Header, Object> cons = (t, h, o) -> { header[0] = h; event[0] = o; }; ReflectionHelper.setField(null, classLoader.loadClass(ContainerResourceMonitoringTracer.class.getName()), "eventHandler", cons); ClassFileTransformer classFileTransformer = new ContainerResourceMonitoringTracer.VcoreUsageTracer().installOnByteBuddyAgent(); try { Class<?> clazz = classLoader.loadClass(ContainerMetrics.class.getName()); Constructor<?> constructor = clazz.getDeclaredConstructor(MetricsSystem.class, ContainerId.class, long.class, long.class); constructor.setAccessible(true); Object inFormat = constructor.newInstance(mock(MetricsSystem.class), ContainerId.fromString("container_e16940_1541383784275_24407_01_000023"), 5L, 5L); Method m = clazz.getDeclaredMethod("recordCpuUsage", int.class, int.class); m.invoke(inFormat, 10, 10000); assertNotNull(header[0]); assertNotNull(event[0]); } finally { ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer); } }
Example #26
Source File: InstrumentationLoadTimeWeaver.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public void addTransformer(ClassFileTransformer transformer) { Assert.notNull(transformer, "Transformer must not be null"); FilteringClassFileTransformer actualTransformer = new FilteringClassFileTransformer(transformer, this.classLoader); synchronized (this.transformers) { if (this.instrumentation == null) { throw new IllegalStateException( "Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation."); } this.instrumentation.addTransformer(actualTransformer); this.transformers.add(actualTransformer); } }
Example #27
Source File: TransformerManager.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public synchronized void addTransformer( ClassFileTransformer transformer) { TransformerInfo[] oldList = mTransformerList; TransformerInfo[] newList = new TransformerInfo[oldList.length + 1]; System.arraycopy( oldList, 0, newList, 0, oldList.length); newList[oldList.length] = new TransformerInfo(transformer); mTransformerList = newList; }
Example #28
Source File: BeamComponentExtension.java From component-runtime with Apache License 2.0 | 5 votes |
@Override public Collection<ClassFileTransformer> getTransformers() { if (Boolean.getBoolean("talend.component.beam.transformers.skip")) { return emptySet(); } final String classes = System.getProperty("talend.component.beam.transformers.io.enhanced"); return singleton(classes == null ? new BeamIOTransformer() : new BeamIOTransformer( Stream.of(classes.split(",")).map(String::trim).filter(it -> !it.isEmpty()).collect(toSet()))); }
Example #29
Source File: TransformerManager.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
boolean setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) { TransformerInfo[] transformerList = getSnapshotTransformerList(); for ( int x = 0; x < transformerList.length; x++ ) { TransformerInfo transformerInfo = transformerList[x]; ClassFileTransformer aTransformer = transformerInfo.transformer(); if ( aTransformer == transformer ) { transformerInfo.setPrefix(prefix); return true; } } return false; }
Example #30
Source File: InstrumentationImpl.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public synchronized boolean removeTransformer(ClassFileTransformer transformer) { if (transformer == null) { throw new NullPointerException("null passed as 'transformer' in removeTransformer"); } TransformerManager mgr = findTransformerManager(transformer); if (mgr != null) { mgr.removeTransformer(transformer); if (mgr.isRetransformable() && mgr.getTransformerCount() == 0) { setHasRetransformableTransformers(mNativeAgent, false); } return true; } return false; }