Skip to content

Commit

Permalink
Fix for #1022: normalize defaults in compiler config
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 16, 2020
1 parent 52a0e33 commit 337fd8c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import static org.junit.Assume.assumeTrue;

import java.io.File;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.jdt.groovy.internal.compiler.ast.EventListener;
import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyClassScope;
import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration;
Expand Down Expand Up @@ -3243,6 +3245,62 @@ public void testImplementingInterface_MethodWithParameters3_JextendsG() {
runConformTest(sources, "success");
}

@Test
public void testCallingJavaFromGroovy1() {
//@formatter:off
String[] sources = {
"p/Main.groovy",
"package p\n" +
"class Main {\n" +
" static main(args) {\n" +
" new J().run()\n" +
" print new J().name\n" +
" }\n" +
"}\n",

"p/J.java",
"package p;\n" +
"public class J {\n" +
" public String name = \"name\";\n" +
" public void run() { System.out.print(\"success\"); }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "successname");
}

@Test
public void testCallingJavaFromGroovy2() {
//@formatter:off
String[] sources = {
"p/Main.groovy",
"package p\n" +
"@Tag(value=4)\n" +
"class Main {\n" +
" static main(args) {\n" +
" new J().run()\n" +
" }\n" +
"}\n",

"p/J.java",
"package p;\n" +
"public class J {\n" +
" public String name = \"name\";\n" +
" public void run() { System.out.print(\"success\"); }\n" +
"}\n",

"p/Tag.java",
"package p;\n" +
"public @interface Tag {\n" +
" int value() default 3;\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "success");
}

@Test
public void testCallingMethods_JcallingG() {
//@formatter:off
Expand Down Expand Up @@ -4270,62 +4328,6 @@ public void testClashingMethodsWithDefaultParams() {
"----------\n");
}

@Test
public void testCallingJavaFromGroovy1() throws Exception {
//@formatter:off
String[] sources = {
"p/Code.groovy",
"package p;\n" +
"class Code {\n" +
" public static void main(String[] argv) {\n" +
" new J().run();\n" +
" print new J().name;\n" +
" }\n" +
"}\n",

"p/J.java",
"package p;\n" +
"public class J {\n" +
" public String name = \"name\";\n" +
" public void run() { System.out.print(\"success\"); }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "successname");
}

@Test
public void testCallingJavaFromGroovy2() throws Exception {
//@formatter:off
String[] sources = {
"p/Code.groovy",
"package p;\n" +
"@Wibble(value=4)\n" +
"class Code {\n" +
" public static void main(String[] argv) {\n" +
" new J().run();\n" +
" }\n" +
"}\n",

"p/J.java",
"package p;\n" +
"public class J {\n" +
" public String name = \"name\";\n" +
" public void run() { System.out.print(\"success\"); }\n" +
"}\n",

"p/Wibble.java",
"package p;\n" +
"public @interface Wibble {\n" +
" int value() default 3;\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "success");
}

@Test
public void testTypeVariableBoundIsRawType() {
//@formatter:off
Expand Down Expand Up @@ -4609,6 +4611,16 @@ public void testScriptCallJava() {
runConformTest(sources, "abc");
}

@Test
public void testConfigDefaults() {
CompilerConfiguration cc = CompilerConfiguration.DEFAULT;
assertEquals(Collections.emptyList(), cc.getClasspath());
assertEquals(Collections.emptyList(), cc.getCompilationCustomizers());
assertEquals(Collections.emptyMap(), cc.getJointCompilationOptions());
assertEquals(Collections.singleton("groovy"), cc.getScriptExtensions());
assertEquals(Collections.emptySet(), cc.getDisabledGlobalASTTransformations());
}

@Test
public void testConfigScriptWithError() {
Map<String, String> options = getCompilerOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,27 @@ public class CompilerConfiguration {
public static final CompilerConfiguration DEFAULT = new CompilerConfiguration() {
@Override
public List<String> getClasspath() {
return Optional.ofNullable(super.getClasspath()).map(Collections::unmodifiableList).orElse(null);
return Collections.unmodifiableList(super.getClasspath());
}
@Override
public List<CompilationCustomizer> getCompilationCustomizers() {
return Collections.unmodifiableList(super.getCompilationCustomizers());
}
@Override
public Set<String> getDisabledGlobalASTTransformations() {
return Optional.ofNullable(super.getDisabledGlobalASTTransformations()).map(Collections::unmodifiableSet).orElse(null);
return Optional.ofNullable(super.getDisabledGlobalASTTransformations()).map(Collections::unmodifiableSet).orElse(Collections.emptySet());
}
@Override
public Map<String, Object> getJointCompilationOptions() {
return Optional.ofNullable(super.getJointCompilationOptions()).map(Collections::unmodifiableMap).orElse(null);
return Optional.ofNullable(super.getJointCompilationOptions()).map(Collections::unmodifiableMap).orElse(Collections.emptyMap());
}
@Override
public Map<String, Boolean> getOptimizationOptions() {
return Optional.ofNullable(super.getOptimizationOptions()).map(Collections::unmodifiableMap).orElse(null);
return Collections.unmodifiableMap(super.getOptimizationOptions());
}
@Override
public Set<String> getScriptExtensions() {
return Optional.ofNullable(super.getScriptExtensions()).map(Collections::unmodifiableSet).orElse(null);
return Collections.unmodifiableSet(super.getScriptExtensions());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public List<CompilationCustomizer> getCompilationCustomizers() {

@Override
public Set<String> getDisabledGlobalASTTransformations() {
return Collections.emptySet();
return Optional.ofNullable(super.getDisabledGlobalASTTransformations()).map(Collections::unmodifiableSet).orElse(Collections.emptySet());
}

@Override
public Map<String, Object> getJointCompilationOptions() {
return Collections.unmodifiableMap(super.getJointCompilationOptions());
return Optional.ofNullable(super.getJointCompilationOptions()).map(Collections::unmodifiableMap).orElse(Collections.emptyMap());
}

@Override
Expand Down

0 comments on commit 337fd8c

Please sign in to comment.