Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for calling a static typed method while doing a import static?

Currently I am doing

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

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

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

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

like image 556
Arindam Avatar asked Feb 21 '23 23:02

Arindam


1 Answers

There is no way to provide type-arguments to statically imported methods (without including the class name as you do in your first snippet). There's simply no such syntax supporting it.

See Section 15.12, Method Invocation 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 involve a preceding dot, and that one does not include the possibility to provide type arguments (as the other ones do).

like image 193
aioobe Avatar answered Mar 05 '23 17:03

aioobe