Skip to content

Commit 81cbd02

Browse files
committed
Extend QDoxFactoryReader to cover behaviour that was just in the reflective reader.
Rely more on QDox 2.0 and ensure that the QDox reader passes all tests that the reflective reader does.
1 parent ff90022 commit 81cbd02

7 files changed

Lines changed: 79 additions & 330 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public String getMatcherClass() {
4646
public String getReturnType() {
4747
return returnType;
4848
}
49-
49+
5050
/**
5151
* Original name of factory method.
5252
*/
@@ -115,7 +115,7 @@ public void setJavaDoc(String javaDoc) {
115115
public String getJavaDoc() {
116116
return javaDoc;
117117
}
118-
118+
119119
// Generated in Eclipse...
120120
// n.b. Doesn't include returnType
121121
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.hamcrest.generator;
22

3-
import com.thoughtworks.qdox.JavaProjectBuilder;
4-
import com.thoughtworks.qdox.model.JavaClass;
5-
63
import java.io.File;
74
import java.io.Reader;
85

6+
import com.thoughtworks.qdox.JavaProjectBuilder;
7+
import com.thoughtworks.qdox.model.JavaClass;
8+
99
/**
1010
* Wraps QDox library. This is because to ease distribution, QDox is bundled into
1111
* hamcrest-generator.jar and has its package renamed to ensure there is no conflict

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

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package org.hamcrest.generator;
22

3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
import java.util.regex.Pattern;
8+
39
import com.thoughtworks.qdox.model.DocletTag;
10+
import com.thoughtworks.qdox.model.JavaAnnotation;
411
import com.thoughtworks.qdox.model.JavaClass;
512
import com.thoughtworks.qdox.model.JavaMethod;
613
import com.thoughtworks.qdox.model.JavaParameter;
714
import com.thoughtworks.qdox.model.JavaType;
8-
import com.thoughtworks.qdox.model.impl.DefaultJavaType;
9-
10-
import java.util.ArrayList;
11-
import java.util.Iterator;
12-
import java.util.List;
13-
import java.util.regex.Pattern;
15+
import com.thoughtworks.qdox.model.JavaTypeVariable;
1416

1517
/**
1618
* Wraps an existing sequence of FactoryMethods, and attempts to pull in
@@ -22,77 +24,77 @@
2224
*/
2325
public class QDoxFactoryReader implements Iterable<FactoryMethod> {
2426

25-
private final Iterable<FactoryMethod> wrapped;
2627
private final JavaClass classSource;
2728

28-
private static final Pattern GENERIC_REGEX = Pattern.compile("<.*>");
29-
private static final Pattern VARARGS_REGEX = Pattern.compile("...", Pattern.LITERAL);
29+
private JavaClass annot;
3030

31-
public QDoxFactoryReader(Iterable<FactoryMethod> wrapped, QDox qdox, String className) {
32-
this.wrapped = wrapped;
31+
private static final Pattern GENERIC_REGEX = Pattern.compile("^<(.*)>$");
32+
33+
public QDoxFactoryReader(QDox qdox, String className) {
3334
this.classSource = qdox.getClassByName(className);
35+
this.annot = qdox.getClassByName("org.hamcrest.Factory");
3436
}
3537

3638
@Override
3739
public Iterator<FactoryMethod> iterator() {
38-
final Iterator<FactoryMethod> iterator = wrapped.iterator();
39-
return new Iterator<FactoryMethod>() {
40-
@Override
41-
public boolean hasNext() {
42-
return iterator.hasNext();
43-
}
40+
Collection<FactoryMethod> methods = new ArrayList<FactoryMethod>();
4441

45-
@Override
46-
public FactoryMethod next() {
47-
return enhance(iterator.next());
42+
for (JavaMethod jm : classSource.getMethods()) {
43+
if (!jm.isPublic() || !jm.isStatic())
44+
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+
}
4855
}
4956

50-
@Override
51-
public void remove() {
52-
iterator.remove();
57+
if (skip)
58+
continue;
59+
60+
FactoryMethod fm = new FactoryMethod(typeToString(classSource), jm.getName(), typeToString(jm.getReturnType()));
61+
62+
for (JavaTypeVariable<?> tv : jm.getTypeParameters()) {
63+
String decl = tv.getGenericFullyQualifiedName();
64+
decl = GENERIC_REGEX.matcher(decl).replaceFirst("$1");
65+
fm.addGenericTypeParameter(decl);
5366
}
54-
};
55-
}
5667

57-
private FactoryMethod enhance(FactoryMethod factoryMethod) {
58-
JavaMethod methodSource = findMethodInSource(factoryMethod);
59-
if (methodSource != null) {
60-
factoryMethod.setJavaDoc(createJavaDocComment(methodSource));
61-
List<JavaParameter> parametersFromSource
62-
= methodSource.getParameters();
63-
List<FactoryMethod.Parameter> parametersFromReflection
64-
= factoryMethod.getParameters();
65-
66-
if (parametersFromReflection.size() == parametersFromSource.size()) {
67-
for (int i = 0; i < parametersFromSource.size(); i++) {
68-
parametersFromReflection.get(i).setName(
69-
parametersFromSource.get(i).getName());
68+
for (JavaParameter p : jm.getParameters()) {
69+
String type = typeToString(p.getType());
70+
71+
// Special case for var args methods.... String[] -> String...
72+
if (p.isVarArgs()) {
73+
type += "...";
7074
}
75+
76+
fm.addParameter(type, p.getName());
77+
}
78+
79+
for (JavaType exception : jm.getExceptions()) {
80+
fm.addException(typeToString(exception));
7181
}
82+
83+
fm.setJavaDoc(createJavaDocComment(jm));
84+
85+
String generifiedType = GENERIC_REGEX.matcher(fm.getReturnType()).replaceFirst("$1");
86+
if (!generifiedType.equals(fm.getReturnType())) {
87+
fm.setGenerifiedType(generifiedType);
88+
}
89+
90+
methods.add(fm);
7291
}
73-
return factoryMethod;
92+
93+
return methods.iterator();
7494
}
7595

76-
/**
77-
* Attempts to locate the source code for a specific method, by cross-referencing
78-
* the signature returned by reflection with the list of methods parsed by QDox.
79-
*/
80-
private JavaMethod findMethodInSource(FactoryMethod factoryMethod) {
81-
// Note, this doesn't always work - it struggles with some kinds of generics.
82-
// This seems to cover most cases though.
83-
List<FactoryMethod.Parameter> params = factoryMethod.getParameters();
84-
List<JavaType> types = new ArrayList<JavaType>();
85-
boolean varArgs = false;
86-
for (int i = 0; i < params.size(); i++) {
87-
String type = params.get(i).getType();
88-
varArgs = VARARGS_REGEX.matcher(type).find();
89-
// QDox ignores varargs and generics, so we strip them out to help QDox.
90-
type = GENERIC_REGEX.matcher(type).replaceAll("");
91-
type = VARARGS_REGEX.matcher(type).replaceAll("");
92-
types.add(new DefaultJavaType(type));
93-
}
94-
List<JavaMethod> methods = classSource.getMethodsBySignature(factoryMethod.getName(), types, false, varArgs);
95-
return methods.size() == 1 ? methods.get(0) : null;
96+
private static String typeToString(JavaType type) {
97+
return type.getGenericFullyQualifiedName();
9698
}
9799

98100
/**
@@ -101,7 +103,7 @@ private JavaMethod findMethodInSource(FactoryMethod factoryMethod) {
101103
private static String createJavaDocComment(JavaMethod methodSource) {
102104
String comment = methodSource.getComment();
103105
List<DocletTag> tags = methodSource.getTags();
104-
if ((comment == null || comment.trim().length() == 0) && tags.size() == 0) {
106+
if ((comment == null || comment.trim().length() == 0) && tags.isEmpty()) {
105107
return null;
106108
}
107109
StringBuilder result = new StringBuilder();

‎hamcrest-generator/src/main/java/org/hamcrest/generator/config/XmlConfigurator.java‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.hamcrest.generator.HamcrestFactoryWriter;
44
import org.hamcrest.generator.QDoxFactoryReader;
55
import org.hamcrest.generator.QuickReferenceWriter;
6-
import org.hamcrest.generator.ReflectiveFactoryReader;
76
import org.hamcrest.generator.SugarConfiguration;
87
import org.hamcrest.generator.SugarGenerator;
98
import org.hamcrest.generator.QDox;
@@ -15,20 +14,19 @@
1514
import javax.xml.parsers.ParserConfigurationException;
1615
import javax.xml.parsers.SAXParser;
1716
import javax.xml.parsers.SAXParserFactory;
17+
1818
import java.io.File;
1919
import java.io.FileWriter;
2020
import java.io.IOException;
2121

2222
public class XmlConfigurator {
2323

2424
private final SugarConfiguration sugarConfiguration;
25-
private final ClassLoader classLoader;
2625
private final SAXParserFactory saxParserFactory;
2726
private final QDox qdox;
2827

29-
public XmlConfigurator(SugarConfiguration sugarConfiguration, ClassLoader classLoader) {
28+
public XmlConfigurator(SugarConfiguration sugarConfiguration) {
3029
this.sugarConfiguration = sugarConfiguration;
31-
this.classLoader = classLoader;
3230
saxParserFactory = SAXParserFactory.newInstance();
3331
saxParserFactory.setNamespaceAware(true);
3432
qdox = new QDox();
@@ -46,20 +44,15 @@ public void load(InputSource inputSource)
4644
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
4745
if (localName.equals("factory")) {
4846
String className = attributes.getValue("class");
49-
try {
50-
addClass(className);
51-
} catch (ClassNotFoundException e) {
52-
throw new SAXException("Cannot find Matcher class : " + className);
53-
}
47+
addClass(className);
5448
}
5549
}
5650
});
5751
}
5852

59-
private void addClass(String className) throws ClassNotFoundException {
60-
Class<?> cls = classLoader.loadClass(className);
53+
private void addClass(String className) {
6154
sugarConfiguration.addFactoryMethods(
62-
new QDoxFactoryReader(new ReflectiveFactoryReader(cls), qdox, className));
55+
new QDoxFactoryReader(qdox, className));
6356
}
6457

6558

@@ -109,7 +102,7 @@ public static void main(String[] args) throws Exception {
109102
sugarGenerator.addWriter(new QuickReferenceWriter(System.out));
110103

111104
XmlConfigurator xmlConfigurator
112-
= new XmlConfigurator(sugarGenerator, XmlConfigurator.class.getClassLoader());
105+
= new XmlConfigurator(sugarGenerator);
113106

114107
if (srcDirs.trim().length() > 0) {
115108
for (String srcDir : srcDirs.split(",")) {

‎hamcrest-generator/src/test/java/org/hamcrest/generator/QDoxFactoryReaderTest.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,21 @@ public final class QDoxFactoryReaderTest {
119119
@Test public void
120120
readsFullyQualifiedGenericType() throws FileNotFoundException {
121121
FactoryMethod method = readMethod("GenerifiedMatchers", "generifiedType");
122-
assertEquals("java.util.Comparator<java.lang.String>", method.getGenerifiedType());
122+
assertEquals("org.hamcrest.Matcher<java.util.Comparator<java.lang.String>>", method.getReturnType());
123123
}
124124

125125
@Test public void
126126
readsNullGenerifiedTypeIfNotPresent() throws FileNotFoundException {
127127
FactoryMethod method = readMethod("GenerifiedMatchers", "noGenerifiedType");
128-
assertNull(method.getGenerifiedType());
128+
assertEquals("org.hamcrest.Matcher", method.getReturnType());
129129
}
130130

131131
@Test public void
132132
readsGenericsInGenericType() throws FileNotFoundException {
133133
FactoryMethod method = readMethod("GenerifiedMatchers", "crazyType");
134134
assertEquals(
135-
"java.util.Map<? extends java.util.Set<java.lang.Long>, org.hamcrest.Factory>",
136-
method.getGenerifiedType());
135+
"org.hamcrest.Matcher<java.util.Map<? extends java.util.Set<java.lang.Long>,org.hamcrest.Factory>>",
136+
method.getReturnType());
137137
}
138138

139139
@Test public void
@@ -203,7 +203,7 @@ public final class QDoxFactoryReaderTest {
203203
assertEquals("T", method.getGenericTypeParameters().get(0));
204204
assertEquals("V extends java.util.List<java.lang.String> & java.lang.Comparable<java.lang.String>",
205205
method.getGenericTypeParameters().get(1));
206-
assertEquals("java.util.Map<T, V[]>", method.getGenerifiedType());
206+
assertEquals("org.hamcrest.Matcher<java.util.Map<T,V[]>>", method.getReturnType());
207207
assertEquals("java.util.Set<T>", method.getParameters().get(0).getType());
208208
assertEquals("V", method.getParameters().get(1).getType());
209209
}

0 commit comments

Comments
 (0)