Skip to content

Commit 49fdbb6

Browse files
committed
Merge pull request hamcrest#66 from josephw/use-qdox-2
Use QDox 2 to generate sugar class from a single pass of parsing source
2 parents 6b5489e + 0b377c4 commit 49fdbb6

23 files changed

Lines changed: 505 additions & 592 deletions

‎build.xml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</target>
2424

2525
<target name="generator" depends="api" description="Build code generator tool">
26-
<path id="cp-hamcrest-generator-main" path="lib/generator/qdox-1.12.jar;build/hamcrest-api-${version}.jar"/>
26+
<path id="cp-hamcrest-generator-main" path="lib/generator/qdox-2.0-M2.jar;build/hamcrest-api-${version}.jar"/>
2727
<path id="cp-hamcrest-generator-test" path=""/>
2828
<compile-module modulename="hamcrest-generator"/>
2929
<jar-module modulename="hamcrest-generator"/>
@@ -34,7 +34,7 @@
3434
<copy file="build/hamcrest-generator-${version}.jar" tofile="build/hamcrest-generator-${version}-nodep.jar"/>
3535
<jarjar jarfile="build/hamcrest-generator-${version}.jar">
3636
<zipfileset src="build/hamcrest-generator-${version}-nodep.jar"/>
37-
<zipfileset src="lib/generator/qdox-1.12.jar"/>
37+
<zipfileset src="lib/generator/qdox-2.0-M2.jar"/>
3838
<rule pattern="com.thoughtworks.qdox.**" result="org.hamcrest.generator.qdox.@1"/>
3939
</jarjar>
4040
</target>
@@ -231,10 +231,10 @@
231231
</macrodef>
232232

233233
<macrodef name="test-module" description="run unit tests.">
234-
<attribute name="modulename" description="Name of the module to jar"/>
234+
<attribute name="modulename" description="Name of the module to test"/>
235235
<sequential>
236236
<mkdir dir="build/temp/@{modulename}/test-wrk"/>
237-
<junit printsummary="no" forkmode="once" tempdir="build/temp/@{modulename}/test-wrk" haltonfailure="${haltonfailure}">
237+
<junit printsummary="no" forkmode="once" tempdir="build/temp/@{modulename}/test-wrk" haltonfailure="${haltonfailure}" dir="@{modulename}">
238238
<formatter type="brief" usefile="no"/>
239239
<classpath refid="cp-@{modulename}-test-complete"/>
240240
<classpath path="build/@{modulename}-${version}-tests.jar"/>

‎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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.hamcrest.generator;
22

3-
import com.thoughtworks.qdox.JavaDocBuilder;
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
@@ -16,7 +16,7 @@
1616
*/
1717
public class QDox {
1818

19-
private final JavaDocBuilder javaDocBuilder = new JavaDocBuilder();
19+
private final JavaProjectBuilder javaDocBuilder = new JavaProjectBuilder();
2020

2121
public void addSourceTree(File sourceDir) {
2222
javaDocBuilder.addSourceTree(sourceDir);

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

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

3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.Comparator;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.regex.Pattern;
10+
311
import com.thoughtworks.qdox.model.DocletTag;
12+
import com.thoughtworks.qdox.model.JavaAnnotation;
413
import com.thoughtworks.qdox.model.JavaClass;
514
import com.thoughtworks.qdox.model.JavaMethod;
615
import com.thoughtworks.qdox.model.JavaParameter;
7-
import com.thoughtworks.qdox.model.Type;
8-
9-
import java.util.Iterator;
10-
import java.util.List;
11-
import java.util.regex.Pattern;
16+
import com.thoughtworks.qdox.model.JavaType;
17+
import com.thoughtworks.qdox.model.JavaTypeVariable;
1218

1319
/**
1420
* Wraps an existing sequence of FactoryMethods, and attempts to pull in
@@ -20,86 +26,105 @@
2026
*/
2127
public class QDoxFactoryReader implements Iterable<FactoryMethod> {
2228

23-
private final Iterable<FactoryMethod> wrapped;
2429
private final JavaClass classSource;
2530

26-
private static final Pattern GENERIC_REGEX = Pattern.compile("<.*>");
27-
private static final Pattern VARARGS_REGEX = Pattern.compile("...", Pattern.LITERAL);
31+
private JavaClass factoryAnnotation;
32+
33+
private static final Pattern GENERIC_REGEX = Pattern.compile("^<(.*)>$");
2834

29-
public QDoxFactoryReader(Iterable<FactoryMethod> wrapped, QDox qdox, String className) {
30-
this.wrapped = wrapped;
35+
public QDoxFactoryReader(QDox qdox, String className) {
3136
this.classSource = qdox.getClassByName(className);
37+
this.factoryAnnotation = qdox.getClassByName("org.hamcrest.Factory");
3238
}
3339

3440
@Override
3541
public Iterator<FactoryMethod> iterator() {
36-
final Iterator<FactoryMethod> iterator = wrapped.iterator();
37-
return new Iterator<FactoryMethod>() {
38-
@Override
39-
public boolean hasNext() {
40-
return iterator.hasNext();
41-
}
42+
List<FactoryMethod> methods = new ArrayList<FactoryMethod>();
4243

43-
@Override
44-
public FactoryMethod next() {
45-
return enhance(iterator.next());
44+
for (JavaMethod jm : classSource.getMethods()) {
45+
if (!isFactoryMethod(jm)) {
46+
continue;
4647
}
4748

48-
@Override
49-
public void remove() {
50-
iterator.remove();
49+
FactoryMethod fm = new FactoryMethod(typeToString(classSource), jm.getName(), typeToString(jm.getReturnType()));
50+
51+
for (JavaTypeVariable<?> tv : jm.getTypeParameters()) {
52+
String decl = tv.getGenericFullyQualifiedName();
53+
decl = GENERIC_REGEX.matcher(decl).replaceFirst("$1");
54+
fm.addGenericTypeParameter(decl);
5155
}
52-
};
53-
}
5456

55-
private FactoryMethod enhance(FactoryMethod factoryMethod) {
56-
JavaMethod methodSource = findMethodInSource(factoryMethod);
57-
if (methodSource != null) {
58-
factoryMethod.setJavaDoc(createJavaDocComment(methodSource));
59-
JavaParameter[] parametersFromSource
60-
= methodSource.getParameters();
61-
List<FactoryMethod.Parameter> parametersFromReflection
62-
= factoryMethod.getParameters();
63-
64-
if (parametersFromReflection.size() == parametersFromSource.length) {
65-
for (int i = 0; i < parametersFromSource.length; i++) {
66-
parametersFromReflection.get(i).setName(
67-
parametersFromSource[i].getName());
57+
for (JavaParameter p : jm.getParameters()) {
58+
String type = typeToString(p.getType());
59+
60+
// Special case for var args methods.... String[] -> String...
61+
if (p.isVarArgs()) {
62+
type += "...";
6863
}
64+
65+
fm.addParameter(type, p.getName());
6966
}
67+
68+
for (JavaType exception : jm.getExceptions()) {
69+
fm.addException(typeToString(exception));
70+
}
71+
72+
fm.setJavaDoc(createJavaDocComment(jm));
73+
74+
String generifiedType = GENERIC_REGEX.matcher(fm.getReturnType()).replaceFirst("$1");
75+
if (!generifiedType.equals(fm.getReturnType())) {
76+
fm.setGenerifiedType(generifiedType);
77+
}
78+
79+
methods.add(fm);
7080
}
71-
return factoryMethod;
81+
82+
Collections.sort(methods, new Comparator<FactoryMethod>() {
83+
@Override public int compare(FactoryMethod o1, FactoryMethod o2) {
84+
return o1.getName().compareTo(o2.getName());
85+
}
86+
});
87+
88+
return methods.iterator();
7289
}
7390

7491
/**
75-
* Attempts to locate the source code for a specific method, by cross-referencing
76-
* the signature returned by reflection with the list of methods parsed by QDox.
92+
* Determine whether a particular method is classified as a matcher factory method.
93+
* <p/>
94+
* <p>The rules for determining this are:
95+
* 1. The method must be public static.
96+
* 2. It must have a return type of org.hamcrest.Matcher (or something that extends this).
97+
* 3. It must be marked with the org.hamcrest.Factory annotation.
98+
* <p/>
99+
* <p>To use another set of rules, override this method.
77100
*/
78-
private JavaMethod findMethodInSource(FactoryMethod factoryMethod) {
79-
// Note, this doesn't always work - it struggles with some kinds of generics.
80-
// This seems to cover most cases though.
81-
List<FactoryMethod.Parameter> params = factoryMethod.getParameters();
82-
Type[] types = new Type[params.size()];
83-
boolean varArgs = false;
84-
for (int i = 0; i < types.length; i++) {
85-
String type = params.get(i).getType();
86-
varArgs = VARARGS_REGEX.matcher(type).find();
87-
// QDox ignores varargs and generics, so we strip them out to help QDox.
88-
type = GENERIC_REGEX.matcher(type).replaceAll("");
89-
type = VARARGS_REGEX.matcher(type).replaceAll("");
90-
types[i] = new Type(type);
101+
protected boolean isFactoryMethod(JavaMethod javaMethod) {
102+
return javaMethod.isStatic()
103+
&& javaMethod.isPublic()
104+
&& hasFactoryAnnotation(javaMethod)
105+
&& !javaMethod.getReturnType().equals(JavaType.VOID);
106+
}
107+
108+
private boolean hasFactoryAnnotation(JavaMethod javaMethod) {
109+
for (JavaAnnotation a : javaMethod.getAnnotations()) {
110+
if (a.getType().equals(factoryAnnotation)) {
111+
return true;
112+
}
91113
}
92-
JavaMethod[] methods = classSource.getMethodsBySignature(factoryMethod.getName(), types, false, varArgs);
93-
return methods.length == 1 ? methods[0] : null;
114+
return false;
115+
}
116+
117+
private static String typeToString(JavaType type) {
118+
return type.getGenericFullyQualifiedName().replace('$', '.');
94119
}
95120

96121
/**
97122
* Reconstructs the JavaDoc as a string for a particular method.
98123
*/
99124
private static String createJavaDocComment(JavaMethod methodSource) {
100125
String comment = methodSource.getComment();
101-
DocletTag[] tags = methodSource.getTags();
102-
if ((comment == null || comment.trim().length() == 0) && tags.length == 0) {
126+
List<DocletTag> tags = methodSource.getTags();
127+
if ((comment == null || comment.trim().length() == 0) && tags.isEmpty()) {
103128
return null;
104129
}
105130
StringBuilder result = new StringBuilder();

0 commit comments

Comments
 (0)