11package org .hamcrest .generator ;
22
3+ import java .lang .annotation .Annotation ;
34import java .lang .reflect .Method ;
45import static java .lang .reflect .Modifier .isPublic ;
56import static java .lang .reflect .Modifier .isStatic ;
@@ -36,12 +37,14 @@ public ReflectiveFactoryReader(Class<?> cls) {
3637 this .classLoader = cls .getClassLoader ();
3738 }
3839
40+ @ Override
3941 public Iterator <FactoryMethod > iterator () {
4042 return new Iterator <FactoryMethod >() {
4143
4244 private int currentMethod = -1 ;
4345 private Method [] allMethods = cls .getMethods ();
4446
47+ @ Override
4548 public boolean hasNext () {
4649 while (true ) {
4750 currentMethod ++;
@@ -53,13 +56,15 @@ public boolean hasNext() {
5356 }
5457 }
5558
59+ @ Override
5660 public FactoryMethod next () {
5761 if (outsideArrayBounds ()) {
5862 throw new IllegalStateException ("next() called without hasNext() check." );
5963 }
6064 return buildFactoryMethod (allMethods [currentMethod ]);
6165 }
6266
67+ @ Override
6368 public void remove () {
6469 throw new UnsupportedOperationException ();
6570 }
@@ -80,26 +85,38 @@ private boolean outsideArrayBounds() {
8085 * <p/>
8186 * <p>To use another set of rules, override this method.
8287 */
83- @ SuppressWarnings ({"unchecked" })
8488 protected boolean isFactoryMethod (Method javaMethod ) {
8589 // We dynamically load these classes, to avoid a compile time
8690 // dependency on org.hamcrest.{Factory,Matcher}. This gets around
8791 // a circular bootstrap issue (because generator is required to
8892 // compile core).
89- Class factoryCls ;
90- Class matcherCls ;
91- try {
92- factoryCls = classLoader .loadClass ("org.hamcrest.Factory" );
93- matcherCls = classLoader .loadClass ("org.hamcrest.Matcher" );
94- } catch (ClassNotFoundException e ) {
95- throw new RuntimeException ("Cannot load hamcrest core" , e );
96- }
9793 return isStatic (javaMethod .getModifiers ())
9894 && isPublic (javaMethod .getModifiers ())
99- && javaMethod .getAnnotation (factoryCls ) != null
100- && matcherCls .isAssignableFrom (javaMethod .getReturnType ());
95+ && hasFactoryAnnotation (javaMethod )
96+ && matcherClass ().isAssignableFrom (javaMethod .getReturnType ());
97+ }
98+
99+ private Class <?> matcherClass () {
100+ try {
101+ return classLoader .loadClass ("org.hamcrest.Matcher" );
102+ } catch (ClassNotFoundException e ) {
103+ throw new RuntimeException ("Cannot load hamcrest core" , e );
104+ }
101105 }
102106
107+ @ SuppressWarnings ("unchecked" )
108+ private boolean hasFactoryAnnotation (Method javaMethod ) {
109+ try {
110+ final Class <?> factoryClass = classLoader .loadClass ("org.hamcrest.Factory" );
111+ if (!Annotation .class .isAssignableFrom (factoryClass )) {
112+ throw new RuntimeException ("Not an annotation class: " + factoryClass .getCanonicalName ());
113+ }
114+ return javaMethod .getAnnotation ((Class <? extends Annotation >)factoryClass ) != null ;
115+ } catch (ClassNotFoundException e ) {
116+ throw new RuntimeException ("Cannot load hamcrest core" , e );
117+ }
118+ }
119+
103120 private FactoryMethod buildFactoryMethod (Method javaMethod ) {
104121 FactoryMethod result = new FactoryMethod (
105122 javaMethod .getDeclaringClass ().getName (),
0 commit comments