本文整理汇总了Java中java.lang.instrument.ClassFileTransformer.transform方法的典型用法代码示例。如果您正苦于以下问题:Java ClassFileTransformer.transform方法的具体用法?Java ClassFileTransformer.transform怎么用?Java ClassFileTransformer.transform使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.instrument.ClassFileTransformer
的用法示例。
在下文中一共展示了ClassFileTransformer.transform方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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;
}
示例4: 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);
}
}
示例5: 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;
}
示例6: WebSphereClassPreDefinePlugin
import java.lang.instrument.ClassFileTransformer; //导入方法依赖的package包/类
/**
* 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);
}
}
示例7: addTransformer
import java.lang.instrument.ClassFileTransformer; //导入方法依赖的package包/类
@Override
public void addTransformer(final ClassFileTransformer transformer) {
this.transformer = transformer;
loader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
Class<?> cls = classes.get(name);
if(cls != null){
return cls;
}
byte[] byteBuffer;
try {
//Le transform doit recevoir les noms des classes et leur path avec des '/' et non le '.' du package.
String classNameTransform = name.replace(".","/");
byteBuffer = transformer.transform(this, classNameTransform, Class.forName(name), null, null);
} catch (IllegalClassFormatException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
cls = byteBuffer != null ? defineClass(name, byteBuffer, 0, byteBuffer.length) : super.loadClass(name);
classes.put(name, cls);
return cls;
}
};
}
示例8: callTransformers
import java.lang.instrument.ClassFileTransformer; //导入方法依赖的package包/类
/**
* Called by the VM or redefineClasses to call each transformer
*
* @param loader the loader of the class
* @param className the name of the class with packages separated with "/"
* @param classBeingRedefined the class being redefined if it's the case,
* null otherwise
* @param protectionDomain the protection domain of the class being defined
* or redefined
* @param classfileBuffer the input byte buffer in class file format
*
* @return the new class file
*/
public byte[] callTransformers(ClassLoader loader, String className,
Class<?> classBeingRedefined, ProtectionDomain protectionDomain,
byte[] classfileBuffer)
{
byte[] newBuffer = null;
byte[] oldBuffer = classfileBuffer;
ClassFileTransformer current;
synchronized (transformers)
{
Iterator<ClassFileTransformer> i = transformers.iterator();
while (i.hasNext())
{
current = i.next();
try
{
newBuffer = current.transform(loader, className,
classBeingRedefined, protectionDomain, oldBuffer);
}
catch (IllegalClassFormatException ignored)
{
//IGNORED
}
if (newBuffer != null)
oldBuffer = newBuffer;
}
}
return oldBuffer;
}
示例9: transform
import java.lang.instrument.ClassFileTransformer; //导入方法依赖的package包/类
public byte[]
transform( ClassLoader loader,
String classname,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) {
boolean someoneTouchedTheBytecode = false;
TransformerInfo[] transformerList = getSnapshotTransformerList();
byte[] bufferToUse = classfileBuffer;
// order matters, gotta run 'em in the order they were added
for ( int x = 0; x < transformerList.length; x++ ) {
TransformerInfo transformerInfo = transformerList[x];
ClassFileTransformer transformer = transformerInfo.transformer();
byte[] transformedBytes = null;
try {
transformedBytes = transformer.transform( loader,
classname,
classBeingRedefined,
protectionDomain,
bufferToUse);
}
catch (Throwable t) {
// don't let any one transformer mess it up for the others.
// This is where we need to put some logging. What should go here? FIXME
}
if ( transformedBytes != null ) {
someoneTouchedTheBytecode = true;
bufferToUse = transformedBytes;
}
}
// if someone modified it, return the modified buffer.
// otherwise return null to mean "no transforms occurred"
byte [] result;
if ( someoneTouchedTheBytecode ) {
result = bufferToUse;
}
else {
result = null;
}
return result;
}
示例10: transform
import java.lang.instrument.ClassFileTransformer; //导入方法依赖的package包/类
public byte[]
transform( Module module,
ClassLoader loader,
String classname,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) {
boolean someoneTouchedTheBytecode = false;
TransformerInfo[] transformerList = getSnapshotTransformerList();
byte[] bufferToUse = classfileBuffer;
// order matters, gotta run 'em in the order they were added
for ( int x = 0; x < transformerList.length; x++ ) {
TransformerInfo transformerInfo = transformerList[x];
ClassFileTransformer transformer = transformerInfo.transformer();
byte[] transformedBytes = null;
try {
transformedBytes = transformer.transform( module,
loader,
classname,
classBeingRedefined,
protectionDomain,
bufferToUse);
}
catch (Throwable t) {
// don't let any one transformer mess it up for the others.
// This is where we need to put some logging. What should go here? FIXME
}
if ( transformedBytes != null ) {
someoneTouchedTheBytecode = true;
bufferToUse = transformedBytes;
}
}
// if someone modified it, return the modified buffer.
// otherwise return null to mean "no transforms occurred"
byte [] result;
if ( someoneTouchedTheBytecode ) {
result = bufferToUse;
}
else {
result = null;
}
return result;
}
示例11: transform
import java.lang.instrument.ClassFileTransformer; //导入方法依赖的package包/类
public byte[]
transform( Module module,
String classname,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) {
boolean someoneTouchedTheBytecode = false;
TransformerInfo[] transformerList = getSnapshotTransformerList();
byte[] bufferToUse = classfileBuffer;
// order matters, gotta run 'em in the order they were added
for ( int x = 0; x < transformerList.length; x++ ) {
TransformerInfo transformerInfo = transformerList[x];
ClassFileTransformer transformer = transformerInfo.transformer();
byte[] transformedBytes = null;
try {
transformedBytes = transformer.transform( module,
classname,
classBeingRedefined,
protectionDomain,
bufferToUse);
}
catch (Throwable t) {
// don't let any one transformer mess it up for the others.
// This is where we need to put some logging. What should go here? FIXME
}
if ( transformedBytes != null ) {
someoneTouchedTheBytecode = true;
bufferToUse = transformedBytes;
}
}
// if someone modified it, return the modified buffer.
// otherwise return null to mean "no transforms occurred"
byte [] result;
if ( someoneTouchedTheBytecode ) {
result = bufferToUse;
}
else {
result = null;
}
return result;
}
示例12: testTransform
import java.lang.instrument.ClassFileTransformer; //导入方法依赖的package包/类
@Test
public void testTransform() throws Exception
{
String className = TestMethods.class.getName();
ClassFileTransformer transformer = new MethodParameterTransformer(new DefaultTraceRegisterBinder(), new AgentTraceContext(ProfilerConfig.load(getClass().getClassLoader(), "light.conf")));
byte[] original = TestUtil.load(className);
byte[] transform = transformer.transform(getClass().getClassLoader(), className, null, null, original);
assertThat(original, not(transform));
TestClassLoader classLoader = new TestClassLoader(getClass().getClassLoader());
Class<?> clazz = classLoader.findClass(className, transform);
assertThat(clazz, notNullValue());
assertThat(clazz.getName(), is(className));
Method method = clazz.getMethod("doWithBoolean", boolean.class);
Object instance = clazz.newInstance();
assertThat((Boolean) method.invoke(instance, true), is(true));
assertThat((Boolean) method.invoke(instance, false), is(false));
method = clazz.getMethod("doWithInt", int.class);
assertThat((Integer) method.invoke(instance, 13), is(13));
}
示例13: testTransform
import java.lang.instrument.ClassFileTransformer; //导入方法依赖的package包/类
@Test
public void testTransform() throws Exception
{
String className = TestMethods.class.getName();
ClassFileTransformer transformer = new MethodReturnTransformer(new DefaultTraceRegisterBinder(), new AgentTraceContext(ProfilerConfig.load(getClass().getClassLoader(), "light.conf")));
byte[] original = TestUtil.load(className);
byte[] transform = transformer.transform(getClass().getClassLoader(), className, null, null, original);
assertThat(original, not(transform));
TestClassLoader classLoader = new TestClassLoader(getClass().getClassLoader());
Class<?> clazz = classLoader.findClass(className, transform);
assertThat(clazz, notNullValue());
assertThat(clazz.getName(), is(className));
Method method = clazz.getMethod("doWithBoolean", boolean.class);
Object instance = clazz.newInstance();
assertThat((Boolean) method.invoke(instance, true), is(true));
assertThat((Boolean) method.invoke(instance, false), is(false));
method = clazz.getMethod("doWithInt", int.class);
assertThat((Integer) method.invoke(instance, 13), is(13));
}