1
2
3
4
5
6
7
8
9
10
11
|
package annotationTest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target (ElementType.METHOD)
@Retention (RetentionPolicy.RUNTIME)
public @interface Test{}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package annotationTest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target (ElementType.METHOD)
@Retention (RetentionPolicy.RUNTIME)
public @interface UserCase {
public int id();
public String description() default "no description" ;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package annotationTest;
import java.util.List;
public class PasswordUtils {
@UserCase (id = 47 , description = "Passwords must contatin at least one numeric" )
public boolean validatePassword(String password) {
return (password.matches( "\\w*\\d\\w*" ));
}
@UserCase (id = 48 )
public String encryptPassword(String password) {
return new StringBuilder(password).reverse().toString();
}
@UserCase (id = 49 , description = "New passwords can't equal previously used ones" )
public boolean checkForNewPassword(List
String password) {
return !prevPasswords.contains(password);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package annotationTest;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UserCaseTracker {
public static void trackUserCases(List
for (Method m : cl.getDeclaredMethods()) {
UserCase uc = m.getAnnotation(UserCase. class );
if (uc != null ) {
System.out.println( "Found Use Case:" + uc.id() + " "
+ uc.description());
useCases.remove( new Integer(uc.id()));
}
}
for ( int i : useCases) {
System.out.println( "Warning: Missing use case-" + i);
}
}
public static void main(String[] args) {
List new ArrayList
Collections.addAll(useCases, 47 , 48 , 49 , 50 );
trackUserCases(useCases, PasswordUtils. class );
}
}
|