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.

Ajc186Tests.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*******************************************************************************
  2. * Copyright (c) 2014 Contributors
  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 API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.ajc186;
  12. import java.io.File;
  13. import java.lang.reflect.Method;
  14. import java.net.URL;
  15. import java.net.URLClassLoader;
  16. import junit.framework.Test;
  17. import org.aspectj.testing.XMLBasedAjcTestCase;
  18. import org.aspectj.weaver.tools.ContextBasedMatcher;
  19. import org.aspectj.weaver.tools.DefaultMatchingContext;
  20. import org.aspectj.weaver.tools.FuzzyBoolean;
  21. import org.aspectj.weaver.tools.MatchingContext;
  22. import org.aspectj.weaver.tools.PointcutDesignatorHandler;
  23. import org.aspectj.weaver.tools.PointcutExpression;
  24. import org.aspectj.weaver.tools.PointcutParser;
  25. /**
  26. * @author Andy Clement
  27. */
  28. public class Ajc186Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
  29. private class FooDesignatorHandler implements PointcutDesignatorHandler {
  30. private String askedToParse;
  31. public boolean simulateDynamicTest = false;
  32. public String getDesignatorName() {
  33. return "foo";
  34. }
  35. public ContextBasedMatcher parse(String expression) {
  36. this.askedToParse = expression;
  37. return new FooPointcutExpression(expression, this.simulateDynamicTest);
  38. }
  39. public String getExpressionLastAskedToParse() {
  40. return this.askedToParse;
  41. }
  42. }
  43. private class FooPointcutExpression implements ContextBasedMatcher {
  44. private final String beanNamePattern;
  45. private final boolean simulateDynamicTest;
  46. public FooPointcutExpression(String beanNamePattern,
  47. boolean simulateDynamicTest) {
  48. this.beanNamePattern = beanNamePattern;
  49. this.simulateDynamicTest = simulateDynamicTest;
  50. }
  51. public boolean couldMatchJoinPointsInType(Class aClass) {
  52. System.out.println("wubble?");
  53. return true;
  54. }
  55. public boolean couldMatchJoinPointsInType(Class aClass,
  56. MatchingContext context) {
  57. System.out.println("wibble?");
  58. if (this.beanNamePattern.equals(context.getBinding("beanName"))) {
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. public boolean mayNeedDynamicTest() {
  65. return this.simulateDynamicTest;
  66. }
  67. public FuzzyBoolean matchesStatically(MatchingContext matchContext) {
  68. System.out.println("wobble?");
  69. if (this.simulateDynamicTest)
  70. return FuzzyBoolean.MAYBE;
  71. if (this.beanNamePattern
  72. .equals(matchContext.getBinding("beanName"))) {
  73. return FuzzyBoolean.YES;
  74. } else {
  75. return FuzzyBoolean.NO;
  76. }
  77. }
  78. public boolean matchesDynamically(MatchingContext matchContext) {
  79. System.out.println("wabble?");
  80. return this.beanNamePattern.equals(matchContext
  81. .getBinding("beanName"));
  82. }
  83. }
  84. public void testLambdaBeans() throws Exception {
  85. runTest("lambda beans");
  86. // Load the 1.8 compiled code
  87. URLClassLoader ucl = new URLClassLoader(new URL[] {ajc.getSandboxDirectory().toURI().toURL()},this.getClass().getClassLoader());
  88. Class<?> applicationClass = Class.forName("Application",false,ucl);
  89. assertNotNull(applicationClass);
  90. Object instance = applicationClass.newInstance();
  91. Method works = applicationClass.getDeclaredMethod("fromInnerClass");
  92. works.setAccessible(true);
  93. Runnable r = (Runnable) works.invoke(instance);
  94. // r.getClass().getName() == Application$1
  95. Method fails = applicationClass.getDeclaredMethod("fromLambdaExpression");
  96. fails.setAccessible(true);
  97. Runnable r2 = (Runnable) fails.invoke(instance);
  98. // r2.getClass().getName() == Application$$Lambda$1/1652149987
  99. // JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), "Application");
  100. PointcutParser parser = PointcutParser
  101. .getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(ucl);
  102. FooDesignatorHandler beanHandler = new FooDesignatorHandler();
  103. parser.registerPointcutDesignatorHandler(beanHandler);
  104. PointcutExpression pc = parser.parsePointcutExpression("foo(myBean)");
  105. DefaultMatchingContext context = new DefaultMatchingContext();
  106. pc.setMatchingContext(context);
  107. context.addContextBinding("beanName", "myBean");
  108. assertTrue(pc.couldMatchJoinPointsInType(r.getClass()));
  109. context.addContextBinding("beanName", "yourBean");
  110. assertFalse(pc.couldMatchJoinPointsInType(r.getClass()));
  111. context.addContextBinding("beanName", "myBean");
  112. assertTrue(pc.couldMatchJoinPointsInType(r2.getClass()));
  113. context.addContextBinding("beanName", "yourBean");
  114. assertFalse(pc.couldMatchJoinPointsInType(r2.getClass()));
  115. }
  116. public void testMissingExtends() throws Exception {
  117. runTest("missing extends on generic target");
  118. }
  119. public void testMissingMethod_462821() throws Exception {
  120. runTest("missing method");
  121. }
  122. // ---
  123. public static Test suite() {
  124. return XMLBasedAjcTestCase.loadSuite(Ajc186Tests.class);
  125. }
  126. @Override
  127. protected File getSpecFile() {
  128. return getClassResource("ajc186.xml");
  129. }
  130. }