Datasheet
Method Description
<T extends Annotation> T Returns the annotation associated with the
getAnnotation(Class<T> annotationType) specified type, or null if none exists.
Annotation[] getAnnotations() Returns an array of all annotations on the
current element, or a zero-length array if no
annotations are present.
Annotation[] Similar to getAnnotations but does not
getDeclaredAnnotations() return inherited annotations— only annota-
tions explicitly declared on this element are
returned. Returns a zero-length array if no
annotations are present.
boolean isAnnotationPresent(Class<? Returns true if the annotationType is
extends Annotation> annotationType) present on the current element, false
otherwise.
Develop an annotation that might be useful in developing a testing framework. The framework invokes
test methods specified in the annotation and expects a Boolean return value from these testing methods.
The reflection API is used to both process the annotation and execute the test methods.
The annotation is listed as follows:
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface TestParameters {
String testStage();
String testMethods();
String testOutputType(); // “db” or “file”
String testOutput(); // filename or data source/table name
}
An example application of this annotation is to a class of utility methods for strings. You might develop
your own utility class and develop testing methods to ensure the utility methods work:
@TestParameters(testStage=”Unit”,
testMethods=”testConcat,testSubstring”,
testOutputType=”screen”,
testOutput=””)
public class StringUtility {
public String concat(String s1, String s2)
{
return(s1 + s2);
}
public String substring(String str, int startIndex, int endIndex)
{
return(str.substring(startIndex, endIndex));
32
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 32