Class AnnotationParser

java.lang.Object
org.eclipse.jetty.annotations.AnnotationParser
Direct Known Subclasses:
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 Details

    • _parsedClassNames

      protected Map<String,Resource> _parsedClassNames
      Map of classnames scanned and the first location from which scan occurred
  • Constructor Details

    • 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 Details

    • 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, 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(Set<? extends AnnotationParser.Handler> handlers, String className) throws Exception
      Parse a given class
      Parameters:
      handlers - the set of handlers to find class
      className - the class name to parse
      Throws:
      Exception - if unable to parse
    • parse

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

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

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

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

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

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

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

      protected void parseJar(Set<? extends AnnotationParser.Handler> handlers, Resource jarResource) 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
      Throws:
      Exception - if unable to parse
    • parseJarEntry

      protected void parseJarEntry(Set<? extends AnnotationParser.Handler> handlers, Resource jar, MultiReleaseJarFile.VersionedJarEntry entry) throws 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:
      Exception - if unable to parse
    • scanClass

      protected void scanClass(Set<? extends AnnotationParser.Handler> handlers, 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.
    • isValidClassFileName

      public boolean isValidClassFileName(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(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