Search in sources :

Example 11 with IllegalClassFormatException

use of java.lang.instrument.IllegalClassFormatException in project jetty.project by eclipse.

the class WebAppClassLoaderTest method testNullClassFileTransformer.

@Test
public void testNullClassFileTransformer() throws Exception {
    _loader.addTransformer(new ClassFileTransformer() {

        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            return null;
        }
    });
    assertCanLoadClass("org.acme.webapp.ClassInJarA");
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 12 with IllegalClassFormatException

use of java.lang.instrument.IllegalClassFormatException in project pinpoint by naver.

the class JavassistTranslator method customTransformer.

private byte[] customTransformer(ClassPool pool, String jvmClassName) {
    logger.info("Modify find classname:{}, loader:{}", jvmClassName, loader);
    MatchableClassFileTransformer transformer = transformerMap.get(jvmClassName);
    if (transformer == null) {
        return null;
    }
    logger.info("Modify jvmClassName:{},  modifier{}, loader:{}", jvmClassName, transformer, loader);
    final Thread thread = Thread.currentThread();
    final ClassLoader beforeClassLoader = thread.getContextClassLoader();
    thread.setContextClassLoader(loader);
    try {
        String javaClassName = JavaAssistUtils.jvmNameToJavaName(jvmClassName);
        byte[] transformBytes = transformer.transform(loader, javaClassName, null, null, null);
        makeClass(pool, transformBytes, jvmClassName);
        return transformBytes;
    } catch (IllegalClassFormatException e) {
        throw new RuntimeException(jvmClassName + " transform fail", e);
    } finally {
        thread.setContextClassLoader(beforeClassLoader);
    }
}
Also used : MatchableClassFileTransformer(com.navercorp.pinpoint.profiler.plugin.MatchableClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException)

Example 13 with IllegalClassFormatException

use of java.lang.instrument.IllegalClassFormatException in project intellij-community by JetBrains.

the class ResetAgent method premain.

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);
        }
    });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) Field(java.lang.reflect.Field) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) String(java.lang.String)

Example 14 with IllegalClassFormatException

use of java.lang.instrument.IllegalClassFormatException in project Payara by payara.

the class ASURLClassLoader method findClass.

/**
 * THREAD SAFETY: what happens when more than one thread requests the same class
 *        and thus works on the same classData?  Or defines the same package?  Maybe
 *        the same work just gets done twice, and that's all.
 *        CAUTION: this method might be overriden, and subclasses must be cautious (also)
 *        about thread safety.
 */
protected Class findClass(String name) throws ClassNotFoundException {
    ClassData classData = findClassData(name);
    // Instruments the classes if the profiler's enabled
    if (PreprocessorUtil.isPreprocessorEnabled()) {
        // search thru the JARs for a file of the form java/lang/Object.class
        final String entryName = name.replace('.', '/') + ".class";
        classData.setClassBytes(PreprocessorUtil.processClass(entryName, classData.getClassBytes()));
    }
    // Define package information if necessary
    int lastPackageSep = name.lastIndexOf('.');
    if (lastPackageSep != -1) {
        String packageName = name.substring(0, lastPackageSep);
        if (getPackage(packageName) == null) {
            try {
                // There's a small chance that one of our parents
                // could define the same package after getPackage
                // returns null but before we call definePackage,
                // since the parent classloader instances
                // are not locked.  So, just catch the exception
                // that is thrown in that case and ignore it.
                // 
                // It's unclear where we would get the info to
                // set all spec and impl data for the package,
                // so just use null.  This is consistent will the
                // JDK code that does the same.
                definePackage(packageName, null, null, null, null, null, null, null);
            } catch (IllegalArgumentException iae) {
                // duplicate attempt to define same package.
                // safe to ignore.
                _logger.log(Level.FINE, "duplicate package " + "definition attempt for " + packageName, iae);
            }
        }
    }
    // Loop though the transformers here!!
    try {
        final ArrayList<ClassFileTransformer> xformers = (ArrayList<ClassFileTransformer>) transformers.clone();
        for (final ClassFileTransformer transformer : xformers) {
            // see javadocs of transform().
            // It expects class name as java/lang/Object
            // as opposed to java.lang.Object
            final String internalClassName = name.replace('.', '/');
            final byte[] transformedBytes = transformer.transform(this, internalClassName, null, classData.pd, classData.getClassBytes());
            if (transformedBytes != null) {
                // null indicates no transformation
                _logger.log(Level.INFO, CULoggerInfo.actuallyTransformed, name);
                classData.setClassBytes(transformedBytes);
            }
        }
    } catch (IllegalClassFormatException icfEx) {
        throw new ClassNotFoundException(icfEx.toString(), icfEx);
    }
    Class clazz = null;
    try {
        byte[] bytes = classData.getClassBytes();
        clazz = defineClass(name, bytes, 0, bytes.length, classData.pd);
        return clazz;
    } catch (UnsupportedClassVersionError ucve) {
        throw new UnsupportedClassVersionError(sm.getString("ejbClassLoader.unsupportedVersion", name, System.getProperty("java.version")));
    }
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException)

Aggregations

IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)14 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)9 ProtectionDomain (java.security.ProtectionDomain)5 MatchableClassFileTransformer (com.navercorp.pinpoint.profiler.plugin.MatchableClassFileTransformer)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URLClassLoader (java.net.URLClassLoader)2 CtClass (javassist.CtClass)2 Test (org.junit.Test)2 BytecodePreprocessor (com.sun.appserv.BytecodePreprocessor)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 String (java.lang.String)1 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 CodeSource (java.security.CodeSource)1 Certificate (java.security.cert.Certificate)1 ArrayList (java.util.ArrayList)1 Manifest (java.util.jar.Manifest)1 ClassPool (javassist.ClassPool)1 CtField (javassist.CtField)1