Skip to content

Commit

Permalink
Merge pull request #184 from groovy/check_log_level_before_string_concat
Browse files Browse the repository at this point in the history
Check if log level is used before doing string concatenation
  • Loading branch information
keeganwitt committed Dec 10, 2020
2 parents 0e645de + 577958d commit 69c9dc3
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 45 deletions.
44 changes: 33 additions & 11 deletions src/main/java/org/codehaus/gmavenplus/mojo/AbstractCompileMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ protected synchronized void doCompile(final Set<File> sources, final List classp

// log compiled classes
List classes = (List) invokeMethod(findMethod(compilationUnitClass, "getClasses"), compilationUnit);
getLog().info("Compiled " + classes.size() + " file" + (classes.size() != 1 ? "s" : "") + ".");
if (getLog().isInfoEnabled()) {
getLog().info("Compiled " + classes.size() + " file" + (classes.size() != 1 ? "s" : "") + ".");
}
}

/**
Expand Down Expand Up @@ -343,7 +345,9 @@ protected Object setupCompilationUnit(final Set<File> sources, final Class<?> co
getLog().debug("Adding Groovy to compile:");
Method addSourceMethod = findMethod(compilationUnitClass, "addSource", File.class);
for (File source : sources) {
getLog().debug(" " + source);
if (getLog().isDebugEnabled()) {
getLog().debug(" " + source);
}
invokeMethod(addSourceMethod, compilationUnit, source);
}

Expand All @@ -366,9 +370,13 @@ protected Object setupCompilerConfiguration(final File compileOutputDirectory, f
Object compilerConfiguration = invokeConstructor(findConstructor(compilerConfigurationClass));
if (configScript != null) {
if (!configScript.exists()) {
getLog().warn("Configuration script file (" + configScript.getAbsolutePath() + ") doesn't exist. Ignoring configScript parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Configuration script file (" + configScript.getAbsolutePath() + ") doesn't exist. Ignoring configScript parameter.");
}
} else if (groovyOlderThan(GROOVY_2_1_0_BETA1)) {
getLog().warn("Requested to use configScript, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_2_1_0_BETA1 + " or newer). Ignoring configScript parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Requested to use configScript, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_2_1_0_BETA1 + " or newer). Ignoring configScript parameter.");
}
} else {
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
Class<?> importCustomizerClass = classWrangler.getClass("org.codehaus.groovy.control.customizers.ImportCustomizer");
Expand All @@ -382,7 +390,9 @@ protected Object setupCompilerConfiguration(final File compileOutputDirectory, f
List compilationCustomizers = (List) invokeMethod(findMethod(compilerConfigurationClass, "getCompilationCustomizers"), shellCompilerConfiguration);
compilationCustomizers.add(importCustomizer);
Object shell = invokeConstructor(findConstructor(groovyShellClass, bindingClass, compilerConfigurationClass), binding, shellCompilerConfiguration);
getLog().debug("Using configuration script " + configScript + " for compilation.");
if (getLog().isDebugEnabled()) {
getLog().debug("Using configuration script " + configScript + " for compilation.");
}
invokeMethod(findMethod(groovyShellClass, "evaluate", File.class), shell, configScript);
}
}
Expand All @@ -394,12 +404,16 @@ protected Object setupCompilerConfiguration(final File compileOutputDirectory, f
if (previewFeatures) {
if (isJavaSupportPreviewFeatures()) {
if (groovyOlderThan(GROOVY_2_5_7) || (groovyAtLeast(GROOVY_2_6_0_ALPHA1) && groovyOlderThan(GROOVY_3_0_0_BETA1))) {
getLog().warn("Requested to use preview features, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_2_5_7 + "/" + GROOVY_3_0_0_BETA1 + " or newer. No 2.6 version is supported. Ignoring previewFeatures parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Requested to use preview features, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_2_5_7 + "/" + GROOVY_3_0_0_BETA1 + " or newer. No 2.6 version is supported. Ignoring previewFeatures parameter.");
}
} else {
invokeMethod(findMethod(compilerConfigurationClass, "setPreviewFeatures", boolean.class), compilerConfiguration, previewFeatures);
}
} else {
getLog().warn("Requested to use to use preview features, but your Java version (" + getJavaVersionString() + ") doesn't support it. Ignoring previewFeatures parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Requested to use to use preview features, but your Java version (" + getJavaVersionString() + ") doesn't support it. Ignoring previewFeatures parameter.");
}
}
}
if (sourceEncoding != null) {
Expand All @@ -415,24 +429,32 @@ protected Object setupCompilerConfiguration(final File compileOutputDirectory, f
optimizationOptions.put("int", false);
getLog().info("invokedynamic enabled.");
} else {
getLog().warn("Requested to use to use invokedynamic, but your Java version (" + getJavaVersionString() + ") doesn't support it. Ignoring invokeDynamic parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Requested to use to use invokedynamic, but your Java version (" + getJavaVersionString() + ") doesn't support it. Ignoring invokeDynamic parameter.");
}
}
} else {
getLog().warn("Requested to use invokedynamic, but your Groovy version doesn't support it (must use have indy classifier). Ignoring invokeDynamic parameter.");
}
} else {
getLog().warn("Requested to use invokeDynamic, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_2_0_0_BETA3 + " or newer). Ignoring invokeDynamic parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Requested to use invokeDynamic, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_2_0_0_BETA3 + " or newer). Ignoring invokeDynamic parameter.");
}
}
}
if (parameters) {
if (groovyAtLeast(GROOVY_2_5_0_ALPHA1)) {
if (isJavaSupportParameters()) {
invokeMethod(findMethod(compilerConfigurationClass, "setParameters", boolean.class), compilerConfiguration, parameters);
} else {
getLog().warn("Requested to use to use parameters, but your Java version (" + getJavaVersionString() + ") doesn't support it. Ignoring parameters parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Requested to use to use parameters, but your Java version (" + getJavaVersionString() + ") doesn't support it. Ignoring parameters parameter.");
}
}
} else {
getLog().warn("Requested to use parameters, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_2_5_0_ALPHA1 + " or newer). Ignoring parameters parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Requested to use parameters, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_2_5_0_ALPHA1 + " or newer). Ignoring parameters parameter.");
}
}
}
if (groovyAtLeast(GROOVY_3_0_5)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,18 @@ protected void addGroovySources(final Set<File> stubSources, final Class<?> comp
for (File stubSource : stubSources) {
scriptExtensions.add(FileUtils.getFileExtension(stubSource));
}
getLog().debug("Detected Groovy file extensions: " + scriptExtensions + ".");
if (getLog().isDebugEnabled()) {
getLog().debug("Detected Groovy file extensions: " + scriptExtensions + ".");
}
if (supportsSettingExtensions()) {
invokeMethod(findMethod(compilerConfigurationClass, "setScriptExtensions", Set.class), compilerConfiguration, scriptExtensions);
}
getLog().debug("Adding Groovy to generate stubs for:");
Method addSource = findMethod(javaStubCompilationUnitClass, "addSource", File.class);
for (File stubSource : stubSources) {
getLog().debug(" " + stubSource);
if (getLog().isDebugEnabled()) {
getLog().debug(" " + stubSource);
}
if (supportsSettingExtensions()) {
invokeMethod(addSource, javaStubCompilationUnit, stubSource);
} else {
Expand All @@ -346,7 +350,9 @@ protected boolean supportsSettingExtensions() {
*/
protected void logGeneratedStubs(File outputDirectory) {
Set<File> stubs = getStubs(outputDirectory);
getLog().info("Generated " + stubs.size() + " stub" + (stubs.size() != 1 ? "s" : "") + ".");
if (getLog().isInfoEnabled()) {
getLog().info("Generated " + stubs.size() + " stub" + (stubs.size() != 1 ? "s" : "") + ".");
}
}

/**
Expand All @@ -361,7 +367,9 @@ protected void resetStubModifiedDates(final Set<File> stubs) {
for (File stub : stubs) {
boolean success = stub.setLastModified(0L);
if (!success) {
getLog().warn("Unable to set modified time on stub " + stub.getAbsolutePath() + ".");
if (getLog().isWarnEnabled()) {
getLog().warn("Unable to set modified time on stub " + stub.getAbsolutePath() + ".");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ protected synchronized void doGroovyDocGeneration(final FileSet[] sourceDirector
}
if (groovyIs(GROOVY_1_6_0_RC1) || groovyIs(GROOVY_1_5_8)) {
// Groovy 1.5.8 and 1.6-RC-1 are blacklisted because of their dependency on org.apache.tools.ant.types.Path in GroovyDocTool constructor
getLog().warn("Groovy " + GROOVY_1_5_8 + " and " + GROOVY_1_6_0_RC1 + " are blacklisted from the supported GroovyDoc versions because of their dependency on Ant. Skipping GroovyDoc generation.");
if (getLog().isWarnEnabled()) {
getLog().warn("Groovy " + GROOVY_1_5_8 + " and " + GROOVY_1_6_0_RC1 + " are blacklisted from the supported GroovyDoc versions because of their dependency on Ant. Skipping GroovyDoc generation.");
}
return;
}

Expand All @@ -296,7 +298,9 @@ protected synchronized void doGroovyDocGeneration(final FileSet[] sourceDirector
if (groovyAtLeast(GROOVY_3_0_0_ALPHA_4)) {
System.setProperty("runtimeGroovydoc", "true");
} else {
getLog().warn("Requested to enable attaching GroovyDoc annotation, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_3_0_0_ALPHA_4 + " or newer). Ignoring enableGroovyDocAnnotation parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Requested to enable attaching GroovyDoc annotation, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be " + GROOVY_3_0_0_ALPHA_4 + " or newer). Ignoring enableGroovyDocAnnotation parameter.");
}
}
}
Properties docProperties = setupProperties();
Expand All @@ -310,7 +314,9 @@ protected synchronized void doGroovyDocGeneration(final FileSet[] sourceDirector
GroovyDocTemplateInfo groovyDocTemplateInfo = new GroovyDocTemplateInfo(classWrangler.getGroovyVersion());
List<?> groovyDocLinks = setupLinks();
if (groovyOlderThan(GROOVY_1_6_0_RC2)) {
getLog().warn("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support GroovyDoc documentation properties (docTitle, footer, header, displayAuthor, overviewFile, and scope). You need Groovy 1.6-RC-2 or newer to support this. Ignoring properties.");
if (getLog().isWarnEnabled()) {
getLog().warn("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support GroovyDoc documentation properties (docTitle, footer, header, displayAuthor, overviewFile, and scope). You need Groovy 1.6-RC-2 or newer to support this. Ignoring properties.");
}
}

// prevent Java stubs (which lack Javadoc) from overwriting GroovyDoc by removing Java sources
Expand Down Expand Up @@ -353,7 +359,9 @@ protected Properties setupProperties() {
properties.setProperty("privateScope", "true");
}
} catch (IllegalArgumentException e) {
getLog().warn("Scope (" + scope + ") was not recognized. Skipping argument.");
if (getLog().isWarnEnabled()) {
getLog().warn("Scope (" + scope + ") was not recognized. Skipping argument.");
}
}

return properties;
Expand Down Expand Up @@ -392,7 +400,9 @@ protected List<?> setupLinks() throws ClassNotFoundException, InvocationTargetEx
linksList.add(linkArgument);
}
} else {
getLog().warn("Requested to use GroovyDoc links, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be 1.5.2 or newer). Ignoring links parameter.");
if (getLog().isWarnEnabled()) {
getLog().warn("Requested to use GroovyDoc links, but your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support it (must be 1.5.2 or newer). Ignoring links parameter.");
}
}
}

Expand Down Expand Up @@ -436,7 +446,9 @@ protected Object createGroovyDocTool(final Class<?> groovyDocToolClass, final Cl
groovyDocLinks
);
if (sourceDirectories.size() > 1) {
getLog().warn("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support more than one GroovyDoc source directory (must be 1.6-RC-2 or newer). Only using first source directory (" + sourceDirectories.get(0) + ").");
if (getLog().isWarnEnabled()) {
getLog().warn("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support more than one GroovyDoc source directory (must be 1.6-RC-2 or newer). Only using first source directory (" + sourceDirectories.get(0) + ").");
}
}
} else {
groovyDocTool = invokeConstructor(findConstructor(groovyDocToolClass, resourceManagerClass, String.class, String[].class, String[].class, String[].class),
Expand All @@ -447,7 +459,9 @@ protected Object createGroovyDocTool(final Class<?> groovyDocToolClass, final Cl
defaultClassTemplates == null ? groovyDocTemplateInfo.defaultClassTemplates() : defaultClassTemplates
);
if (sourceDirectories.size() > 1) {
getLog().warn("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support more than one GroovyDoc source directory (must be 1.6-RC-2 or newer). Only using first source directory (" + sourceDirectories.get(0) + ").");
if (getLog().isWarnEnabled()) {
getLog().warn("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support more than one GroovyDoc source directory (must be 1.6-RC-2 or newer). Only using first source directory (" + sourceDirectories.get(0) + ").");
}
}
}

Expand Down Expand Up @@ -500,7 +514,9 @@ protected void generateGroovyDoc(final File outputDirectory, final Class<?> groo
getLog().debug("Adding sources to generate GroovyDoc for:");
if (getLog().isDebugEnabled()) {
for (String groovyDocSource : groovyDocSources) {
getLog().debug(" " + groovyDocSource);
if (getLog().isDebugEnabled()) {
getLog().debug(" " + groovyDocSource);
}
}
}
if (groovyAtLeast(GROOVY_1_6_0_RC2)) {
Expand All @@ -520,7 +536,9 @@ protected void generateGroovyDoc(final File outputDirectory, final Class<?> groo
* @param outputDirectory The output directory to copy the stylesheet to
*/
protected void copyStylesheet(final File outputDirectory) {
getLog().info("Using stylesheet from " + stylesheetFile.getAbsolutePath() + ".");
if (getLog().isInfoEnabled()) {
getLog().info("Using stylesheet from " + stylesheetFile.getAbsolutePath() + ".");
}
try {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
Expand All @@ -547,7 +565,9 @@ protected void copyStylesheet(final File outputDirectory) {
FileUtils.closeQuietly(bufferedWriter);
}
} catch (IOException e) {
getLog().warn("Unable to copy specified stylesheet (" + stylesheetFile.getAbsolutePath() + ").");
if (getLog().isWarnEnabled()) {
getLog().warn("Unable to copy specified stylesheet (" + stylesheetFile.getAbsolutePath() + ").");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ protected void logPluginClasspath() {
sb.append(", ");
}
}
getLog().debug("Plugin classpath:\n" + sb.toString());
if (getLog().isDebugEnabled()) {
getLog().debug("Plugin classpath:\n" + sb.toString());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public void execute() {
*/
protected void addSourcePath(final String path) {
if (!project.getCompileSourceRoots().contains(path)) {
getLog().debug("Added source directory: " + path);
if (getLog().isDebugEnabled()) {
getLog().debug("Added source directory: " + path);
}
project.addCompileSourceRoot(path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public class AddStubSourcesMojo extends AbstractGroovyStubSourcesMojo {
*/
@Override
public void execute() {
getLog().debug("Added stub directory " + stubsOutputDirectory.getAbsolutePath() + " to project sources.");
if (getLog().isDebugEnabled()) {
getLog().debug("Added stub directory " + stubsOutputDirectory.getAbsolutePath() + " to project sources.");
}
project.addCompileSourceRoot(stubsOutputDirectory.getAbsolutePath());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public void execute() {
*/
protected void addTestSourcePath(final String path) {
if (!project.getTestCompileSourceRoots().contains(path)) {
getLog().debug("Added test source directory: " + path);
if (getLog().isDebugEnabled()) {
getLog().debug("Added test source directory: " + path);
}
project.addTestCompileSourceRoot(path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public class AddTestStubSourcesMojo extends AbstractGroovyStubSourcesMojo {
@Override
public void execute() {
if (!skipTests) {
getLog().debug("Added test stub directory " + testStubsOutputDirectory.getAbsolutePath() + " to project test sources.");
if (getLog().isDebugEnabled()) {
getLog().debug("Added test stub directory " + testStubsOutputDirectory.getAbsolutePath() + " to project test sources.");
}
project.addTestCompileSourceRoot(testStubsOutputDirectory.getAbsolutePath());
}
}
Expand Down
Loading

0 comments on commit 69c9dc3

Please sign in to comment.