Skip to content

Commit

Permalink
Excavator: Format Java files (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
svc-excavator-bot authored and bulldozer-bot[bot] committed Jan 6, 2020
1 parent 956e80e commit 1020dcb
Show file tree
Hide file tree
Showing 16 changed files with 413 additions and 338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks",
linkType = BugPattern.LinkType.CUSTOM,
severity = BugPattern.SeverityLevel.SUGGESTION,
summary = "Prefer to declare more specific throws types than Exception and Throwable. When methods are "
+ "updated to throw new checked exceptions they expect callers to handle failure types explicitly. "
+ "Throwing broad types defeats the type system. By throwing the most specific types possible we "
+ "leverage existing compiler functionality to detect unreachable code.\n"
+ "Note: Checked exceptions are only validated by the compiler and can be thrown by non-standard "
+ "bytecode at runtime, for example when java code calls into groovy or scala generated bytecode "
+ "a checked exception can be thrown despite not being declared. In these scenarios we recommend "
+ "suppressing this check using @SuppressWarnings(\"ThrowSpecificity\") and a comment describing "
+ "the reason. Remaining instances can be automatically fixed using "
+ "./gradlew compileJava -PerrorProneApply=ThrowSpecificity")
summary =
"Prefer to declare more specific throws types than Exception and Throwable. When methods are updated"
+ " to throw new checked exceptions they expect callers to handle failure types explicitly."
+ " Throwing broad types defeats the type system. By throwing the most specific types possible we"
+ " leverage existing compiler functionality to detect unreachable code.\n"
+ "Note: Checked exceptions are only validated by the compiler and can be thrown by non-standard"
+ " bytecode at runtime, for example when java code calls into groovy or scala generated bytecode"
+ " a checked exception can be thrown despite not being declared. In these scenarios we recommend"
+ " suppressing this check using @SuppressWarnings(\"ThrowSpecificity\") and a comment describing"
+ " the reason. Remaining instances can be automatically fixed using ./gradlew compileJava"
+ " -PerrorProneApply=ThrowSpecificity")
public final class ThrowSpecificity extends BugChecker implements BugChecker.MethodTreeMatcher {

// Maximum of three checked exception types to avoid unreadable long catch statements.
Expand Down Expand Up @@ -95,9 +96,12 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
}
SuggestedFix.Builder fix = SuggestedFix.builder();
return buildDescription(throwsExpression)
.addFix(fix.replace(throwsExpression, checkedExceptions.stream()
.map(checkedException -> MoreSuggestedFixes.prettyType(state, fix, checkedException))
.collect(Collectors.joining(", ")))
.addFix(fix.replace(
throwsExpression,
checkedExceptions.stream()
.map(checkedException ->
MoreSuggestedFixes.prettyType(state, fix, checkedException))
.collect(Collectors.joining(", ")))
.build())
.build();
}
Expand All @@ -116,8 +120,8 @@ private static boolean safeToModifyThrowsClause(MethodTree tree) {
// Don't suggest modifying public API
&& !methodModifiers.contains(Modifier.PUBLIC)
&& (symbol.isStatic()
|| methodModifiers.contains(Modifier.FINAL)
|| ASTHelpers.enclosingClass(symbol).getModifiers().contains(Modifier.FINAL));
|| methodModifiers.contains(Modifier.FINAL)
|| ASTHelpers.enclosingClass(symbol).getModifiers().contains(Modifier.FINAL));
}

private static boolean containsBroadException(Collection<Type> exceptions, VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public final class ValidateConstantMessage extends BugChecker implements BugChec

private static final long serialVersionUID = 1L;

private static final Matcher<ExpressionTree> VALIDATE_METHODS =
MethodMatchers.staticMethod()
.onClassAny("org.apache.commons.lang3.Validate", "org.apache.commons.lang.Validate");
private static final Matcher<ExpressionTree> VALIDATE_METHODS = MethodMatchers.staticMethod()
.onClassAny("org.apache.commons.lang3.Validate", "org.apache.commons.lang.Validate");

private final Matcher<ExpressionTree> compileTimeConstExpressionMatcher =
new CompileTimeConstantExpressionMatcher();
Expand Down Expand Up @@ -88,9 +87,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState

ExpressionTree messageArg = args.get(messageArgNumber - 1);
boolean isStringType = ASTHelpers.isSameType(
ASTHelpers.getType(messageArg),
state.getTypeFromString("java.lang.String"),
state);
ASTHelpers.getType(messageArg), state.getTypeFromString("java.lang.String"), state);
boolean isConstantString = compileTimeConstExpressionMatcher.matches(messageArg, state);
if (!isStringType || isConstantString) {
return Description.NO_MATCH;
Expand All @@ -100,7 +97,8 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return Description.NO_MATCH;
}

return buildDescription(tree).setMessage(
"Validate.X() statement uses a non-constant message").build();
return buildDescription(tree)
.setMessage("Validate.X() statement uses a non-constant message")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ public class BracesRequiredTest {

@Test
void testFix_if_then() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param) System.out.println();",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param) {",
Expand All @@ -43,8 +44,8 @@ void testFix_if_then() {

@Test
void testFix_if_else() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param) {",
Expand All @@ -53,7 +54,8 @@ void testFix_if_else() {
" System.out.println(\"else\");",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param) {",
Expand All @@ -68,8 +70,8 @@ void testFix_if_else() {

@Test
void testFix_if_both() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param)",
Expand All @@ -78,7 +80,8 @@ void testFix_if_both() {
" System.out.println(\"else\");",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param) {",
Expand All @@ -93,15 +96,16 @@ void testFix_if_both() {

@Test
void testFix_if_emptyThen() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param); else",
" System.out.println(\"else\");",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param); else {",
Expand All @@ -114,14 +118,15 @@ void testFix_if_emptyThen() {

@Test
void testFix_while() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" while (param) System.out.println();",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" while (param) {",
Expand All @@ -134,14 +139,15 @@ void testFix_while() {

@Test
void testFix_doWhile() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" do System.out.println(); while (param);",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" do {",
Expand All @@ -154,14 +160,15 @@ void testFix_doWhile() {

@Test
void testFix_for() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" for (int i = 0; i < 5; i++) System.out.println();",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" for (int i = 0; i < 5; i++) {",
Expand All @@ -174,15 +181,16 @@ void testFix_for() {

@Test
void testFix_enhancedFor() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"import java.util.List;",
"class Test {",
" void f(List<String> list) {",
" for (String item : list) System.out.println(item);",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"import java.util.List;",
"class Test {",
" void f(List<String> list) {",
Expand All @@ -196,16 +204,17 @@ void testFix_enhancedFor() {

@Test
void testFix_nested() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param) if (param) {",
" System.out.println();",
" }",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"class Test {",
" void f(boolean param) {",
" if (param) {",
Expand All @@ -220,15 +229,16 @@ void testFix_nested() {

@Test
void testFix_elseIf() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"class Test {",
" void f(boolean p0, boolean p1) {",
" if (p0) System.out.println();",
" else if (p1) System.out.println();",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"class Test {",
" void f(boolean p0, boolean p1) {",
" if (p0) {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
@Execution(ExecutionMode.CONCURRENT)
public class CatchBlockLogExceptionTest {

private static final String errorMsg = "BUG: Diagnostic contains: "
+ "Catch block contains log statements but thrown exception is never logged";
private static final String errorMsg =
"BUG: Diagnostic contains: " + "Catch block contains log statements but thrown exception is never logged";

private CompilationTestHelper compilationHelper;

Expand Down Expand Up @@ -74,8 +74,8 @@ public void testNoLogStatement() {

@Test
public void testFix_simple() {
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"import org.slf4j.Logger;",
"import org.slf4j.LoggerFactory;",
"class Test {",
Expand All @@ -88,7 +88,8 @@ public void testFix_simple() {
" }",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"import org.slf4j.Logger;",
"import org.slf4j.LoggerFactory;",
"class Test {",
Expand All @@ -107,8 +108,8 @@ public void testFix_simple() {
@Test
public void testFix_ambiguous() {
// In this case there are multiple options, no fixes should be suggested.
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"import org.slf4j.Logger;",
"import org.slf4j.LoggerFactory;",
"class Test {",
Expand All @@ -122,7 +123,8 @@ public void testFix_ambiguous() {
" }",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"import org.slf4j.Logger;",
"import org.slf4j.LoggerFactory;",
"class Test {",
Expand All @@ -142,8 +144,8 @@ public void testFix_ambiguous() {
@Test
public void testFix_getMessage() {
// In this case there are multiple options, no fixes should be suggested.
fix()
.addInputLines("Test.java",
fix().addInputLines(
"Test.java",
"import org.slf4j.Logger;",
"import org.slf4j.LoggerFactory;",
"class Test {",
Expand All @@ -156,7 +158,8 @@ public void testFix_getMessage() {
" }",
" }",
"}")
.addOutputLines("Test.java",
.addOutputLines(
"Test.java",
"import org.slf4j.Logger;",
"import org.slf4j.LoggerFactory;",
"class Test {",
Expand Down
Loading

0 comments on commit 1020dcb

Please sign in to comment.