문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top