Skip to content

Commit 0c89275

Browse files
committed
Use more structure from the original reader.
Copy over some structure from the original reader implementation, to clarify behaviour.
1 parent 95b769c commit 0c89275

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

‎hamcrest-generator/src/main/java/org/hamcrest/generator/QDoxFactoryReader.java‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,24 @@ public class QDoxFactoryReader implements Iterable<FactoryMethod> {
2626

2727
private final JavaClass classSource;
2828

29-
private JavaClass annot;
29+
private JavaClass factoryAnnotation;
3030

3131
private static final Pattern GENERIC_REGEX = Pattern.compile("^<(.*)>$");
3232

3333
public QDoxFactoryReader(QDox qdox, String className) {
3434
this.classSource = qdox.getClassByName(className);
35-
this.annot = qdox.getClassByName("org.hamcrest.Factory");
35+
this.factoryAnnotation = qdox.getClassByName("org.hamcrest.Factory");
3636
}
3737

3838
@Override
3939
public Iterator<FactoryMethod> iterator() {
4040
Collection<FactoryMethod> methods = new ArrayList<FactoryMethod>();
4141

4242
for (JavaMethod jm : classSource.getMethods()) {
43-
if (!jm.isPublic() || !jm.isStatic())
43+
if (!isFactoryMethod(jm)) {
4444
continue;
45-
46-
if (jm.getReturnType().equals(JavaType.VOID))
47-
continue;
48-
49-
boolean skip = true;
50-
51-
for (JavaAnnotation a : jm.getAnnotations()) {
52-
if (a.getType().equals(annot)) {
53-
skip = false;
54-
}
5545
}
5646

57-
if (skip)
58-
continue;
59-
6047
FactoryMethod fm = new FactoryMethod(typeToString(classSource), jm.getName(), typeToString(jm.getReturnType()));
6148

6249
for (JavaTypeVariable<?> tv : jm.getTypeParameters()) {
@@ -93,6 +80,32 @@ public Iterator<FactoryMethod> iterator() {
9380
return methods.iterator();
9481
}
9582

83+
/**
84+
* Determine whether a particular method is classified as a matcher factory method.
85+
* <p/>
86+
* <p>The rules for determining this are:
87+
* 1. The method must be public static.
88+
* 2. It must have a return type of org.hamcrest.Matcher (or something that extends this).
89+
* 3. It must be marked with the org.hamcrest.Factory annotation.
90+
* <p/>
91+
* <p>To use another set of rules, override this method.
92+
*/
93+
protected boolean isFactoryMethod(JavaMethod javaMethod) {
94+
return javaMethod.isStatic()
95+
&& javaMethod.isPublic()
96+
&& hasFactoryAnnotation(javaMethod)
97+
&& !javaMethod.getReturnType().equals(JavaType.VOID);
98+
}
99+
100+
private boolean hasFactoryAnnotation(JavaMethod javaMethod) {
101+
for (JavaAnnotation a : javaMethod.getAnnotations()) {
102+
if (a.getType().equals(factoryAnnotation)) {
103+
return true;
104+
}
105+
}
106+
return false;
107+
}
108+
96109
private static String typeToString(JavaType type) {
97110
return type.getGenericFullyQualifiedName().replace('$', '.');
98111
}

0 commit comments

Comments
 (0)