Reputation: 3750
In one of our projects I stumbled upon the following line of code (it uses hamcrest matchers 1.3):
assertThat(someReport.getSomeException(), Matchers.<SomeException>notNullValue())
Two questions:
Would somebody please explain this syntax to me? I've never seen the use of < and > in that context.
Eclipse highlights the latter part of that line as en error: The method notNullValue() of type Matchers is not generic; it cannot be parameterized with arguments <SomeException>
. However, using maven on the commandline the project builds without problems. So why is there an error?
Upvotes: 2
Views: 278
Reputation: 32949
This is proper syntax for specifying the generic parameter when calling a static method. If you have the following method...
public T myMethod();
You could call it as MyClass.<String>myMethod()
. In many cases type inference allow for not including the <>
. For example the work work for the above: String myVal = MyClass.myMethod()
.
The javadoc for Matchers
has the following signature for notNullValue
public static Matcher notNullValue()
Notice that the is no generic variable <T>
therefore it is not proper to specify one. If it is compiling, I assume it is because the compiler is ignoring it.
Upvotes: 3