What is the syntax for invoking a static typed method when performing a static import?

I'm currently doing

import org.easymock.EasyMock; ... foo.bar(EasyMock.<List<String>>anyObject()); 

I wonder if there is a way to avoid mentioning the EasyMock class. I have something like this:

 import static org.easymock.EasyMock.anyObject; ... foo.bar(anyObject<List<String>>()); 

which, however, does not compile. Any other way to do this?

+4
source share
3 answers

It is not possible to provide type arguments to statically imported methods (without including the class name, as in the first fragment). There is simply no such syntax supporting it.

See Section 15.12, Method Call Expressions in the Java Language Specification:

 MethodInvocation: MethodName ( ArgumentListopt ) Primary . NonWildTypeArgumentsopt Identifier (ArgumentListopt) super . NonWildTypeArgumentsopt Identifier (ArgumentListopt) ClassName . super . NonWildTypeArgumentsopt Identifier (ArgumentListopt) TypeName . NonWildTypeArguments Identifier (ArgumentListopt) 

The first option is the only one that does not include the previous point, and that it does not include the possibility of providing type arguments (as others do).

+6
source

There is no such syntax. However, you can assign a value to some variable so that java infers the type for you. Unfortunately, it will not give you more readable code.

+1
source

I use

 import static org.easymock.EasyMock.anyObject; ... foo.bar((List<String>) anyObject()); 
0
source

Source: https://habr.com/ru/post/1389028/


All Articles