Skip to content

Commit feb6dd2

Browse files
committed
GROOVY-11666: add test case
1 parent d2249c6 commit feb6dd2

File tree

5 files changed

+177
-90
lines changed

5 files changed

+177
-90
lines changed

subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovyc.java

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@
176176
*/
177177
public class Groovyc extends MatchingTask {
178178

179-
private static final File[] EMPTY_FILE_ARRAY = new File[0];
180-
private static final String[] EMPTY_STRING_ARRAY = new String[0];
179+
private static final File[] EMPTY_FILE_ARRAY = {};
180+
private static final String[] EMPTY_STRING_ARRAY = {};
181181

182182
private final LoggingHelper log = new LoggingHelper(this);
183183

@@ -306,12 +306,7 @@ public String getScriptExtension() {
306306
* @param version the bytecode compatibility level
307307
*/
308308
public void setTargetBytecode(String version) {
309-
for (String allowedJdk : CompilerConfiguration.ALLOWED_JDKS) {
310-
if (allowedJdk.equals(version)) {
311-
this.targetBytecode = version;
312-
break;
313-
}
314-
}
309+
targetBytecode = Arrays.asList(CompilerConfiguration.ALLOWED_JDKS).contains(version) ? version : null;
315310
}
316311

317312
/**
@@ -320,7 +315,7 @@ public void setTargetBytecode(String version) {
320315
* @return bytecode compatibility level. Can be one of the values in {@link CompilerConfiguration#ALLOWED_JDKS}.
321316
*/
322317
public String getTargetBytecode() {
323-
return this.targetBytecode;
318+
return targetBytecode;
324319
}
325320

326321
/**
@@ -885,9 +880,8 @@ public void execute() throws BuildException {
885880
}
886881

887882
compile();
888-
if (updatedProperty != null
889-
&& taskSuccess
890-
&& compileList.length != 0) {
883+
884+
if (taskSuccess && compileList.length > 0 && updatedProperty != null) {
891885
getProject().setNewProperty(updatedProperty, "true");
892886
}
893887
}
@@ -909,21 +903,17 @@ protected void resetFileLists() {
909903
* @param files An array of filenames
910904
*/
911905
protected void scanDir(File srcDir, File destDir, String[] files) {
912-
GlobPatternMapper m = new GlobPatternMapper();
906+
GlobPatternMapper gpm = new GlobPatternMapper();
913907
SourceFileScanner sfs = new SourceFileScanner(this);
914-
File[] newFiles;
915908
for (String extension : getScriptExtensions()) {
916-
m.setFrom("*." + extension);
917-
m.setTo("*.class");
918-
newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
919-
addToCompileList(newFiles);
909+
gpm.setFrom("*." + extension);
910+
gpm.setTo("*.class");
911+
addToCompileList(sfs.restrictAsFiles(files, srcDir, destDir, gpm));
920912
}
921-
922913
if (jointCompilation) {
923-
m.setFrom("*.java");
924-
m.setTo("*.class");
925-
newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
926-
addToCompileList(newFiles);
914+
gpm.setFrom("*.java");
915+
gpm.setTo("*.class");
916+
addToCompileList(sfs.restrictAsFiles(files, srcDir, destDir, gpm));
927917
}
928918
}
929919

@@ -942,26 +932,20 @@ protected void addToCompileList(File[] newFiles) {
942932
* @return the list of files as an array
943933
*/
944934
public File[] getFileList() {
945-
return Arrays.copyOf(compileList, compileList.length);
935+
return compileList.clone();
946936
}
947937

948938
protected void checkParameters() throws BuildException {
949-
if (src == null) {
950-
throw new BuildException("srcdir attribute must be set!", getLocation());
951-
}
952-
if (src.size() == 0) {
939+
if (src == null || src.isEmpty()) {
953940
throw new BuildException("srcdir attribute must be set!", getLocation());
954941
}
955942

956943
if (destDir != null && !destDir.isDirectory()) {
957-
throw new BuildException("destination directory \""
958-
+ destDir
959-
+ "\" does not exist or is not a directory",
960-
getLocation());
944+
throw new BuildException("destination directory \"" + destDir + "\" does not exist or is not a directory", getLocation());
961945
}
962946

963947
if (encoding != null && !Charset.isSupported(encoding)) {
964-
throw new BuildException("encoding \"" + encoding + "\" not supported.");
948+
throw new BuildException("encoding \"" + encoding + "\" not supported.", getLocation());
965949
}
966950
}
967951

@@ -1028,14 +1012,10 @@ private List<String> extractJointOptions(Path classpath) {
10281012
|| key.equals("release")
10291013
|| key.equals("source")
10301014
|| key.equals("target")) {
1031-
switch (key) {
1032-
case "nativeheaderdir":
1033-
key = "h";
1034-
break;
1035-
case "release":
1036-
key = "-" + key; // to get "--" when passed to javac
1037-
break;
1038-
default:
1015+
if (key.equals("nativeheaderdir")) {
1016+
key = "h";
1017+
} else if (key.equals("release")) {
1018+
key = "-" + key; // to get "--" when passed to javac
10391019
}
10401020
// map "depend", "encoding", etc. to "-Jkey=val"
10411021
jointOptions.add("-J" + key + "=" + getProject().replaceProperties(e.getValue().toString()));
@@ -1129,7 +1109,7 @@ private void doForkCommandLineList(List<String> commandLineList, Path classpath,
11291109
if (targetBytecode != null) {
11301110
commandLineList.add("-Dgroovy.target.bytecode=" + targetBytecode);
11311111
}
1132-
if (!"*.groovy".equals(getScriptExtension())) {
1112+
if (!getScriptExtension().equals("*.groovy")) {
11331113
String tmpExtension = getScriptExtension();
11341114
if (tmpExtension.startsWith("*."))
11351115
tmpExtension = tmpExtension.substring(1);
@@ -1182,7 +1162,7 @@ private static URI getLocation(Class<?> clazz) {
11821162
* @param classpath
11831163
*/
11841164
private void doNormalCommandLineList(List<String> commandLineList, List<String> jointOptions, Path classpath) {
1185-
if (!fork) {
1165+
if (!fork && !classpath.isEmpty()) {
11861166
commandLineList.add("--classpath");
11871167
commandLineList.add(classpath.toString());
11881168
}
@@ -1291,8 +1271,7 @@ private void runCompiler(String[] commandLine) {
12911271
// hand crank it so we can add our own compiler configuration
12921272
try {
12931273
FileSystemCompiler.CompilationOptions options = new FileSystemCompiler.CompilationOptions();
1294-
CommandLine parser = FileSystemCompiler.configureParser(options);
1295-
parser.parseArgs(commandLine);
1274+
FileSystemCompiler.configureParser(options).parseArgs(commandLine);
12961275
configuration = options.toCompilerConfiguration();
12971276
configuration.setScriptExtensions(getScriptExtensions());
12981277
String tmpExtension = getScriptExtension();
@@ -1303,7 +1282,7 @@ private void runCompiler(String[] commandLine) {
13031282
configuration.setTargetBytecode(targetBytecode);
13041283
}
13051284

1306-
// Load the file name list
1285+
// load the file name list
13071286
String[] fileNames = options.generateFileNames();
13081287
boolean fileNameErrors = (fileNames == null || !FileSystemCompiler.validateFiles(fileNames));
13091288
if (!fileNameErrors) {
@@ -1410,7 +1389,7 @@ protected GroovyClassLoader buildClassLoaderFor() {
14101389

14111390
ClassLoader loader = getClass().getClassLoader();
14121391
if (loader instanceof AntClassLoader) {
1413-
AntClassLoader antLoader = (AntClassLoader) loader;
1392+
@SuppressWarnings("resource") AntClassLoader antLoader = (AntClassLoader) loader;
14141393
String[] pathElm = antLoader.getClasspath().split(File.pathSeparator, -1);
14151394
List<String> classpath = configuration.getClasspath();
14161395
/*
@@ -1441,7 +1420,7 @@ protected GroovyClassLoader buildClassLoaderFor() {
14411420
}
14421421
}
14431422

1444-
@SuppressWarnings("removal") // TODO a future Groovy version should perform the operation not as a privileged action
1423+
@SuppressWarnings("removal") // TODO: a future Groovy version should perform the operation not as a privileged action
14451424
GroovyClassLoader groovyLoader = java.security.AccessController.doPrivileged((PrivilegedAction<GroovyClassLoader>) () ->
14461425
new GroovyClassLoader(loader, configuration));
14471426

0 commit comments

Comments
 (0)