本文整理汇总了Java中java.lang.instrument.ClassFileTransformer类的典型用法代码示例。如果您正苦于以下问题:Java ClassFileTransformer类的具体用法?Java ClassFileTransformer怎么用?Java ClassFileTransformer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ClassFileTransformer类属于java.lang.instrument包,在下文中一共展示了ClassFileTransformer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: patchByteCode
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
public static byte[] patchByteCode(ClassLoader l, String className, ProtectionDomain pd, byte[] arr) throws IllegalClassFormatException {
if (ACTIVE == null) {
return arr;
}
if (Boolean.TRUE.equals(IN.get())) {
return arr;
}
try {
IN.set(Boolean.TRUE);
for (NbInstrumentation inst : ACTIVE) {
for (ClassFileTransformer t : inst.transformers) {
arr = t.transform(l, className, null, pd, arr);
}
}
} finally {
IN.set(null);
}
return arr;
}
示例2: execute
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
protected <T> Class<T> execute(Class<T> clazz, Configuration config) throws Exception {
String className = dotToSlash(clazz.getName());
String classAsPath = className + ".class";
ClassFileTransformer cft = new AnnotatedMetricClassTransformer(config, true);
byte[] classfileBuffer = cft.transform(
clazz.getClassLoader(),
className,
null,
null,
IOUtils.toByteArray(clazz.getClassLoader().getResourceAsStream(classAsPath)));
traceBytecode(classfileBuffer);
verifyBytecode(classfileBuffer);
return getClassFromBytes(clazz, classfileBuffer);
}
示例3: addTransformer
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
/**
* 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()));
}
示例4: ReflectiveLoadTimeWeaver
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
/**
* 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(ClassLoader classLoader) {
Assert.notNull(classLoader, "ClassLoader must not be null");
this.classLoader = classLoader;
this.addTransformerMethod = ClassUtils.getMethodIfAvailable(
this.classLoader.getClass(), ADD_TRANSFORMER_METHOD_NAME,
new Class<?>[] {ClassFileTransformer.class});
if (this.addTransformerMethod == null) {
throw new IllegalStateException(
"ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide an " +
"'addTransformer(ClassFileTransformer)' method.");
}
this.getThrowawayClassLoaderMethod = ClassUtils.getMethodIfAvailable(
this.classLoader.getClass(), GET_THROWAWAY_CLASS_LOADER_METHOD_NAME, new Class<?>[0]);
// getThrowawayClassLoader method is optional
if (this.getThrowawayClassLoaderMethod == null) {
if (logger.isInfoEnabled()) {
logger.info("The ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide a " +
"'getThrowawayClassLoader()' method; SimpleThrowawayClassLoader will be used instead.");
}
}
}
示例5: transformIfNecessary
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
/**
* Apply transformation on a given class byte definition.
* The method will always return a non-null byte array (if no transformation has taken place
* the array content will be identical to the original one).
* @param className the full qualified name of the class in dot format (i.e. some.package.SomeClass)
* @param internalName class name internal name in / format (i.e. some/package/SomeClass)
* @param bytes class byte definition
* @param pd protection domain to be used (can be null)
* @return (possibly transformed) class byte definition
*/
public byte[] transformIfNecessary(String className, String internalName, byte[] bytes, ProtectionDomain pd) {
byte[] result = bytes;
for (ClassFileTransformer cft : this.transformers) {
try {
byte[] transformed = cft.transform(this.classLoader, internalName, null, pd, result);
if (transformed != null) {
result = transformed;
}
}
catch (IllegalClassFormatException ex) {
throw new IllegalStateException("Class file transformation failed", ex);
}
}
return result;
}
示例6: applyTransformers
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
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);
}
}
示例7: addTransformer
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
@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);
}
}
示例8: JBossModulesAdapter
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
public JBossModulesAdapter(ClassLoader loader) {
this.classLoader = loader;
try {
Field transformer = ReflectionUtils.findField(loader.getClass(), "transformer");
transformer.setAccessible(true);
this.delegatingTransformer = transformer.get(loader);
if (!this.delegatingTransformer.getClass().getName().equals(DELEGATING_TRANSFORMER_CLASS_NAME)) {
throw new IllegalStateException("Transformer not of the expected type DelegatingClassFileTransformer: " +
this.delegatingTransformer.getClass().getName());
}
this.addTransformer = ReflectionUtils.findMethod(this.delegatingTransformer.getClass(),
"addTransformer", ClassFileTransformer.class);
this.addTransformer.setAccessible(true);
}
catch (Exception ex) {
throw new IllegalStateException("Could not initialize JBoss 7 LoadTimeWeaver", ex);
}
}
示例9: addTransformer
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
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);
}
}
示例10: setNativeMethodPrefix
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
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());
}
示例11: addTransformer
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
/**
* 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()));
}
示例12: transform
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
@Override
public byte[] transform(String name, String transformedName, byte[] classBuffer) {
String internalClassName = name.replace('.', '/');
for (ClassFileTransformer transformer : AuthlibInjectorTweaker.transformers) {
byte[] result = null;
try {
result = transformer.transform(Launch.classLoader, internalClassName, null, null, classBuffer);
} catch (IllegalClassFormatException e) {
log("exception while invoking {0}: {1}", transformer, e);
e.printStackTrace();
}
if (result != null) {
classBuffer = result;
}
}
return classBuffer;
}
示例13: premain
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
public static void premain(String options, Instrumentation inst) {
// Handle duplicate agents
if (initialized) {
return;
}
initialized = true;
inst.addTransformer(new ClassFileTransformer() {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
if (classBeingRedefined != null) {
try {
Field callSiteArrayField = classBeingRedefined.getDeclaredField("$callSiteArray");
callSiteArrayField.setAccessible(true);
callSiteArrayField.set(null, null);
} catch (Throwable ignored) {
}
}
return removeTimestampField(classfileBuffer);
}
});
}
示例14: premainTest
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
@Test
public void premainTest() {
// Prepare to verify the instrumentation added
Instrumentation mockIntrumentation = mock(Instrumentation.class);
JVoid.premain(DEFAULT_TEST_CONFIGURATION_FILE, mockIntrumentation);
// The configuration is loaded
// assertTrue(!JVoidContext.getConfiguration().dbLocation().isEmpty());
// JVoidExecutionContext jvoidExecutionContext;
// // The execution is setup
// JExecution execution = jvoidExecutionContext.getCurrentExecution();
// assertNotNull(execution);
// assertEquals(JVoidContext.getCurrentExecution(), execution);
// The transformer is added
ArgumentCaptor<ClassFileTransformer> argument = ArgumentCaptor.forClass(ClassFileTransformer.class);
verify(mockIntrumentation).addTransformer(argument.capture());
assertTrue(argument.getValue() instanceof JVoidClassFileTransformer);
}
示例15: ReflectiveLoadTimeWeaver
import java.lang.instrument.ClassFileTransformer; //导入依赖的package包/类
/**
* 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(ClassLoader classLoader) {
Assert.notNull(classLoader, "ClassLoader must not be null");
this.classLoader = classLoader;
this.addTransformerMethod = ClassUtils.getMethodIfAvailable(
this.classLoader.getClass(), ADD_TRANSFORMER_METHOD_NAME, ClassFileTransformer.class);
if (this.addTransformerMethod == null) {
throw new IllegalStateException(
"ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide an " +
"'addTransformer(ClassFileTransformer)' method.");
}
this.getThrowawayClassLoaderMethod = ClassUtils.getMethodIfAvailable(
this.classLoader.getClass(), GET_THROWAWAY_CLASS_LOADER_METHOD_NAME);
// getThrowawayClassLoader method is optional
if (this.getThrowawayClassLoaderMethod == null) {
if (logger.isInfoEnabled()) {
logger.info("The ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide a " +
"'getThrowawayClassLoader()' method; SimpleThrowawayClassLoader will be used instead.");
}
}
}