Skip to content

Commit

Permalink
Merge pull request #4397 from benkard/mulk/feature/devmode-compiler-f…
Browse files Browse the repository at this point in the history
…lags

Maven plugin: Support <compilerArgs/>, <source/>, <target/> in dev mode
  • Loading branch information
geoand authored Oct 14, 2019
2 parents 9dcaa18 + ee5f273 commit 085338d
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 5 deletions.
17 changes: 17 additions & 0 deletions core/devmode/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager-embedded</artifactId>
</dependency>

<!-- test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ public ClassLoaderCompiler(ClassLoader classLoader,
new File(i.getProjectDirectory()),
new File(sourcePath),
new File(i.getClassesPath()),
context.getSourceEncoding()));
context.getSourceEncoding(),
context.getCompilerOptions(),
context.getSourceJavaVersion(),
context.getTargetJvmVersion()));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public interface CompilationProvider {
Expand All @@ -27,21 +29,30 @@ class Context {
private final File sourceDirectory;
private final File outputDirectory;
private final Charset sourceEncoding;
private final List<String> compilerOptions;
private final String sourceJavaVersion;
private final String targetJvmVersion;

public Context(
String name,
Set<File> classpath,
File projectDirectory,
File sourceDirectory,
File outputDirectory,
String sourceEncoding) {
String sourceEncoding,
List<String> compilerOptions,
String sourceJavaVersion,
String targetJvmVersion) {

this.name = name;
this.classpath = classpath;
this.projectDirectory = projectDirectory;
this.sourceDirectory = sourceDirectory;
this.outputDirectory = outputDirectory;
this.sourceEncoding = sourceEncoding == null ? StandardCharsets.UTF_8 : Charset.forName(sourceEncoding);
this.compilerOptions = compilerOptions == null ? new ArrayList<String>() : compilerOptions;
this.sourceJavaVersion = sourceJavaVersion;
this.targetJvmVersion = targetJvmVersion;
}

public String getName() {
Expand All @@ -67,5 +78,17 @@ public File getOutputDirectory() {
public Charset getSourceEncoding() {
return sourceEncoding;
}

public List<String> getCompilerOptions() {
return compilerOptions;
}

public String getSourceJavaVersion() {
return sourceJavaVersion;
}

public String getTargetJvmVersion() {
return targetJvmVersion;
}
}
}
75 changes: 75 additions & 0 deletions core/devmode/src/main/java/io/quarkus/dev/CompilerFlags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.quarkus.dev;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import io.quarkus.runtime.util.StringUtil;

/**
* A set of compiler flags for javac.
*
* Can combine system-provided default flags with user-supplied flags and <code>-source</code>
* and <code>-target</code> settings.
*/
public class CompilerFlags {

private final Set<String> defaultFlags;
private final List<String> userFlags;
private final String sourceJavaVersion; //can be null
private final String targetJavaVersion; //can be null

public CompilerFlags(
Set<String> defaultFlags,
Collection<String> userFlags,
String sourceJavaVersion,
String targetJavaVersion) {

this.defaultFlags = defaultFlags == null ? new HashSet<>() : new HashSet<>(defaultFlags);
this.userFlags = userFlags == null ? new ArrayList<>() : new ArrayList<>(userFlags);
this.sourceJavaVersion = sourceJavaVersion;
this.targetJavaVersion = targetJavaVersion;
}

public List<String> toList() {
List<String> flagList = new ArrayList<>();

// The set of effective default flags is the set of default flags except the ones also
// set by the user. This ensures that we do not needlessly pass the default flags twice.
Set<String> effectiveDefaultFlags = new HashSet<>(this.defaultFlags);
effectiveDefaultFlags.removeAll(userFlags);

flagList.addAll(effectiveDefaultFlags);

// Add -source and -target flags.
if (sourceJavaVersion != null) {
flagList.add("-source");
flagList.add(sourceJavaVersion);
}
if (targetJavaVersion != null) {
flagList.add("-target");
flagList.add(targetJavaVersion);
}

flagList.addAll(userFlags);

return flagList;
}

@Override
public int hashCode() {
return toList().hashCode();
}

@Override
public boolean equals(Object obj) {
return obj instanceof CompilerFlags && toList().equals(((CompilerFlags) obj).toList());
}

@Override
public String toString() {
return "CompilerFlags@{" + StringUtil.join(", ", toList().iterator()) + "}";
}
}
28 changes: 28 additions & 0 deletions core/devmode/src/main/java/io/quarkus/dev/DevModeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class DevModeContext implements Serializable {
private boolean test;
private boolean abortOnFailedStart;

private List<String> compilerOptions;
private String sourceJavaVersion;
private String targetJvmVersion;

public List<URL> getClassPath() {
return classPath;
}
Expand Down Expand Up @@ -90,6 +94,30 @@ public void setAbortOnFailedStart(boolean abortOnFailedStart) {
this.abortOnFailedStart = abortOnFailedStart;
}

public List<String> getCompilerOptions() {
return compilerOptions;
}

public void setCompilerOptions(List<String> compilerOptions) {
this.compilerOptions = compilerOptions;
}

public String getSourceJavaVersion() {
return sourceJavaVersion;
}

public void setSourceJavaVersion(String sourceJavaVersion) {
this.sourceJavaVersion = sourceJavaVersion;
}

public String getTargetJvmVersion() {
return targetJvmVersion;
}

public void setTargetJvmVersion(String targetJvmVersion) {
this.targetJvmVersion = targetJvmVersion;
}

public static class ModuleInfo implements Serializable {

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

import javax.tools.Diagnostic;
Expand All @@ -28,7 +28,7 @@ public class JavaCompilationProvider implements CompilationProvider {
// -g is used to make the java compiler generate all debugging info
// -parameters is used to generate metadata for reflection on method parameters
// this is useful when people using debuggers against their hot-reloaded app
private static final List<String> COMPILER_OPTIONS = Arrays.asList("-g", "-parameters");
private static final Set<String> COMPILER_OPTIONS = new HashSet<>(Arrays.asList("-g", "-parameters"));

@Override
public Set<String> handledExtensions() {
Expand All @@ -48,9 +48,12 @@ public void compile(Set<File> filesToCompile, Context context) {
fileManager.setLocation(StandardLocation.CLASS_PATH, context.getClasspath());
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(context.getOutputDirectory()));

CompilerFlags compilerFlags = new CompilerFlags(COMPILER_OPTIONS, context.getCompilerOptions(),
context.getSourceJavaVersion(), context.getTargetJvmVersion());

Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjectsFromFiles(filesToCompile);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics,
COMPILER_OPTIONS, null, sources);
compilerFlags.toList(), null, sources);

if (!task.call()) {
throw new RuntimeException("Compilation failed" + diagnostics.getDiagnostics());
Expand Down
91 changes: 91 additions & 0 deletions core/devmode/src/test/java/io/quarkus/dev/CompilerFlagsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.quarkus.dev;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.Test;

public class CompilerFlagsTest {

@Test
void nullHandling() {
assertAll(
() -> assertEquals(
new CompilerFlags(null, null, null, null),
new CompilerFlags(setOf(), listOf(), null, null)));
}

@Test
void defaulting() {
assertAll(
() -> assertEquals(
new CompilerFlags(setOf("-a", "-b"), listOf(), null, null),
new CompilerFlags(setOf(), listOf("-a", "-b"), null, null)),
() -> assertEquals(
new CompilerFlags(setOf("-a", "-b"), listOf("-c", "-d"), null, null),
new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null)));
}

@Test
void redundancyReduction() {
assertAll(
() -> assertEquals(
new CompilerFlags(setOf("-a", "-b"), listOf(), null, null),
new CompilerFlags(setOf(), listOf("-a", "-b"), null, null)),
() -> assertEquals(
new CompilerFlags(setOf("-a", "-b", "-c"), listOf("-a", "-b"), null, null),
new CompilerFlags(setOf("-c"), listOf("-a", "-b"), null, null)));
}

@Test
void sourceAndTarget() {
assertAll(
() -> assertEquals(
new CompilerFlags(setOf(), listOf(), "1", null),
new CompilerFlags(setOf(), listOf("-source", "1"), null, null)),
() -> assertEquals(
new CompilerFlags(setOf(), listOf(), null, "2"),
new CompilerFlags(setOf(), listOf("-target", "2"), null, null)),
() -> assertEquals(
new CompilerFlags(setOf(), listOf(), "1", "2"),
new CompilerFlags(setOf(), listOf("-source", "1", "-target", "2"), null, null)),
() -> assertEquals(
new CompilerFlags(setOf(), listOf("-source", "3", "-target", "4"), "1", "2"),
new CompilerFlags(setOf(), listOf("-source", "1", "-target", "2", "-source", "3", "-target", "4"), null,
null)));
}

@Test
void allFeatures() {
assertAll(
() -> assertEquals(
new CompilerFlags(setOf("-b", "-c", "-d"), listOf("-a", "-b", "-c"), "1", "2"),
new CompilerFlags(setOf(), listOf("-d", "-source", "1", "-target", "2", "-a", "-b", "-c"), null,
null)));
}

@Test
void listConversion() {
assertAll(
() -> assertEquals(
new CompilerFlags(null, null, null, null).toList(),
listOf()),
() -> assertEquals(
new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null).toList(),
listOf("-a", "-b", "-c", "-d")));
}

private List<String> listOf(String... strings) {
return Arrays.asList(strings);
}

private Set<String> setOf(String... strings) {
return new HashSet<>(Arrays.asList(strings));
}

}
Loading

0 comments on commit 085338d

Please sign in to comment.