From 8deb80454be1ea220f7d2296323fbb29a6f088a7 Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Mon, 28 Jan 2019 13:08:30 -0800 Subject: [PATCH] Merged loadtime5 into loadtime --- loadtime/loadtime5.mf.txt | 12 ++++ .../org/aspectj/weaver/loadtime/Agent.java | 67 +++++++++++++++++++ .../ClassPreProcessorAgentAdapter.java | 53 +++++++++++++++ .../loadtime/Loadtime5ModuleTests.java | 37 ++++++++++ .../aspectj/weaver/loadtime/LoadtimeTest.java | 25 +++++++ 5 files changed, 194 insertions(+) create mode 100644 loadtime/loadtime5.mf.txt create mode 100644 loadtime/src/main/java/org/aspectj/weaver/loadtime/Agent.java create mode 100644 loadtime/src/main/java/org/aspectj/weaver/loadtime/ClassPreProcessorAgentAdapter.java create mode 100644 loadtime/src/test/java/org/aspectj/loadtime/Loadtime5ModuleTests.java create mode 100644 loadtime/src/test/java/org/aspectj/weaver/loadtime/LoadtimeTest.java diff --git a/loadtime/loadtime5.mf.txt b/loadtime/loadtime5.mf.txt new file mode 100644 index 000000000..cea0e5eda --- /dev/null +++ b/loadtime/loadtime5.mf.txt @@ -0,0 +1,12 @@ +Manifest-Version: 1.0 +Automatic-Module-Name: org.aspectj.weaver +Name: org/aspectj/weaver/ +Specification-Title: AspectJ Weaver Classes +Specification-Version: @build.version.short@ +Specification-Vendor: @company.name@ +Implementation-Title: org.aspectj.weaver +Implementation-Version: @build.version.short@ +Implementation-Vendor: @company.name@ +Premain-Class: org.aspectj.weaver.loadtime.Agent +Agent-Class: org.aspectj.weaver.loadtime.Agent +Can-Redefine-Classes: true diff --git a/loadtime/src/main/java/org/aspectj/weaver/loadtime/Agent.java b/loadtime/src/main/java/org/aspectj/weaver/loadtime/Agent.java new file mode 100644 index 000000000..eeca8aeef --- /dev/null +++ b/loadtime/src/main/java/org/aspectj/weaver/loadtime/Agent.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2005 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://eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexandre Vasseur initial implementation + *******************************************************************************/ +package org.aspectj.weaver.loadtime; + +import java.lang.instrument.Instrumentation; +import java.lang.instrument.ClassFileTransformer; + +/** + * Java 1.5 preMain agent to hook in the class pre processor + * Can be used with -javaagent:aspectjweaver.jar + * + * @author Alexandre Vasseur + * @author Alexander Kriegisch + */ +public class Agent { + + /** + * The instrumentation instance + */ + private static Instrumentation s_instrumentation; + + /** + * The ClassFileTransformer wrapping the weaver + */ + private static ClassFileTransformer s_transformer = new ClassPreProcessorAgentAdapter(); + + /** + * JSR-163 preMain Agent entry method + * + * @param options + * @param instrumentation + */ + public static void premain(String options, Instrumentation instrumentation) { + /* Handle duplicate agents */ + if (s_instrumentation != null) { + return; + } + s_instrumentation = instrumentation; + s_instrumentation.addTransformer(s_transformer); + } + + public static void agentmain(String options, Instrumentation instrumentation) { + premain(options, instrumentation); + } + + /** + * Returns the Instrumentation system level instance + */ + public static Instrumentation getInstrumentation() { + if (s_instrumentation == null) { + throw new UnsupportedOperationException( + "AspectJ weaving agent was neither started via '-javaagent' (preMain) " + + "nor attached via 'VirtualMachine.loadAgent' (agentMain)"); + } + return s_instrumentation; + } + +} diff --git a/loadtime/src/main/java/org/aspectj/weaver/loadtime/ClassPreProcessorAgentAdapter.java b/loadtime/src/main/java/org/aspectj/weaver/loadtime/ClassPreProcessorAgentAdapter.java new file mode 100644 index 000000000..a3201816a --- /dev/null +++ b/loadtime/src/main/java/org/aspectj/weaver/loadtime/ClassPreProcessorAgentAdapter.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2005,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://eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.aspectj.weaver.loadtime; + +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.security.ProtectionDomain; + +/** + * Java 1.5 adapter for class pre processor + * + * @author Alexandre Vasseur + * @author Andy Clement + */ +public class ClassPreProcessorAgentAdapter implements ClassFileTransformer { + + private static ClassPreProcessor classPreProcessor; + + static { + try { + classPreProcessor = new Aj(); + classPreProcessor.initialize(); + } catch (Exception e) { + throw new ExceptionInInitializerError("could not initialize JSR163 preprocessor due to: " + e.toString()); + } + } + + /** + * Invokes the weaver to modify some set of input bytes. + * + * @param loader the defining class loader + * @param className the name of class being loaded + * @param classBeingRedefined is set when hotswap is being attempted + * @param protectionDomain the protection domain for the class being loaded + * @param bytes the incoming bytes (before weaving) + * @return the woven bytes + */ + @Override + public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, + byte[] bytes) throws IllegalClassFormatException { + if (classBeingRedefined != null) { + System.err.println("INFO: (Enh120375): AspectJ attempting reweave of '" + className + "'"); + classPreProcessor.prepareForRedefinition(loader, className); + } + return classPreProcessor.preProcess(className, bytes, loader, protectionDomain); + } +} diff --git a/loadtime/src/test/java/org/aspectj/loadtime/Loadtime5ModuleTests.java b/loadtime/src/test/java/org/aspectj/loadtime/Loadtime5ModuleTests.java new file mode 100644 index 000000000..665aecdc6 --- /dev/null +++ b/loadtime/src/test/java/org/aspectj/loadtime/Loadtime5ModuleTests.java @@ -0,0 +1,37 @@ +package org.aspectj.loadtime; +/******************************************************************************* + * Copyright (c) 2005 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://eclipse.org/legal/epl-v10.html + * + * Contributors: (See CVS logs) + * + *******************************************************************************/ + +import org.aspectj.testing.util.TestUtil; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + */ +public class Loadtime5ModuleTests extends TestCase { + + public static Test suite() { + TestSuite suite = new TestSuite(Loadtime5ModuleTests.class.getName()); + if (TestUtil.is15VMOrGreater()) { + TestUtil.loadTestsReflectively(suite, "Loadtime515ModuleTests", true); + } else { + suite.addTest(TestUtil.testNamed("all tests require 1.5")); + } + return suite; + } + public static void main(String[] args) { + junit.textui.TestRunner.main(new String[] {Loadtime5ModuleTests.class.getName()}); + } + +} diff --git a/loadtime/src/test/java/org/aspectj/weaver/loadtime/LoadtimeTest.java b/loadtime/src/test/java/org/aspectj/weaver/loadtime/LoadtimeTest.java new file mode 100644 index 000000000..94fae75e2 --- /dev/null +++ b/loadtime/src/test/java/org/aspectj/weaver/loadtime/LoadtimeTest.java @@ -0,0 +1,25 @@ +/* ******************************************************************* + * Copyright (c) 2005-2019 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://eclipse.org/legal/epl-v10.html + * ******************************************************************/ +package org.aspectj.weaver.loadtime; + +import java.lang.instrument.Instrumentation; + +import junit.framework.TestCase; + +/** + * @author Andy Clement + * @author Wes Isberg + */ +public class LoadtimeTest extends TestCase { + + public void testPremain() throws Exception { + Class[] paramTypes = {String.class, Instrumentation.class }; + assertNotNull(Agent.class.getMethod("premain", paramTypes)); + } +} -- 2.39.5