Search in sources :

Example 1 with ClassParser

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

the class TesterPerformance method testClassParserPerformance.

@Test
public void testClassParserPerformance() throws IOException {
    File libDir = new File(JAR_LOCATION);
    String[] libs = libDir.list();
    Assert.assertNotNull(libs);
    Set<URL> jarURLs = new HashSet<URL>();
    for (String lib : libs) {
        if (!lib.toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
            continue;
        }
        jarURLs.add(new URL("jar:" + new File(libDir, lib).toURI().toURL().toExternalForm() + "!/"));
    }
    long duration = 0;
    for (URL jarURL : jarURLs) {
        Jar jar = JarFactory.newInstance(jarURL);
        try {
            jar.nextEntry();
            String jarEntryName = jar.getEntryName();
            while (jarEntryName != null) {
                if (jarEntryName.endsWith(".class")) {
                    InputStream is = jar.getEntryInputStream();
                    long start = System.nanoTime();
                    ClassParser cp = new ClassParser(is);
                    cp.parse();
                    duration += System.nanoTime() - start;
                }
                jar.nextEntry();
                jarEntryName = jar.getEntryName();
            }
        } finally {
            jar.close();
        }
    }
    System.out.println("ClassParser performance test took: " + duration + " ns");
}
Also used : InputStream(java.io.InputStream) Jar(org.apache.tomcat.util.scan.Jar) File(java.io.File) URL(java.net.URL) HashSet(java.util.HashSet) ClassParser(org.apache.tomcat.util.bcel.classfile.ClassParser) Test(org.junit.Test)

Example 2 with ClassParser

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

the class ContextConfig method processAnnotationsStream.

protected void processAnnotationsStream(InputStream is, WebXml fragment, boolean handlesTypesOnly) throws ClassFormatException, IOException {
    ClassParser parser = new ClassParser(is);
    JavaClass clazz = parser.parse();
    checkHandlesTypes(clazz);
    if (handlesTypesOnly) {
        return;
    }
    String className = clazz.getClassName();
    AnnotationEntry[] annotationsEntries = clazz.getAnnotationEntries();
    if (annotationsEntries != null) {
        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)

Example 3 with ClassParser

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

the class ContextConfig method populateJavaClassCache.

private void populateJavaClassCache(String className) {
    if (!javaClassCache.containsKey(className)) {
        String name = className.replace('.', '/') + ".class";
        InputStream is = context.getLoader().getClassLoader().getResourceAsStream(name);
        if (is == null) {
            return;
        }
        ClassParser parser = new ClassParser(is);
        try {
            JavaClass clazz = parser.parse();
            populateJavaClassCache(clazz.getClassName(), clazz);
        } catch (ClassFormatException e) {
            log.debug(sm.getString("contextConfig.invalidSciHandlesTypes", className), e);
        } catch (IOException e) {
            log.debug(sm.getString("contextConfig.invalidSciHandlesTypes", className), e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
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 4 with ClassParser

use of org.apache.tomcat.util.bcel.classfile.ClassParser 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;
    }
    processClass(fragment, clazz);
}
Also used : JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) ClassParser(org.apache.tomcat.util.bcel.classfile.ClassParser)

Example 5 with ClassParser

use of org.apache.tomcat.util.bcel.classfile.ClassParser 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 | 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)

Aggregations

ClassParser (org.apache.tomcat.util.bcel.classfile.ClassParser)7 InputStream (java.io.InputStream)5 JavaClass (org.apache.tomcat.util.bcel.classfile.JavaClass)4 IOException (java.io.IOException)3 URL (java.net.URL)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 HashSet (java.util.HashSet)2 ClassFormatException (org.apache.tomcat.util.bcel.classfile.ClassFormatException)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 LogFacade (org.apache.meecrowave.logging.tomcat.LogFacade)1 Jar (org.apache.tomcat.Jar)1 AnnotationEntry (org.apache.tomcat.util.bcel.classfile.AnnotationEntry)1 WebXml (org.apache.tomcat.util.descriptor.web.WebXml)1 Jar (org.apache.tomcat.util.scan.Jar)1