aboutsummaryrefslogtreecommitdiffstats
path: root/tests/bugs170/xmldefs/Hello5.java
blob: 361415a3e0f4b97a7cefbfc988dac6f4ead20b58 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.*;
public class Hello5 {

	public static int field1;
	public static int field2;

        public static void main(String[] args) {
                printAnnos("field1");
                printAnnos("field2");
        } 

        public static void printAnnos(String fieldname) {
        	try {
	        	Field m = Hello5.class.getDeclaredField(fieldname);
	        	Annotation[] annos = m.getAnnotations();
	        	System.out.println("Annotations on "+fieldname+"? "+(annos!=null && annos.length!=0));
	        	if (annos!=null && annos.length>0) {
		        	List<Annotation> la = new ArrayList<Annotation>();
		        	for (Annotation anno: annos) {
		        		la.add(anno);
		        	}
		        	Collections.<Annotation>sort(la,new AnnoComparator());
		        	
	        		System.out.println("Annotation count is "+annos.length);
	        		for (Annotation anno: la) {
	        			System.out.println(anno);
	        		}
	        	}
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        }
        
        static class AnnoComparator implements Comparator<Annotation> {
        	public int compare(Annotation a, Annotation b) {
        		return a.toString().compareTo(b.toString());
        	}
        }
}