com.tc.backport175.bytecode
Class AnnotationReader

java.lang.Object
  extended by com.tc.backport175.bytecode.AnnotationReader

public class AnnotationReader
extends java.lang.Object

Reads Java 5 java.lang.annotation.RetentionPolicy.RUNTIME and java.lang.annotation.RetentionPolicy.CLASS annotations from the class' bytecode.

Can be used with a custom implementation of the org.codehaus.backport175.reader.bytecode.spi.BytecodeProvider interface.

Note: does not handles Inherited feature. This has to be done in the higher level that knows about the class hierarchy (see backport175.Annotations f.e)

Author:
Jonas Bonér, Alexandre Vasseur

Nested Class Summary
static class AnnotationReader.ClassKey
          Contains info about the class being parsed.
static class AnnotationReader.MemberKey
          Unique key for class members (methods, fields and constructors) to be used in hash maps etc.
 
Method Summary
 AnnotationVisitor createAnnotationVisitor(AnnotationElement.Annotation annotation)
          Returns the annotation visitor to use.
 Annotation getAnnotation(java.lang.String annotationName)
          Returns the class annotation with the name specified.
 Annotation getAnnotation(java.lang.String annotationName, java.lang.reflect.Constructor constructor)
          Returns the constructor annotation with the name specified for the constructor specified.
 Annotation getAnnotation(java.lang.String annotationName, java.lang.reflect.Field field)
          Returns the field annotation with the name specified for the field specified.
 Annotation getAnnotation(java.lang.String annotationName, java.lang.reflect.Method method)
          Returns the method annotation with the name specified for the method specified.
 AnnotationElement.Annotation getAnnotationElement(java.lang.String annotationName)
          Returns the class annotation element with the name specified.
 AnnotationElement.Annotation[] getAnnotationElements()
          Returns all the class annotations.
 Annotation[] getAnnotations()
          Returns all the class annotations.
 Annotation[] getAnnotations(java.lang.reflect.Constructor constructor)
          Returns all the constructor annotations.
 Annotation[] getAnnotations(java.lang.reflect.Field field)
          Returns all the field annotations.
 Annotation[] getAnnotations(java.lang.reflect.Method method)
          Returns all the method annotations.
static byte[] getBytecodeFor(java.lang.String className, java.lang.ClassLoader loader)
          Returns the bytecode for a class.
static BytecodeProvider getBytecodeProviderFor(java.lang.Class klass)
          Returns the bytecode provider.
static BytecodeProvider getBytecodeProviderFor(java.lang.String className, java.lang.ClassLoader loader)
          Returns the bytecode provider.
 Annotation getConstructorAnnotation(java.lang.String annotationName, java.lang.String constructorDesc, java.lang.ClassLoader loader)
          Returns the constructor annotation with the name specified for the constructor specified.
 AnnotationElement.Annotation getConstructorAnnotationElement(java.lang.String annotationName, java.lang.String desc)
          Returns the annotation with the name specified for the constructor specified.
 AnnotationElement.Annotation[] getConstructorAnnotationElements(java.lang.String desc)
          Returns all the constructor annotation elements.
static BytecodeProvider getDefaultBytecodeProvider()
          Returns the bytecode provider.
 Annotation getFieldAnnotation(java.lang.String annotationName, java.lang.String fieldName, java.lang.String fieldDesc, java.lang.ClassLoader loader)
          Returns the field annotation with the name specified for the field specified.
 AnnotationElement.Annotation getFieldAnnotationElement(java.lang.String annotationName, java.lang.String name, java.lang.String desc)
          Returns the annotation with the name specified for the field specified.
 AnnotationElement.Annotation[] getFieldAnnotationElements(java.lang.String name, java.lang.String desc)
          Returns all the field annotation elements.
 Annotation getMethodAnnotation(java.lang.String annotationName, java.lang.String methodName, java.lang.String methodDesc, java.lang.ClassLoader loader)
          Returns the method annotation with the name specified for the method specified.
 AnnotationElement.Annotation getMethodAnnotationElement(java.lang.String annotationName, java.lang.String name, java.lang.String desc)
          Returns the method annotation with the name specified for the method specified.
 AnnotationElement.Annotation[] getMethodAnnotationElements(java.lang.String name, java.lang.String desc)
          Returns all the method annotation elements.
static AnnotationReader getReaderFor(AnnotationReader.ClassKey classKey)
          Returns the annotation reader for the class specified.
static AnnotationReader getReaderFor(java.lang.Class klass)
          Returns the annotation reader for the class specified.
static AnnotationReader getReaderFor(java.lang.String className, java.lang.ClassLoader loader)
          Returns the annotation reader for the class specified.
 boolean isAnnotationPresent(java.lang.String annotationName)
          Checks if an annotation is present at a specific class.
 boolean isAnnotationPresent(java.lang.String annotationName, java.lang.reflect.Constructor constructor)
          Checks if an annotation is present at a specific constructor.
 boolean isAnnotationPresent(java.lang.String annotationName, java.lang.reflect.Field field)
          Checks if an annotation is present at a specific field.
 boolean isAnnotationPresent(java.lang.String annotationName, java.lang.reflect.Method method)
          Checks if an annotation is present at a specific method.
 boolean isConstructorAnnotationPresent(java.lang.String annotationName, java.lang.String desc)
          Checks if an annotation is present at a specific constructor.
 boolean isFieldAnnotationPresent(java.lang.String annotationName, java.lang.String name, java.lang.String desc)
          Checks if an annotation is present at a specific field.
 boolean isMethodAnnotationPresent(java.lang.String annotationName, java.lang.String name, java.lang.String desc)
          Checks if an annotation is present at a specific method.
static void refresh(java.lang.Class klass)
          Resets the annotation reader for the class specified and triggers a new parsing of the newly read bytecode.
static void refresh(java.lang.String className, java.lang.ClassLoader loader)
          Resets the annotation reader for the class specified and triggers a new parsing of the newly read bytecode.
static void refreshAll()
          Resets *all* the annotation reader and triggers a new parsing of the newly read bytecode.
static void setBytecodeProviderFor(java.lang.Class klass, BytecodeProvider bytecodeProvider)
          Sets the bytecode provider.
static void setBytecodeProviderFor(java.lang.String className, java.lang.ClassLoader loader, BytecodeProvider bytecodeProvider)
          Sets the bytecode provider.
static void setDefaultBytecodeProvider(BytecodeProvider bytecodeProvider)
          Sets the bytecode provider.
static java.lang.String toJavaName(java.lang.String desc)
          Converts the annotion class description to a Java class name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setDefaultBytecodeProvider

public static void setDefaultBytecodeProvider(BytecodeProvider bytecodeProvider)
Sets the bytecode provider.

If a custom provider is not set then a default impl will be used (which reads the bytecode from disk).

Parameters:
bytecodeProvider -

getDefaultBytecodeProvider

public static BytecodeProvider getDefaultBytecodeProvider()
Returns the bytecode provider.

Returns:
the bytecode provider

setBytecodeProviderFor

public static void setBytecodeProviderFor(java.lang.Class klass,
                                          BytecodeProvider bytecodeProvider)
Sets the bytecode provider.

If a custom provider is not set then a default impl will be used (which reads the bytecode from disk).

Parameters:
klass -
bytecodeProvider -

setBytecodeProviderFor

public static void setBytecodeProviderFor(java.lang.String className,
                                          java.lang.ClassLoader loader,
                                          BytecodeProvider bytecodeProvider)
Sets the bytecode provider.

If a custom provider is not set then a default impl will be used (which reads the bytecode from disk).

Parameters:
className -
loader -
bytecodeProvider -

getBytecodeProviderFor

public static BytecodeProvider getBytecodeProviderFor(java.lang.Class klass)
Returns the bytecode provider.

Parameters:
klass -
Returns:
the bytecode provider

getBytecodeProviderFor

public static BytecodeProvider getBytecodeProviderFor(java.lang.String className,
                                                      java.lang.ClassLoader loader)
Returns the bytecode provider.

Parameters:
className -
loader -
Returns:
the bytecode provider

getBytecodeFor

public static byte[] getBytecodeFor(java.lang.String className,
                                    java.lang.ClassLoader loader)
                             throws java.lang.ClassNotFoundException,
                                    java.io.IOException
Returns the bytecode for a class.

Parameters:
className -
loader -
Returns:
the bytecode for a class
Throws:
java.lang.ClassNotFoundException
java.io.IOException

getReaderFor

public static AnnotationReader getReaderFor(java.lang.Class klass)
Returns the annotation reader for the class specified.

The annotation reader is created and cached if non-existant.

Parameters:
klass -
Returns:
the annotation reader

getReaderFor

public static AnnotationReader getReaderFor(java.lang.String className,
                                            java.lang.ClassLoader loader)
Returns the annotation reader for the class specified.

The annotation reader is created and cached if non-existant.

Parameters:
className -
loader -
Returns:
the annotation reader

getReaderFor

public static AnnotationReader getReaderFor(AnnotationReader.ClassKey classKey)
Returns the annotation reader for the class specified.

The annotation reader is created and cached if non-existant.

Parameters:
classKey -
Returns:
the annotation reader

refresh

public static void refresh(java.lang.Class klass)
Resets the annotation reader for the class specified and triggers a new parsing of the newly read bytecode.

This method calls parse and is therefore all the is needed to invoke to get a fully updated reader.

Parameters:
klass -

refresh

public static void refresh(java.lang.String className,
                           java.lang.ClassLoader loader)
Resets the annotation reader for the class specified and triggers a new parsing of the newly read bytecode.

This method calls parse and is therefore all the is needed to invoke to get a fully updated reader.

Parameters:
className -
loader -

refreshAll

public static void refreshAll()
Resets *all* the annotation reader and triggers a new parsing of the newly read bytecode.

This method will force parsing of all classes bytecode which might be very time consuming, use with care.

This method calls parse and is therefore all the is needed to invoke to get a fully updated reader.


toJavaName

public static java.lang.String toJavaName(java.lang.String desc)
Converts the annotion class description to a Java class name. Caution: Does not handles array type or primitive.

Parameters:
desc -
Returns:

isAnnotationPresent

public boolean isAnnotationPresent(java.lang.String annotationName)
Checks if an annotation is present at a specific class.

Parameters:
annotationName - the annotation name
Returns:
true if the annotation is present else false

getAnnotation

public Annotation getAnnotation(java.lang.String annotationName)
Returns the class annotation with the name specified.

Parameters:
annotationName -
Returns:
the class annotation

getAnnotations

public Annotation[] getAnnotations()
Returns all the class annotations.

Returns:
an array with the class annotations

isAnnotationPresent

public boolean isAnnotationPresent(java.lang.String annotationName,
                                   java.lang.reflect.Constructor constructor)
Checks if an annotation is present at a specific constructor.

Parameters:
annotationName - the annotation name
constructor - the java.lang.reflect.Constructor object to find the annotations on.
Returns:
true if the annotation is present else false

getAnnotation

public Annotation getAnnotation(java.lang.String annotationName,
                                java.lang.reflect.Constructor constructor)
Returns the constructor annotation with the name specified for the constructor specified.

Parameters:
annotationName - the annotation name
constructor - the java.lang.reflect.Constructor object to find the annotations on.
Returns:
the constructor annotation

getConstructorAnnotation

public Annotation getConstructorAnnotation(java.lang.String annotationName,
                                           java.lang.String constructorDesc,
                                           java.lang.ClassLoader loader)
Returns the constructor annotation with the name specified for the constructor specified.

Parameters:
annotationName -
constructorDesc -
loader -
Returns:

getAnnotations

public Annotation[] getAnnotations(java.lang.reflect.Constructor constructor)
Returns all the constructor annotations.

Parameters:
constructor - the java.lang.reflect.Constructor object to find the annotations on.
Returns:
an array with the constructor annotations

isAnnotationPresent

public boolean isAnnotationPresent(java.lang.String annotationName,
                                   java.lang.reflect.Method method)
Checks if an annotation is present at a specific method.

Parameters:
annotationName - the annotation name
method - the java.lang.reflect.Method object to find the annotations on.
Returns:
true if the annotation is present else false

getAnnotation

public Annotation getAnnotation(java.lang.String annotationName,
                                java.lang.reflect.Method method)
Returns the method annotation with the name specified for the method specified.

Parameters:
annotationName - the annotation name
method - the java.lang.reflect.Method object to find the annotations on.
Returns:
the method annotation

getMethodAnnotation

public Annotation getMethodAnnotation(java.lang.String annotationName,
                                      java.lang.String methodName,
                                      java.lang.String methodDesc,
                                      java.lang.ClassLoader loader)
Returns the method annotation with the name specified for the method specified.

Parameters:
annotationName -
methodName -
methodDesc -
loader -
Returns:

getAnnotations

public Annotation[] getAnnotations(java.lang.reflect.Method method)
Returns all the method annotations.

Parameters:
method - the java.lang.reflect.Method object to find the annotations on.
Returns:
an array with the method annotations

isAnnotationPresent

public boolean isAnnotationPresent(java.lang.String annotationName,
                                   java.lang.reflect.Field field)
Checks if an annotation is present at a specific field.

Parameters:
annotationName - the annotation name
field - the java.lang.reflect.Field object to find the annotations on.
Returns:
true if the annotation is present else false

getAnnotation

public Annotation getAnnotation(java.lang.String annotationName,
                                java.lang.reflect.Field field)
Returns the field annotation with the name specified for the field specified.

Parameters:
annotationName - the annotation name
field - the java.lang.reflect.Field object to find the annotations on.
Returns:
the field annotation

getFieldAnnotation

public Annotation getFieldAnnotation(java.lang.String annotationName,
                                     java.lang.String fieldName,
                                     java.lang.String fieldDesc,
                                     java.lang.ClassLoader loader)
Returns the field annotation with the name specified for the field specified.

Parameters:
annotationName -
fieldName -
fieldDesc -
loader -
Returns:

getAnnotations

public Annotation[] getAnnotations(java.lang.reflect.Field field)
Returns all the field annotations.

Parameters:
field - the java.lang.reflect.Field object to find the annotations on.
Returns:
an array with the field annotations

getAnnotationElement

public AnnotationElement.Annotation getAnnotationElement(java.lang.String annotationName)
Returns the class annotation element with the name specified.

Parameters:
annotationName -
Returns:
the class annotation

getAnnotationElements

public AnnotationElement.Annotation[] getAnnotationElements()
Returns all the class annotations.

Returns:
an array with the class annotations

isConstructorAnnotationPresent

public boolean isConstructorAnnotationPresent(java.lang.String annotationName,
                                              java.lang.String desc)
Checks if an annotation is present at a specific constructor.

Parameters:
annotationName - the annotation name
desc - the constructor desc
Returns:
true if the annotation is present else false

getConstructorAnnotationElement

public AnnotationElement.Annotation getConstructorAnnotationElement(java.lang.String annotationName,
                                                                    java.lang.String desc)
Returns the annotation with the name specified for the constructor specified.

Parameters:
annotationName - the annotation name
desc - the constructor desc
Returns:
the constructor annotation element

getConstructorAnnotationElements

public AnnotationElement.Annotation[] getConstructorAnnotationElements(java.lang.String desc)
Returns all the constructor annotation elements.

Parameters:
desc - the constructor desc
Returns:
an array with the constructor annotation elements

isMethodAnnotationPresent

public boolean isMethodAnnotationPresent(java.lang.String annotationName,
                                         java.lang.String name,
                                         java.lang.String desc)
Checks if an annotation is present at a specific method.

Parameters:
annotationName - the annotation name
name - the method name
desc - the method desc
Returns:
true if the annotation is present else false

getMethodAnnotationElement

public AnnotationElement.Annotation getMethodAnnotationElement(java.lang.String annotationName,
                                                               java.lang.String name,
                                                               java.lang.String desc)
Returns the method annotation with the name specified for the method specified.

Parameters:
annotationName - the annotation name
name - the method name
desc - the method desc
Returns:
the method annotation element

getMethodAnnotationElements

public AnnotationElement.Annotation[] getMethodAnnotationElements(java.lang.String name,
                                                                  java.lang.String desc)
Returns all the method annotation elements.

Parameters:
name - the method name
desc - the method desc
Returns:
an array with the method annotation elements

isFieldAnnotationPresent

public boolean isFieldAnnotationPresent(java.lang.String annotationName,
                                        java.lang.String name,
                                        java.lang.String desc)
Checks if an annotation is present at a specific field.

Parameters:
annotationName - the annotation name
name - the field name
desc - the field desc
Returns:
true if the annotation is present else false

getFieldAnnotationElement

public AnnotationElement.Annotation getFieldAnnotationElement(java.lang.String annotationName,
                                                              java.lang.String name,
                                                              java.lang.String desc)
Returns the annotation with the name specified for the field specified.

Parameters:
annotationName - the annotation name
name - the field name
desc - the field desc
Returns:
the field annotation element

getFieldAnnotationElements

public AnnotationElement.Annotation[] getFieldAnnotationElements(java.lang.String name,
                                                                 java.lang.String desc)
Returns all the field annotation elements.

Parameters:
name - the field name
desc - the field desc
Returns:
an array with the field annotation elements

createAnnotationVisitor

public AnnotationVisitor createAnnotationVisitor(AnnotationElement.Annotation annotation)
Returns the annotation visitor to use.

Swap to the 'tracing' visitor for simple debugging.

Parameters:
annotation -
Returns:


Copyright © 2010 Terracotta, Inc.. All Rights Reserved.