Search in sources :

Example 1 with JavaClass

use of org.apache.tomcat.util.bcel.classfile.JavaClass in project tomcat by apache.

the class ContextConfig method checkHandlesTypes.

/**
     * For classes packaged with the web application, the class and each
     * super class needs to be checked for a match with {@link HandlesTypes} or
     * for an annotation that matches {@link HandlesTypes}.
     * @param javaClass the class to check
     * @param javaClassCache a class cache
     */
protected void checkHandlesTypes(JavaClass javaClass, Map<String, JavaClassCacheEntry> javaClassCache) {
    // Skip this if we can
    if (typeInitializerMap.size() == 0) {
        return;
    }
    if ((javaClass.getAccessFlags() & org.apache.tomcat.util.bcel.Const.ACC_ANNOTATION) > 0) {
        // Skip annotations.
        return;
    }
    String className = javaClass.getClassName();
    Class<?> clazz = null;
    if (handlesTypesNonAnnotations) {
        // This *might* be match for a HandlesType.
        populateJavaClassCache(className, javaClass, javaClassCache);
        JavaClassCacheEntry entry = javaClassCache.get(className);
        if (entry.getSciSet() == null) {
            try {
                populateSCIsForCacheEntry(entry, javaClassCache);
            } catch (StackOverflowError soe) {
                throw new IllegalStateException(sm.getString("contextConfig.annotationsStackOverflow", context.getName(), classHierarchyToString(className, entry, javaClassCache)));
            }
        }
        if (!entry.getSciSet().isEmpty()) {
            // Need to try and load the class
            clazz = Introspection.loadClass(context, className);
            if (clazz == null) {
                // Can't load the class so no point continuing
                return;
            }
            for (ServletContainerInitializer sci : entry.getSciSet()) {
                Set<Class<?>> classes = initializerClassMap.get(sci);
                if (classes == null) {
                    classes = new HashSet<>();
                    initializerClassMap.put(sci, classes);
                }
                classes.add(clazz);
            }
        }
    }
    if (handlesTypesAnnotations) {
        AnnotationEntry[] annotationEntries = javaClass.getAnnotationEntries();
        if (annotationEntries != null) {
            for (Map.Entry<Class<?>, Set<ServletContainerInitializer>> entry : typeInitializerMap.entrySet()) {
                if (entry.getKey().isAnnotation()) {
                    String entryClassName = entry.getKey().getName();
                    for (AnnotationEntry annotationEntry : annotationEntries) {
                        if (entryClassName.equals(getClassName(annotationEntry.getAnnotationType()))) {
                            if (clazz == null) {
                                clazz = Introspection.loadClass(context, className);
                                if (clazz == null) {
                                    // continuing
                                    return;
                                }
                            }
                            for (ServletContainerInitializer sci : entry.getValue()) {
                                initializerClassMap.get(sci).add(clazz);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
}
Also used : Set(java.util.Set) RuleSet(org.apache.tomcat.util.digester.RuleSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with JavaClass

use of org.apache.tomcat.util.bcel.classfile.JavaClass in project tomcat by apache.

the class ContextConfig method populateJavaClassCache.

private void populateJavaClassCache(String className, Map<String, JavaClassCacheEntry> javaClassCache) {
    if (!javaClassCache.containsKey(className)) {
        String name = className.replace('.', '/') + ".class";
        try (InputStream is = context.getLoader().getClassLoader().getResourceAsStream(name)) {
            if (is == null) {
                return;
            }
            ClassParser parser = new ClassParser(is);
            JavaClass clazz = parser.parse();
            populateJavaClassCache(clazz.getClassName(), clazz, javaClassCache);
        } catch (ClassFormatException e) {
            log.debug(sm.getString("contextConfig.invalidSciHandlesTypes", className), e);
        } catch (IOException e) {
            log.debug(sm.getString("contextConfig.invalidSciHandlesTypes", className), e);
        }
    }
}
Also used : JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ClassParser(org.apache.tomcat.util.bcel.classfile.ClassParser) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException)

Example 3 with JavaClass

use of org.apache.tomcat.util.bcel.classfile.JavaClass in project tomcat by apache.

the class ContextConfig method processAnnotationsStream.

protected void processAnnotationsStream(InputStream is, WebXml fragment, boolean handlesTypesOnly, Map<String, JavaClassCacheEntry> javaClassCache) throws ClassFormatException, IOException {
    ClassParser parser = new ClassParser(is);
    JavaClass clazz = parser.parse();
    checkHandlesTypes(clazz, javaClassCache);
    if (handlesTypesOnly) {
        return;
    }
    AnnotationEntry[] annotationsEntries = clazz.getAnnotationEntries();
    if (annotationsEntries != null) {
        String className = clazz.getClassName();
        for (AnnotationEntry ae : annotationsEntries) {
            String type = ae.getAnnotationType();
            if ("Ljavax/servlet/annotation/WebServlet;".equals(type)) {
                processAnnotationWebServlet(className, ae, fragment);
            } else if ("Ljavax/servlet/annotation/WebFilter;".equals(type)) {
                processAnnotationWebFilter(className, ae, fragment);
            } else if ("Ljavax/servlet/annotation/WebListener;".equals(type)) {
                fragment.addListener(className);
            } else {
            // Unknown annotation - ignore
            }
        }
    }
}
Also used : AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) ClassParser(org.apache.tomcat.util.bcel.classfile.ClassParser)

Aggregations

JavaClass (org.apache.tomcat.util.bcel.classfile.JavaClass)3 AnnotationEntry (org.apache.tomcat.util.bcel.classfile.AnnotationEntry)2 ClassParser (org.apache.tomcat.util.bcel.classfile.ClassParser)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)1 ClassFormatException (org.apache.tomcat.util.bcel.classfile.ClassFormatException)1 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)1 RuleSet (org.apache.tomcat.util.digester.RuleSet)1