Package org.scannotation
Class AnnotationDB
- java.lang.Object
-
- org.scannotation.AnnotationDB
-
- All Implemented Interfaces:
java.io.Serializable
public class AnnotationDB extends java.lang.Object implements java.io.Serializable
The class allows you to scan an arbitrary set of "archives" for .class files. These class files are parsed to see what annotations they use. Two indexes are created. The javax, java, sun, com.sun, and javassist packages will not be scanned by default. One is a map of annotations and what classes use those annotations. This could be used, for example, by an EJB deployer to find all the EJBs contained in the archive Another is a mpa of classes and what annotations those classes use.- Version:
- $Revision: 1 $
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
AnnotationDB.CrossReferenceException
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,java.util.Set<java.lang.String>>
annotationIndex
protected java.util.Map<java.lang.String,java.util.Set<java.lang.String>>
classIndex
protected boolean
ignoreBadURLs
protected java.lang.String[]
ignoredPackages
protected java.util.Map<java.lang.String,java.util.Set<java.lang.String>>
implementsIndex
protected boolean
scanClassAnnotations
protected boolean
scanFieldAnnotations
protected boolean
scanMethodAnnotations
protected java.lang.String[]
scanPackages
protected boolean
scanParameterAnnotations
-
Constructor Summary
Constructors Constructor Description AnnotationDB()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addIgnoredPackages(java.lang.String... ignored)
void
crossReferenceImplementedInterfaces()
Sometimes you want to see if a particular class implements an interface with certain annotations After you have loaded all your classpaths with the scanArchive() method, call this method to cross reference a class's implemented interfaces.void
crossReferenceMetaAnnotations()
This method will cross reference annotations in the annotation index with any meta-annotations that they have and create additional entries as needed.java.util.Map<java.lang.String,java.util.Set<java.lang.String>>
getAnnotationIndex()
returns a map keyed by the fully qualified string name of a annotation class.java.util.Map<java.lang.String,java.util.Set<java.lang.String>>
getClassIndex()
returns a map keyed by the list of classes scanned.java.lang.String[]
getIgnoredPackages()
java.lang.String[]
getScanPackages()
private boolean
ignoreScan(java.lang.String intf)
void
outputAnnotationIndex(java.io.PrintWriter writer)
Prints out annotationIndexprotected void
populate(javassist.bytecode.annotation.Annotation[] annotations, java.lang.String className)
void
scanArchives(java.net.URL... urls)
Scan a url that represents an "archive" this is a classpath directory or jar filevoid
scanClass(java.io.InputStream bits)
Parse a .class file for annotationsprotected void
scanClass(javassist.bytecode.ClassFile cf)
protected void
scanFields(javassist.bytecode.ClassFile cf)
protected void
scanMethods(javassist.bytecode.ClassFile cf)
Scanns both the method and its parameters for annotations.void
setIgnoreBadURLs(boolean ignoreBadURLs)
Whether or not you want AnnotationDB to ignore bad URLs passed to scanArchives.void
setIgnoredPackages(java.lang.String[] ignoredPackages)
Override/overwrite any ignored packagesvoid
setScanClassAnnotations(boolean scanClassAnnotations)
Whether or not you want AnnotationDB to scan for class level annotationsvoid
setScanFieldAnnotations(boolean scanFieldAnnotations)
Whether or not you want AnnotationDB to scan for parameter level annotationsvoid
setScanMethodAnnotations(boolean scanMethodAnnotations)
Wheter or not you want AnnotationDB to scan for method level annotationsvoid
setScanPackages(java.lang.String[] scanPackages)
Set explicit packages to scan.void
setScanParameterAnnotations(boolean scanParameterAnnotations)
Whether or not you want AnnotationDB to scan for parameter level annotations
-
-
-
Field Detail
-
annotationIndex
protected java.util.Map<java.lang.String,java.util.Set<java.lang.String>> annotationIndex
-
implementsIndex
protected java.util.Map<java.lang.String,java.util.Set<java.lang.String>> implementsIndex
-
classIndex
protected java.util.Map<java.lang.String,java.util.Set<java.lang.String>> classIndex
-
scanClassAnnotations
protected transient boolean scanClassAnnotations
-
scanMethodAnnotations
protected transient boolean scanMethodAnnotations
-
scanParameterAnnotations
protected transient boolean scanParameterAnnotations
-
scanFieldAnnotations
protected transient boolean scanFieldAnnotations
-
ignoredPackages
protected transient java.lang.String[] ignoredPackages
-
scanPackages
protected transient java.lang.String[] scanPackages
-
ignoreBadURLs
protected transient boolean ignoreBadURLs
-
-
Method Detail
-
getScanPackages
public java.lang.String[] getScanPackages()
-
setScanPackages
public void setScanPackages(java.lang.String[] scanPackages)
Set explicit packages to scan. Set to null to enable ignore list.- Parameters:
scanPackages
- packages to scan or null
-
getIgnoredPackages
public java.lang.String[] getIgnoredPackages()
-
setIgnoredPackages
public void setIgnoredPackages(java.lang.String[] ignoredPackages)
Override/overwrite any ignored packages- Parameters:
ignoredPackages
- cannot be null
-
addIgnoredPackages
public void addIgnoredPackages(java.lang.String... ignored)
-
crossReferenceMetaAnnotations
public void crossReferenceMetaAnnotations() throws AnnotationDB.CrossReferenceException
This method will cross reference annotations in the annotation index with any meta-annotations that they have and create additional entries as needed. For example:
-
crossReferenceImplementedInterfaces
public void crossReferenceImplementedInterfaces() throws AnnotationDB.CrossReferenceException
Sometimes you want to see if a particular class implements an interface with certain annotations After you have loaded all your classpaths with the scanArchive() method, call this method to cross reference a class's implemented interfaces. The cross references will be added to the annotationIndex and classIndex indexes- Throws:
AnnotationDB.CrossReferenceException
- an Exception thrown if referenced interfaces haven't been scanned
-
ignoreScan
private boolean ignoreScan(java.lang.String intf)
-
getAnnotationIndex
public java.util.Map<java.lang.String,java.util.Set<java.lang.String>> getAnnotationIndex()
returns a map keyed by the fully qualified string name of a annotation class. The Set returne is a list of classes that use that annotation somehow.
-
getClassIndex
public java.util.Map<java.lang.String,java.util.Set<java.lang.String>> getClassIndex()
returns a map keyed by the list of classes scanned. The value set returned is a list of annotations used by that class.
-
setScanClassAnnotations
public void setScanClassAnnotations(boolean scanClassAnnotations)
Whether or not you want AnnotationDB to scan for class level annotations- Parameters:
scanClassAnnotations
-
-
setScanMethodAnnotations
public void setScanMethodAnnotations(boolean scanMethodAnnotations)
Wheter or not you want AnnotationDB to scan for method level annotations- Parameters:
scanMethodAnnotations
-
-
setScanParameterAnnotations
public void setScanParameterAnnotations(boolean scanParameterAnnotations)
Whether or not you want AnnotationDB to scan for parameter level annotations- Parameters:
scanParameterAnnotations
-
-
setScanFieldAnnotations
public void setScanFieldAnnotations(boolean scanFieldAnnotations)
Whether or not you want AnnotationDB to scan for parameter level annotations- Parameters:
scanFieldAnnotations
-
-
setIgnoreBadURLs
public void setIgnoreBadURLs(boolean ignoreBadURLs)
Whether or not you want AnnotationDB to ignore bad URLs passed to scanArchives. Default is to throw an IOException.- Parameters:
ignoreBadURLs
-
-
scanArchives
public void scanArchives(java.net.URL... urls) throws java.io.IOException
Scan a url that represents an "archive" this is a classpath directory or jar file- Parameters:
urls
- variable list of URLs to scan as archives- Throws:
java.io.IOException
-
scanClass
public void scanClass(java.io.InputStream bits) throws java.io.IOException
Parse a .class file for annotations- Parameters:
bits
- input stream pointing to .class file bits- Throws:
java.io.IOException
-
scanClass
protected void scanClass(javassist.bytecode.ClassFile cf)
-
scanMethods
protected void scanMethods(javassist.bytecode.ClassFile cf)
Scanns both the method and its parameters for annotations.- Parameters:
cf
-
-
scanFields
protected void scanFields(javassist.bytecode.ClassFile cf)
-
populate
protected void populate(javassist.bytecode.annotation.Annotation[] annotations, java.lang.String className)
-
outputAnnotationIndex
public void outputAnnotationIndex(java.io.PrintWriter writer)
Prints out annotationIndex- Parameters:
writer
-
-
-