Permalink
Browse files

relax the constraints on factory methods to allow for builder-pattern…

… matcher factories that do not initially return a type that extends Matcher.
  • Loading branch information...
1 parent 2f76c0f commit be54cb4a6e7df1f5466c0a44d460c9add082f79a @scarytom scarytom committed Jun 17, 2012
View
27 hamcrest-generator/src/main/java/org/hamcrest/generator/ReflectiveFactoryReader.java
@@ -86,26 +86,18 @@ private boolean outsideArrayBounds() {
* <p>To use another set of rules, override this method.
*/
protected boolean isFactoryMethod(Method javaMethod) {
- // We dynamically load these classes, to avoid a compile time
- // dependency on org.hamcrest.{Factory,Matcher}. This gets around
- // a circular bootstrap issue (because generator is required to
- // compile core).
return isStatic(javaMethod.getModifiers())
&& isPublic(javaMethod.getModifiers())
&& hasFactoryAnnotation(javaMethod)
- && matcherClass().isAssignableFrom(javaMethod.getReturnType());
- }
-
- private Class<?> matcherClass() {
- try {
- return classLoader.loadClass("org.hamcrest.Matcher");
- } catch (ClassNotFoundException e) {
- throw new RuntimeException("Cannot load hamcrest core", e);
- }
+ && !Void.TYPE.equals(javaMethod.getReturnType());
}
@SuppressWarnings("unchecked")
private boolean hasFactoryAnnotation(Method javaMethod) {
+ // We dynamically load the Factory class, to avoid a compile time
+ // dependency on org.hamcrest.Factory. This gets around
+ // a circular bootstrap issue (because generator is required to
+ // compile core).
try {
final Class<?> factoryClass = classLoader.loadClass("org.hamcrest.Factory");
if (!Annotation.class.isAssignableFrom(factoryClass)) {
@@ -119,9 +111,9 @@ private boolean hasFactoryAnnotation(Method javaMethod) {
private static FactoryMethod buildFactoryMethod(Method javaMethod) {
FactoryMethod result = new FactoryMethod(
- javaMethod.getDeclaringClass().getName(),
+ classToString(javaMethod.getDeclaringClass()),
javaMethod.getName(),
- javaMethod.getReturnType().getName());
+ classToString(javaMethod.getReturnType()));
for (TypeVariable<Method> typeVariable : javaMethod.getTypeParameters()) {
boolean hasBound = false;
@@ -175,7 +167,8 @@ private static String typeToString(Type type) {
}
private static String classToString(Class<?> cls) {
- return cls.isArray() ? cls.getComponentType().getName() + "[]" : cls.getName();
+ final String name = cls.isArray() ? cls.getComponentType().getName() + "[]" : cls.getName();
+ return name.replace('$', '.');
}
-}
+}
View
13 hamcrest-unit-test/src/main/java/org/hamcrest/generator/ReflectiveFactoryReaderTest.java
@@ -37,12 +37,12 @@ public void testIteratesOverFactoryMethods() {
assertTrue("Expected first method", methods.hasNext());
FactoryMethod firstMethod = methods.next();
assertEquals("firstMethod", firstMethod.getName());
- assertEquals(SimpleSetOfMatchers.class.getName(), firstMethod.getMatcherClass());
+ assertEquals(SimpleSetOfMatchers.class.getName().replace('$', '.'), firstMethod.getMatcherClass());
assertTrue("Expected second method", methods.hasNext());
FactoryMethod secondMethod = methods.next();
assertEquals("secondMethod", secondMethod.getName());
- assertEquals(SimpleSetOfMatchers.class.getName(), secondMethod.getMatcherClass());
+ assertEquals(SimpleSetOfMatchers.class.getName().replace('$', '.'), secondMethod.getMatcherClass());
assertFalse("Expected no more methods", methods.hasNext());
}
@@ -69,18 +69,17 @@ public void testIteratesOverFactoryMethods() {
}
@Factory
- public static Matcher<String> anotherGoodMethod() {
+ public static String anotherGoodMethod() {
return null;
}
@Factory
- public static String wrongReturnType() {
- return null;
+ public static void wrongReturnType() {
}
}
- public void testOnlyReadsPublicStaticAnnotatedMethodsThatReturnMatcher() {
+ public void testOnlyReadsPublicStaticAnnotatedMethodsThatReturnNonVoid() {
Iterable<FactoryMethod> reader = new ReflectiveFactoryReader(MatchersWithDodgySignatures.class);
Iterator<FactoryMethod> methods = reader.iterator();
@@ -267,4 +266,4 @@ private static FactoryMethod readMethod(Class<?> cls, String methodName) {
return null;
}
-}
+}
View
6 hamcrest-unit-test/src/main/java/org/hamcrest/generator/config/XmlConfiguratorTest.java
@@ -36,11 +36,11 @@ public void testAddsMatcherFactoryMethodsToConfiguration() throws Exception {
"</matchers>"));
assertThat(sugarConfiguration.factoryMethods(),
- hasItem(new FactoryMethod(SomeMatcher.class.getName(), "matcher1", "org.hamcrest.Matcher")));
+ hasItem(new FactoryMethod(SomeMatcher.class.getName().replace('$', '.'), "matcher1", "org.hamcrest.Matcher")));
assertThat(sugarConfiguration.factoryMethods(),
- hasItem(new FactoryMethod(SomeMatcher.class.getName(), "matcher2", "org.hamcrest.Matcher")));
+ hasItem(new FactoryMethod(SomeMatcher.class.getName().replace('$', '.'), "matcher2", "org.hamcrest.Matcher")));
assertThat(sugarConfiguration.factoryMethods(),
- hasItem(new FactoryMethod(AnotherMatcher.class.getName(), "matcher3", "org.hamcrest.CombinableMatcher")));
+ hasItem(new FactoryMethod(AnotherMatcher.class.getName().replace('$', '.'), "matcher3", "org.hamcrest.CombinableMatcher")));
}
private static InputSource createXml(String xml) {

0 comments on commit be54cb4

Please sign in to comment.