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.util.List;
 import java.util.Map;
 import java.util.Set;


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

Author(s):
Keegan Witt
Since:
1.0-beta-1
 
 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.
      */

    
Groovy 2.9.0 beta-1 version.
 
     protected static final Version GROOVY_1_9_0_BETA1 = new Version(1, 9, 0, "beta-1");

    
Groovy 1.9.0 beta-3 version.
 
     protected static final Version GROOVY_1_9_0_BETA3 = new Version(1, 9, 0, "beta-3");

    
Groovy 1.8.3 version.
 
     protected static final Version GROOVY_1_8_3 = new Version(1, 8, 3);

    
The encoding of source files.

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

    
The file extensions of Groovy source files.

Since:
1.0-beta-2
Parameter:
 
     protected Set<StringscriptExtensions;

    
The Groovy compiler bytecode compatibility. One of
  • 1.4
  • 1.5
  • 1.6
  • 1.7
  • 1.8
Note that prior to Groovy 2.1.3, only 1.4 and 1.5 were supported. If an invalid selection is made, Groovy will default to VM determined version.

Since:
1.0-beta-3
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 {
         = new ClassWrangler(classpathgetLog());
        if (stubSources == null || stubSources.isEmpty()) {
            getLog().info("No sources specified for stub generation.  Skipping.");
            return;
        }
        if (groovyVersionSupportsAction()) {
            logPluginClasspath();
            if (getLog().isDebugEnabled()) {
                try {
                    getLog().debug("Project compile classpath:\n" + .getCompileClasspathElements());
                } catch (DependencyResolutionRequiredException e) {
                    getLog().warn("Unable to log project compile classpath"e);
                }
            }
        } else {
            getLog().error("Your Groovy version (" + .getGroovyVersion() + ") doesn't support stub generation.  The minimum version of Groovy required is " +  + ".  Skipping stub generation.");
            return;
        }
        // get classes we need with reflection
        Class compilerConfigurationClass = .getClass("org.codehaus.groovy.control.CompilerConfiguration");
        Class javaStubCompilationUnitClass = .getClass("org.codehaus.groovy.tools.javac.JavaStubCompilationUnit");
        Class groovyClassLoaderClass = .getClass("groovy.lang.GroovyClassLoader");
        // setup stub generation options
        Object compilerConfiguration = setupCompilerConfiguration(outputDirectorycompilerConfigurationClass);
        Object groovyClassLoader = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(groovyClassLoaderClassClassLoader.classcompilerConfigurationClass), .getClassLoader(), compilerConfiguration);
        Object javaStubCompilationUnit = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(javaStubCompilationUnitClasscompilerConfigurationClassgroovyClassLoaderClassFile.class), compilerConfigurationgroovyClassLoaderoutputDirectory);
        // add Groovy sources
        addGroovySources(stubSourcescompilerConfigurationClassjavaStubCompilationUnitClasscompilerConfigurationjavaStubCompilationUnit);
        // 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" : "") + ".");
    }

    
Sets up the CompilerConfiguration to use for stub generation.

Parameters:
outputDirectory the directory to write the stub files to
compilerConfigurationClass the CompilerConfiguration class
Returns:
the CompilerConfiguration to use for stub generation
Throws:
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
    protected Object setupCompilerConfiguration(final File outputDirectoryfinal Class compilerConfigurationClassthrows InvocationTargetExceptionIllegalAccessExceptionInstantiationException {
        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);
        ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(compilerConfigurationClass"setTargetBytecode"String.class), compilerConfiguration);
        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);
        return compilerConfiguration;
    }

    
Adds the Groovy sources to the CompilationUnit.

Parameters:
stubSources the sources to perform stub generation on
compilerConfigurationClass the CompilerConfiguration class
javaStubCompilationUnitClass the JavaStubCompilationUnit class
compilerConfiguration the CompilerConfiguration to use for stub generation
javaStubCompilationUnit the JavaStubCompilationUnit to use for stub generation
Throws:
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
    protected void addGroovySources(final Set<FilestubSourcesfinal Class compilerConfigurationClassfinal Class javaStubCompilationUnitClassfinal Object compilerConfigurationfinal Object javaStubCompilationUnitthrows InvocationTargetExceptionIllegalAccessException {
        getLog().debug("Adding Groovy to generate stubs for:");
        for (File source : stubSources) {
            if (getLog().isDebugEnabled()) {
                getLog().debug("    " + source);
            }
                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);
            }
        }
    }

    
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