當前位置: 首頁>>代碼示例>>Java>>正文


Java ClassFileTransformer.transform方法代碼示例

本文整理匯總了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;
}
 
開發者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: 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

示例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);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:14,代碼來源:ShadowingClassLoader.java

示例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;
}
 
開發者ID:to2mbn,項目名稱:authlib-injector,代碼行數:18,代碼來源:TweakerTransformerAdapter.java

示例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);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:20,代碼來源:WebSphereClassPreDefinePlugin.java

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

}
 
開發者ID:BenoitRonflette,項目名稱:FreeYourCode,代碼行數:28,代碼來源:InstrumentationMock.java

示例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;
}
 
開發者ID:vilie,項目名稱:javify,代碼行數:42,代碼來源:InstrumentationImpl.java

示例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;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:49,代碼來源:TransformerManager.java

示例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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:51,代碼來源:TransformerManager.java

示例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;
}
 
開發者ID:campolake,項目名稱:openjdk9,代碼行數:49,代碼來源:TransformerManager.java

示例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));
}
 
開發者ID:geekflow,項目名稱:light,代碼行數:29,代碼來源:MethodParameterTransformerTest.java

示例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));
}
 
開發者ID:geekflow,項目名稱:light,代碼行數:30,代碼來源:MethodReturnTransformerTest.java


注:本文中的java.lang.instrument.ClassFileTransformer.transform方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。