From 7d47cba01043c93bab95b59e66b727580351e85f Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Wed, 28 Feb 2018 11:53:14 -0800 Subject: Bug#531694: generate more optional thisJoinPoint construction code This commit introduces some new methods into the runtime Factory class and modifies code generation to use them (and to use the form of the LDC bytecode that loads class constants). --- tests/features190/efficientTJP/Advice.java | 19 +++ tests/features190/efficientTJP/Clinit.java | 16 ++ tests/features190/efficientTJP/ClinitE.java | 16 ++ tests/features190/efficientTJP/Fields.java | 46 ++++++ tests/features190/efficientTJP/Fields2.java | 40 +++++ tests/features190/efficientTJP/FieldsE.java | 46 ++++++ tests/features190/efficientTJP/Four.java | 21 +++ tests/features190/efficientTJP/FourA.java | 21 +++ tests/features190/efficientTJP/Init.java | 24 +++ tests/features190/efficientTJP/One.java | 12 ++ tests/features190/efficientTJP/Three.java | 19 +++ tests/features190/efficientTJP/ThreeA.java | 19 +++ tests/features190/efficientTJP/Two.java | 12 ++ .../systemtest/ajc151/NewarrayJoinpointTests.java | 18 ++- .../systemtest/ajc190/AllTestsAspectJ190.java | 2 +- .../systemtest/ajc190/EfficientTJPTests.java | 126 +++++++++++++++ .../org/aspectj/systemtest/ajc190/features190.xml | 172 +++++++++++++++++++++ 17 files changed, 624 insertions(+), 5 deletions(-) create mode 100644 tests/features190/efficientTJP/Advice.java create mode 100644 tests/features190/efficientTJP/Clinit.java create mode 100644 tests/features190/efficientTJP/ClinitE.java create mode 100644 tests/features190/efficientTJP/Fields.java create mode 100644 tests/features190/efficientTJP/Fields2.java create mode 100644 tests/features190/efficientTJP/FieldsE.java create mode 100644 tests/features190/efficientTJP/Four.java create mode 100644 tests/features190/efficientTJP/FourA.java create mode 100644 tests/features190/efficientTJP/Init.java create mode 100644 tests/features190/efficientTJP/One.java create mode 100644 tests/features190/efficientTJP/Three.java create mode 100644 tests/features190/efficientTJP/ThreeA.java create mode 100644 tests/features190/efficientTJP/Two.java create mode 100644 tests/src/org/aspectj/systemtest/ajc190/EfficientTJPTests.java create mode 100644 tests/src/org/aspectj/systemtest/ajc190/features190.xml (limited to 'tests') diff --git a/tests/features190/efficientTJP/Advice.java b/tests/features190/efficientTJP/Advice.java new file mode 100644 index 000000000..a85a445c2 --- /dev/null +++ b/tests/features190/efficientTJP/Advice.java @@ -0,0 +1,19 @@ +public class Advice { + public static void main(String []argv) { + } +} + +aspect X { + before(): execution(* main(..)) {} +} + +aspect Y { + before(): adviceexecution() && within(X) { + System.out.println("tjp:"+thisJoinPointStaticPart.getSignature()); + } + + before(): adviceexecution() && within(X) { + System.out.println("tejp:"+thisEnclosingJoinPointStaticPart.getSignature()); + } + +} diff --git a/tests/features190/efficientTJP/Clinit.java b/tests/features190/efficientTJP/Clinit.java new file mode 100644 index 000000000..e8acea835 --- /dev/null +++ b/tests/features190/efficientTJP/Clinit.java @@ -0,0 +1,16 @@ +public class Clinit { + public static void main(String []argv) { + new Inner(); + } + + static class Inner {} +} + +aspect X { + before(): staticinitialization(Clinit.Inner) { + System.out.println(thisJoinPointStaticPart.getSignature()); + } + before(): staticinitialization(Clinit) { + System.out.println(thisJoinPointStaticPart.getSignature()); + } +} diff --git a/tests/features190/efficientTJP/ClinitE.java b/tests/features190/efficientTJP/ClinitE.java new file mode 100644 index 000000000..a007a2014 --- /dev/null +++ b/tests/features190/efficientTJP/ClinitE.java @@ -0,0 +1,16 @@ +public class ClinitE { + public static void main(String []argv) { + new Inner(); + } + + static class Inner {} +} + +aspect X { + before(): staticinitialization(ClinitE.Inner) { + System.out.println(thisEnclosingJoinPointStaticPart.getSignature()); + } + before(): staticinitialization(ClinitE) { + System.out.println(thisEnclosingJoinPointStaticPart.getSignature()); + } +} diff --git a/tests/features190/efficientTJP/Fields.java b/tests/features190/efficientTJP/Fields.java new file mode 100644 index 000000000..ad1246568 --- /dev/null +++ b/tests/features190/efficientTJP/Fields.java @@ -0,0 +1,46 @@ +public class Fields { + int a = 1; + String s = "hello"; + double d = 1.0d; + boolean b = true; + + short ps = (short)1; + float fs = 1.0f; + long ls = 1L; + byte bs = (byte)3; + char cs = 'a'; + + + Inner obj = new Inner(); + static int as = 1; + static String ss = "hello"; + static double ds = 1.0d; + static Inner objs = new Inner(); + + public static void main(String []argv) { + Fields f = new Fields(); + int a2 = f.a; + String s2 = f.s; + double d2 = f.d; + Inner obj2 = f.obj; + + short ps2 = f.ps; + float fs2 = f.fs; + long ls2 = f.ls; + byte bs2 = f.bs; + char cs2 = f.cs; + + int a3 = as; + String s3 = ss; + double d3 = ds; + Inner obj3 = objs; + } + + static class Inner {} +} + +aspect X { + before(): within(Fields) && get(* *) { + System.out.println(thisJoinPointStaticPart.getSignature()); + } +} diff --git a/tests/features190/efficientTJP/Fields2.java b/tests/features190/efficientTJP/Fields2.java new file mode 100644 index 000000000..0a8aed75c --- /dev/null +++ b/tests/features190/efficientTJP/Fields2.java @@ -0,0 +1,40 @@ +public class Fields2 { + int a = 1; + String s = "hello"; + double d = 1.0d; + boolean b = true; + + short ps = (short)1; + float fs = 1.0f; + long ls = 1L; + byte bs = (byte)3; + char cs = 'a'; + + + Inner obj = new Inner(); + static int as = 1; + static String ss = "hello"; + static double ds = 1.0d; + static Inner objs = new Inner(); + + public static void main(String []argv) { + Fields2 f = new Fields2(); + f.a = 2; + f.ps = (short)3; + f.d = 2.0d; + f.obj = new Inner(); + f.s = "helo"; + f.fs = 4f; + f.ls = 3L; + f.bs = (byte)23; + f.cs = 'a'; + } + + static class Inner {} +} + +aspect X { + before(): within(Fields2) && set(* *) && withincode(* main(..)){ + System.out.println(thisJoinPointStaticPart.getSignature()); + } +} diff --git a/tests/features190/efficientTJP/FieldsE.java b/tests/features190/efficientTJP/FieldsE.java new file mode 100644 index 000000000..51bc923fa --- /dev/null +++ b/tests/features190/efficientTJP/FieldsE.java @@ -0,0 +1,46 @@ +public class FieldsE { + int a = 1; + String s = "hello"; + double d = 1.0d; + boolean b = true; + + short ps = (short)1; + float fs = 1.0f; + long ls = 1L; + byte bs = (byte)3; + char cs = 'a'; + + + Inner obj = new Inner(); + static int as = 1; + static String ss = "hello"; + static double ds = 1.0d; + static Inner objs = new Inner(); + + public static void main(String []argv) { + FieldsE f = new FieldsE(); + int a2 = f.a; + String s2 = f.s; + double d2 = f.d; + Inner obj2 = f.obj; + + short ps2 = f.ps; + float fs2 = f.fs; + long ls2 = f.ls; + byte bs2 = f.bs; + char cs2 = f.cs; + + int a3 = as; + String s3 = ss; + double d3 = ds; + Inner obj3 = objs; + } + + static class Inner {} +} + +aspect X { + before(): within(FieldsE) && get(* *) { + System.out.println(thisEnclosingJoinPointStaticPart.getSignature()); + } +} diff --git a/tests/features190/efficientTJP/Four.java b/tests/features190/efficientTJP/Four.java new file mode 100644 index 000000000..9857c8c62 --- /dev/null +++ b/tests/features190/efficientTJP/Four.java @@ -0,0 +1,21 @@ +public class Four { + public static void main(String []argv) { + new Four().run(); + } + + public void run() { + try { + System.out.println("run() running"); + throw new IllegalStateException(); + } catch (Throwable t) { + System.out.println("caught something"); + } + } + +} + +aspect X { + before(Throwable t): handler(Throwable) && args(t) { + System.out.println(thisJoinPointStaticPart.getSignature()); + } +} diff --git a/tests/features190/efficientTJP/FourA.java b/tests/features190/efficientTJP/FourA.java new file mode 100644 index 000000000..47694dcbf --- /dev/null +++ b/tests/features190/efficientTJP/FourA.java @@ -0,0 +1,21 @@ +public class FourA { + public static void main(String []argv) { + new FourA().run(); + } + + public void run() { + try { + System.out.println("run() running"); + throw new IllegalStateException(); + } catch (Throwable t) { + System.out.println("caught something"); + } + } + +} + +aspect X { + before(Throwable t): handler(Throwable) && args(t) { + System.out.println(thisEnclosingJoinPointStaticPart.getSignature()); + } +} diff --git a/tests/features190/efficientTJP/Init.java b/tests/features190/efficientTJP/Init.java new file mode 100644 index 000000000..200ce18ce --- /dev/null +++ b/tests/features190/efficientTJP/Init.java @@ -0,0 +1,24 @@ +public class Init { + public static void main(String []argv) { + new A(); + new B(); + } +} + +class A {} +class B {} + +aspect X { + before(): preinitialization(A.new(..)) && !within(X) { + System.out.println(thisJoinPointStaticPart.getSignature()); + } + before(): preinitialization(A.new(..)) && !within(X) { + System.out.println(thisEnclosingJoinPointStaticPart.getSignature()); + } + before(): initialization(B.new(..)) && !within(X) { + System.out.println(thisJoinPointStaticPart.getSignature()); + } + before(): initialization(B.new(..)) && !within(X) { + System.out.println(thisEnclosingJoinPointStaticPart.getSignature()); + } +} diff --git a/tests/features190/efficientTJP/One.java b/tests/features190/efficientTJP/One.java new file mode 100644 index 000000000..4fafcdaf7 --- /dev/null +++ b/tests/features190/efficientTJP/One.java @@ -0,0 +1,12 @@ +public class One { + public static void main(String []argv) { + System.out.println("One running"); + } +} + +aspect X { + void around(): execution(* main(..)) { + System.out.println(thisJoinPoint.getSignature()); + proceed(); + } +} diff --git a/tests/features190/efficientTJP/Three.java b/tests/features190/efficientTJP/Three.java new file mode 100644 index 000000000..479c7f7d1 --- /dev/null +++ b/tests/features190/efficientTJP/Three.java @@ -0,0 +1,19 @@ +public class Three { + public static void main(String []argv) { + System.out.println("Three running"); + new Three(); + new Three("abc"); + new Three(1,"abc"); + } + + Three() {} + Three(String s) {} + Three(int i, String s) {} +} + +aspect X { + void around(): execution(new(..)) && !within(X) { + System.out.println(thisJoinPointStaticPart.getSignature()); + proceed(); + } +} diff --git a/tests/features190/efficientTJP/ThreeA.java b/tests/features190/efficientTJP/ThreeA.java new file mode 100644 index 000000000..e3ffac941 --- /dev/null +++ b/tests/features190/efficientTJP/ThreeA.java @@ -0,0 +1,19 @@ +public class ThreeA { + public static void main(String []argv) { + System.out.println("ThreeA running"); + new ThreeA(); + new ThreeA("abc"); + new ThreeA(1,"abc"); + } + + ThreeA() {} + ThreeA(String s) {} + ThreeA(int i, String s) {} +} + +aspect X { + void around(): execution(new(..)) && !within(X) { + System.out.println(thisEnclosingJoinPointStaticPart.getSignature()); + proceed(); + } +} diff --git a/tests/features190/efficientTJP/Two.java b/tests/features190/efficientTJP/Two.java new file mode 100644 index 000000000..2d5d1da69 --- /dev/null +++ b/tests/features190/efficientTJP/Two.java @@ -0,0 +1,12 @@ +public class Two { + public static void main(String []argv) { + System.out.println("Two running"); + } +} + +aspect X { + void around(): execution(* main(..)) { + System.out.println(thisEnclosingJoinPointStaticPart.getSignature()); + proceed(); + } +} diff --git a/tests/src/org/aspectj/systemtest/ajc151/NewarrayJoinpointTests.java b/tests/src/org/aspectj/systemtest/ajc151/NewarrayJoinpointTests.java index 9c92f8488..9654c04dd 100644 --- a/tests/src/org/aspectj/systemtest/ajc151/NewarrayJoinpointTests.java +++ b/tests/src/org/aspectj/systemtest/ajc151/NewarrayJoinpointTests.java @@ -13,13 +13,13 @@ package org.aspectj.systemtest.ajc151; import java.io.File; import java.util.List; -import junit.framework.Test; - import org.aspectj.asm.AsmManager; import org.aspectj.asm.IProgramElement; import org.aspectj.asm.IRelationship; import org.aspectj.testing.XMLBasedAjcTestCase; +import junit.framework.Test; + /* * The design: * @@ -59,6 +59,15 @@ public class NewarrayJoinpointTests extends XMLBasedAjcTestCase { runTest("thisjoinpoint"); } + public void testThisJoinPoint19() { + try { + System.setProperty("ASPECTJ_OPTS", "-Xajruntimetarget:1.9"); + runTest("thisjoinpoint"); + } finally { + System.setProperty("ASPECTJ_OPTS", ""); + } + } + public void testDifferentAdviceKinds() { runTest("different advice kinds"); } @@ -105,9 +114,9 @@ public class NewarrayJoinpointTests extends XMLBasedAjcTestCase { assertTrue("Couldnt find 'Five' type in the model", ipe != null); List kids = ipe.getChildren(); assertTrue("Couldn't find 'main' method in the 'Five' type", kids != null && kids.size() == 1); - List codenodes = ((IProgramElement) kids.get(0)).getChildren(); + List codenodes = kids.get(0).getChildren(); assertTrue("Couldn't find nodes below 'main' method", codenodes != null && codenodes.size() == 1); - IProgramElement arrayCtorCallNode = (IProgramElement) codenodes.get(0); + IProgramElement arrayCtorCallNode = codenodes.get(0); String exp = "constructor-call(void java.lang.Integer[].(int))"; assertTrue("Expected '" + exp + "' but found " + arrayCtorCallNode.toString(), arrayCtorCallNode.toString().equals(exp)); List rels = AsmManager.lastActiveStructureModel.getRelationshipMap().get(arrayCtorCallNode); @@ -119,6 +128,7 @@ public class NewarrayJoinpointTests extends XMLBasedAjcTestCase { return XMLBasedAjcTestCase.loadSuite(NewarrayJoinpointTests.class); } + @Override protected File getSpecFile() { return getClassResource("newarray_joinpoint.xml"); } diff --git a/tests/src/org/aspectj/systemtest/ajc190/AllTestsAspectJ190.java b/tests/src/org/aspectj/systemtest/ajc190/AllTestsAspectJ190.java index 2ebc12e7c..d0f7734f3 100644 --- a/tests/src/org/aspectj/systemtest/ajc190/AllTestsAspectJ190.java +++ b/tests/src/org/aspectj/systemtest/ajc190/AllTestsAspectJ190.java @@ -20,7 +20,7 @@ public class AllTestsAspectJ190 { // $JUnit-BEGIN$ suite.addTest(Ajc190Tests.suite()); suite.addTest(SanityTests19.suite()); -// suite.addTest(EfficientTJPTests.suite()); + suite.addTest(EfficientTJPTests.suite()); suite.addTest(ModuleTests.suite()); suite.addTest(Annotations.suite()); // $JUnit-END$ diff --git a/tests/src/org/aspectj/systemtest/ajc190/EfficientTJPTests.java b/tests/src/org/aspectj/systemtest/ajc190/EfficientTJPTests.java new file mode 100644 index 000000000..3564f9469 --- /dev/null +++ b/tests/src/org/aspectj/systemtest/ajc190/EfficientTJPTests.java @@ -0,0 +1,126 @@ +/******************************************************************************* + * Copyright (c) 2018 Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.aspectj.systemtest.ajc190; + +import java.io.File; + +import org.aspectj.apache.bcel.classfile.JavaClass; +import org.aspectj.apache.bcel.classfile.Method; +import org.aspectj.testing.XMLBasedAjcTestCase; + +import junit.framework.Assert; +import junit.framework.Test; + +/** + * + * @author Andy Clement + */ +public class EfficientTJPTests extends XMLBasedAjcTestCase { + + public void testThisJoinPointMethodExecution() { + // Test setting it via sys props rather than passing the option directly + try { + System.setProperty("ASPECTJ_OPTS", "-Xajruntimetarget:1.9"); + runTest("tjp 1"); + checkPreClinitContains("One","Factory.makeMethodSJP"); + } finally { + System.setProperty("ASPECTJ_OPTS", ""); + } + } + + public void testThisEnclosingJoinPointMethodExecution() { + runTest("tjp 2"); + checkPreClinitContains("Two","Factory.makeMethodESJP"); + } + + public void testThisJoinPointConstructorExecution() { + runTest("tjp 3"); + checkPreClinitContains("Three","Factory.makeConstructorSJP"); + } + + public void testThisEnclosingJoinPointConstructorExecution() { + runTest("tjp 3a"); + checkPreClinitContains("ThreeA","Factory.makeConstructorESJP"); + } + + public void testThisJoinPointHandler() { + runTest("tjp 4"); + checkPreClinitContains("Four","Factory.makeCatchClauseSJP"); + } + + public void testThisEnclosingJoinPointHandler() { + runTest("tjp 4a"); + checkPreClinitContains("FourA","Factory.makeMethodESJP"); + } + + public void testThisJoinPointFieldGet() { + runTest("tjp get fields"); + checkPreClinitContains("Fields","Factory.makeFieldSJP"); + } + + public void testThisEnclosingJoinPointFieldGet() { + runTest("tjp get fieldsE"); + checkPreClinitContains("FieldsE","Factory.makeMethodESJP"); + } + + public void testThisJoinPointFieldSet() { + runTest("tjp set fields"); + checkPreClinitContains("Fields2","Factory.makeFieldSJP"); + } + + public void testThisJoinPointClinit() { + runTest("tjp clinit"); + checkPreClinitContains("Clinit","Factory.makeInitializerSJP"); + } + + public void testThisEnclosingJoinPointClinit() { + runTest("tejp clinit"); + checkPreClinitContains("ClinitE","Factory.makeInitializerESJP"); + } + + public void testThisJoinPointAdvice() { + // covers enclosing joinpoint too + runTest("tjp advice"); + checkPreClinitContains("X","Factory.makeAdviceESJP"); + } + + public void testThisJoinPointInitialization() { + runTest("tjp init"); + checkPreClinitContains("A","Factory.makeConstructorESJP"); + checkPreClinitContains("B","Factory.makeConstructorESJP"); + } + + // /////////////////////////////////////// + public static Test suite() { + return XMLBasedAjcTestCase.loadSuite(EfficientTJPTests.class); + } + + @Override + protected File getSpecFile() { + return getClassResource("features190.xml"); + } + + public void checkPreClinitContains(String classname, String text) { + try { + JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), classname); + Method[] meths = jc.getMethods(); + for (int i = 0; i < meths.length; i++) { + Method method = meths[i]; + if (method.getName().equals("ajc$preClinit")) { + String code = method.getCode().getCodeString(); + assertTrue("Expected to contain '"+text+"':\n"+code,code.contains(text)); + return; + } + } + Assert.fail("Unable to find ajc$preClinit in class "+classname); + } catch (Exception e) { + Assert.fail(e.toString()); + } + } + +} diff --git a/tests/src/org/aspectj/systemtest/ajc190/features190.xml b/tests/src/org/aspectj/systemtest/ajc190/features190.xml new file mode 100644 index 000000000..1fb931e97 --- /dev/null +++ b/tests/src/org/aspectj/systemtest/ajc190/features190.xml @@ -0,0 +1,172 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3