Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2014 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.codehaus.gmavenplus.util;
 
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;


The base compile mojo, which all compile mojos extend.

Author(s):
Keegan Witt
Since:
1.2
 
 public class ClassWrangler {

    
Cached Groovy version.
 
     protected String groovyVersion = null;

    
ClassLoader to use for class wrangling.
 
     protected ClassLoader classLoader;

    
Plugin log.
 
     protected Log log;

    
Creates a new ClassWrangler using the specified ClassLoader.

Parameters:
classLoaderForLoading the ClassLoader to use to load classes
pluginLog the Maven log to use for logging
 
     public ClassWrangler(final ClassLoader classLoaderForLoadingfinal Log pluginLog) {
          = pluginLog;
          = classLoaderForLoading;
     }

    
Creates a new ClassWrangler using a new ClassLoader, loaded with the items from the specified classpath.

Parameters:
classpath the classpath to load the new ClassLoader with
pluginLog the Maven log to use for logging
Throws:
java.net.MalformedURLException
 
     public ClassWrangler(final List classpathfinal Log pluginLogthrows MalformedURLException {
          = pluginLog;
         // create an isolated ClassLoader with all the appropriate project dependencies in it
          = createNewClassLoader(classpath);
     }

    
Gets the version string of Groovy used from the dependency information.

Returns:
The version string of Groovy used by the project
 
     public String getGroovyVersionString() {
         /*
          * You can call InvokerHelper.getVersion() for versions 1.0 - 1.8.x but
          * not for 1.9+.
          * You can call GroovySystem.getVersion() for versions 1.6.6+.
          * And for some reason InvokerHelper.getVersion() was returning an empty
          * String for 1.5.0, so I decided to just get it from the jar itself.
          */
         if ( == null) {
             String jar = getGroovyJar();
             int idx = .;
             for (int i = 0; i < 9; i++) {
                 int newIdx = jar.indexOf("-" + i);
                 if (newIdx >= 0 && newIdx < idx) {
                     idx = newIdx;
                 }
             }
             if (idx < .) {
                  = jar.substring(idx + 1, jar.length() - 4);
             }
         }
        return ;
    }

    
Gets the version of Groovy used from the dependency information.

Returns:
The version of Groovy used by the project
    public Version getGroovyVersion() {
        try {
            return Version.parseFromString(getGroovyVersionString().replace("-indy"""));
        } catch (Exception e) {
            .error("Unable to determine Groovy version.  Is Groovy declared as a dependency?");
            return null;
        }
    }

    
Gets the version of Groovy used from the dependency information.

Returns:
true if the version of Groovy uses InvokeDynamic, false if not or Groovy dependency cannot be found.
    public boolean isGroovyIndy() {
        return getGroovyVersionString().contains("-indy");
    }

    
Returns the filename of the Groovy jar on the classpath.

Returns:
the Groovy jar filename
    public String getGroovyJar() {
        try {
            String groovyObjectClassPath = getJarPath();
            String groovyJar = null;
            if (groovyObjectClassPath != null) {
                groovyJar = groovyObjectClassPath.replaceAll("!.+""");
                groovyJar = groovyJar.substring(groovyJar.lastIndexOf("/") + 1, groovyJar.length());
            }
            return groovyJar;
        } catch (ClassNotFoundException e) {
            .error("Unable to determine Groovy version.  Is Groovy declared as a dependency?");
            return null;
        }
    }

    
Returns the path of the Groovy jar on the classpath.

Returns:
the path of the Groovy jar
Throws:
java.lang.ClassNotFoundException when Groovu couldn't be found on the classpath
    protected String getJarPath() throws ClassNotFoundException {
        Class groovyObjectClass = Class.forName("groovy.lang.GroovyObject"true);
        String groovyObjectClassPath = String.valueOf(groovyObjectClass.getResource("/" + groovyObjectClass.getName().replace('.''/') + ".class"));
        if (groovyObjectClassPath == null) {
            CodeSource codeSource = groovyObjectClass.getProtectionDomain().getCodeSource();
            if (codeSource != null) {
                groovyObjectClassPath = String.valueOf(codeSource.getLocation());
            }
        }
        return groovyObjectClassPath;
    }

    
Logs the version of groovy used by this mojo.

Parameters:
goal The goal to mention in the log statement showing Groovy version
    public void logGroovyVersion(final String goal) {
        if (.isInfoEnabled()) {
            .info("Using Groovy " + getGroovyVersionString() + " to perform " + goal + ".");
        }
    }

    
Creates a new ClassLoader with the specified classpath.

Parameters:
classpath the classpath (a list of file path Strings) to include in the new loader
Returns:
the new ClassLoader
Throws:
java.net.MalformedURLException when a classpath element provides a malformed URL
    public ClassLoader createNewClassLoader(final List classpaththrows MalformedURLException {
        List<URLurlsList = new ArrayList<URL>();
        for (Object classPathObject : classpath) {
            String path = (StringclassPathObject;
            urlsList.add(new File(path).toURI().toURL());
        }
        URL[] urlsArray = urlsList.toArray(new URL[urlsList.size()]);
        return new URLClassLoader(urlsArray, ClassLoader.getSystemClassLoader());
    }

    
Gets a class for the given class name.

Parameters:
className the class name to retrieve the class for
Returns:
the class for the given class name
Throws:
java.lang.ClassNotFoundException when a class for the specified class name cannot be found
    public Class getClass(final String classNamethrows ClassNotFoundException {
        return Class.forName(classNametrue);
    }

    
Returns the classloader used for loading classes.

Returns:
the classloader used for loading classes
    public ClassLoader getClassLoader() {
        return ;
    }
New to GrepCode? Check out our FAQ X