@Test public void defaultValues() throws Exception { TestUtils.makeSource(src, "x.A", "import java.lang.annotation.*;", "@Retention(RetentionPolicy.RUNTIME)", "@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})", "@net.java.sezpoz.Indexable", "public @interface A {", "int x() default 5;", "}"); TestUtils.makeSource(src, "y.C", "import x.A;", "public class C {", "@A", "public static Void m1() {return null;}", "@A(x=5)", "public static Void m2() {return null;}", "@A(x=17)", "public static Void m3() {return null;}", "}"); TestUtils.runApt(src, null, clz, new File[0], null); Class<? extends Annotation> a = loader.loadClass("x.A").asSubclass(Annotation.class); int cnt = 0; Annotation[] proxyAnns = new Annotation[3]; Annotation[] liveAnns = new Annotation[3]; for (IndexItem item : Index.load(a, Object.class, loader)) { cnt++; assertEquals("y.C", item.className()); Annotation ann = item.annotation(); Method x = a.getMethod("x"); int index; if (item.memberName().equals("m1")) { index = 0; assertEquals(5, x.invoke(ann)); } else if (item.memberName().equals("m2")) { index = 1; assertEquals(5, x.invoke(ann)); } else { assertEquals("m3", item.memberName()); index = 2; assertEquals(17, x.invoke(ann)); } proxyAnns[index] = ann; liveAnns[index] = item.element().getAnnotation(a); assertNotNull(liveAnns[index]); } assertEquals(3, cnt); Set<Annotation> all = new HashSet<Annotation>(); all.addAll(Arrays.asList(proxyAnns)); all.addAll(Arrays.asList(liveAnns)); assertEquals("all annotations are either with x=5 or x=17", 2, all.size()); Map<Annotation,Void> all2 = new IdentityHashMap<Annotation,Void>(); for (Annotation ann : proxyAnns) { all2.put(ann, null); } for (Annotation ann : liveAnns) { all2.put(ann, null); } assertEquals(6, all2.size()); }
public static <A extends Annotation,I> Index<A,I> load(Class<A> annotation, Class<I> instanceType) throws IllegalArgumentException { return load(annotation, instanceType, Thread.currentThread().getContextClassLoader()); }
Index implements Iterable<IndexItem<A,I>> { public static <A extends Annotation,I> Index<A,I> load(Class<A> annotation, Class<I> instanceType) throws IllegalArgumentException { return load(annotation, instanceType, Thread.currentThread().getContextClassLoader()); } }
Index implements Iterable<IndexItem<A,I>> { public static <A extends Annotation,I> Index<A,I> load(Class<A> annotation, Class<I> instanceType) throws IllegalArgumentException { return load(annotation, instanceType, Thread.currentThread().getContextClassLoader()); } private  Index(Class<A> annotation, Class<I> instance, ClassLoader loader); }
Index implements Iterable<IndexItem<A,I>> { public static <A extends Annotation,I> Index<A,I> load(Class<A> annotation, Class<I> instanceType) throws IllegalArgumentException { return load(annotation, instanceType, Thread.currentThread().getContextClassLoader()); } private  Index(Class<A> annotation, Class<I> instance, ClassLoader loader); static Index<A,I> load(Class<A> annotation, Class<I> instanceType); static Index<A,I> load(Class<A> annotation, Class<I> instanceType, ClassLoader loader); Iterator<IndexItem<A,I>> iterator(); }
Index implements Iterable<IndexItem<A,I>> { public static <A extends Annotation,I> Index<A,I> load(Class<A> annotation, Class<I> instanceType) throws IllegalArgumentException { return load(annotation, instanceType, Thread.currentThread().getContextClassLoader()); } private  Index(Class<A> annotation, Class<I> instance, ClassLoader loader); static Index<A,I> load(Class<A> annotation, Class<I> instanceType); static Index<A,I> load(Class<A> annotation, Class<I> instanceType, ClassLoader loader); Iterator<IndexItem<A,I>> iterator();  }