Skip to content

Commit 1bb58e4

Browse files
committed
minor tidies, and moving to Java 6
1 parent 950ab88 commit 1bb58e4

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

hamcrest-examples/src/main/java/org/hamcrest/examples/junit4/ExampleWithAssertThat.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ public void showMismatch() {
5151
assertThat(complicated, shouldBe("the wrong thing"));
5252
}
5353

54-
private Matcher<ComplicatedClass> shouldBe(@SuppressWarnings("unused") String string) {
54+
private Matcher<ComplicatedClass> shouldBe(String string) {
5555
return new TypeSafeMatcher<ComplicatedClass>() {
56+
@Override
5657
public void describeTo(Description description) { } // no op
5758
@Override
5859
public boolean matchesSafely(ComplicatedClass item) { return false; }

hamcrest-generator/src/main/java/org/hamcrest/generator/ReflectiveFactoryReader.java

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

3+
import java.lang.annotation.Annotation;
34
import java.lang.reflect.Method;
45
import static java.lang.reflect.Modifier.isPublic;
56
import static java.lang.reflect.Modifier.isStatic;
@@ -36,12 +37,14 @@ public ReflectiveFactoryReader(Class<?> cls) {
3637
this.classLoader = cls.getClassLoader();
3738
}
3839

40+
@Override
3941
public Iterator<FactoryMethod> iterator() {
4042
return new Iterator<FactoryMethod>() {
4143

4244
private int currentMethod = -1;
4345
private Method[] allMethods = cls.getMethods();
4446

47+
@Override
4548
public boolean hasNext() {
4649
while (true) {
4750
currentMethod++;
@@ -53,13 +56,15 @@ public boolean hasNext() {
5356
}
5457
}
5558

59+
@Override
5660
public FactoryMethod next() {
5761
if (outsideArrayBounds()) {
5862
throw new IllegalStateException("next() called without hasNext() check.");
5963
}
6064
return buildFactoryMethod(allMethods[currentMethod]);
6165
}
6266

67+
@Override
6368
public void remove() {
6469
throw new UnsupportedOperationException();
6570
}
@@ -80,26 +85,38 @@ private boolean outsideArrayBounds() {
8085
* <p/>
8186
* <p>To use another set of rules, override this method.
8287
*/
83-
@SuppressWarnings({"unchecked"})
8488
protected boolean isFactoryMethod(Method javaMethod) {
8589
// We dynamically load these classes, to avoid a compile time
8690
// dependency on org.hamcrest.{Factory,Matcher}. This gets around
8791
// a circular bootstrap issue (because generator is required to
8892
// compile core).
89-
Class factoryCls;
90-
Class matcherCls;
91-
try {
92-
factoryCls = classLoader.loadClass("org.hamcrest.Factory");
93-
matcherCls = classLoader.loadClass("org.hamcrest.Matcher");
94-
} catch (ClassNotFoundException e) {
95-
throw new RuntimeException("Cannot load hamcrest core", e);
96-
}
9793
return isStatic(javaMethod.getModifiers())
9894
&& isPublic(javaMethod.getModifiers())
99-
&& javaMethod.getAnnotation(factoryCls) != null
100-
&& matcherCls.isAssignableFrom(javaMethod.getReturnType());
95+
&& hasFactoryAnnotation(javaMethod)
96+
&& matcherClass().isAssignableFrom(javaMethod.getReturnType());
97+
}
98+
99+
private Class<?> matcherClass() {
100+
try {
101+
return classLoader.loadClass("org.hamcrest.Matcher");
102+
} catch (ClassNotFoundException e) {
103+
throw new RuntimeException("Cannot load hamcrest core", e);
104+
}
101105
}
102106

107+
@SuppressWarnings("unchecked")
108+
private boolean hasFactoryAnnotation(Method javaMethod) {
109+
try {
110+
final Class<?> factoryClass = classLoader.loadClass("org.hamcrest.Factory");
111+
if (!Annotation.class.isAssignableFrom(factoryClass)) {
112+
throw new RuntimeException("Not an annotation class: " + factoryClass.getCanonicalName());
113+
}
114+
return javaMethod.getAnnotation((Class<? extends Annotation>)factoryClass) != null;
115+
} catch (ClassNotFoundException e) {
116+
throw new RuntimeException("Cannot load hamcrest core", e);
117+
}
118+
}
119+
103120
private FactoryMethod buildFactoryMethod(Method javaMethod) {
104121
FactoryMethod result = new FactoryMethod(
105122
javaMethod.getDeclaringClass().getName(),

0 commit comments

Comments
 (0)