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.

CovarianceTests.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Andy Clement - initial implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.ajc150;
  12. import org.aspectj.testing.XMLBasedAjcTestCase;
  13. import junit.framework.Test;
  14. /*
  15. class Car {}
  16. class FastCar extends Car {}
  17. class Super {
  18. Car getCar() {
  19. return new Car();
  20. }
  21. }
  22. class Sub extends Super {
  23. FastCar getCar() {
  24. return new FastCar();
  25. }
  26. }
  27. public class CovBaseProgram01 {
  28. public static void main(String[] argv) {
  29. new CovBaseProgram01().run();
  30. }
  31. public void run() {
  32. Super instance_super = new Super();
  33. Sub instance_sub = new Sub();
  34. Car c1 = instance_super.getCar(); // Line 26
  35. Car c2 = instance_sub.getCar(); // Line 27
  36. }
  37. }
  38. // Line26: callJPs: call(Car Super.getCar())
  39. // Line27: callJPs: call(FastCar Sub.getCar()) call(Car Super.getCar())
  40. */
  41. /**
  42. * Covariance is simply where a type overrides some inherited implementation and narrows the return type.
  43. */
  44. public class CovarianceTests extends XMLBasedAjcTestCase {
  45. public static Test suite() {
  46. return XMLBasedAjcTestCase.loadSuite(CovarianceTests.class);
  47. }
  48. protected java.net.URL getSpecFile() {
  49. return getClassResource("ajc150.xml");
  50. }
  51. private boolean verbose = false;
  52. /**
  53. * call(* getCar()) should match both
  54. */
  55. public void testCOV001() {
  56. runTest("covariance 1");
  57. }
  58. /**
  59. * call(* Super.getCar()) should match both
  60. *
  61. * This test required a change to the compiler. When we are looking at signatures and comparing them we walk up
  62. * the hierarchy looking for supertypes that declare the same method. The problem is that in the comparison for
  63. * whether to methods are compatible we were including the return type - this meant 'Car getCar()' on Super was
  64. * different to 'FastCar getCar()' on Sub - it thought they were entirely different methods. In fact the return
  65. * type is irrelevant here, we just want to make sure the names and the parameter types are the same - so I
  66. * added a parameterSignature to the Member class that looks like '()' where the full signature looks like
  67. * '()LFastCar;' (which includes the return type). If the full signature comparison fails then it looks at the
  68. * parameter signature - I did it that way to try and preserve some performance. I haven't changed the
  69. * definition of 'signature' for a member as trimming the return type off it seems rather serious !
  70. *
  71. * What might break:
  72. * - 'matches' can now return true for things that have different return types - I guess whether this is a problem
  73. * depends on what the caller of matches is expecting, their code will have been written before covariance was
  74. * a possibility. All the tests pass so I'll leave it like this for now.
  75. */
  76. public void testCOV002() {
  77. runTest("covariance 2");
  78. }
  79. /**
  80. * call(Car getCar()) should match both
  81. *
  82. * Had to implement proper covariance support here...
  83. */
  84. public void testCOV003() {
  85. runTest("covariance 3");
  86. }
  87. /**
  88. * *** Different base program, where Sub does not extend Super.
  89. * call(Car Super.getCar()) should only match first call to getCar()
  90. */
  91. public void testCOV004() {
  92. runTest("covariance 4");
  93. }
  94. /**
  95. * *** Original base program
  96. * call(Car Super.getCar()) should match both
  97. */
  98. public void testCOV005() {
  99. runTest("covariance 5");
  100. }
  101. /**
  102. * call(Car Sub.getCar()) should not match anything
  103. */
  104. public void testCOV006() {
  105. runTest("covariance 6");
  106. }
  107. /**
  108. * call(Car+ Sub.getCar()) should match 2nd call with xlint for the 1st call
  109. */
  110. public void testCOV007() {
  111. runTest("covariance 7");
  112. }
  113. /**
  114. * *** aspect now contains two pointcuts and two pieces of advice
  115. * call(FastCar getCar()) matches on 2nd call
  116. * call(FastCar Sub.getCar()) matches on 2nd call
  117. */
  118. public void testCOV008() {
  119. runTest("covariance 8");
  120. }
  121. /**
  122. * call(FastCar Super.getCar()) matches nothing
  123. */
  124. public void testCOV009() {
  125. runTest("covariance 9");
  126. }
  127. /**
  128. * call(Car+ getCar()) matches both
  129. */
  130. public void testCOV010() {
  131. runTest("covariance 10");
  132. }
  133. public void testAJDKExamples() {
  134. runTest("ajdk: covariance");
  135. }
  136. }