Package org.eclipse.jetty.annotations
Class AnnotationParser
java.lang.Object
org.eclipse.jetty.annotations.AnnotationParser
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Convenience base class to provide no-ops for all Handler methods.class
Immutable information gathered by parsing class header.class
Immutable information gathered by parsing a field on a class.static interface
Signature for all handlers that respond to parsing class files.class
Immutable information gathered by parsing a method on a class.class
ASM visitor for a class.class
An ASM visitor for parsing Fields.class
ASM Visitor for parsing a method. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
private final int
Map of classnames scanned and the first location from which scan occurredprivate static final int
private static final Logger
-
Constructor Summary
ConstructorsConstructorDescriptionAnnotationParser
(int javaPlatform) AnnotationParser
(int javaPlatform, int asmVersion) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addParsedClass
(String classname, Resource location) Add a class as having been parsed.private static int
Determine the runtime version of asm.getParsedLocations
(String classname) Deprecated.List of duplicate locations no longer storedboolean
isValidClassFileName
(String name) Check that the given path represents a valid class file name.boolean
isValidClassFilePath
(String path) Check that the given path does not contain hidden directoriesstatic String
Convert internal name to simple namestatic String[]
Convert internal names to simple names.void
parse
(Set<? extends AnnotationParser.Handler> handlers, Class<?> clazz, boolean visitSuperClasses) Parse the given class, optionally walking its inheritance hierarchyvoid
parse
(Set<? extends AnnotationParser.Handler> handlers, ClassLoader loader, boolean visitParents, boolean nullInclusive) Deprecated.void
parse
(Set<? extends AnnotationParser.Handler> handlers, String className) Parse a given classvoid
parse
(Set<? extends AnnotationParser.Handler> handlers, String[] classNames) Parse the given classesvoid
parse
(Set<? extends AnnotationParser.Handler> handlers, URI uri) Parse a particular urivoid
parse
(Set<? extends AnnotationParser.Handler> handlers, URI[] uris) Parse classes in the supplied uris.void
parse
(Set<? extends AnnotationParser.Handler> handlers, List<String> classNames) Parse the given classesvoid
parse
(Set<? extends AnnotationParser.Handler> handlers, Resource r) Parse a resourceprotected void
parseDir
(Set<? extends AnnotationParser.Handler> handlers, Resource root) Parse all classes in a directoryprotected void
parseJar
(Set<? extends AnnotationParser.Handler> handlers, Resource jarResource) Parse a resource that is a jar file.protected void
parseJarEntry
(Set<? extends AnnotationParser.Handler> handlers, Resource jar, MultiReleaseJarFile.VersionedJarEntry entry) Parse a single entry in a jar filevoid
Remove any parsed class names.protected void
scanClass
(Set<? extends AnnotationParser.Handler> handlers, Resource containingResource, InputStream is) Use ASM on a class
-
Field Details
-
LOG
-
ASM_VERSION
private static final int ASM_VERSION -
_parsedClassNames
Map of classnames scanned and the first location from which scan occurred -
_javaPlatform
private final int _javaPlatform -
_asmVersion
private final int _asmVersion
-
-
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
-
asmVersion
private static int asmVersion()Determine the runtime version of asm.- Returns:
- the
Opcodes
ASM value matching the runtime version of asm.
-
normalize
Convert internal name to simple name- Parameters:
name
- the internal name- Returns:
- the simple name
-
normalize
Convert internal names to simple names.- Parameters:
list
- the list of internal names- Returns:
- the list of simple names
-
addParsedClass
Add a class as having been parsed.- Parameters:
classname
- the name of the classlocation
- the fully qualified location of the class
-
getParsedLocations
Deprecated.List of duplicate locations no longer storedGet 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) throws Exception Parse a given class- Parameters:
handlers
- the set of handlers to find classclassName
- 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 inclazz
- the class to look forvisitSuperClasses
- 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 inclassNames
- 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 inclassNames
- the class names- 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 inroot
- the resource directory to look for classes- 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 inloader
- the classloader for the classesvisitParents
- if true, visit parent classloaders toonullInclusive
- if true, an empty pattern means all names match, if false, none match- Throws:
Exception
- if unable to parse
-
parse
Parse classes in the supplied uris.- Parameters:
handlers
- the handlers to look for classes inuris
- the uris for the jars- Throws:
Exception
- if unable to parse
-
parse
Parse a particular uri- Parameters:
handlers
- the handlers to look for classes inuri
- the uri for the jar- Throws:
Exception
- if unable to parse
-
parse
Parse a resource- Parameters:
handlers
- the handlers to look for classes inr
- the resource to parse- 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 injarResource
- 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 injar
- the jar fileentry
- the entry in the potentially MultiRelease jar resource to parse- 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 incontainingResource
- the dir or jar that the class is contained within, can be null if not knownis
- the input stream to parse- Throws:
IOException
- if unable to parse
-
resetParsedClasses
public void resetParsedClasses()Remove any parsed class names. -
isValidClassFileName
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
Check that the given path does not contain hidden directories- Parameters:
path
- the class file path- Returns:
- whether the class file path is valid
-