Class AnnotationParser

  • Direct Known Subclasses:
    AnnotationParser

    public class AnnotationParser
    extends java.lang.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

      • _parsedClassNames

        protected java.util.Map<java.lang.String,​Resource> _parsedClassNames
        Map of classnames scanned and the first location from which scan occurred
    • 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

      • normalize

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

        public static java.lang.String[] normalize​(java.lang.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​(java.lang.String classname,
                                   Resource location)
        Add a class as having been parsed.
        Parameters:
        classname - the name of the class
        location - the fully qualified location of the class
      • parse

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

        public void parse​(java.util.Set<? extends AnnotationParser.Handler> handlers,
                          java.lang.Class<?> clazz,
                          boolean visitSuperClasses)
                   throws java.lang.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
        visitSuperClasses - if true, also visit super classes for parse
        Throws:
        java.lang.Exception - if unable to parse class
      • parse

        public void parse​(java.util.Set<? extends AnnotationParser.Handler> handlers,
                          java.lang.String[] classNames)
                   throws java.lang.Exception
        Parse the given classes
        Parameters:
        handlers - the set of handlers to look for class in
        classNames - the class name
        Throws:
        java.lang.Exception - if unable to parse
      • parse

        public void parse​(java.util.Set<? extends AnnotationParser.Handler> handlers,
                          java.util.List<java.lang.String> classNames)
                   throws java.lang.Exception
        Parse the given classes
        Parameters:
        handlers - the set of handlers to look for class in
        classNames - the class names
        Throws:
        java.lang.Exception - if unable to parse
      • parse

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

        public void parse​(java.util.Set<? extends AnnotationParser.Handler> handlers,
                          java.net.URI uri)
                   throws java.lang.Exception
        Parse a particular uri
        Parameters:
        handlers - the handlers to look for classes in
        uri - the uri for the jar
        Throws:
        java.lang.Exception - if unable to parse
      • parse

        public void parse​(java.util.Set<? extends AnnotationParser.Handler> handlers,
                          Resource r)
                   throws java.lang.Exception
        Parse a resource
        Parameters:
        handlers - the handlers to look for classes in
        r - the resource to parse
        Throws:
        java.lang.Exception - if unable to parse
      • parseDir

        protected void parseDir​(java.util.Set<? extends AnnotationParser.Handler> handlers,
                                Resource root)
                         throws java.lang.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
        Throws:
        java.lang.Exception - if unable to parse
      • parseJar

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

        protected void parseJarEntry​(java.util.Set<? extends AnnotationParser.Handler> handlers,
                                     Resource jar,
                                     MultiReleaseJarFile.VersionedJarEntry entry)
                              throws java.lang.Exception
        Parse a single entry in a jar file
        Parameters:
        handlers - the handlers to look for classes in
        entry - the entry in the potentially MultiRelease jar resource to parse
        jar - the jar file
        Throws:
        java.lang.Exception - if unable to parse
      • scanClass

        protected void scanClass​(java.util.Set<? extends AnnotationParser.Handler> handlers,
                                 Resource containingResource,
                                 java.io.InputStream is)
                          throws java.io.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:
        java.io.IOException - if unable to parse
      • resetParsedClasses

        public void resetParsedClasses()
        Remove any parsed class names.
      • isValidClassFileName

        public boolean isValidClassFileName​(java.lang.String name)
        Check that the given path represents a valid class file name. The check is fairly cursory, checking that:
        • the name ends with .class
        • it isn't a dot file or in a hidden directory
        • the name of the class at least begins with a valid identifier for a class name
        Parameters:
        name - the class file name
        Returns:
        whether the class file name is valid
      • isValidClassFilePath

        public boolean isValidClassFilePath​(java.lang.String path)
        Check that the given path does not contain hidden directories
        Parameters:
        path - the class file path
        Returns:
        whether the class file path is valid