When I create a sample project with GWT 2.6, I end up with the following code for RPC proxy initialisation:
private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
GreetingService is defined as:
@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
String greetServer(String name) throws IllegalArgumentException;
}
and GreetingServiceAsync is defined as:
public interface GreetingServiceAsync {
void greetServer(String input, AsyncCallback<String> callback)
throws IllegalArgumentException;
}
The signature of GWT.Create is:
public static <T> T create(Class<?> classLiteral)
Given all of this, I would expect the assignment of GWT.create to greetingService to fail. Because greetingService is of type GreetingServiceAsync and GWT.create would return GreetingService which has no relation to Async version. I would expect the Java compiler to mark this assignment as invalid, but it is not doing it.
Where is the link between the Async interface and its non async version? Java compiler obviously knows this, but I could not see it in the project source code.
about the fact that it compiles without error: notice how the parameters's type to GWT.create()
does not make use of the T
type parameter; so what the signature says is: you can pass any class, and it can return any type of object (T
is shorthand for T extends Object
), there's no connection between the GreetingService.class
passed as argument and the GreetingServiceAsync
returned, at least not in the GWT.create()
method signature.
the connection is in the GWT compiler (actually, in the generator for RPC, which is triggered for every class passed to GWT.create()
that extends RemoteService
), and it's simply based on a naming rule: return the type whose name is the one of the argument class suffixed with Async
. Of course checks are made: both have to be interfaces, and the methods in the async interface have to be the same as the one in the "non async" interface, except the return type of the non-async is moved to a type argument of an additional AsyncCallback
parameter. BTW, this specific case is the only reason that GWT.create()
still has this signature where the return type is not related to the parameter type.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加