Java Code Examples for java.lang.instrument.ClassFileTransformer

The following examples show how to use java.lang.instrument.ClassFileTransformer. These examples are extracted from open source projects. 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 Project: Tomcat8-Source-Read   Source File: WebappClassLoaderBase.java    License: MIT License 6 votes vote down vote up
/**
 * 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
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 Project: spring-analysis-note   Source File: ReflectiveLoadTimeWeaver.java    License: MIT License 6 votes vote down vote up
/**
 * 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 4
Source Project: garmadon   Source File: MapReduceInputFormatTracerTest.java    License: Apache License 2.0 6 votes vote down vote up
@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 5
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 6
Source Project: hottub   Source File: InstrumentationImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
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
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 8
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 9
Source Project: tomcatsrc   Source File: WebappClassLoaderBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 10
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
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 12
Source Project: hottub   Source File: TransformerManager.java    License: GNU General Public License v2.0 5 votes vote down vote up
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 13
Source Project: lams   Source File: JBossMCAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void addTransformer(ClassFileTransformer transformer) {
	InvocationHandler adapter = new JBossMCTranslatorAdapter(transformer);
	Object adapterInstance = Proxy.newProxyInstance(this.translatorClass.getClassLoader(),
			new Class<?>[] {this.translatorClass}, adapter);
	try {
		this.addTranslator.invoke(this.target, adapterInstance);
	}
	catch (Throwable ex) {
		throw new IllegalStateException("Could not add transformer on JBoss 6 ClassLoader " + this.classLoader, ex);
	}
}
 
Example 14
Source Project: bistoury   Source File: DefaultMonitor.java    License: GNU General Public License v3.0 5 votes vote down vote up
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 Project: ns4_gear_watchdog   Source File: NS4Agent.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 16
Source Project: spring-analysis-note   Source File: GlassFishLoadTimeWeaver.java    License: MIT License 5 votes vote down vote up
/**
 * 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 17
Source Project: jdk8u60   Source File: TransformerManager.java    License: GNU General Public License v2.0 5 votes vote down vote up
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 18
/**
 * Create a new {@link WebSphereClassPreDefinePlugin}.
 * @param transformer the {@link ClassFileTransformer} to be adapted
 * (must not be {@code null})
 */
public WebSphereClassPreDefinePlugin(ClassFileTransformer transformer) {
	this.transformer = transformer;
	ClassLoader classLoader = transformer.getClass().getClassLoader();

	// First force the full class loading of the weaver by invoking transformation on a dummy class
	try {
		String dummyClass = Dummy.class.getName().replace('.', '/');
		byte[] bytes = FileCopyUtils.copyToByteArray(classLoader.getResourceAsStream(dummyClass + ".class"));
		transformer.transform(classLoader, dummyClass, null, null, bytes);
	}
	catch (Throwable ex) {
		throw new IllegalArgumentException("Cannot load transformer", ex);
	}
}
 
Example 19
Source Project: spring-analysis-note   Source File: ShadowingClassLoader.java    License: MIT License 5 votes vote down vote up
private byte[] applyTransformers(String name, byte[] bytes) {
	String internalName = StringUtils.replace(name, ".", "/");
	try {
		for (ClassFileTransformer transformer : this.classFileTransformers) {
			byte[] transformed = transformer.transform(this, internalName, null, null, bytes);
			bytes = (transformed != null ? transformed : bytes);
		}
		return bytes;
	}
	catch (IllegalClassFormatException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example 20
@Test
public void testCtorWithClassLoaderThatDoesNotExposeAGetThrowawayClassLoaderMethodIsOkay() {
	JustAddTransformerClassLoader classLoader = new JustAddTransformerClassLoader();
	ReflectiveLoadTimeWeaver weaver = new ReflectiveLoadTimeWeaver(classLoader);
	weaver.addTransformer(new ClassFileTransformer() {
		@Override
		public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
			return "CAFEDEAD".getBytes();
		}
	});
	assertEquals(1, classLoader.getNumTimesGetThrowawayClassLoaderCalled());
}
 
Example 21
private TransformerManager
findTransformerManager(ClassFileTransformer transformer) {
    if (mTransformerManager.includesTransformer(transformer)) {
        return mTransformerManager;
    }
    if (mRetransfomableTransformerManager != null &&
            mRetransfomableTransformerManager.includesTransformer(transformer)) {
        return mRetransfomableTransformerManager;
    }
    return null;
}
 
Example 22
@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 23
Source Project: jdk8u-jdk   Source File: TransformerManager.java    License: GNU General Public License v2.0 5 votes vote down vote up
synchronized boolean
includesTransformer(ClassFileTransformer transformer) {
    for (TransformerInfo info : mTransformerList) {
        if ( info.transformer() == transformer ) {
            return true;
        }
    }
    return false;
}
 
Example 24
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
@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 Project: jdk8u-jdk   Source File: TransformerManager.java    License: GNU General Public License v2.0 5 votes vote down vote up
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 27
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 28
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 29
Source Project: jdk8u_jdk   Source File: TransformerManager.java    License: GNU General Public License v2.0 5 votes vote down vote up
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 30
Source Project: component-runtime   Source File: BeamComponentExtension.java    License: Apache License 2.0 5 votes vote down vote up
@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())));
}