Search in sources :

Example 1 with ClassReader

use of org.springframework.asm.ClassReader in project spring-boot by spring-projects.

the class AutoConfigurationMetadata method readAutoConfiguration.

private Properties readAutoConfiguration() throws IOException {
    Properties autoConfiguration = CollectionFactory.createSortedProperties(true);
    Set<String> classNames = new LinkedHashSet<>();
    classNames.addAll(readSpringFactories());
    classNames.addAll(readAutoConfigurationsFile());
    Set<String> publicClassNames = new LinkedHashSet<>();
    for (String className : classNames) {
        File classFile = findClassFile(className);
        if (classFile == null) {
            throw new IllegalStateException("Auto-configuration class '" + className + "' not found.");
        }
        try (InputStream in = new FileInputStream(classFile)) {
            int access = new ClassReader(in).getAccess();
            if ((access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC) {
                publicClassNames.add(className);
            }
        }
    }
    autoConfiguration.setProperty("autoConfigurationClassNames", String.join(",", publicClassNames));
    autoConfiguration.setProperty("module", getProject().getName());
    return autoConfiguration;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassReader(org.springframework.asm.ClassReader) Properties(java.util.Properties) File(java.io.File) OutputFile(org.gradle.api.tasks.OutputFile) FileInputStream(java.io.FileInputStream)

Example 2 with ClassReader

use of org.springframework.asm.ClassReader in project spring-boot by spring-projects.

the class MainClassFinder method createClassDescriptor.

private static ClassDescriptor createClassDescriptor(InputStream inputStream) {
    try {
        ClassReader classReader = new ClassReader(inputStream);
        ClassDescriptor classDescriptor = new ClassDescriptor();
        classReader.accept(classDescriptor, ClassReader.SKIP_CODE);
        return classDescriptor;
    } catch (IOException ex) {
        return null;
    }
}
Also used : ClassReader(org.springframework.asm.ClassReader) IOException(java.io.IOException)

Example 3 with ClassReader

use of org.springframework.asm.ClassReader in project spring-boot by spring-projects.

the class DatabaseDriverClassNameTests method getInterfaceNames.

private List<String> getInterfaceNames(String className) throws IOException {
    // Use ASM to avoid unwanted side-effects of loading JDBC drivers
    ClassReader classReader = new ClassReader(getClass().getResourceAsStream("/" + className + ".class"));
    List<String> interfaceNames = new ArrayList<>();
    for (String name : classReader.getInterfaces()) {
        interfaceNames.add(name);
        interfaceNames.addAll(getInterfaceNames(name));
    }
    String superName = classReader.getSuperName();
    if (superName != null) {
        interfaceNames.addAll(getInterfaceNames(superName));
    }
    return interfaceNames;
}
Also used : ArrayList(java.util.ArrayList) ClassReader(org.springframework.asm.ClassReader)

Example 4 with ClassReader

use of org.springframework.asm.ClassReader in project spring-framework by spring-projects.

the class AbstractClassGenerator method generate.

protected Class generate(ClassLoaderData data) {
    Class gen;
    Object save = CURRENT.get();
    CURRENT.set(this);
    try {
        ClassLoader classLoader = data.getClassLoader();
        if (classLoader == null) {
            throw new IllegalStateException("ClassLoader is null while trying to define class " + getClassName() + ". It seems that the loader has been expired from a weak reference somehow. " + "Please file an issue at cglib's issue tracker.");
        }
        synchronized (classLoader) {
            String name = generateClassName(data.getUniqueNamePredicate());
            data.reserveName(name);
            this.setClassName(name);
        }
        if (attemptLoad) {
            try {
                gen = classLoader.loadClass(getClassName());
                return gen;
            } catch (ClassNotFoundException e) {
            // ignore
            }
        }
        byte[] b = strategy.generate(this);
        String className = ClassNameReader.getClassName(new ClassReader(b));
        ProtectionDomain protectionDomain = getProtectionDomain();
        synchronized (classLoader) {
            // just in case
            // SPRING PATCH BEGIN
            gen = ReflectUtils.defineClass(className, b, classLoader, protectionDomain, contextClass);
        // SPRING PATCH END
        }
        return gen;
    } catch (RuntimeException | Error ex) {
        throw ex;
    } catch (Exception ex) {
        throw new CodeGenerationException(ex);
    } finally {
        CURRENT.set(save);
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassReader(org.springframework.asm.ClassReader)

Example 5 with ClassReader

use of org.springframework.asm.ClassReader in project spring-framework by spring-projects.

the class LocalVariableTableParameterNameDiscoverer method inspectClass.

/**
 * Inspects the target class.
 * <p>Exceptions will be logged, and a marker map returned to indicate the
 * lack of debug information.
 */
private Map<Executable, String[]> inspectClass(Class<?> clazz) {
    InputStream is = clazz.getResourceAsStream(ClassUtils.getClassFileName(clazz));
    if (is == null) {
        // simply means this method of discovering parameter names won't work.
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot find '.class' file for class [" + clazz + "] - unable to determine constructor/method parameter names");
        }
        return NO_DEBUG_INFO_MAP;
    }
    // custom handling of the close() method in a finally-block.
    try {
        ClassReader classReader = new ClassReader(is);
        Map<Executable, String[]> map = new ConcurrentHashMap<>(32);
        classReader.accept(new ParameterNameDiscoveringVisitor(clazz, map), 0);
        return map;
    } catch (IOException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Exception thrown while reading '.class' file for class [" + clazz + "] - unable to determine constructor/method parameter names", ex);
        }
    } catch (IllegalArgumentException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("ASM ClassReader failed to parse class file [" + clazz + "], probably due to a new Java class file version that isn't supported yet " + "- unable to determine constructor/method parameter names", ex);
        }
    } finally {
        try {
            is.close();
        } catch (IOException ex) {
        // ignore
        }
    }
    return NO_DEBUG_INFO_MAP;
}
Also used : InputStream(java.io.InputStream) ClassReader(org.springframework.asm.ClassReader) IOException(java.io.IOException) Executable(java.lang.reflect.Executable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ClassReader (org.springframework.asm.ClassReader)6 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ClassInjector (grails.compiler.ast.ClassInjector)1 GlobalClassInjector (grails.compiler.ast.GlobalClassInjector)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Executable (java.lang.reflect.Executable)1 ProtectionDomain (java.security.ProtectionDomain)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 OutputFile (org.gradle.api.tasks.OutputFile)1 FileSystemResource (org.grails.io.support.FileSystemResource)1 PathMatchingResourcePatternResolver (org.grails.io.support.PathMatchingResourcePatternResolver)1 Resource (org.grails.io.support.Resource)1 AnnotationVisitor (org.springframework.asm.AnnotationVisitor)1 ClassVisitor (org.springframework.asm.ClassVisitor)1