I have the following (stripped-down) code. The abstract class
SessionHandler<T> is used by other code to define and run an operation that needs a session to something. This is best done in my code with anonymous classes, because of the shear number of operations defined. In the
EntityOps<T> class, these work great. But, in the last class shown here,
SomeClass, the anonymous class definition fails, though the semantics are almost identical. (
List<T> vs.
List<AnotherClass>) What am I doing wrong here? Or is this a bug in Java?
Thanks, Tom
public interface IEntityOps<T> {
T get();
List<t> getAll();
}
public abstract class SessionHandler<T> {
abstract T handle(Session session) throws Throwable;
public final T perform() {
... calls handle(session) ...
}
}
// These anonymous class definitions compile fine!
public class EntityOps<T> implements IEntityOps<T> {
public T get() {
T ret = null;
ret = new SessionHandler<T>() {
T handle(Session s) throws Throwable {
T ret = (some T object calculation);
return ret;
}
}.perform();
return ret;
}
public List<T> getAll() {
T ret = null;
return new SessionHandler<List<T>>() {
List<T> handle(Session s) throws Throwable {
List<T> ret = (some List<T> calculation);
return ret;
}
}.perform();
}
}
// This anonymous class definition fails with the error:
// "SomeClass.java": <anonymous someMethod> is not abstract and does not override abstract method handle()
// in SessionHandler at line XX, column XX
public class SomeClass {
public List<AnotherClass> someMethod() throws {
List<AnotherClass> ret = null;
ret = new SessionHandler<List<AnotherClass>>() {
List<AnotherClass> handle(Session s) throws Throwable {
List<AnotherClass> ret = (some List<AnotherClass> calculation);
return ret;
}
}.perform();
return ret;
}
}