Skip to content

Commit

Permalink
Automated rollback of 44a1774
Browse files Browse the repository at this point in the history
RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178389082
  • Loading branch information
cushon committed Dec 14, 2017
1 parent 0e899bf commit fa5d5de
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 138 deletions.
24 changes: 0 additions & 24 deletions annotation/src/main/java/com/google/errorprone/BugPattern.java
Expand Up @@ -207,33 +207,9 @@ public enum SeverityLevel {
SUGGESTION
}

/** @deprecated use {@link #suppressionAnnotations} instead. */
@Deprecated
Suppressibility suppressibility() default Suppressibility.DEFAULT;

/** @deprecated use {@link #suppressionAnnotations} instead. */
@Deprecated
public enum Suppressibility {
/**
* Can be suppressed using the standard {@code SuppressWarnings("foo")} mechanism. This setting
* should be used unless there is a good reason otherwise, e.g. security.
*/
SUPPRESS_WARNINGS,
/** Can be suppressed with a custom annotation on a parent AST node. */
CUSTOM_ANNOTATION,
/** Cannot be suppressed. */
UNSUPPRESSIBLE,
/** The default - uses {@link #suppressionAnnotations} instead. */
DEFAULT;
}

/** True if the check can be disabled using command-line flags. */
boolean disableable() default true;

/** @deprecated use {@link #suppressionAnnotations} instead. */
@Deprecated
Class<? extends Annotation>[] customSuppressionAnnotations() default {};

/**
* A set of annotation types that can be used to suppress the check.
*
Expand Down
56 changes: 16 additions & 40 deletions check_api/src/main/java/com/google/errorprone/BugCheckerInfo.java
Expand Up @@ -22,14 +22,12 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.Suppressibility;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.Tree;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
Expand Down Expand Up @@ -76,16 +74,12 @@ public class BugCheckerInfo implements Serializable {
*/
private final String linkUrl;

/**
* Whether this check may be suppressed. Corresponds to the {@code suppressibility} attribute from
* its {@code BugPattern}.
*/
private final Suppressibility suppressibility;
private final boolean supportsSuppressWarnings;

/**
* A set of custom suppression annotations for this check. Computed from the {@code
* suppressibility} and {@code customSuppressionAnnotations} attributes from its {@code
* BugPattern}. May be empty if there are no custom suppression annotations for this check.
* A set of suppression annotations for this check. Computed from the {@code
* suppressionAnnotations} attributes from its {@code BugPattern}. May be empty if there are no
* suppression annotations for this check.
*/
private final Set<Class<? extends Annotation>> customSuppressionAnnotations;

Expand Down Expand Up @@ -118,34 +112,16 @@ private BugCheckerInfo(Class<? extends BugChecker> checker, BugPattern pattern)
pattern.summary(),
pattern.severity(),
createLinkUrl(pattern),
suppressibility(pattern),
customSuppressionAnnotations(pattern),
Stream.of(pattern.suppressionAnnotations()).anyMatch(a -> isSuppressWarnings(a)),
Stream.of(pattern.suppressionAnnotations())
.filter(a -> !isSuppressWarnings(a))
.collect(toImmutableSet()),
ImmutableSet.copyOf(pattern.tags()),
pattern.disableable());
}

private static Suppressibility suppressibility(BugPattern pattern) {
if (pattern.suppressibility() == Suppressibility.DEFAULT) {
if (pattern.suppressionAnnotations().length == 0) {
return Suppressibility.UNSUPPRESSIBLE;
}
if (Arrays.asList(pattern.suppressionAnnotations()).contains(SuppressWarnings.class)) {
return Suppressibility.SUPPRESS_WARNINGS;
}
return Suppressibility.CUSTOM_ANNOTATION;
}
return pattern.suppressibility();
}

private static Set<Class<? extends Annotation>> customSuppressionAnnotations(BugPattern pattern) {
if (pattern.suppressibility() == Suppressibility.DEFAULT) {
return Stream.of(pattern.suppressionAnnotations())
.filter(a -> !a.equals(SuppressWarnings.class))
.collect(toImmutableSet());
}
return pattern.suppressibility() == Suppressibility.CUSTOM_ANNOTATION
? ImmutableSet.copyOf(pattern.customSuppressionAnnotations())
: ImmutableSet.of();
private static boolean isSuppressWarnings(Class<? extends Annotation> annotation) {
return annotation.getSimpleName().equals("SuppressWarnings");
}

private BugCheckerInfo(
Expand All @@ -155,7 +131,7 @@ private BugCheckerInfo(
String message,
SeverityLevel defaultSeverity,
String linkUrl,
Suppressibility suppressibility,
boolean supportsSuppressWarnings,
Set<Class<? extends Annotation>> customSuppressionAnnotations,
ImmutableSet<String> tags,
boolean disableable) {
Expand All @@ -165,7 +141,7 @@ private BugCheckerInfo(
this.message = message;
this.defaultSeverity = defaultSeverity;
this.linkUrl = linkUrl;
this.suppressibility = suppressibility;
this.supportsSuppressWarnings = supportsSuppressWarnings;
this.customSuppressionAnnotations = customSuppressionAnnotations;
this.tags = tags;
this.disableable = disableable;
Expand All @@ -187,7 +163,7 @@ public BugCheckerInfo withCustomDefaultSeverity(SeverityLevel defaultSeverity) {
message,
defaultSeverity,
linkUrl,
suppressibility,
supportsSuppressWarnings,
customSuppressionAnnotations,
tags,
disableable);
Expand Down Expand Up @@ -255,16 +231,16 @@ public String linkUrl() {
return linkUrl;
}

public Suppressibility suppressibility() {
return suppressibility;
public boolean supportsSuppressWarnings() {
return supportsSuppressWarnings;
}

public Set<Class<? extends Annotation>> customSuppressionAnnotations() {
return customSuppressionAnnotations;
}

public boolean disableable() {
return suppressibility() != Suppressibility.UNSUPPRESSIBLE && disableable;
return disableable;
}

public ImmutableSet<String> getTags() {
Expand Down
Expand Up @@ -18,7 +18,6 @@

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.Suppressibility;
import com.google.errorprone.matchers.Suppressible;
import com.google.errorprone.util.ASTHelpers;
import com.sun.tools.javac.code.Attribute;
Expand Down Expand Up @@ -164,21 +163,15 @@ public static boolean isSuppressed(
SeverityLevel severityLevel,
boolean inGeneratedCode,
boolean disableWarningsInGeneratedCode) {
if (suppressible.suppressibility() == Suppressibility.UNSUPPRESSIBLE) {
return false;
}
if (inGeneratedCode && disableWarningsInGeneratedCode && severityLevel != SeverityLevel.ERROR) {
return true;
}
switch (suppressible.suppressibility()) {
case CUSTOM_ANNOTATION:
return !Collections.disjoint(
suppressible.customSuppressionAnnotations(), customSuppressionsOnCurrentPath);
case SUPPRESS_WARNINGS:
return !Collections.disjoint(suppressible.allNames(), suppressionsOnCurrentPath);
default:
throw new IllegalStateException("No case for: " + suppressible.suppressibility());
if (suppressible.supportsSuppressWarnings()
&& !Collections.disjoint(suppressible.allNames(), suppressionsOnCurrentPath)) {
return true;
}
return !Collections.disjoint(
suppressible.customSuppressionAnnotations(), customSuppressionsOnCurrentPath);
}

private static boolean isGenerated(Symbol sym, VisitorState state) {
Expand Down
Expand Up @@ -21,7 +21,6 @@
import com.google.common.base.Preconditions;
import com.google.errorprone.BugCheckerInfo;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.Suppressibility;
import com.google.errorprone.VisitorState;
import com.google.errorprone.fixes.Fix;
import com.google.errorprone.matchers.Description;
Expand Down Expand Up @@ -176,8 +175,8 @@ public String linkUrl() {
}

@Override
public Suppressibility suppressibility() {
return info.suppressibility();
public boolean supportsSuppressWarnings() {
return info.supportsSuppressWarnings();
}

@Override
Expand Down Expand Up @@ -422,11 +421,16 @@ public boolean equals(Object obj) {
BugChecker that = (BugChecker) obj;
return this.canonicalName().equals(that.canonicalName())
&& this.defaultSeverity().equals(that.defaultSeverity())
&& this.suppressibility().equals(that.suppressibility());
&& this.supportsSuppressWarnings() == that.supportsSuppressWarnings()
&& this.customSuppressionAnnotations().equals(that.customSuppressionAnnotations());
}

@Override
public int hashCode() {
return Objects.hash(canonicalName(), defaultSeverity(), suppressibility());
return Objects.hash(
canonicalName(),
defaultSeverity(),
supportsSuppressWarnings(),
customSuppressionAnnotations());
}
}
Expand Up @@ -16,7 +16,6 @@

package com.google.errorprone.matchers;

import com.google.errorprone.BugPattern;
import java.lang.annotation.Annotation;
import java.util.Set;

Expand All @@ -31,11 +30,8 @@ public interface Suppressible {
/** The canonical name of the check. */
String canonicalName();

/**
* Returns how this checker can be suppressed (e.g., via {@code @SuppressWarnings} or a custom
* suppression annotation.
*/
BugPattern.Suppressibility suppressibility();
/** Returns true if this checker can be suppressed using {@code @SuppressWarnings}. */
boolean supportsSuppressWarnings();

/** Returns the custom suppression annotations for this checker, if custom suppression is used. */
Set<Class<? extends Annotation>> customSuppressionAnnotations();
Expand Down
Expand Up @@ -20,7 +20,6 @@
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.Suppressibility;
import com.google.errorprone.ErrorProneError;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
Expand Down Expand Up @@ -251,9 +250,7 @@ protected Set<Class<? extends Annotation>> getCustomSuppressionAnnotations() {
private final List<WildcardTreeMatcher> wildcardMatchers = new ArrayList<>();

private void registerNodeTypes(BugChecker checker) {
if (checker.suppressibility() == Suppressibility.CUSTOM_ANNOTATION) {
customSuppressionAnnotations.addAll(checker.customSuppressionAnnotations());
}
customSuppressionAnnotations.addAll(checker.customSuppressionAnnotations());

if (checker instanceof AnnotationTreeMatcher) {
annotationMatchers.add((AnnotationTreeMatcher) checker);
Expand Down
Expand Up @@ -474,7 +474,7 @@ public void disablingPackageLocation_suppressible() throws Exception {
category = JDK,
severity = ERROR,
suppressionAnnotations = {},
disableable = true
disableable = false
)
public static class UnsuppressiblePackageLocation extends PackageLocation {}

Expand Down
Expand Up @@ -19,7 +19,6 @@
import com.google.common.base.Preconditions;
import com.google.errorprone.BugPattern.ProvidesFix;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.Suppressibility;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -59,51 +58,15 @@ public static BugPatternInstance fromElement(Element element) {
instance.providesFix = annotation.providesFix();

Map<String, Object> keyValues = getAnnotation(element, BugPattern.class.getName());
Suppressibility suppressibility;
try {
suppressibility = annotation.suppressibility();
} catch (EnumConstantNotPresentException e) {
suppressibility = Suppressibility.DEFAULT;
}

switch (suppressibility) {
case UNSUPPRESSIBLE:
instance.suppressionAnnotations = new String[0];
break;
case SUPPRESS_WARNINGS:
instance.suppressionAnnotations = new String[] {SuppressWarnings.class.getName()};
break;
case CUSTOM_ANNOTATION:
// Deprecated in favor of DEFAULT/suppressionAnnotations
Object customSuppression = keyValues.get("customSuppressionAnnotations");
if (customSuppression == null) {
instance.suppressionAnnotations = new String[0];
} else {
Preconditions.checkState(customSuppression instanceof List);
// The doc for AnnotationValue says that if the value is an array, then
// AnnotationValue#getValue() will return a List<? extends AnnotationValue>.
@SuppressWarnings("unchecked")
List<? extends AnnotationValue> resultList =
(List<? extends AnnotationValue>) customSuppression;
instance.suppressionAnnotations =
resultList.stream().map(AnnotationValue::toString).toArray(String[]::new);
}
break;
case DEFAULT:
Object suppression = keyValues.get("suppressionAnnotations");
if (suppression == null) {
instance.suppressionAnnotations = new String[] {SuppressWarnings.class.getName()};
} else {
Preconditions.checkState(suppression instanceof List);
@SuppressWarnings("unchecked") // Always List<? extends AnnotationValue>, see above.
List<? extends AnnotationValue> resultList =
(List<? extends AnnotationValue>) suppression;
instance.suppressionAnnotations =
resultList.stream().map(AnnotationValue::toString).toArray(String[]::new);
}
break;
default:
throw new AssertionError(suppressibility);
Object suppression = keyValues.get("suppressionAnnotations");
if (suppression == null) {
instance.suppressionAnnotations = new String[] {SuppressWarnings.class.getName()};
} else {
Preconditions.checkState(suppression instanceof List);
@SuppressWarnings("unchecked") // Always List<? extends AnnotationValue>, see above.
List<? extends AnnotationValue> resultList = (List<? extends AnnotationValue>) suppression;
instance.suppressionAnnotations =
resultList.stream().map(AnnotationValue::toString).toArray(String[]::new);
}

instance.generateExamplesFromTestCases =
Expand Down

0 comments on commit fa5d5de

Please sign in to comment.