Search in sources :

Example 11 with ClassReader

use of jodd.asm5.ClassReader in project jodd by oblac.

the class Paramo method resolveParameters.

/**
	 * Resolves method parameters from a method or constructor.
	 * Returns an empty array when target does not contain any parameter.
	 * No caching is involved in this process, i.e. class bytecode
	 * is examined every time this method is called.
	 */
public static MethodParameter[] resolveParameters(AccessibleObject methodOrCtor) {
    Class[] paramTypes;
    Class declaringClass;
    String name;
    if (methodOrCtor instanceof Method) {
        Method method = (Method) methodOrCtor;
        paramTypes = method.getParameterTypes();
        name = method.getName();
        declaringClass = method.getDeclaringClass();
    } else {
        Constructor constructor = (Constructor) methodOrCtor;
        paramTypes = constructor.getParameterTypes();
        declaringClass = constructor.getDeclaringClass();
        name = CTOR_METHOD;
    }
    if (paramTypes.length == 0) {
        return MethodParameter.EMPTY_ARRAY;
    }
    InputStream stream;
    try {
        stream = ClassLoaderUtil.getClassAsStream(declaringClass);
    } catch (IOException ioex) {
        throw new ParamoException("Failed to read class bytes: " + declaringClass.getName(), ioex);
    }
    if (stream == null) {
        throw new ParamoException("Class not found: " + declaringClass);
    }
    try {
        ClassReader reader = new ClassReader(stream);
        MethodFinder visitor = new MethodFinder(declaringClass, name, paramTypes);
        reader.accept(visitor, 0);
        return visitor.getResolvedParameters();
    } catch (IOException ioex) {
        throw new ParamoException(ioex);
    } finally {
        StreamUtil.close(stream);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InputStream(java.io.InputStream) ClassReader(jodd.asm5.ClassReader) Method(java.lang.reflect.Method) IOException(java.io.IOException)

Example 12 with ClassReader

use of jodd.asm5.ClassReader in project intellij-community by JetBrains.

the class AbstractClassGenerator method create.

protected Object create(Object key) {
    try {
        Class gen = null;
        synchronized (source) {
            ClassLoader loader = getClassLoader();
            Map cache2 = null;
            cache2 = (Map) source.cache.get(loader);
            if (cache2 == null) {
                cache2 = new HashMap();
                cache2.put(NAME_KEY, new HashSet());
                source.cache.put(loader, cache2);
            } else if (useCache) {
                Reference ref = (Reference) cache2.get(key);
                gen = (Class) SoftReference.dereference(ref);
            }
            if (gen == null) {
                Object save = CURRENT.get();
                CURRENT.set(this);
                try {
                    this.key = key;
                    if (attemptLoad) {
                        try {
                            gen = loader.loadClass(getClassName());
                        } catch (ClassNotFoundException e) {
                        // ignore
                        }
                    }
                    if (gen == null) {
                        byte[] b = strategy.generate(this);
                        String className = ClassNameReader.getClassName(new $ClassReader(b));
                        getClassNameCache(loader).add(className);
                        gen = ReflectUtils.defineClass(className, b, loader);
                    }
                    if (useCache) {
                        cache2.put(key, new WeakReference(gen));
                    }
                    return firstInstance(gen);
                } finally {
                    CURRENT.set(save);
                }
            }
        }
        return firstInstance(gen);
    } catch (RuntimeException e) {
        throw e;
    } catch (Error e) {
        throw e;
    } catch (Exception e) {
        throw new CodeGenerationException(e);
    }
}
Also used : Reference(java.lang.ref.Reference) SoftReference(com.intellij.reference.SoftReference) WeakReference(java.lang.ref.WeakReference) net.sf.cglib.asm.$ClassReader(net.sf.cglib.asm.$ClassReader) WeakReference(java.lang.ref.WeakReference)

Example 13 with ClassReader

use of jodd.asm5.ClassReader in project apex-core by apache.

the class FastClassIndexReaderTest method testPerformance.

@Test
public void testPerformance() throws Exception {
    String javahome = System.getProperty("java.home");
    String jdkJar = javahome + "/lib/rt.jar";
    JarFile jar = new JarFile(jdkJar);
    java.util.Enumeration<JarEntry> entriesEnum = jar.entries();
    long time = System.currentTimeMillis();
    while (entriesEnum.hasMoreElements()) {
        JarEntry jarEntry = entriesEnum.nextElement();
        if (jarEntry.getName().endsWith("class")) {
            InputStream ins = jar.getInputStream(jarEntry);
            //FastClassSignatureReader fastClassSignatureReader = new FastClassSignatureReader(ins);
            ClassReader classReader = new ClassReader(ins);
            ClassNodeType classN = new ClassNodeType();
            classReader.accept(classN, ClassReader.SKIP_CODE);
            CompactClassNode ccn = CompactUtil.compactClassNode(classN);
            ins.close();
        }
    }
    LOG.info("The time to scan jdk using ASM ClassReader {} ", System.currentTimeMillis() - time);
    jar.close();
    jar = new JarFile(jdkJar);
    entriesEnum = jar.entries();
    time = System.currentTimeMillis();
    while (entriesEnum.hasMoreElements()) {
        JarEntry jarEntry = entriesEnum.nextElement();
        if (jarEntry.getName().endsWith("class")) {
            InputStream ins = jar.getInputStream(jarEntry);
            FastClassIndexReader fastClassIndexReader = new FastClassIndexReader(ins);
            ins.close();
        }
    }
    jar.close();
    LOG.info("The time to scan jdk using FastClassIndexReader {} ", System.currentTimeMillis() - time);
}
Also used : InputStream(java.io.InputStream) ClassReader(org.apache.xbean.asm5.ClassReader) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Example 14 with ClassReader

use of jodd.asm5.ClassReader in project tomee by apache.

the class TempClassLoader method isEnum.

/**
     * Fast-parse the given class bytecode to determine if it is an
     * enum class.
     */
private static boolean isEnum(final byte[] bytes) {
    final IsEnumVisitor isEnumVisitor = new IsEnumVisitor();
    final ClassReader classReader = new ClassReader(bytes);
    classReader.accept(isEnumVisitor, ClassReader.SKIP_DEBUG);
    return isEnumVisitor.isEnum;
}
Also used : ClassReader(org.apache.xbean.asm5.ClassReader)

Example 15 with ClassReader

use of jodd.asm5.ClassReader in project tomee by apache.

the class DynamicSubclass method copyClassAnnotations.

private static void copyClassAnnotations(final Class<?> clazz, final ClassVisitor newClass) throws ProxyGenerationException {
    try {
        final ClassReader classReader = new ClassReader(readClassFile(clazz));
        final ClassVisitor visitor = new CopyClassAnnotations(newClass);
        classReader.accept(visitor, ClassReader.SKIP_CODE);
    } catch (final IOException e) {
        throw new ProxyGenerationException(e);
    }
}
Also used : ProxyGenerationException(org.apache.openejb.util.proxy.ProxyGenerationException) ClassReader(org.apache.xbean.asm5.ClassReader) ClassVisitor(org.apache.xbean.asm5.ClassVisitor) IOException(java.io.IOException)

Aggregations

ClassReader (org.apache.xbean.asm5.ClassReader)11 IOException (java.io.IOException)9 InputStream (java.io.InputStream)7 ClassReader (jodd.asm5.ClassReader)4 URL (java.net.URL)2 JarEntry (java.util.jar.JarEntry)2 JarFile (java.util.jar.JarFile)2 ProxyGenerationException (org.apache.openejb.util.proxy.ProxyGenerationException)2 ClassVisitor (org.apache.xbean.asm5.ClassVisitor)2 Test (org.junit.Test)2 SoftReference (com.intellij.reference.SoftReference)1 BufferedInputStream (java.io.BufferedInputStream)1 Reference (java.lang.ref.Reference)1 WeakReference (java.lang.ref.WeakReference)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 JarInputStream (java.util.jar.JarInputStream)1