jocarreira / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Make TypeSafeMatcher.matchesSafely public #6

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
1. Create a subclass of TypeSafeMatcher
2. Use Eclipse to automatically generate stub implementations of the
abstract methods.
3. Try to construct your new subclass

Since TypeSafeMatcher.matchesSafely is protected, Eclipse makes the
overriding method protected, which means that the reflective trick used to
find the type matched fails on any subclass outside of org.hamcrest.

I suggest making TypeSafeMatcher.matchesSafely public in HEAD.

Original issue reported on code.google.com by david.s...@gmail.com on 20 Dec 2006 at 10:35

GoogleCodeExporter commented 8 years ago
Hi David

Which reflective trick are you talking about? I'm not aware of anything that 
should
be calling matchesSafely() outside of the TypeSafeMatcher. 

I'd like to understand the requirement a bit deeper before making this change.

cheers
-Joe

Original comment by joe.wal...@gmail.com on 21 Dec 2006 at 10:31

GoogleCodeExporter commented 8 years ago
Joe,

Sorry, I thought Google Code would ping me back when you responded.  Here's a 
failing
testcase to make it clearer:

package not.org.hamcrest;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;

public class SomeStringMatcher extends TypeSafeMatcher<String> {
    public static class SomeStringMatcherTest {
        @Test public void canConstructScratchMatcher() {
            new SomeStringMatcher();
        }
    }

    @Override
    protected boolean matchesSafely(String item) {
        return false;
    }

    public void describeTo(Description description) {
    }
}

On my system, this throws:

java.lang.Error: Cannot determine correct type for matchesSafely() method.
    at org.hamcrest.TypeSafeMatcher.<init>(TypeSafeMatcher.java:30)
    at not.org.hamcrest.SomeStringMatcher.<init>(SomeStringMatcher.java:7)
    at
not.org.hamcrest.SomeStringMatcher$SomeStringMatcherTest.canConstructScratchMatc
her(SomeStringMatcher.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at
org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.j
ava:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunne
r.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at
org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMeth
odsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunne
r.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReferen
ce.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:460)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.jav
a:196)

Original comment by david.s...@gmail.com on 31 Jan 2007 at 8:57

GoogleCodeExporter commented 8 years ago
I never managed to reproduce this problem - odd. 

Anyway, I've made the method public. I can't see any harm in this, particularly 
as
it's part of the published API anyway.

Original comment by joe.wal...@gmail.com on 21 May 2007 at 10:03

GoogleCodeExporter commented 8 years ago
That's because I checked in a fix for the issue a while ago, but didn't think to
check the issue tracker and close this issue off.  My bad.  Sorry.

Original comment by nat.pr...@gmail.com on 22 May 2007 at 1:27