Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 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.mojo;
 
 import java.io.File;
 import java.net.URL;
 import java.util.*;


The base generate stubs mojo, which all generate stubs mojos extend.

Author(s):
Keegan Witt
 
 public abstract class AbstractGenerateStubsMojo extends AbstractGroovyStubSourcesMojo {
     // TODO: support Groovy 1.5.0 - 1.8.1?
     /*
      * For some reason, the JavaStubCompilationUnit is silently not creating my
      * stubs (although it does create the target directory) when I use other
      * versions.
      */

    
The minimum version of Groovy that this mojo supports.
 
     protected static final Version MIN_GROOVY_VERSION = new Version(1, 8, 2);

    
The encoding of source files.

Parameter:
default-value="${project.build.sourceEncoding}"
 
     protected String sourceEncoding;

    
The file extensions of Groovy source files.

Parameter:
 
     protected Set<StringscriptExtensions;

    
The Groovy compiler bytecode compatibility. One of
  • 1.4
  • 1.5
  • 1.6
  • 1.7
  • 1.8

Parameter:
default-value="1.5"
 
     protected String targetBytecode;

    
Whether Groovy compiler should be set to debug.

Parameter:
default-value="false"
 
     protected boolean debug;

    
Whether Groovy compiler should be set to verbose.

Parameter:
default-value="false"
 
     protected boolean verbose;

    
Groovy compiler warning level. Should be one of:
0
None
1
Likely Errors
2
Possible Errors
3
Paranoia

Parameter:
default-value="1"
    protected int warningLevel;

    
Groovy compiler error tolerance (the number of non-fatal errors (per unit) that should be tolerated before compilation is aborted).

Parameter:
default-value="0"
    protected int tolerance;

    
Performs the stub generation on the specified source files.

Parameters:
stubSources the sources to perform stub generation on
classpath The classpath to use for compilation
outputDirectory the directory to write the stub files to
Throws:
java.lang.ClassNotFoundException When a class needed for stub generation cannot be found
java.lang.InstantiationException When a class needed for stub generation cannot be instantiated
java.lang.IllegalAccessException When a method needed for stub generation cannot be accessed
java.lang.reflect.InvocationTargetException When a reflection invocation needed for stub generation cannot be completed
java.net.MalformedURLException When a classpath element provides a malformed URL
    protected synchronized void doStubGeneration(final Set<FilestubSourcesfinal List classpathfinal File outputDirectorythrows ClassNotFoundExceptionInvocationTargetExceptionIllegalAccessExceptionInstantiationExceptionMalformedURLException {
        if (stubSources == null || stubSources.isEmpty()) {
            getLog().info("No sources specified for stub generation.  Skipping.");
            return;
        }
        // get classes we need with reflection
        Class<?> compilerConfigurationClass = Class.forName("org.codehaus.groovy.control.CompilerConfiguration");
        Class<?> javaStubCompilationUnitClass = Class.forName("org.codehaus.groovy.tools.javac.JavaStubCompilationUnit");
        Class<?> groovyClassLoaderClass = Class.forName("groovy.lang.GroovyClassLoader");
        // set up compile options
        Object compilerConfiguration = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(compilerConfigurationClass));
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setDebug"boolean.class), compilerConfiguration);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setVerbose"boolean.class), compilerConfiguration);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setWarningLevel"int.class), compilerConfiguration);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTolerance"int.class), compilerConfiguration);
        String bytecode;
        if (getGroovyVersion().compareTo(new Version(2, 1, 3)) >= 0) {
            bytecode = ;
        } else {
            bytecode = "1.5";
        }
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTargetBytecode"String.class), compilerConfigurationbytecode);
        if ( != null) {
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setSourceEncoding"String.class), compilerConfiguration);
        }
        Map<StringObjectoptions = new HashMap<StringObject>();
        options.put("stubDir"outputDirectory);
        options.put("keepStubs".);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setJointCompilationOptions"Map.class), compilerConfigurationoptions);
        // append project classpath to groovyClassLoader
        ClassLoader parent = ClassLoader.getSystemClassLoader();
        Object groovyClassLoader = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(groovyClassLoaderClassClassLoader.classcompilerConfigurationClass), parentcompilerConfiguration);
        Object javaStubCompilationUnit = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(javaStubCompilationUnitClasscompilerConfigurationClassgroovyClassLoaderClassFile.class), compilerConfigurationgroovyClassLoaderoutputDirectory);
        getLog().debug("Classpath: ");
        if (classpath != null) {
            for (Object classpathElement : classpath) {
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyClassLoaderClass"addURL"URL.class), groovyClassLoadernew File((StringclasspathElement).toURI().toURL());
                getLog().debug("    " + classpathElement);
            }
        }
        // add Groovy sources
        getLog().debug("Adding Groovy to generate stubs for:");
        for (File source : stubSources) {
            getLog().debug("    " + source);
            if (getGroovyVersion().compareTo(new Version(1, 8, 3)) >= 0) {
                Set<Stringextensions;
                if ( != null && !.isEmpty()) {
                    extensions = ;
                } else {
                    extensions = DotGroovyFile.defaultScriptExtensions();
                }
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setScriptExtensions"Set.class), compilerConfigurationextensions);
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(javaStubCompilationUnitClass"addSource"File.class), javaStubCompilationUnitsource);
            } else {
                DotGroovyFile dotGroovyFile = new DotGroovyFile(source);
                Set<Stringextensions;
                if ( != null && !.isEmpty()) {
                    extensions = ;
                } else {
                    extensions = DotGroovyFile.defaultScriptExtensions();
                }
                dotGroovyFile.setScriptExtensions(extensions);
                ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(javaStubCompilationUnitClass"addSource"File.class), javaStubCompilationUnitdotGroovyFile);
            }
        }
        // generate the stubs
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(javaStubCompilationUnitClass"compile"), javaStubCompilationUnit);
        // log generated stubs
        getLog().info("Generated " + getStubs().size() + " stub" + (getStubs().size() > 1 || getStubs().size() == 0 ? "s" : "") + ".");
    }

    
Determines whether this mojo can be run with the version of Groovy supplied. Must be >= 1.8.2 because not all the classes needed were available and functioning correctly in previous versions.

Returns:
true only if the version of Groovy supports this mojo.
    protected boolean groovyVersionSupportsAction() {
        return getGroovyVersion() != null && getGroovyVersion().compareTo() >= 0;
    }

    
This is a fix for http://jira.codehaus.org/browse/MGROOVY-187 It modifies the dates of the created stubs to 1/1/1970, ensuring that the Java compiler will not overwrite perfectly good compiled Groovy just because it has a newer source stub. Basically, this prevents the stubs from causing a side effect with the Java compiler, but still allows stubs to work with JavaDoc.

Parameters:
stubs the files on which to reset the modified date
    protected void resetStubModifiedDates(final Set<Filestubs) {
        for (File stub : stubs) {
            boolean success = stub.setLastModified(0L);
            if (!success) {
                getLog().warn("Unable to set modified time on stub " + stub.getAbsolutePath() + ".");
            }
        }
    }
New to GrepCode? Check out our FAQ X