Java: how does this code work (return type undefined)
In the sun extended RMI tutorial, they have some interesting code which implements a 'compute engine', using RMI they pass the function over to the compute engine, which then returns the results. More details here: http://java.sun.com/docs/books/tutorial/rmi/designing.html
This means that at the onset the return type is not known, so they use the following work-around:
package compute;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Compute extends Re开发者_高级运维mote {
<T> T executeTask(Task<T> t) throws RemoteException;
}
and package compute;
public interface Task<T> {
T execute();
}
how is this able to work? what property of java does implement? what could i read up more about this?
thanks!
how does this code work (return type undefined)
This code is using generics, and the T
is called a generic type parameter. The return type isn't really "undefined". Instead, we say it's parameterized: the return type will eventually be replaced by a real, concrete type. That is, the return type is defined by whatever kind of Task<T>
you've created.
You can think of this like a function which accepts a particular type and generates a specialized version of each Task<T>
that is requested. (That's not quite what happens under the covers -- in fact, ultimately the compiler discards the information about a generic object's runtime type -- but a more in-depth treatment is probably outside the scope of this StackOverflow answer.)
Under the covers, when you compile this code, the Java compiler will make sure that no rules are broken about the creation of Task<T>
's and the relationship to its execute()
methods. For example, the compiler will flag this as illegal:
public class Boat { ... }
public class Car { ... }
public CarFactory implements Task<Car> {
public Boat execute() { ... } // Error! A Boat is not a Car.
}
This is known as Java Generics.
To go more in depth, Angelika Langer's FAQ is excellent.
This is using Generics. You can read more here http://java.sun.com/developer/technicalArticles/J2SE/generics/
More specifically, copying from the link above
"Here, T represents the type of elements contained in the collection. Think of T as a placeholder that will be replaced by a concrete type"
The return type is defined, it's of type T. The code says something the implements the Compute interface must implement a method named executeTask. The signature of that method must take a parameterized Task and return something of the same parametric type as the Task. So valid signatures woudl be:
Object executeTask(Task<Object> t){...}
Integer executeTask(Task<Integer> t){...}
etc.
In addition to generics, there is another concept at work here: the remote class loader provided by Java RMI.
If the "compute engine" has remote class loading enabled (which requires it to have a SecurityManager
, to prevent a client from running a malicious task), it can load new classes at runtime from the client.
精彩评论