Search in sources :

Example 16 with ClassReader

use of org.apache.xbean.asm6.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 17 with ClassReader

use of org.apache.xbean.asm6.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 18 with ClassReader

use of org.apache.xbean.asm6.ClassReader in project geronimo-xbean by apache.

the class XbeanAsmParameterNameLoader method getAllMethodParameters.

/**
 * Gets the parameter names of all methods with the specified name or null if the class was compiled without debug symbols on.
 * @param clazz the class for which the method parameter names should be retrieved
 * @param methodName the of the method for which the parameters should be retrieved
 * @return a map from Method object to the parameter names or null if the class was compiled without debug symbols on
 */
public Map<Method, List<String>> getAllMethodParameters(Class clazz, String methodName) {
    // Determine the constructors?
    Method[] methods = getMethods(clazz, methodName);
    if (methods.length == 0) {
        return Collections.emptyMap();
    }
    // Check the cache
    if (methodCache.containsKey(methods[0])) {
        Map<Method, List<String>> methodParameters = new HashMap<Method, List<String>>();
        for (Method method : methods) {
            methodParameters.put(method, methodCache.get(method));
        }
        return methodParameters;
    }
    // Load the parameter names using ASM
    Map<Method, List<String>> methodParameters = new HashMap<Method, List<String>>();
    try {
        ClassReader reader = XbeanAsmParameterNameLoader.createClassReader(clazz);
        XbeanAsmParameterNameLoader.AllParameterNamesDiscoveringVisitor visitor = new XbeanAsmParameterNameLoader.AllParameterNamesDiscoveringVisitor(clazz, methodName);
        reader.accept(visitor, 0);
        Map exceptions = visitor.getExceptions();
        if (exceptions.size() == 1) {
            throw new RuntimeException((Exception) exceptions.values().iterator().next());
        }
        if (!exceptions.isEmpty()) {
            throw new RuntimeException(exceptions.toString());
        }
        methodParameters = visitor.getMethodParameters();
    } catch (IOException ex) {
    }
    // Cache the names
    for (Method method : methods) {
        methodCache.put(method, methodParameters.get(method));
    }
    return methodParameters;
}
Also used : HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) Method(java.lang.reflect.Method) IOException(java.io.IOException) ClassReader(org.apache.xbean.asm6.ClassReader) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 19 with ClassReader

use of org.apache.xbean.asm6.ClassReader in project geronimo-xbean by apache.

the class XbeanAsmParameterNameLoader method getAllConstructorParameters.

/**
 * Gets the parameter names of all constructor or null if the class was compiled without debug symbols on.
 * @param clazz the class for which the constructor parameter names should be retrieved
 * @return a map from Constructor object to the parameter names or null if the class was compiled without debug symbols on
 */
public Map<Constructor, List<String>> getAllConstructorParameters(Class clazz) {
    // Determine the constructors?
    List<Constructor> constructors = new ArrayList<Constructor>(Arrays.asList(clazz.getConstructors()));
    constructors.addAll(Arrays.asList(clazz.getDeclaredConstructors()));
    if (constructors.isEmpty()) {
        return Collections.emptyMap();
    }
    // Check the cache
    if (constructorCache.containsKey(constructors.get(0))) {
        Map<Constructor, List<String>> constructorParameters = new HashMap<Constructor, List<String>>();
        for (Constructor constructor : constructors) {
            constructorParameters.put(constructor, constructorCache.get(constructor));
        }
        return constructorParameters;
    }
    // Load the parameter names using ASM
    Map<Constructor, List<String>> constructorParameters = new HashMap<Constructor, List<String>>();
    try {
        ClassReader reader = XbeanAsmParameterNameLoader.createClassReader(clazz);
        XbeanAsmParameterNameLoader.AllParameterNamesDiscoveringVisitor visitor = new XbeanAsmParameterNameLoader.AllParameterNamesDiscoveringVisitor(clazz);
        reader.accept(visitor, 0);
        Map exceptions = visitor.getExceptions();
        if (exceptions.size() == 1) {
            throw new RuntimeException((Exception) exceptions.values().iterator().next());
        }
        if (!exceptions.isEmpty()) {
            throw new RuntimeException(exceptions.toString());
        }
        constructorParameters = visitor.getConstructorParameters();
    } catch (IOException ex) {
    }
    // Cache the names
    for (Constructor constructor : constructors) {
        constructorCache.put(constructor, constructorParameters.get(constructor));
    }
    return constructorParameters;
}
Also used : HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ClassReader(org.apache.xbean.asm6.ClassReader) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 20 with ClassReader

use of org.apache.xbean.asm6.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)

Aggregations

IOException (java.io.IOException)15 InputStream (java.io.InputStream)12 ClassReader (org.apache.xbean.asm5.ClassReader)11 ClassReader (org.apache.xbean.asm6.ClassReader)8 JarEntry (java.util.jar.JarEntry)5 ClassReader (jodd.asm5.ClassReader)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Optional.ofNullable (java.util.Optional.ofNullable)3 JarOutputStream (java.util.jar.JarOutputStream)3 Stream (java.util.stream.Stream)3 EXPAND_FRAMES (org.apache.xbean.asm6.ClassReader.EXPAND_FRAMES)3 ClassWriter (org.apache.xbean.asm6.ClassWriter)3 COMPUTE_FRAMES (org.apache.xbean.asm6.ClassWriter.COMPUTE_FRAMES)3 ClassRemapper (org.apache.xbean.asm6.commons.ClassRemapper)3 Remapper (org.apache.xbean.asm6.commons.Remapper)3