当前位置: 首页>>代码示例>>Java>>正文


Java ClassFileTransformer类代码示例

本文整理汇总了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;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:20,代码来源:NbInstrumentation.java

示例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);
}
 
开发者ID:willfleury,项目名称:prometheus-metrics-agent,代码行数:18,代码来源:BaseMetricTest.java

示例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()));

}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:27,代码来源:WebappClassLoaderBase.java

示例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.");
		}
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:29,代码来源:ReflectiveLoadTimeWeaver.java

示例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;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:26,代码来源:WeavingTransformer.java

示例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);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:14,代码来源:ShadowingClassLoader.java

示例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);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:15,代码来源:InstrumentationLoadTimeWeaver.java

示例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);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:19,代码来源:JBossModulesAdapter.java

示例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);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:22,代码来源:InstrumentationImpl.java

示例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());
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:20,代码来源:InstrumentationImpl.java

示例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()));

}
 
开发者ID:how2j,项目名称:lazycat,代码行数:28,代码来源:WebappClassLoaderBase.java

示例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;
}
 
开发者ID:to2mbn,项目名称:authlib-injector,代码行数:18,代码来源:TweakerTransformerAdapter.java

示例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);
    }

  });
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:22,代码来源:ResetAgent.java

示例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);
}
 
开发者ID:jVoid,项目名称:jVoid,代码行数:22,代码来源:JVoidAgentPremainTest.java

示例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.");
		}
	}
}
 
开发者ID:txazo,项目名称:spring,代码行数:28,代码来源:ReflectiveLoadTimeWeaver.java


注:本文中的java.lang.instrument.ClassFileTransformer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。