Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an exception on redundant options #401

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions src/main/java/com/google/testing/compile/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.processing.Processor;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
Expand All @@ -53,6 +55,17 @@
@SuppressWarnings("JavaLangClash")
public abstract class Compiler {

private static final Set<String> REDUNDANT_OPTIONS = ImmutableSet.of(
"--add-exports",
"--add-opens",
"--add-reads"
);

@VisibleForTesting
static final String REDUNDANT_OPTIONS_ERROR_TEMPLATE =
"Following options were passed to the compiler: %s."
+ "These options will have no effect and should instead be passed as VM options.";

/** Returns the {@code javac} compiler. */
public static Compiler javac() {
return compiler(getSystemJavaCompiler());
Expand All @@ -61,7 +74,8 @@ public static Compiler javac() {
/** Returns a {@link Compiler} that uses a given {@link JavaCompiler} instance. */
public static Compiler compiler(JavaCompiler javaCompiler) {
return new AutoValue_Compiler(
javaCompiler, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty());
javaCompiler, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(),
false);
}

abstract JavaCompiler javaCompiler();
Expand All @@ -80,6 +94,12 @@ public static Compiler compiler(JavaCompiler javaCompiler) {
*/
public abstract Optional<ImmutableList<File>> annotationProcessorPath();

/**
* Whether to ignore redundant options check.
* @see #REDUNDANT_OPTIONS
*/
public abstract boolean shouldIgnoreRedundantOptionsCheck();

/**
* Uses annotation processors during compilation. These replace any previously specified.
*
Expand All @@ -100,7 +120,11 @@ public final Compiler withProcessors(Processor... processors) {
*/
public final Compiler withProcessors(Iterable<? extends Processor> processors) {
return copy(
ImmutableList.copyOf(processors), options(), classPath(), annotationProcessorPath());
ImmutableList.copyOf(processors),
options(),
classPath(),
annotationProcessorPath(),
false);
}

/**
Expand All @@ -122,7 +146,8 @@ public final Compiler withOptions(Iterable<? extends Object> options) {
processors(),
FluentIterable.from(options).transform(toStringFunction()).toList(),
classPath(),
annotationProcessorPath());
annotationProcessorPath(),
false);
}

/**
Expand All @@ -141,7 +166,8 @@ public final Compiler withClasspathFrom(ClassLoader classloader) {
processors(),
options(),
Optional.of(getClasspathFromClassloader(classloader)),
annotationProcessorPath());
annotationProcessorPath(),
false);
}

/** Uses the given classpath for the compilation instead of the system classpath. */
Expand All @@ -150,7 +176,8 @@ public final Compiler withClasspath(Iterable<File> classPath) {
processors(),
options(),
Optional.of(ImmutableList.copyOf(classPath)),
annotationProcessorPath());
annotationProcessorPath(),
false);
}

/**
Expand All @@ -162,7 +189,17 @@ public final Compiler withAnnotationProcessorPath(Iterable<File> annotationProce
processors(),
options(),
classPath(),
Optional.of(ImmutableList.copyOf(annotationProcessorPath)));
Optional.of(ImmutableList.copyOf(annotationProcessorPath)),
false);
}

public final Compiler ignoreRedundantOptionsCheck() {
return copy(
processors(),
options(),
classPath(),
annotationProcessorPath(),
true);
}

/**
Expand All @@ -180,6 +217,11 @@ public final Compilation compile(JavaFileObject... files) {
* @return the results of the compilation
*/
public final Compilation compile(Iterable<? extends JavaFileObject> files) {
final List<String> redundantOptions = getRedundantOptions();
if (!shouldIgnoreRedundantOptionsCheck() && !redundantOptions.isEmpty()) {
throw new IllegalArgumentException(
String.format(REDUNDANT_OPTIONS_ERROR_TEMPLATE, redundantOptions));
}
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
try (StandardJavaFileManager standardFileManager = standardFileManager(diagnosticCollector);
InMemoryJavaFileManager fileManager = new InMemoryJavaFileManager(standardFileManager)) {
Expand Down Expand Up @@ -295,8 +337,16 @@ private Compiler copy(
ImmutableList<Processor> processors,
ImmutableList<String> options,
Optional<ImmutableList<File>> classPath,
Optional<ImmutableList<File>> annotationProcessorPath) {
Optional<ImmutableList<File>> annotationProcessorPath,
boolean shouldIgnoreRedundantOptionsCheck) {
return new AutoValue_Compiler(
javaCompiler(), processors, options, classPath, annotationProcessorPath);
javaCompiler(), processors, options, classPath, annotationProcessorPath,
shouldIgnoreRedundantOptionsCheck);
}

private List<String> getRedundantOptions() {
return options().stream()
.filter(option -> REDUNDANT_OPTIONS.stream().anyMatch(option::startsWith))
.collect(Collectors.toList());
}
}
133 changes: 0 additions & 133 deletions src/test/java/com/google/testing/compile/CompilationTest.java

This file was deleted.

Loading