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

[improvement] Support auto-applying error-prone suggested fixes #660

Merged
merged 9 commits into from
Jun 24, 2019
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.baseline.extensions;

import com.google.common.collect.ImmutableList;
import org.gradle.api.Project;
import org.gradle.api.provider.ListProperty;

public class BaselineErrorProneExtension {

private static final ImmutableList<String> DEFAULT_PATCH_CHECKS = ImmutableList.of(
// Baseline checks
"PreferBuiltInConcurrentKeySet",
"PreferCollectionTransform",
"PreferListsPartition",
"PreferSafeLoggableExceptions",
"PreferSafeLoggingPreconditions",

// Built-in checks
"ArrayEquals",
"MissingOverride");

private final ListProperty<String> patchChecks;

public BaselineErrorProneExtension(Project project) {
patchChecks = project.getObjects().listProperty(String.class);
patchChecks.set(DEFAULT_PATCH_CHECKS);
}

public final ListProperty<String> getPatchChecks() {
return patchChecks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package com.palantir.baseline.plugins;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.palantir.baseline.extensions.BaselineErrorProneExtension;
import java.io.File;
import java.util.AbstractList;
import java.util.List;
Expand All @@ -37,27 +39,43 @@

public final class BaselineErrorProne implements Plugin<Project> {

public static final String EXTENSION_NAME = "baselineErrorProne";

private static final String ERROR_PRONE_JAVAC_VERSION = "9+181-r4173-1";
private static final String PROP_ERROR_PRONE_APPLY = "errorProneApply";

@Override
public void apply(Project project) {
project.getPluginManager().withPlugin("java", plugin -> {
BaselineErrorProneExtension errorProneExtension = project.getExtensions()
.create(EXTENSION_NAME, BaselineErrorProneExtension.class, project);
project.getPluginManager().apply(ErrorPronePlugin.class);
String version = Optional.ofNullable(getClass().getPackage().getImplementationVersion())
.orElse("latest.release");
project.getDependencies().add(
ErrorPronePlugin.CONFIGURATION_NAME,
"com.palantir.baseline:baseline-error-prone:" + version);

project.getTasks().withType(JavaCompile.class).configureEach(javaCompile ->
((ExtensionAware) javaCompile.getOptions()).getExtensions()
.configure(ErrorProneOptions.class, errorProneOptions -> {
errorProneOptions.setEnabled(true);
errorProneOptions.setDisableWarningsInGeneratedCode(true);
errorProneOptions.check("EqualsHashCode", CheckSeverity.ERROR);
errorProneOptions.check("EqualsIncompatibleType", CheckSeverity.ERROR);
errorProneOptions.check("StreamResourceLeak", CheckSeverity.ERROR);
}));
project.getTasks().withType(JavaCompile.class).configureEach(javaCompile -> {
((ExtensionAware) javaCompile.getOptions()).getExtensions()
.configure(ErrorProneOptions.class, errorProneOptions -> {
errorProneOptions.setEnabled(true);
errorProneOptions.setDisableWarningsInGeneratedCode(true);
errorProneOptions.check("EqualsHashCode", CheckSeverity.ERROR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we read this from the extension?

errorProneOptions.check("EqualsIncompatibleType", CheckSeverity.ERROR);
errorProneOptions.check("StreamResourceLeak", CheckSeverity.ERROR);

if (project.hasProperty(PROP_ERROR_PRONE_APPLY)) {
// TODO(gatesn): Is there a way to discover error-prone checks?
// Maybe service-load from a ClassLoader configured with annotation processor path?
// https://github.com/google/error-prone/pull/947
errorProneOptions.getErrorproneArgumentProviders().add(() -> ImmutableList.of(
"-XepPatchChecks:" + Joiner.on(',')
.join(errorProneExtension.getPatchChecks().get()),
"-XepPatchLocation:IN_PLACE"));
}
});
});

project.getPluginManager().withPlugin("java-gradle-plugin", appliedPlugin -> {
project.getTasks().withType(JavaCompile.class).configureEach(javaCompile ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.palantir.baseline


import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome

Expand All @@ -41,7 +42,7 @@ class BaselineErrorProneIntegrationTest extends AbstractPluginTest {
public class Test { void test() {} }
'''.stripIndent()

def inValidJavaFile = '''
def invalidJavaFile = '''
package test;
public class Test {
void test() {
Expand Down Expand Up @@ -83,7 +84,7 @@ class BaselineErrorProneIntegrationTest extends AbstractPluginTest {
def 'compileJava fails when error-prone finds errors'() {
when:
buildFile << standardBuildFile
file('src/main/java/test/Test.java') << inValidJavaFile
file('src/main/java/test/Test.java') << invalidJavaFile

then:
BuildResult result = with('compileJava').buildAndFail()
Expand All @@ -100,4 +101,28 @@ class BaselineErrorProneIntegrationTest extends AbstractPluginTest {
BuildResult result = with('compileJava').build()
result.task(":compileJava").outcome == TaskOutcome.SUCCESS
}

def 'compileJava applies patches when error-prone finds errors'() {
when:
buildFile << standardBuildFile
file('src/main/java/test/Test.java') << invalidJavaFile

then:
BuildResult result = with('compileJava', '-PerrorProneApply').build()
result.task(":compileJava").outcome == TaskOutcome.SUCCESS
file('src/main/java/test/Test.java').text == '''
package test;

import java.util.Arrays;
public class Test {
void test() {
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
if (Arrays.equals(a, b)) {
System.out.println("arrays are equal!");
}
}
}
'''.stripIndent()
}
}