Java Code Examples for org.apache.tomcat.util.bcel.classfile.ClassParser

The following examples show how to use org.apache.tomcat.util.bcel.classfile.ClassParser. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Tomcat8-Source-Read   Source File: ContextConfig.java    License: MIT License 5 votes vote down vote up
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);
}
 
Example 2
Source Project: Tomcat8-Source-Read   Source File: TesterPerformance.java    License: MIT License 5 votes vote down vote up
@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");
}
 
Example 3
Source Project: Tomcat7.0.67   Source File: ContextConfig.java    License: Apache License 2.0 5 votes vote down vote up
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
            }
        }
    }
}
 
Example 4
@Override
protected void processClasses(final WebXml webXml, final Set<WebXml> orderedFragments) {
    final ClassLoader loader = context.getLoader().getClassLoader();
    orderedFragments.forEach(fragment -> {
        final WebXml annotations = new WebXml();
        annotations.setDistributable(true);
        final URL url = fragment.getURL();
        String urlString = url.toExternalForm();
        Collection<Class<?>> classes = webClasses.get(urlString);
        if (classes == null) { // mainly java 11, no need on java 8
            if (urlString.startsWith("file:") && urlString.endsWith("jar")) {
                urlString = "jar:" + urlString + "!/";
            } else {
                return;
            }
            classes = webClasses.get(urlString);
            if (classes == null) {
                return;
            }
        }
        classes.forEach(clazz -> {
            try (final InputStream stream = loader.getResourceAsStream(clazz.getName().replace('.', '/') + ".class")) {
                processClass(annotations, new ClassParser(stream).parse());
            } catch (final IOException e) {
                new LogFacade(MeecrowaveContextConfig.class.getName()).error("Can't parse " + clazz);
            }
        });
        fragment.merge(singleton(annotations));
    });
}
 
Example 5
Source Project: Tomcat7.0.67   Source File: TesterPerformance.java    License: Apache License 2.0 4 votes vote down vote up
@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");
}
 
Example 6
Source Project: tomcatsrc   Source File: TesterPerformance.java    License: Apache License 2.0 4 votes vote down vote up
@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");
}