|
1 | 1 | package org.hamcrest.generator; |
2 | 2 |
|
| 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 | + |
3 | 11 | import com.thoughtworks.qdox.model.DocletTag; |
| 12 | +import com.thoughtworks.qdox.model.JavaAnnotation; |
4 | 13 | import com.thoughtworks.qdox.model.JavaClass; |
5 | 14 | import com.thoughtworks.qdox.model.JavaMethod; |
6 | 15 | 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; |
12 | 18 |
|
13 | 19 | /** |
14 | 20 | * Wraps an existing sequence of FactoryMethods, and attempts to pull in |
|
20 | 26 | */ |
21 | 27 | public class QDoxFactoryReader implements Iterable<FactoryMethod> { |
22 | 28 |
|
23 | | - private final Iterable<FactoryMethod> wrapped; |
24 | 29 | private final JavaClass classSource; |
25 | 30 |
|
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("^<(.*)>$"); |
28 | 34 |
|
29 | | - public QDoxFactoryReader(Iterable<FactoryMethod> wrapped, QDox qdox, String className) { |
30 | | - this.wrapped = wrapped; |
| 35 | + public QDoxFactoryReader(QDox qdox, String className) { |
31 | 36 | this.classSource = qdox.getClassByName(className); |
| 37 | + this.factoryAnnotation = qdox.getClassByName("org.hamcrest.Factory"); |
32 | 38 | } |
33 | 39 |
|
34 | 40 | @Override |
35 | 41 | 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>(); |
42 | 43 |
|
43 | | - @Override |
44 | | - public FactoryMethod next() { |
45 | | - return enhance(iterator.next()); |
| 44 | + for (JavaMethod jm : classSource.getMethods()) { |
| 45 | + if (!isFactoryMethod(jm)) { |
| 46 | + continue; |
46 | 47 | } |
47 | 48 |
|
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); |
51 | 55 | } |
52 | | - }; |
53 | | - } |
54 | 56 |
|
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 += "..."; |
68 | 63 | } |
| 64 | + |
| 65 | + fm.addParameter(type, p.getName()); |
69 | 66 | } |
| 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); |
70 | 80 | } |
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(); |
72 | 89 | } |
73 | 90 |
|
74 | 91 | /** |
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. |
77 | 100 | */ |
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 | + } |
91 | 113 | } |
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('$', '.'); |
94 | 119 | } |
95 | 120 |
|
96 | 121 | /** |
97 | 122 | * Reconstructs the JavaDoc as a string for a particular method. |
98 | 123 | */ |
99 | 124 | private static String createJavaDocComment(JavaMethod methodSource) { |
100 | 125 | 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()) { |
103 | 128 | return null; |
104 | 129 | } |
105 | 130 | StringBuilder result = new StringBuilder(); |
|
0 commit comments