Skip to content

Commit 7654822

Browse files
committed
Making additional compiler arguments configurable instead of enablePreview flag.
1 parent 67f2803 commit 7654822

5 files changed

Lines changed: 71 additions & 38 deletions

File tree

src/main/java/pl/joegreen/lambdaFromString/LambdaFactory.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
import pl.joegreen.lambdaFromString.classFactory.ClassCompilationException;
44
import pl.joegreen.lambdaFromString.classFactory.ClassFactory;
5-
import pl.joegreen.lambdaFromString.classFactory.JavaVersionProvider;
65

76
import javax.tools.JavaCompiler;
87
import java.lang.reflect.Method;
9-
import java.util.Collections;
108
import java.util.List;
119
import java.util.Optional;
1210

@@ -36,7 +34,7 @@ public static LambdaFactory get(LambdaFactoryConfiguration configuration) {
3634
configuration.getStaticImports(),
3735
configuration.getCompilationClassPath(),
3836
configuration.getParentClassLoader(),
39-
configuration.getEnablePreview(),
37+
configuration.getCompilerArguments(),
4038
configuration.getJavaVersion());
4139
}
4240

@@ -48,11 +46,11 @@ public static LambdaFactory get(LambdaFactoryConfiguration configuration) {
4846
private final String compilationClassPath;
4947
private final ClassLoader parentClassLoader;
5048
private final int javaVersion;
51-
private final boolean enablePreview;
49+
private final List<String> compilerArguments;
5250

5351
private LambdaFactory(HelperClassSourceProvider helperProvider, ClassFactory classFactory,
5452
JavaCompiler javaCompiler, List<String> imports, List<String> staticImports,
55-
String compilationClassPath, ClassLoader parentClassLoader, boolean enablePreview,
53+
String compilationClassPath, ClassLoader parentClassLoader, List<String> compilerArguments,
5654
int javaVersion) {
5755
this.helperProvider = helperProvider;
5856
this.classFactory = classFactory;
@@ -61,7 +59,7 @@ private LambdaFactory(HelperClassSourceProvider helperProvider, ClassFactory cla
6159
this.staticImports = staticImports;
6260
this.compilationClassPath = compilationClassPath;
6361
this.parentClassLoader = parentClassLoader;
64-
this.enablePreview = enablePreview;
62+
this.compilerArguments = compilerArguments;
6563
this.javaVersion = javaVersion;
6664
}
6765

@@ -78,9 +76,8 @@ private LambdaFactory(HelperClassSourceProvider helperProvider, ClassFactory cla
7876
public <T> T createLambda(String code, TypeReference<T> typeReference) throws LambdaCreationException {
7977
String helperClassSource = helperProvider.getHelperClassSource(typeReference.toString(), code, imports, staticImports);
8078
try {
81-
Class<?> helperClass = classFactory.createClass(helperProvider.getHelperClassName(),
82-
helperClassSource, javaCompiler,javaVersion, compilationClassPath,
83-
createAdditionalCompilerOptions(enablePreview), parentClassLoader);
79+
Class<?> helperClass = classFactory.createClass(helperProvider.getHelperClassName(), helperClassSource,
80+
javaCompiler,javaVersion, compilationClassPath, compilerArguments, parentClassLoader);
8481
Method lambdaReturningMethod = helperClass.getMethod(helperProvider.getLambdaReturningMethodName());
8582
@SuppressWarnings("unchecked")
8683
// the whole point of the class template and runtime compilation is to make this cast work well :-)
@@ -95,14 +92,6 @@ public <T> T createLambda(String code, TypeReference<T> typeReference) throws La
9592
}
9693
}
9794

98-
private List<String> createAdditionalCompilerOptions(boolean enablePreview) {
99-
if (enablePreview && JavaVersionProvider.getJavaVersion() >= 11) {
100-
return Collections.singletonList("--enable-preview");
101-
}
102-
103-
return Collections.emptyList();
104-
}
105-
10695
/**
10796
* Convenience wrapper for {@link #createLambda(String, TypeReference)}
10897
* which throws unchecked exception instead of checked one.

src/main/java/pl/joegreen/lambdaFromString/LambdaFactoryConfiguration.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class LambdaFactoryConfiguration {
2222
private String compilationClassPath;
2323
private ClassLoader parentClassLoader;
2424
private JavaCompiler javaCompiler;
25-
private boolean enablePreview;
25+
private List<String> compilerArguments;
2626
private int javaVersion;
2727

2828
public static LambdaFactoryConfiguration get() {
@@ -37,7 +37,7 @@ private LambdaFactoryConfiguration() {
3737
compilationClassPath = ClassPathExtractor.getJavaPropertyClassPath();
3838
parentClassLoader = this.getClass().getClassLoader();
3939
javaCompiler = DEFAULT_COMPILER.orElse(null);
40-
enablePreview = false;
40+
compilerArguments = Collections.unmodifiableList(new ArrayList<>());
4141
javaVersion = getJavaVersionSafe();
4242
}
4343

@@ -58,7 +58,7 @@ private LambdaFactoryConfiguration copy() {
5858
.setCompilationClassPath(compilationClassPath)
5959
.setParentClassLoader(parentClassLoader)
6060
.setJavaCompiler(javaCompiler)
61-
.setEnablePreview(enablePreview)
61+
.setCompilerArguments(compilerArguments)
6262
.setJavaVersion(javaVersion);
6363
}
6464

@@ -91,8 +91,8 @@ public JavaCompiler getJavaCompiler() {
9191
return javaCompiler;
9292
}
9393

94-
public boolean getEnablePreview() {
95-
return enablePreview;
94+
public List<String> getCompilerArguments() {
95+
return compilerArguments;
9696
}
9797

9898
public int getJavaVersion() {
@@ -171,8 +171,12 @@ public LambdaFactoryConfiguration withJavaCompiler(JavaCompiler javaCompiler) {
171171
return copy().setJavaCompiler(javaCompiler);
172172
}
173173

174-
public LambdaFactoryConfiguration withEnablePreview(boolean enablePreview) {
175-
return copy().setEnablePreview(enablePreview);
174+
/**
175+
* Makes it possible to provide additional compiler arguments.
176+
* By default '-source', '-target' and '-classpath' arguments are added.
177+
*/
178+
public LambdaFactoryConfiguration withCompilerArguments(String... newCompilerArguments) {
179+
return copy().setCompilerArguments(listWithNewElements(compilerArguments, newCompilerArguments));
176180
}
177181

178182
/**
@@ -218,8 +222,8 @@ private LambdaFactoryConfiguration setJavaCompiler(JavaCompiler javaCompiler) {
218222
return this;
219223
}
220224

221-
private LambdaFactoryConfiguration setEnablePreview(boolean enablePreview) {
222-
this.enablePreview = enablePreview;
225+
private LambdaFactoryConfiguration setCompilerArguments(List<String> compilerArguments) {
226+
this.compilerArguments = compilerArguments;
223227
return this;
224228
}
225229

@@ -237,20 +241,19 @@ public boolean equals(Object o) {
237241
if (this == o) return true;
238242
if (o == null || getClass() != o.getClass()) return false;
239243
LambdaFactoryConfiguration that = (LambdaFactoryConfiguration) o;
240-
return enablePreview == that.enablePreview &&
241-
javaVersion == that.javaVersion &&
244+
return javaVersion == that.javaVersion &&
242245
Objects.equals(helperClassSourceProvider, that.helperClassSourceProvider) &&
243246
Objects.equals(classFactory, that.classFactory) &&
244247
Objects.equals(staticImports, that.staticImports) &&
245248
Objects.equals(imports, that.imports) &&
246249
Objects.equals(compilationClassPath, that.compilationClassPath) &&
247250
Objects.equals(parentClassLoader, that.parentClassLoader) &&
248-
Objects.equals(javaCompiler, that.javaCompiler);
251+
Objects.equals(javaCompiler, that.javaCompiler) &&
252+
Objects.equals(compilerArguments, that.compilerArguments);
249253
}
250254

251255
@Override
252256
public int hashCode() {
253-
return Objects.hash(helperClassSourceProvider, classFactory, staticImports, imports, compilationClassPath,
254-
parentClassLoader, javaCompiler, enablePreview, javaVersion);
257+
return Objects.hash(helperClassSourceProvider, classFactory, staticImports, imports, compilationClassPath, parentClassLoader, javaCompiler, compilerArguments, javaVersion);
255258
}
256259
}

src/test/java/pl/joegreen/lambdaFromString/LambdaFactoryConfigurationTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public void addingImportsAndStaticImportsWithMultipleSteps() {
2929
assertEquals(staticImports, conf.getStaticImports());
3030
}
3131

32+
@Test
33+
public void addingCompilerArgumentsWithMultipleSteps() {
34+
List<String> compilerArguments = Arrays.asList("-a1", "--a2", "---a3");
35+
36+
LambdaFactoryConfiguration conf = LambdaFactoryConfiguration.get()
37+
.withCompilerArguments(compilerArguments.get(0))
38+
.withCompilerArguments(compilerArguments.get(1), compilerArguments.get(2));
39+
assertEquals(compilerArguments, conf.getCompilerArguments());
40+
}
41+
3242
@Test
3343
public void addingImportsCreatesNewConfiguration() {
3444
LambdaFactoryConfiguration defaultConf = LambdaFactoryConfiguration.get();
@@ -44,6 +54,7 @@ public void usingWithSetsParameters() {
4454
ClassFactory classFactory = new DefaultClassFactory();
4555
String[] staticImports = new String[] { "si1", "si2" };
4656
String[] imports = new String[] { "i1", "i2" };
57+
String[] compilerArguments = new String[] { "-a1", "--a2" };
4758
String compilationClassPath = "compilationClassPath";
4859
ClassLoader parentClassLoader = new URLClassLoader(new URL[] {});
4960
JavaCompiler javaCompiler = new EclipseCompiler();
@@ -56,7 +67,7 @@ public void usingWithSetsParameters() {
5667
.withCompilationClassPath(compilationClassPath)
5768
.withParentClassLoader(parentClassLoader)
5869
.withJavaCompiler(javaCompiler)
59-
.withEnablePreview(true)
70+
.withCompilerArguments(compilerArguments)
6071
.withJavaVersion(17);
6172

6273
assertSame(helper, changedConfiguration.getDefaultHelperClassSourceProvider());
@@ -66,7 +77,7 @@ public void usingWithSetsParameters() {
6677
assertEquals(compilationClassPath, changedConfiguration.getCompilationClassPath());
6778
assertSame(parentClassLoader, changedConfiguration.getParentClassLoader());
6879
assertSame(javaCompiler, changedConfiguration.getJavaCompiler());
69-
assertTrue(changedConfiguration.getEnablePreview());
80+
assertEquals(Arrays.asList(compilerArguments), changedConfiguration.getCompilerArguments());
7081
assertEquals(17, changedConfiguration.getJavaVersion());
7182
}
7283

src/test/java/pl/joegreen/lambdaFromString/LambdaFactoryTest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.jupiter.params.provider.Arguments;
66
import org.junit.jupiter.params.provider.MethodSource;
77
import pl.joegreen.lambdaFromString.classFactory.JavaVersionProvider;
8+
import pl.joegreen.lambdaFromString.dummy.ClassWithDeprecatedMethod;
89
import pl.joegreen.lambdaFromString.dummy.CustomInterface;
910
import pl.joegreen.lambdaFromString.dummy.CustomInterfaceUsingInnerClass;
1011

@@ -33,11 +34,15 @@ static Stream<Arguments> jdkAndEclipse() {
3334
int javaVersion = JavaVersionProvider.getJavaVersion();
3435

3536
if (javaVersion <= 8) {
36-
return Stream.of(Arguments.of(JavaCompilerProvider.getJdkJavaCompiler().get()),
37+
return Stream.of(Arguments.of(getJdkCompiler()),
3738
Arguments.of(new EclipseCompiler()));
3839
}
3940

40-
return Stream.of(Arguments.of(JavaCompilerProvider.getJdkJavaCompiler().get()));
41+
return Stream.of(Arguments.of(getJdkCompiler()));
42+
}
43+
44+
private static JavaCompiler getJdkCompiler() {
45+
return JavaCompilerProvider.getJdkJavaCompiler().get();
4146
}
4247

4348
@ParameterizedTest
@@ -134,7 +139,7 @@ void lambdaCreatingMapEntry(JavaCompiler jc) {
134139
void lambdaCreatingComplicatedGenericType(JavaCompiler jc) {
135140
String code = "() -> ( x -> new ArrayList<>())";
136141
LambdaFactory factory = LambdaFactory.get(LambdaFactoryConfiguration.get()
137-
.withImports(SimpleEntry.class, ArrayList.class).withJavaCompiler(jc).withEnablePreview(true));
142+
.withImports(SimpleEntry.class, ArrayList.class).withJavaCompiler(jc));
138143
Supplier<Function<CustomInterfaceUsingInnerClass.InnerClass[], List<SimpleEntry<?, ?>>>> lambda =
139144
factory.createLambdaUnchecked(code, new TypeReference<Supplier<Function<CustomInterfaceUsingInnerClass.InnerClass[], List<SimpleEntry<?, ?>>>>>() {});
140145
Function<CustomInterfaceUsingInnerClass.InnerClass[], List<SimpleEntry<?, ?>>> function = lambda.get();
@@ -163,7 +168,7 @@ void integerMultiplyWithDynamicTypeReference(JavaCompiler jc) {
163168
@MethodSource("jdkAndEclipse")
164169
void lambdaImplementingNonStandardInterface(JavaCompiler jc) {
165170
LambdaFactory factory = LambdaFactory.get(LambdaFactoryConfiguration.get().withImports(CustomInterface.class)
166-
.withJavaCompiler(jc).withEnablePreview(true));
171+
.withJavaCompiler(jc));
167172
String code = " x -> 10";
168173
CustomInterface lambda = factory.createLambdaUnchecked(code, new TypeReference<CustomInterface>() {});
169174
assertEquals(lambda.customFunction("abc"), 10);
@@ -212,7 +217,7 @@ void lambdaImplementingBinaryClassFileOnCustomClassPathContainingSpace(JavaCompi
212217
void lambdaImplementingNonStandardInterfaceUsingInnerClass(JavaCompiler jc) {
213218
LambdaFactory factory = LambdaFactory.get(LambdaFactoryConfiguration.get()
214219
.withImports(CustomInterfaceUsingInnerClass.class, CustomInterfaceUsingInnerClass.InnerClass.class)
215-
.withJavaCompiler(jc).withEnablePreview(true));
220+
.withJavaCompiler(jc));
216221
String code = " () -> new InnerClass()";
217222
CustomInterfaceUsingInnerClass lambda = factory.createLambdaUnchecked(code, new TypeReference<CustomInterfaceUsingInnerClass>() {});
218223
assertEquals(CustomInterfaceUsingInnerClass.InnerClass.class, lambda.createInnerClass().getClass());
@@ -257,4 +262,22 @@ void incorrectJavaVersionFailsCompilation(JavaCompiler jc) {
257262
() -> factory.createLambdaUnchecked("i -> i+1", new TypeReference<Function<Integer, Integer>>() {}));
258263
assertTrue(ex.getNestedCheckedException().getMessage().contains("115"));
259264
}
265+
266+
@ParameterizedTest
267+
@MethodSource("jdkAndEclipse")
268+
void shouldPassAdditionalArgumentToCompiler(JavaCompiler jc) {
269+
String[] deprecationWarningAsErrorArgs;
270+
if (jc instanceof EclipseCompiler) {
271+
deprecationWarningAsErrorArgs = new String[]{"-err:deprecation"};
272+
} else {
273+
deprecationWarningAsErrorArgs = new String[]{"-Xlint:deprecation", "-Werror"};
274+
}
275+
LambdaFactory factory = LambdaFactory.get(LambdaFactoryConfiguration.get()
276+
.withJavaCompiler(jc)
277+
.withImports(ClassWithDeprecatedMethod.class)
278+
.withCompilerArguments(deprecationWarningAsErrorArgs));
279+
LambdaCreationRuntimeException ex = assertThrows(LambdaCreationRuntimeException.class, () -> factory.createLambdaUnchecked(
280+
"i -> ClassWithDeprecatedMethod.deprecatedMethod()", new TypeReference<Function<Integer, Integer>>() {}));
281+
assertTrue(ex.getNestedCheckedException().getMessage().contains("deprecated"));
282+
}
260283
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pl.joegreen.lambdaFromString.dummy;
2+
3+
public class ClassWithDeprecatedMethod {
4+
@Deprecated public static int deprecatedMethod(){
5+
return 5;
6+
}
7+
}

0 commit comments

Comments
 (0)