Search in sources :

Example 6 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)

Example 7 with ClassParser

use of org.apache.tomcat.util.bcel.classfile.ClassParser in project tomcat 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<>();
    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) {
        try (Jar jar = JarFactory.newInstance(jarURL)) {
            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();
            }
        }
    }
    System.out.println("ClassParser performance test took: " + duration + " ns");
}
Also used : InputStream(java.io.InputStream) Jar(org.apache.tomcat.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)

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