aboutsummaryrefslogtreecommitdiffstats
path: root/loadtime/src
diff options
context:
space:
mode:
authorAndy Clement <aclement@pivotal.io>2019-01-28 13:08:30 -0800
committerAndy Clement <aclement@pivotal.io>2019-01-28 13:08:30 -0800
commit8deb80454be1ea220f7d2296323fbb29a6f088a7 (patch)
tree6d79c139a40c79871e25892a8eae38da08364e08 /loadtime/src
parenta46d7f1cfdf9828116cdd1b065dde35bd5f06cb6 (diff)
downloadaspectj-8deb80454be1ea220f7d2296323fbb29a6f088a7.tar.gz
aspectj-8deb80454be1ea220f7d2296323fbb29a6f088a7.zip
Merged loadtime5 into loadtime
Diffstat (limited to 'loadtime/src')
-rw-r--r--loadtime/src/main/java/org/aspectj/weaver/loadtime/Agent.java67
-rw-r--r--loadtime/src/main/java/org/aspectj/weaver/loadtime/ClassPreProcessorAgentAdapter.java53
-rw-r--r--loadtime/src/test/java/org/aspectj/loadtime/Loadtime5ModuleTests.java37
-rw-r--r--loadtime/src/test/java/org/aspectj/weaver/loadtime/LoadtimeTest.java25
4 files changed, 182 insertions, 0 deletions
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));
+ }
+}