本文整理汇总了Java中com.google.errorprone.annotations.Immutable类的典型用法代码示例。如果您正苦于以下问题:Java Immutable类的具体用法?Java Immutable怎么用?Java Immutable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Immutable类属于com.google.errorprone.annotations包,在下文中一共展示了Immutable类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkSubtype
import com.google.errorprone.annotations.Immutable; //导入依赖的package包/类
/** Check for classes without {@code @Immutable} that have immutable supertypes. */
private Description checkSubtype(ClassTree tree, VisitorState state) {
ClassSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return NO_MATCH;
}
Type superType = immutableSupertype(sym, state);
if (superType == null) {
return NO_MATCH;
}
String message =
String.format(
"Class extends @Immutable type %s, but is not annotated as immutable", superType);
Fix fix =
SuggestedFix.builder()
.prefixWith(tree, "@Immutable ")
.addImport(Immutable.class.getName())
.build();
return buildDescription(tree).setMessage(message).addFix(fix).build();
}
示例2: immutableSupertype
import com.google.errorprone.annotations.Immutable; //导入依赖的package包/类
/**
* Returns the type of the first superclass or superinterface in the hierarchy annotated with
* {@code @Immutable}, or {@code null} if no such super type exists.
*/
private static Type immutableSupertype(Symbol sym, VisitorState state) {
for (Type superType : state.getTypes().closure(sym.type)) {
if (superType.equals(sym.type)) {
continue;
}
// Don't use getImmutableAnnotation here: subtypes of trusted types are
// also trusted, only check for explicitly annotated supertypes.
if (ASTHelpers.hasAnnotation(superType.tsym, Immutable.class, state)) {
return superType;
}
// We currently trust that @interface annotations are immutable, but don't enforce that
// custom interface implementations are also immutable. That means the check can be
// defeated by writing a custom mutable annotation implementation, and passing it around
// using the superclass type.
//
// TODO(b/25630189): fix this
//
// if (superType.tsym.getKind() == ElementKind.ANNOTATION_TYPE) {
// return superType;
// }
}
return null;
}
示例3: matchClass
import com.google.errorprone.annotations.Immutable; //导入依赖的package包/类
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ClassSymbol symbol = getSymbol(tree);
if (symbol == null || !symbol.isEnum()) {
return NO_MATCH;
}
if (ASTHelpers.hasAnnotation(symbol, Immutable.class, state)
&& !implementsImmutableInterface(symbol)) {
AnnotationTree annotation =
ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Immutable");
if (annotation != null) {
state.reportMatch(
buildDescription(annotation)
.setMessage(ANNOTATED_ENUM_MESSAGE)
.addFix(SuggestedFix.delete(annotation))
.build());
} else {
state.reportMatch(buildDescription(tree).setMessage(ANNOTATED_ENUM_MESSAGE).build());
}
}
Violation info =
new ImmutableAnalysis(
this,
state,
wellKnownMutability,
ImmutableSet.of(
Immutable.class.getName(),
javax.annotation.concurrent.Immutable.class.getName()))
.checkForImmutability(
Optional.of(tree), ImmutableSet.of(), getType(tree), this::describe);
if (!info.isPresent()) {
return NO_MATCH;
}
return describe(tree, info).build();
}
示例4: ImmutableAnalysis
import com.google.errorprone.annotations.Immutable; //导入依赖的package包/类
ImmutableAnalysis(
BugChecker bugChecker, VisitorState state, WellKnownMutability wellKnownMutability) {
this(
bugChecker,
state,
wellKnownMutability,
ImmutableSet.of(Immutable.class.getName()));
}
示例5: implementsImmutableInterface
import com.google.errorprone.annotations.Immutable; //导入依赖的package包/类
private static boolean implementsImmutableInterface(ClassSymbol symbol) {
return Streams.concat(symbol.getInterfaces().stream(), Stream.of(symbol.getSuperclass()))
.anyMatch(supertype -> supertype.asElement().getAnnotation(Immutable.class) != null);
}
示例6: matchClass
import com.google.errorprone.annotations.Immutable; //导入依赖的package包/类
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ClassSymbol symbol = getSymbol(tree);
if (symbol == null
|| symbol.isAnnotationType()
|| !WellKnownMutability.isAnnotation(state, symbol.type)) {
return NO_MATCH;
}
if (!Collections.disjoint(getGeneratedBy(symbol, state), PROCESSOR_BLACKLIST)) {
return NO_MATCH;
}
if (ASTHelpers.hasAnnotation(symbol, Immutable.class, state)) {
AnnotationTree annotation =
ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Immutable");
if (annotation != null) {
state.reportMatch(
buildDescription(annotation)
.setMessage(ANNOTATED_ANNOTATION_MESSAGE)
.addFix(SuggestedFix.delete(annotation))
.build());
} else {
state.reportMatch(buildDescription(tree).setMessage(ANNOTATED_ANNOTATION_MESSAGE).build());
}
}
Violation info =
new ImmutableAnalysis(
this,
state,
wellKnownMutability,
ImmutableSet.of(
Immutable.class.getName(),
javax.annotation.concurrent.Immutable.class.getName()))
.checkForImmutability(
Optional.of(tree), ImmutableSet.of(), getType(tree), this::describeClass);
if (!info.isPresent()) {
return NO_MATCH;
}
return describeClass(tree, info).build();
}