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 AbstractGroovySourcesMojo {
     // TODO: support Groovy 1.5.0 - 1.6.9?
     /*
      * For some reason, the JavaStubCompilationUnit is silently not creating my
      * stubs (although it does create the target directory) when I use older
      * versions.
      */

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

    
The location for the compiled classes.

Parameter:
default-value="${project.build.directory}/generated-sources/groovy-stubs/main"
 
     protected File stubsOutputDirectory;

    
The location for the compiled test classes.

Parameter:
default-value="${project.build.directory}/generated-sources/groovy-stubs/test"
 
     protected File testStubsOutputDirectory;

    
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;
 
     // if plugin only runs on 1.5, then can assume 1.5
     
The Groovy compiler bytecode compatibility ("1.4" or "1.5").

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="0"@
    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;

    
Gets the set of files of the main stubs.

Returns:
The set of files of the main stubs
    protected Set<FilegetStubs() {
        Set<Filefiles = new HashSet<File>();
        FileSetManager fileSetManager = new FileSetManager(getLog());
        FileSet fileSet = new FileSet();
        fileSet.setIncludes(Arrays.asList());
        for (String file : fileSetManager.getIncludedFiles(fileSet)) {
            files.add(new File(file));
        }
        return files;
    }

    
Gets the set of files of the test stubs.

Returns:
The set of files of the test stubs
    protected Set<FilegetTestStubs() {
        Set<Filefiles = new HashSet<File>();
        FileSetManager fileSetManager = new FileSetManager(getLog());
        FileSet fileSet = new FileSet();
        fileSet.setIncludes(Arrays.asList());
        for (String file : fileSetManager.getIncludedFiles(fileSet)) {
            files.add(new File(file));
        }
        return files;
    }

    
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
mavenBuildOutputDirectory Maven's build output directory
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 String mavenBuildOutputDirectoryfinal File outputDirectorythrows ClassNotFoundExceptionInvocationTargetExceptionIllegalAccessExceptionInstantiationExceptionMalformedURLException {
        // 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");
        Class<?> phasesClass = Class.forName("org.codehaus.groovy.control.Phases");
        // 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);
        if (Version.parseFromString(getGroovyVersion()).compareTo(new Version(1, 5, 0)) >= 0) {
            // if plugin only runs on 1.5, then can assume 1.5
//            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass, "setTargetBytecode", String.class), compilerConfiguration, targetBytecode);
            //
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTargetBytecode"String.class), compilerConfiguration"1.5");
        }
        if ( != null) {
            ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setSourceEncoding"String.class), compilerConfiguration);
        }
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTargetDirectory"String.class), compilerConfigurationoutputDirectory.getAbsolutePath());
        // 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: ");
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(groovyClassLoaderClass"addClasspath"String.class), groovyClassLoadermavenBuildOutputDirectory);
        getLog().debug("    " + mavenBuildOutputDirectory);
        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 (Version.parseFromString(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
        Object convPhase = ReflectionUtils.getStaticField(ReflectionUtils.findField(phasesClass"CONVERSION"int.class));
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(javaStubCompilationUnitClass"compile"int.class), javaStubCompilationUnitconvPhase);
        // log generated stubs
        getLog().debug("Generated " + stubSources.size() + "stubs.");
    }

    
Determines whether this mojo can be run with the version of Groovy supplied. Must be >= 1.7.0 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 Version.parseFromString(getGroovyVersion()).compareTo() >= 0;
    }

    
This is a fix for fix for http://jira.codehaus.org/browse/MGROOVY-187 It modifies the dates of the created stubs to 1970, ensuring that the Java compiler will not come along and 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 the stubs to work with JavaDoc. Ideally, the code for this should be added to the code that creates the stubs.

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