Java Code Examples for java.lang.instrument.ClassFileTransformer#transform()

The following examples show how to use java.lang.instrument.ClassFileTransformer#transform() . 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 File: WeavingTransformer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: WeavingTransformer.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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, @Nullable 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;
}
 
Example 3
Source File: NbInstrumentation.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: WeavingTransformer.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: WebSphereClassPreDefinePlugin.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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 6
Source File: WebSphereClassPreDefinePlugin.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 7
Source File: ShadowingClassLoader.java    From lams with GNU General Public License v2.0 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 8
Source File: WebSphereClassPreDefinePlugin.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 9
Source File: ShadowingClassLoader.java    From java-technology-stack with 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 10
Source File: WebSphereClassPreDefinePlugin.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 11
Source File: ShadowingClassLoader.java    From spring4-understanding with Apache License 2.0 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 12
Source File: ShadowingClassLoader.java    From spring-analysis-note with 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 13
Source File: TransformerManager.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 14
Source File: TransformerManager.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 15
Source File: TransformerManager.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 16
Source File: TransformerManager.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 17
Source File: TransformerManager.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 18
Source File: TransformerManager.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 19
Source File: TransformerManager.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 20
Source File: TransformerManager.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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;
}