Skip to content

Commit 5f02100

Browse files
author
smgfreeman
committed
sf. Fixed (I think) Issue 29. Untyped instanceOf breaks jMock parameter matching
Also tidied up some warnings.
1 parent fbcac75 commit 5f02100

32 files changed

Lines changed: 63 additions & 34 deletions

‎hamcrest-core/src/main/java/org/hamcrest/core/Is.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static <T> Matcher<? super T> is(T value) {
6666
* vs. assertThat(cheese, is(Cheddar.class))
6767
*/
6868
@Factory
69-
public static Matcher<Object> is(Class<?> type) {
69+
public static <T> Matcher<? super T> is(Class<T> type) {
7070
return is(instanceOf(type));
7171
}
7272
}

‎hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public void describeTo(Description description) {
3636
/**
3737
* Is the value an instance of a particular type?
3838
*/
39+
@SuppressWarnings("unchecked")
3940
@Factory
40-
public static Matcher<Object> instanceOf(Class<?> type) {
41-
return new IsInstanceOf(type);
41+
public static <T> Matcher<T> instanceOf(Class<T> type) {
42+
return (Matcher<T>) new IsInstanceOf(type);
4243
}
4344
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,8 @@ private JavaMethod findMethodInSource(FactoryMethod factoryMethod) {
8383
type = VARARGS_REGEX.matcher(type).replaceAll("");
8484
types[i] = new Type(type);
8585
}
86-
JavaMethod[] methods = classSource.getMethodsBySignature(
87-
factoryMethod.getName(), types, false);
88-
if (methods.length == 1) {
89-
return methods[0];
90-
} else {
91-
return null;
92-
}
86+
JavaMethod[] methods = classSource.getMethodsBySignature(factoryMethod.getName(), types, false);
87+
return methods.length == 1 ? methods[0] : null;
9388
}
9489

9590
/**

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,18 @@ private FactoryMethod buildFactoryMethod(Method javaMethod) {
145145
return result;
146146
}
147147

148-
/**
148+
/*
149149
* Get String representation of Type (e.g. java.lang.String or Map&lt;Stuff,? extends Cheese&gt;).
150150
* <p/>
151151
* Annoyingly this method wouldn't be needed if java.lang.reflect.Type.toString() behaved consistently
152152
* across implementations. Rock on Liskov.
153153
*/
154-
private String typeToString(Type type) {
155-
if (type instanceof Class<?>) {
156-
Class<?> cls = (Class<?>) type;
157-
if (cls.isArray()) {
158-
return cls.getComponentType().getName() + "[]";
159-
} else {
160-
return cls.getName();
161-
}
162-
} else {
163-
return type.toString();
164-
}
154+
private static String typeToString(Type type) {
155+
return type instanceof Class<?> ? classToString((Class<?>) type): type.toString();
156+
}
157+
158+
private static String classToString(Class<?> cls) {
159+
return cls.isArray() ? cls.getComponentType().getName() + "[]" : cls.getName();
165160
}
166161

167162
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void load(InputSource inputSource)
4242
throws ParserConfigurationException, SAXException, IOException {
4343
SAXParser saxParser = saxParserFactory.newSAXParser();
4444
saxParser.parse(inputSource, new DefaultHandler() {
45+
@Override
4546
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
4647
if (localName.equals("factory")) {
4748
String className = attributes.getValue("class");

‎hamcrest-integration/src/main/java/org/hamcrest/integration/EasyMock2Adapter.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public EasyMock2Adapter(Matcher<?> matcher) {
3131
this.hamcrestMatcher = matcher;
3232
}
3333

34-
@SuppressWarnings({"unchecked"})
3534
public boolean matches(Object argument) {
3635
return hamcrestMatcher.matches(argument);
3736
}

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public IsArrayContaining(Matcher<? super T> elementMatcher) {
1616
this.elementMatcher = elementMatcher;
1717
}
1818

19+
@Override
1920
public boolean matchesSafely(T[] array) {
2021
for (T item : array) {
2122
if (elementMatcher.matches(item)) {

‎hamcrest-unit-test/src/main/java/org/hamcrest/beans/HasPropertyTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class HasPropertyTest extends AbstractMatcherTest {
2020
private final HasPropertyWithValueTest.BeanWithoutInfo bean
2121
= new HasPropertyWithValueTest.BeanWithoutInfo("a bean");
2222

23+
@Override
2324
protected Matcher<?> createMatcher() {
2425
return hasProperty("irrelevant");
2526
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsArrayContainingTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
public class IsArrayContainingTest extends AbstractMatcherTest {
88

9+
@Override
910
protected Matcher<?> createMatcher() {
1011
return hasItemInArray("irrelevant");
1112
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsArrayTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
@SuppressWarnings("unchecked")
1010
public class IsArrayTest extends AbstractMatcherTest {
1111

12+
@Override
1213
protected Matcher<?> createMatcher() {
1314
return array(equalTo("irrelevant"));
1415
}

0 commit comments

Comments
 (0)