当前位置: 首页>>代码示例>>Java>>正文


Java Immutable类代码示例

本文整理汇总了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();
}
 
开发者ID:google,项目名称:error-prone,代码行数:21,代码来源:ImmutableChecker.java

示例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;
}
 
开发者ID:google,项目名称:error-prone,代码行数:28,代码来源:ImmutableChecker.java

示例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();
}
 
开发者ID:google,项目名称:error-prone,代码行数:40,代码来源:ImmutableEnumChecker.java

示例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()));
}
 
开发者ID:google,项目名称:error-prone,代码行数:9,代码来源:ImmutableAnalysis.java

示例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);
}
 
开发者ID:google,项目名称:error-prone,代码行数:5,代码来源:ImmutableEnumChecker.java

示例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();
}
 
开发者ID:google,项目名称:error-prone,代码行数:43,代码来源:ImmutableAnnotationChecker.java


注:本文中的com.google.errorprone.annotations.Immutable类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。