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.

SuperInIntroduction.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import org.aspectj.testing.Tester;
  2. public class SuperInIntroduction {
  3. public static void main (String[] args) {
  4. int result = new Sub().getInt();
  5. Tester.check(8==result, "new Sub().getInt() !8==" + result);
  6. ObjectSub sb = new ObjectSub().getClone();
  7. Tester.check(null != sb, "null new ObjectSub().getClone()");
  8. sb = new ObjectSub().getSuperClone();
  9. Tester.check(null != sb, "null new ObjectSub().getSuperClone()");
  10. }
  11. }
  12. class Super {
  13. protected int protectedInt = 1;
  14. protected int protectedIntMethod() { return protectedInt; }
  15. int defaultInt = 1;
  16. int defaultIntMethod() { return defaultInt; }
  17. }
  18. class Sub extends Super { }
  19. class ObjectSub { }
  20. aspect A {
  21. /** @testcase accessing protected method and field of class within code the compiler controls */
  22. public int Sub.getInt() {
  23. int result;
  24. result = super.protectedInt;
  25. result += super.protectedIntMethod();
  26. result += protectedInt;
  27. result += protectedIntMethod();
  28. result += defaultInt;
  29. result += defaultIntMethod();
  30. result += super.defaultInt;
  31. result += super.defaultIntMethod();
  32. return result;
  33. }
  34. /** @testcase accessing protected method of class outside code the compiler controls */
  35. public ObjectSub ObjectSub.getClone() {
  36. try {
  37. Object result = clone();
  38. return (ObjectSub) result;
  39. } catch (CloneNotSupportedException e) {
  40. return this;
  41. }
  42. }
  43. /** @testcase using super to access protected method of class outside code the compiler controls */
  44. public ObjectSub ObjectSub.getSuperClone() {
  45. ObjectSub result = null;
  46. try {
  47. result = (ObjectSub) super.clone();
  48. Tester.check(false, "expecting CloneNotSupportedException");
  49. } catch (CloneNotSupportedException e) {
  50. result = this; // bad programming - ok for testing
  51. }
  52. return result;
  53. }
  54. }