changeset 457:bbb7c7d0b22b

jmh-generator-maven-plugin: add project depedencies to the execution, able to load classes from language libraries.
author shade
date Mon, 03 Mar 2014 01:22:17 +0400
parents 8fca1e8a318f
children 08022b9c9c91
files jmh-core/src/main/java/org/openjdk/jmh/generators/bytecode/ClassInfoRepo.java jmh-generator-maven-plugin/pom.xml jmh-generator-maven-plugin/src/main/java/org/openjdk/jmh/JmhMojo.java
diffstat 3 files changed, 46 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/bytecode/ClassInfoRepo.java	Sat Mar 01 02:56:40 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/bytecode/ClassInfoRepo.java	Mon Mar 03 01:22:17 2014 +0400
@@ -43,7 +43,7 @@
         }
 
         try {
-            return new RFClassInfo(Class.forName(desc));
+            return new RFClassInfo(Class.forName(desc, false, Thread.currentThread().getContextClassLoader()));
         } catch (ClassNotFoundException e) {
             throw new IllegalStateException("Unable to resolve class: " + desc);
         }
--- a/jmh-generator-maven-plugin/pom.xml	Sat Mar 01 02:56:40 2014 +0400
+++ b/jmh-generator-maven-plugin/pom.xml	Mon Mar 03 01:22:17 2014 +0400
@@ -33,6 +33,11 @@
             <version>2.0.9</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-project</artifactId>
+            <version>2.0.9</version>
+        </dependency>
+        <dependency>
             <groupId>org.openjdk.jmh</groupId>
             <artifactId>jmh-core</artifactId>
             <version>${project.version}</version>
--- a/jmh-generator-maven-plugin/src/main/java/org/openjdk/jmh/JmhMojo.java	Sat Mar 01 02:56:40 2014 +0400
+++ b/jmh-generator-maven-plugin/src/main/java/org/openjdk/jmh/JmhMojo.java	Mon Mar 03 01:22:17 2014 +0400
@@ -1,19 +1,27 @@
 package org.openjdk.jmh;
 
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
 import org.openjdk.jmh.generators.bytecode.ASMGeneratorSource;
 import org.openjdk.jmh.generators.bytecode.SourceError;
 import org.openjdk.jmh.generators.core.BenchmarkGenerator;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 /**
+ * @requiresDependencyResolution compile+runtime
  * @goal generate
  * @phase process-sources
  */
@@ -43,7 +51,39 @@
      */
     private File outputSourceDirectory;
 
+    /**
+     * @parameter expression="${project}"
+     * @required
+     */
+    private MavenProject project;
+
+    public void extendPluginClasspath() throws MojoExecutionException {
+        try {
+            Set<URL> urls = new HashSet<URL>();
+            List<String> elements = new ArrayList<String>();
+            elements.addAll(project.getTestClasspathElements());
+            elements.addAll(project.getRuntimeClasspathElements());
+            elements.addAll(project.getCompileClasspathElements());
+            elements.addAll(project.getSystemClasspathElements());
+            for (String element : elements) {
+                urls.add(new File(element).toURI().toURL());
+            }
+
+            ClassLoader contextClassLoader = URLClassLoader.newInstance(
+                    urls.toArray(new URL[urls.size()]),
+                    Thread.currentThread().getContextClassLoader());
+
+            Thread.currentThread().setContextClassLoader(contextClassLoader);
+        } catch (DependencyResolutionRequiredException e) {
+            throw new MojoExecutionException("Internal error", e);
+        } catch (MalformedURLException e) {
+            throw new MojoExecutionException("Internal error", e);
+        }
+    }
+
     public void execute() throws MojoExecutionException, MojoFailureException {
+        extendPluginClasspath();
+
         ASMGeneratorSource source = new ASMGeneratorSource(outputResourceDirectory, outputSourceDirectory);
 
         try {