You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Hello7.java 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import java.lang.annotation.Annotation;
  2. import java.lang.reflect.Field;
  3. import java.util.Collections;
  4. import java.util.*;
  5. public class Hello7 {
  6. public static void main(String[] args) {
  7. printAnnos(Hello7.class);
  8. }
  9. public static void printAnnos(Class clazz) {
  10. try {
  11. Annotation[] annos = clazz.getAnnotations();
  12. System.out.println("Annotations on "+clazz.getName()+"? "+(annos!=null && annos.length!=0));
  13. if (annos!=null && annos.length>0) {
  14. List<Annotation> la = new ArrayList<Annotation>();
  15. for (Annotation anno: annos) {
  16. la.add(anno);
  17. }
  18. Collections.<Annotation>sort(la,new AnnoComparator());
  19. System.out.println("Annotation count is "+annos.length);
  20. for (Annotation anno: la) {
  21. System.out.println(anno);
  22. }
  23. }
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. static class AnnoComparator implements Comparator<Annotation> {
  29. public int compare(Annotation a, Annotation b) {
  30. return a.toString().compareTo(b.toString());
  31. }
  32. }
  33. }