Skip to content

Commit 0f183a1

Browse files
committed
JavaBuilder: Reintroduce the -extra_checks flag.
Fixes #1570. -- MOS_MIGRATED_REVID=128585415
1 parent 8329568 commit 0f183a1

File tree

4 files changed

+163
-18
lines changed

4 files changed

+163
-18
lines changed

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/BazelJavaBuilder.java

+36-6
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import com.google.devtools.build.buildjar.javac.plugins.errorprone.ErrorPronePlugin;
2424
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest;
2525
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse;
26-
2726
import java.io.IOException;
2827
import java.io.OutputStreamWriter;
2928
import java.io.PrintWriter;
3029
import java.io.StringWriter;
3130
import java.nio.charset.Charset;
3231
import java.util.Arrays;
3332
import java.util.List;
33+
import java.util.ListIterator;
3434

3535
/**
3636
* The JavaBuilder main called by bazel.
@@ -99,6 +99,28 @@ public static int processRequest(List<String> args, PrintWriter err) {
9999
}
100100
}
101101

102+
private static boolean processAndRemoveExtraChecksOptions(List<String> args) {
103+
// error-prone is enabled by default for Bazel.
104+
boolean errorProneEnabled = true;
105+
106+
ListIterator<String> arg = args.listIterator();
107+
while (arg.hasNext()) {
108+
switch (arg.next()) {
109+
case "-extra_checks":
110+
case "-extra_checks:on":
111+
errorProneEnabled = true;
112+
arg.remove();
113+
break;
114+
case "-extra_checks:off":
115+
errorProneEnabled = false;
116+
arg.remove();
117+
break;
118+
}
119+
}
120+
121+
return errorProneEnabled;
122+
}
123+
102124
/**
103125
* Parses the list of arguments into a {@link JavaLibraryBuildRequest}. The returned
104126
* {@link JavaLibraryBuildRequest} object can be then used to configure the compilation itself.
@@ -110,12 +132,20 @@ public static int processRequest(List<String> args, PrintWriter err) {
110132
@VisibleForTesting
111133
public static JavaLibraryBuildRequest parse(List<String> args) throws IOException,
112134
InvalidCommandLineException {
113-
ImmutableList<BlazeJavaCompilerPlugin> plugins =
114-
ImmutableList.<BlazeJavaCompilerPlugin>of(
115-
new ClassLoaderMaskingPlugin(),
116-
new ErrorPronePlugin());
135+
OptionsParser optionsParser = new OptionsParser(args);
136+
ImmutableList.Builder<BlazeJavaCompilerPlugin> plugins = ImmutableList.builder();
137+
plugins.add(new ClassLoaderMaskingPlugin());
138+
139+
// Support for -extra_checks:off was removed from ErrorPronePlugin, but Bazel still needs it,
140+
// so we'll emulate support for this here by handling the flag ourselves and not loading the
141+
// plug-in when it is specified.
142+
boolean errorProneEnabled = processAndRemoveExtraChecksOptions(optionsParser.getJavacOpts());
143+
if (errorProneEnabled) {
144+
plugins.add(new ErrorPronePlugin());
145+
}
146+
117147
JavaLibraryBuildRequest build =
118-
new JavaLibraryBuildRequest(args, plugins, new DependencyModule.Builder());
148+
new JavaLibraryBuildRequest(optionsParser, plugins.build(), new DependencyModule.Builder());
119149
build.setJavacOpts(JavacOptions.normalizeOptions(build.getJavacOpts()));
120150
return build;
121151
}

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/JavaLibraryBuildRequest.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.devtools.build.buildjar.javac.plugins.BlazeJavaCompilerPlugin;
1919
import com.google.devtools.build.buildjar.javac.plugins.dependency.DependencyModule;
2020
import com.google.devtools.build.buildjar.javac.plugins.processing.AnnotationProcessingModule;
21-
2221
import java.io.IOException;
2322
import java.nio.file.Paths;
2423
import java.util.List;
@@ -86,34 +85,33 @@ public final class JavaLibraryBuildRequest {
8685
private final ImmutableList<BlazeJavaCompilerPlugin> plugins;
8786

8887
/**
89-
* Constructs a build from a list of command args. Sets the same JavacRunner
90-
* for both compilation and annotation processing.
88+
* Constructs a build from a list of command args. Sets the same JavacRunner for both compilation
89+
* and annotation processing.
9190
*
92-
* @param args the list of command line args
91+
* @param optionsParser the parsed command line args.
9392
* @param extraPlugins extraneous plugins to use in addition to the strict dependency module.
9493
* @throws InvalidCommandLineException on any command line error
9594
*/
96-
public JavaLibraryBuildRequest(List<String> args, List<BlazeJavaCompilerPlugin> extraPlugins)
95+
public JavaLibraryBuildRequest(
96+
OptionsParser optionsParser, List<BlazeJavaCompilerPlugin> extraPlugins)
9797
throws InvalidCommandLineException, IOException {
98-
this(args, extraPlugins, new DependencyModule.Builder());
98+
this(optionsParser, extraPlugins, new DependencyModule.Builder());
9999
}
100100

101101
/**
102-
* Constructs a build from a list of command args. Sets the same JavacRunner
103-
* for both compilation and annotation processing.
102+
* Constructs a build from a list of command args. Sets the same JavacRunner for both compilation
103+
* and annotation processing.
104104
*
105-
* @param args the list of command line args
105+
* @param optionsParser the parsed command line args.
106106
* @param extraPlugins extraneous plugins to use in addition to the strict dependency module.
107107
* @param depsBuilder a preconstructed dependency module builder.
108108
* @throws InvalidCommandLineException on any command line error
109109
*/
110110
public JavaLibraryBuildRequest(
111-
List<String> args,
111+
OptionsParser optionsParser,
112112
List<BlazeJavaCompilerPlugin> extraPlugins,
113113
DependencyModule.Builder depsBuilder)
114114
throws InvalidCommandLineException, IOException {
115-
OptionsParser optionsParser = new OptionsParser(args);
116-
117115
depsBuilder.addDirectMappings(optionsParser.getDirectMappings());
118116
depsBuilder.addIndirectMappings(optionsParser.getIndirectMappings());
119117
if (optionsParser.getStrictJavaDeps() != null) {

src/test/shell/bazel/BUILD

+7
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ sh_test(
110110
shard_count = 3,
111111
)
112112

113+
sh_test(
114+
name = "bazel_java_test",
115+
size = "large",
116+
srcs = ["bazel_java_test.sh"],
117+
data = [":test-deps"],
118+
)
119+
113120
sh_test(
114121
name = "bazel_rules_test",
115122
size = "large",
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2016 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# Tests the examples provided in Bazel
18+
#
19+
20+
# Load test environment
21+
source $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/test-setup.sh \
22+
|| { echo "test-setup.sh not found!" >&2; exit 1; }
23+
24+
function write_hello_library_files() {
25+
mkdir -p java/main
26+
cat >java/main/BUILD <<EOF
27+
java_binary(name = 'main',
28+
deps = ['//java/hello_library'],
29+
srcs = ['Main.java'],
30+
main_class = 'main.Main')
31+
EOF
32+
33+
cat >java/main/Main.java <<EOF
34+
package main;
35+
import hello_library.HelloLibrary;
36+
public class Main {
37+
public static void main(String[] args) {
38+
HelloLibrary.funcHelloLibrary();
39+
System.out.println("Hello, World!");
40+
}
41+
}
42+
EOF
43+
44+
mkdir -p java/hello_library
45+
cat >java/hello_library/BUILD <<EOF
46+
package(default_visibility=['//visibility:public'])
47+
java_library(name = 'hello_library',
48+
srcs = ['HelloLibrary.java']);
49+
EOF
50+
51+
cat >java/hello_library/HelloLibrary.java <<EOF
52+
package hello_library;
53+
public class HelloLibrary {
54+
public static void funcHelloLibrary() {
55+
System.out.print("Hello, Library!;");
56+
}
57+
}
58+
EOF
59+
}
60+
61+
function test_build_hello_world() {
62+
write_hello_library_files
63+
64+
bazel build //java/main:main &> $TEST_log || fail "build failed"
65+
}
66+
67+
function test_errorprone_error_fails_build_by_default() {
68+
write_hello_library_files
69+
# Trigger an error-prone error by comparing two arrays via #equals().
70+
cat >java/hello_library/HelloLibrary.java <<EOF
71+
package hello_library;
72+
public class HelloLibrary {
73+
public static boolean funcHelloLibrary() {
74+
int[] arr1 = {1, 2, 3};
75+
int[] arr2 = {1, 2, 3};
76+
return arr1.equals(arr2);
77+
}
78+
}
79+
EOF
80+
81+
bazel build //java/main:main &> $TEST_log && fail "build should have failed" || true
82+
expect_log "error: \[ArrayEquals\] Reference equality used to compare arrays"
83+
}
84+
85+
function test_extrachecks_off_disables_errorprone() {
86+
write_hello_library_files
87+
# Trigger an error-prone error by comparing two arrays via #equals().
88+
cat >java/hello_library/HelloLibrary.java <<EOF
89+
package hello_library;
90+
public class HelloLibrary {
91+
public static boolean funcHelloLibrary() {
92+
int[] arr1 = {1, 2, 3};
93+
int[] arr2 = {1, 2, 3};
94+
return arr1.equals(arr2);
95+
}
96+
}
97+
EOF
98+
# Disable error-prone for this target, though.
99+
cat >java/hello_library/BUILD <<EOF
100+
package(default_visibility=['//visibility:public'])
101+
java_library(name = 'hello_library',
102+
srcs = ['HelloLibrary.java'],
103+
javacopts = ['-extra_checks:off'],);
104+
EOF
105+
106+
bazel build //java/main:main &> $TEST_log || fail "build failed"
107+
expect_not_log "error: \[ArrayEquals\] Reference equality used to compare arrays"
108+
}
109+
110+
run_suite "Java integration tests"

0 commit comments

Comments
 (0)