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.5KB

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