Class AnnotationParser


  • public class AnnotationParser
    extends Object
    AnnotationParser

    Use asm to scan classes for annotations. A SAX-style parsing is done. Handlers are registered which will be called back when various types of entity are encountered, eg a class, a method, a field.

    Handlers are not called back in any particular order and are assumed to be order-independent.

    As a registered Handler will be called back for each annotation discovered on a class, a method, a field, the Handler should test to see if the annotation is one that it is interested in.

    For the servlet spec, we are only interested in annotations on classes, methods and fields, so the callbacks for handling finding a class, a method a field are themselves not fully implemented.

    • Field Detail

      • ASM_OPCODE_VERSION

        protected static int ASM_OPCODE_VERSION
    • Constructor Detail

      • AnnotationParser

        public AnnotationParser()
      • AnnotationParser

        public AnnotationParser​(int javaPlatform)
        Parameters:
        javaPlatform - The target java version or 0 for the current runtime.
      • AnnotationParser

        public AnnotationParser​(int javaPlatform,
                                int asmVersion)
    • Method Detail

      • asmVersion

        public static int asmVersion()
        Determine the runtime version of asm.
        Returns:
        the org.objectweb.asm.Opcode matching the runtime version of asm.
      • normalize

        public static String normalize​(String name)
        Convert internal name to simple name
        Parameters:
        name - the internal name
        Returns:
        the simple name
      • normalize

        public static String[] normalize​(String[] list)
        Convert internal names to simple names.
        Parameters:
        list - the list of internal names
        Returns:
        the list of simple names
      • addParsedClass

        public void addParsedClass​(String classname,
                                   org.eclipse.jetty.util.resource.Resource location)
        Add a class as having been parsed.
        Parameters:
        classname - the name of the class
        location - the fully qualified location of the class
      • isParsed

        public boolean isParsed​(String className)
        True if the class has already been processed, false otherwise
        Parameters:
        className - the classname
        Returns:
        true if class was parsed, false if not
      • getParsedLocations

        public List<String> getParsedLocations​(String classname)
        Get the locations of the given classname. There may be more than one location if there are duplicates of the same class.
        Parameters:
        classname - the name of the class
        Returns:
        an immutable list of locations
      • parse

        public void parse​(Set<? extends AnnotationParser.Handler> handlers,
                          String className,
                          ClassNameResolver resolver)
                   throws Exception
        Parse a given class
        Parameters:
        handlers - the set of handlers to find class
        className - the class name to parse
        resolver - the class name resolver to use
        Throws:
        Exception - if unable to parse
      • parse

        public void parse​(Set<? extends AnnotationParser.Handler> handlers,
                          Class<?> clazz,
                          ClassNameResolver resolver,
                          boolean visitSuperClasses)
                   throws Exception
        Parse the given class, optionally walking its inheritance hierarchy
        Parameters:
        handlers - the handlers to look for class in
        clazz - the class to look for
        resolver - the resolver to look up class with
        visitSuperClasses - if true, also visit super classes for parse
        Throws:
        Exception - if unable to parse class
      • parseDir

        protected void parseDir​(Set<? extends AnnotationParser.Handler> handlers,
                                org.eclipse.jetty.util.resource.Resource root,
                                ClassNameResolver resolver)
                         throws Exception
        Parse all classes in a directory
        Parameters:
        handlers - the set of handlers to look for classes in
        root - the resource directory to look for classes
        resolver - the class name resolver
        Throws:
        Exception - if unable to parse
      • parse

        @Deprecated
        public void parse​(Set<? extends AnnotationParser.Handler> handlers,
                          ClassLoader loader,
                          boolean visitParents,
                          boolean nullInclusive)
                   throws Exception
        Deprecated.
        Parse classes in the supplied classloader. Only class files in jar files will be scanned.
        Parameters:
        handlers - the handlers to look for classes in
        loader - the classloader for the classes
        visitParents - if true, visit parent classloaders too
        nullInclusive - if true, an empty pattern means all names match, if false, none match
        Throws:
        Exception - if unable to parse
      • parse

        public void parse​(Set<? extends AnnotationParser.Handler> handlers,
                          URI[] uris,
                          ClassNameResolver resolver)
                   throws Exception
        Parse classes in the supplied uris.
        Parameters:
        handlers - the handlers to look for classes in
        uris - the uris for the jars
        resolver - the class name resolver
        Throws:
        Exception - if unable to parse
      • parse

        public void parse​(Set<? extends AnnotationParser.Handler> handlers,
                          org.eclipse.jetty.util.resource.Resource r,
                          ClassNameResolver resolver)
                   throws Exception
        Parse a resource
        Parameters:
        handlers - the handlers to look for classes in
        r - the resource to parse
        resolver - the class name resolver
        Throws:
        Exception - if unable to parse
      • parseJar

        protected void parseJar​(Set<? extends AnnotationParser.Handler> handlers,
                                org.eclipse.jetty.util.resource.Resource jarResource,
                                ClassNameResolver resolver)
                         throws Exception
        Parse a resource that is a jar file.
        Parameters:
        handlers - the handlers to look for classes in
        jarResource - the jar resource to parse
        resolver - the class name resolver
        Throws:
        Exception - if unable to parse
      • parseJarEntry

        protected void parseJarEntry​(Set<? extends AnnotationParser.Handler> handlers,
                                     org.eclipse.jetty.util.resource.Resource jar,
                                     org.eclipse.jetty.util.MultiReleaseJarFile.VersionedJarEntry entry,
                                     ClassNameResolver resolver)
                              throws Exception
        Parse a single entry in a jar file
        Parameters:
        handlers - the handlers to look for classes in
        jar - The jar from which entry was obtained
        entry - the entry in the potentially MultiRelease jar resource to parse
        resolver - the class name resolver
        Throws:
        Exception - if unable to parse
      • scanClass

        protected void scanClass​(Set<? extends AnnotationParser.Handler> handlers,
                                 org.eclipse.jetty.util.resource.Resource containingResource,
                                 InputStream is)
                          throws IOException
        Use ASM on a class
        Parameters:
        handlers - the handlers to look for classes in
        containingResource - the dir or jar that the class is contained within, can be null if not known
        is - the input stream to parse
        Throws:
        IOException - if unable to parse
      • resetParsedClasses

        public void resetParsedClasses()
        Remove any parsed class names.