Java源码示例:org.scannotation.AnnotationDB
示例1
protected List<JarFileAnnotationPlugin> findAnnotatedClassFiles( String annotationClassName ) {
JarFileCache jarFileCache = JarFileCache.getInstance();
List<JarFileAnnotationPlugin> classFiles = new ArrayList<>();
// We want to scan the plugins folder for plugin.xml files...
//
for ( IPluginFolder pluginFolder : getPluginFolders() ) {
if ( pluginFolder.isPluginAnnotationsFolder() ) {
try {
// Get all the jar files in the plugin folder...
//
File[] fileObjects = jarFileCache.getFileObjects( pluginFolder );
if ( fileObjects != null ) {
for ( File fileObject : fileObjects ) {
// These are the jar files : find annotations in it...
//
AnnotationDB annotationDB = jarFileCache.getAnnotationDB( fileObject );
Set<String> impls = annotationDB.getAnnotationIndex().get( annotationClassName );
if ( impls != null ) {
for ( String fil : impls ) {
classFiles.add( new JarFileAnnotationPlugin( fil, fileObject.toURI().toURL(), fileObject.getParentFile().toURI().toURL() ) );
}
}
}
}
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
return classFiles;
}
示例2
public static List<String> findAnnotatedClasses( String folder, String annotationClassName ) throws HopException {
JarFileCache jarFileCache = JarFileCache.getInstance();
List<String> classNames = new ArrayList<>();
// Scan only jar files with @Transform and @ExtensionPointPlugin annotations
// No plugin.xml format supported for the moment
//
PluginFolder pluginFolder = new PluginFolder( "plugins/" + folder, false, true, false );
try {
// Get all the jar files in the plugin folder...
//
File[] fileObjects = jarFileCache.getFileObjects( pluginFolder );
if ( fileObjects != null ) {
// System.out.println( "Found " + fileObjects.length + " jar files in folder " + pluginFolder.getFolder() );
for ( File fileObject : fileObjects ) {
// These are the jar files : find annotations in it...
//
AnnotationDB annotationDB = jarFileCache.getAnnotationDB( fileObject );
// These are the jar files : find annotations in it...
//
Set<String> impls = annotationDB.getAnnotationIndex().get( annotationClassName );
if ( impls != null ) {
for ( String fil : impls ) {
classNames.add( fil );
}
}
}
} else {
System.out.println( "No jar files found in plugin folder " + pluginFolder.getFolder() );
}
} catch ( Exception e ) {
throw new HopException( "Unable to find annotated classes of class " + annotationClassName, e );
}
return classNames;
}
示例3
public static void patchEnums(ClassLoader loader, ClassPool pool, URL... urls)
throws IOException, ClassNotFoundException, NotFoundException, CannotCompileException
{
AnnotationDB db = new AnnotationDB();
db.setScanClassAnnotations(false);
db.setScanMethodAnnotations(false);
db.scanArchives(urls);
Set<String> annotations = db.getAnnotationIndex().get(SpireEnum.class.getName());
if (annotations == null) {
return;
}
boolean hasPrintedWarning = false;
for (String s : annotations) {
Class<?> cls = loader.loadClass(s);
for (Field field : cls.getDeclaredFields()) {
SpireEnum spireEnum = field.getDeclaredAnnotation(SpireEnum.class);
if (spireEnum != null) {
String enumName = field.getName();
if (!spireEnum.name().isEmpty()) {
enumName = spireEnum.name();
}
// Patch new field onto the enum
try {
CtClass ctClass = pool.get(field.getType().getName());
CtField f = new CtField(ctClass, enumName, ctClass);
f.setModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL | Modifier.ENUM);
ctClass.addField(f);
} catch (DuplicateMemberException ignore) {
// Field already exists
if (!Loader.DEBUG && !hasPrintedWarning) {
hasPrintedWarning = true;
System.out.println();
}
System.out.println(String.format("Warning: @SpireEnum %s %s is already defined.", field.getType().getName(), enumName));
}
}
}
}
}
示例4
public static void bustEnums(ClassLoader loader, URL... urls)
throws IOException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException
{
AnnotationDB db = new AnnotationDB();
db.setScanClassAnnotations(false);
db.setScanMethodAnnotations(false);
db.scanArchives(urls);
Set<String> annotations = db.getAnnotationIndex().get(SpireEnum.class.getName());
if (annotations == null) {
return;
}
for (String s : annotations) {
Class<?> cls = loader.loadClass(s);
for (Field field : cls.getDeclaredFields()) {
SpireEnum spireEnum = field.getDeclaredAnnotation(SpireEnum.class);
if (spireEnum != null) {
String enumName = field.getName();
if (!spireEnum.name().isEmpty()) {
enumName = spireEnum.name();
}
EnumBusterReflect buster;
if (enumBusterMap.containsKey(field.getType())) {
buster = enumBusterMap.get(field.getType());
} else {
buster = new EnumBusterReflect(loader, field.getType());
enumBusterMap.put(field.getType(), buster);
}
Enum<?> enumValue = buster.make(enumName);
buster.addByValue(enumValue);
try {
Field constantField = field.getType().getField(enumName);
ReflectionHelper.setStaticFinalField(constantField, enumValue);
} catch (NoSuchFieldException ignored) {
}
field.setAccessible(true);
field.set(null, enumValue);
}
}
}
}
示例5
public static List<String> findAnnotatedClasses( String folder, String annotationClassName ) throws KettleException {
JarFileCache jarFileCache = JarFileCache.getInstance();
List<String> classnames = new ArrayList<>();
// Scan only jar files with @Step and @ExtensionPointPlugin annotations
// No plugin.xml format supported for the moment
//
PluginFolder pluginFolder = new PluginFolder( "plugins/" + folder, false, true, false );
try {
// Get all the jar files in the plugin folder...
//
FileObject[] fileObjects = jarFileCache.getFileObjects( pluginFolder );
if ( fileObjects != null ) {
// System.out.println( "Found " + fileObjects.length + " jar files in folder " + pluginFolder.getFolder() );
for ( FileObject fileObject : fileObjects ) {
// These are the jar files : find annotations in it...
//
AnnotationDB annotationDB = jarFileCache.getAnnotationDB( fileObject );
// These are the jar files : find annotations in it...
//
Set<String> impls = annotationDB.getAnnotationIndex().get( annotationClassName );
if ( impls != null ) {
for ( String fil : impls ) {
classnames.add( fil );
}
}
}
} else {
System.out.println( "No jar files found in plugin folder " + pluginFolder.getFolder() );
}
} catch ( Exception e ) {
throw new KettleException( "Unable to find annotated classes of class "+annotationClassName, e);
}
return classnames;
}
示例6
/**
* Constructor.
*/
public ScannotationDB( )
{
_db = new AnnotationDB( );
}
示例7
protected List<JarFileAnnotationPlugin> findAnnotatedClassFiles( String annotationClassName ) {
JarFileCache jarFileCache = JarFileCache.getInstance();
List<JarFileAnnotationPlugin> classFiles = new ArrayList<>();
// We want to scan the plugins folder for plugin.xml files...
//
for ( PluginFolderInterface pluginFolder : getPluginFolders() ) {
if ( pluginFolder.isPluginAnnotationsFolder() ) {
FileObject[] fileObjects = null;
try {
// Get all the jar files in the plugin folder...
//
fileObjects = jarFileCache.getFileObjects( pluginFolder );
} catch ( Exception e ) {
log.logError( e.getMessage(), e );
}
if ( fileObjects != null ) {
for ( FileObject fileObject : fileObjects ) {
// These are the jar files : find annotations in it...
//
try {
AnnotationDB annotationDB = jarFileCache.getAnnotationDB( fileObject );
Set<String> impls = annotationDB.getAnnotationIndex().get( annotationClassName );
if ( impls != null ) {
for ( String fil : impls ) {
classFiles.add( new JarFileAnnotationPlugin( fil, fileObject.getURL(), fileObject
.getParent().getURL() ) );
}
}
} catch ( Exception jarPluginLoadError ) {
LogChannel.GENERAL.logError( "Error while finding annotations for jar plugin: '"
+ fileObject + "'" );
LogChannel.GENERAL.logDebug( "Error while finding annotations for jar plugin: '"
+ fileObject + "'", jarPluginLoadError );
}
}
}
}
}
return classFiles;
}