blob: cafe5bcbc12e779609b6f2f55da0f3e4c6d2150a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import java.lang.reflect.Method;
@EntityController
public class MyAnnotatedController<T> {
public void doSomething() {
System.out.println("Doing something");
}
public static void main(String[] args) {
// Use class type directly so as to call its method
MyAnnotatedController<String> annotatedTextController = new MyAnnotatedController<>();
annotatedTextController.doSomething();
// Print all declared methods (should also show interface methods introduced via ITD)
for (Method method : annotatedTextController.getClass().getDeclaredMethods()) {
if (!method.getName().startsWith("ajc$"))
System.out.println(method);
}
// Prove that class type is compatible with interface type
//
// NOTE: As explained in https://bugs.eclipse.org/bugs/show_bug.cgi?id=442425#c2, @DeclareParents will add the
// raw form of the parent to the target class, not the generic one. Therefore, the additional cast is necessary.
// Otherwise, AJC would throw:
// Type mismatch: cannot convert from MyAnnotatedController<String> to IEntityController<String>
IEntityController<String> entityTextController = (IEntityController<String>) annotatedTextController;
entityTextController.setEntity("foo");
// Would not work here because generic interface type is type-safe:
// entityNumberController.setEntity(123);
System.out.println("Entity value = " + entityTextController.getEntity());
// Create another object and directly assign it to interface type.
//
// NOTE: As explained in https://bugs.eclipse.org/bugs/show_bug.cgi?id=442425#c2, @DeclareParents will add the
// raw form of the parent to the target class, not the generic one. Therefore, the additional cast is necessary.
// Otherwise, AJC would throw:
// Cannot infer type arguments for MyAnnotatedController<>
IEntityController<Integer> entityNumberController = (IEntityController<Integer>) new MyAnnotatedController<>();
entityNumberController.setEntity(123);
// Would not work here because generic interface type is type-safe:
// entityNumberController.setEntity("foo");
System.out.println("Entity value = " + entityNumberController.getEntity());
}
}
|