Pregunta

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?

¿Fue útil?

Solución

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).

Otros consejos

There's no such a syntax. What you can do though is to assign a value to some variable so that java would infer the type for you. Unfortunately, it won't give you more readable code.

I use

import static org.easymock.EasyMock.anyObject;
...
foo.bar((List<String>) anyObject());
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top