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.

MoveInstructionsWeaveTestCase.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import org.aspectj.apache.bcel.generic.InstructionFactory;
  16. import org.aspectj.weaver.NameMangler;
  17. import org.aspectj.weaver.Shadow;
  18. public class MoveInstructionsWeaveTestCase extends WeaveTestCase {
  19. {
  20. regenerate = false;
  21. }
  22. public MoveInstructionsWeaveTestCase(String name) {
  23. super(name);
  24. }
  25. public void testHello() throws IOException {
  26. BcelAdvice p = new BcelAdvice(null, makePointcutAll(), null, 0, -1, -1, null, null) {
  27. public void specializeOn(Shadow s) {
  28. super.specializeOn(s);
  29. ((BcelShadow) s).initializeForAroundClosure();
  30. }
  31. public boolean implementOn(Shadow s) {
  32. BcelShadow shadow = (BcelShadow) s;
  33. LazyMethodGen newMethod = shadow.extractShadowInstructionsIntoNewMethod(NameMangler.getExtractableName(shadow
  34. .getSignature())
  35. + "_extracted", 0, this.getSourceLocation(), new ArrayList(),shadow.getEnclosingClass().isInterface());
  36. shadow.getRange().append(shadow.makeCallToCallback(newMethod));
  37. if (!shadow.isFallsThrough()) {
  38. shadow.getRange().append(InstructionFactory.createReturn(newMethod.getReturnType()));
  39. }
  40. return true;
  41. }
  42. };
  43. weaveTest("HelloWorld", "ExtractedHelloWorld", p);
  44. }
  45. static int counter = 0;
  46. public void testFancyHello() throws IOException {
  47. // Reset counter, just in case this test runs multiple times in one JVM. This can happen e.g. due to "run all tests"
  48. // in IntelliJ IDEA, which directly runs this test class and als WeaverModuleTests, both of which implement
  49. // junit.framework.TestCase. In that case, during the second run the counter would start at a higher base count,
  50. // making the 2nd test run fail.
  51. counter = 0;
  52. BcelAdvice p = new BcelAdvice(null, makePointcutAll(), null, 0, -1, -1, null, null) {
  53. public void specializeOn(Shadow s) {
  54. super.specializeOn(s);
  55. ((BcelShadow) s).initializeForAroundClosure();
  56. }
  57. public boolean implementOn(Shadow s) {
  58. BcelShadow shadow = (BcelShadow) s;
  59. LazyMethodGen newMethod =
  60. shadow.extractShadowInstructionsIntoNewMethod(NameMangler.getExtractableName(shadow
  61. .getSignature())
  62. + "_extracted" + counter++, 0, this.getSourceLocation(), new ArrayList(),shadow.getEnclosingClass().isInterface());
  63. shadow.getRange().append(shadow.makeCallToCallback(newMethod));
  64. if (!shadow.isFallsThrough()) {
  65. shadow.getRange().append(InstructionFactory.createReturn(newMethod.getReturnType()));
  66. }
  67. return true;
  68. }
  69. };
  70. weaveTest("FancyHelloWorld", "ExtractedFancyHelloWorld", p);
  71. }
  72. }