You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MyAnnotatedController.java 2.2KB

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