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


Java VarSymbol.isStatic方法代码示例

本文整理汇总了Java中com.sun.tools.javac.code.Symbol.VarSymbol.isStatic方法的典型用法代码示例。如果您正苦于以下问题:Java VarSymbol.isStatic方法的具体用法?Java VarSymbol.isStatic怎么用?Java VarSymbol.isStatic使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.sun.tools.javac.code.Symbol.VarSymbol的用法示例。


在下文中一共展示了VarSymbol.isStatic方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: matchSynchronized

import com.sun.tools.javac.code.Symbol.VarSymbol; //导入方法依赖的package包/类
@Override
public Description matchSynchronized(SynchronizedTree tree, VisitorState state) {
  Symbol symbol = ASTHelpers.getSymbol(stripParentheses(tree.getExpression()));
  if (!(symbol instanceof VarSymbol)) {
    return NO_MATCH;
  }

  // TODO(cushon): check that the receiver doesn't contain mutable state.
  // Currently 'this.locks[i].mu' is accepted if 'mu' is final but 'locks' is non-final.
  VarSymbol varSymbol = (VarSymbol) symbol;
  if (varSymbol.isLocal() || varSymbol.isStatic() || (varSymbol.flags() & Flags.FINAL) != 0) {
    return NO_MATCH;
  }
  if (ASTHelpers.hasAnnotation(varSymbol, LazyInit.class, state)) {
    return NO_MATCH;
  }

  Name ownerName = varSymbol.owner.enclClass().getQualifiedName();
  if (Stream.of("java.io.Writer", "java.io.Reader").anyMatch(ownerName::contentEquals)) {
    // These classes contain a non-final 'lock' variable available to subclasses, and we can't
    // make these locks final.
    return NO_MATCH;
  }

  return describeMatch(tree.getExpression());
}
 
开发者ID:google,项目名称:error-prone,代码行数:27,代码来源:SynchronizeOnNonFinalField.java

示例2: matchVariable

import com.sun.tools.javac.code.Symbol.VarSymbol; //导入方法依赖的package包/类
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
  if (tree.getInitializer() == null) {
    return NO_MATCH;
  }
  VarSymbol sym = ASTHelpers.getSymbol(tree);
  if (sym == null || sym.getKind() != ElementKind.FIELD) {
    return NO_MATCH;
  }
  String name = sym.getSimpleName().toString();
  if (!(sym.isStatic() && sym.getModifiers().contains(Modifier.FINAL))) {
    return NO_MATCH;
  }
  if (!name.equals(name.toUpperCase())) {
    return NO_MATCH;
  }
  if (!isSubtype(getType(tree), state.getTypeFromString("java.text.DateFormat"), state)) {
    return NO_MATCH;
  }
  return buildDescription(tree)
      .addFix(threadLocalFix(tree, state, sym))
      .addFix(
          renameVariable(
              tree,
              CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tree.getName().toString()),
              state))
      .build();
}
 
开发者ID:google,项目名称:error-prone,代码行数:29,代码来源:DateFormatConstant.java

示例3: matchNewClass

import com.sun.tools.javac.code.Symbol.VarSymbol; //导入方法依赖的package包/类
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
  if (!NEW_THREAD_LOCAL.matches(tree, state)) {
    return NO_MATCH;
  }
  if (wellKnownTypeArgument(tree, state)) {
    return NO_MATCH;
  }
  Tree parent = state.getPath().getParentPath().getLeaf();
  if (!(parent instanceof VariableTree)) {
    // If the ThreadLocal is created outside of a field we can't easily make assumptions about its
    // scope.
    return NO_MATCH;
  }
  VarSymbol sym = getSymbol((VariableTree) parent);
  if (sym != null && sym.isStatic()) {
    return NO_MATCH;
  }
  if (Streams.stream(state.getPath())
      .filter(ClassTree.class::isInstance)
      .map(ClassTree.class::cast)
      .anyMatch(
          c -> {
            if (hasDirectAnnotationWithSimpleName(getSymbol(c), "Singleton")) {
              return true;
            }
            Type scopeType = state.getTypeFromString("com.google.inject.Scope");
            if (isSubtype(getType(c), scopeType, state)) {
              return true;
            }
            return false;
          })) {
    // The instance X thread issue doesn't apply if there's only one instance.
    return NO_MATCH;
  }
  return describeMatch(tree);
}
 
开发者ID:google,项目名称:error-prone,代码行数:38,代码来源:ThreadLocalUsage.java

示例4: matchVariable

import com.sun.tools.javac.code.Symbol.VarSymbol; //导入方法依赖的package包/类
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
  if (!matchWithinClass) {
    return Description.NO_MATCH;
  }
  VarSymbol symbol = ASTHelpers.getSymbol(tree);
  if (symbol.type.isPrimitive() && tree.getInitializer() != null) {
    return doUnboxingCheck(state, tree.getInitializer());
  }
  if (!symbol.getKind().equals(ElementKind.FIELD)) {
    return Description.NO_MATCH;
  }
  if (fieldsWithInitializationErrors.contains(symbol)) {
    // be careful to delete entry to avoid leaks
    fieldsWithInitializationErrors.remove(symbol);
    if (symbolHasSuppressInitalizationWarningsAnnotation(symbol)) {
      return Description.NO_MATCH;
    }
    if (symbol.isStatic()) {
      return createErrorDescription(
          MessageTypes.FIELD_NO_INIT,
          tree,
          "@NonNull static field " + symbol + " not initialized",
          tree);
    }
    return createErrorDescription(
        MessageTypes.FIELD_NO_INIT, tree, "@NonNull field " + symbol + " not initialized", tree);
  }
  ExpressionTree initializer = tree.getInitializer();
  if (initializer != null) {
    if (!symbol.type.isPrimitive() && !skipDueToFieldAnnotation(symbol)) {
      if (mayBeNullExpr(state, initializer)) {
        return createErrorDescriptionForNullAssignment(
            MessageTypes.ASSIGN_FIELD_NULLABLE,
            tree,
            "assigning @Nullable expression to @NonNull field",
            initializer,
            tree);
      }
    }
  }
  return Description.NO_MATCH;
}
 
开发者ID:uber,项目名称:NullAway,代码行数:44,代码来源:NullAway.java

示例5: isVisible

import com.sun.tools.javac.code.Symbol.VarSymbol; //导入方法依赖的package包/类
private static boolean isVisible(VarSymbol var, final TreePath path) {
  switch (var.getKind()) {
    case ENUM_CONSTANT:
    case FIELD:
      // TODO(eaftan): Switch collector to ImmutableList.toImmutableList() when released
      List<ClassSymbol> enclosingClasses =
          StreamSupport.stream(path.spliterator(), false)
              .filter(tree -> tree instanceof ClassTree)
              .map(ClassTree.class::cast)
              .map(ASTHelpers::getSymbol)
              .collect(Collectors.toCollection(ArrayList::new));

      if (!var.isStatic()) {
        // Instance fields are not visible if we are in a static context...
        if (inStaticContext(path)) {
          return false;
        }

        // ... or if we're in a static nested class and the instance fields are declared outside
        // the enclosing static nested class (JLS 8.5.1).
        if (lowerThan(
            path,
            (curr, unused) ->
                curr instanceof ClassTree && ASTHelpers.getSymbol((ClassTree) curr).isStatic(),
            (curr, unused) ->
                curr instanceof ClassTree
                    && ASTHelpers.getSymbol((ClassTree) curr).equals(var.owner))) {
          return false;
        }
      }

      // If we're lexically enclosed by the same class that defined var, we can access private
      // fields (JLS 6.6.1).
      if (enclosingClasses.contains(ASTHelpers.enclosingClass(var))) {
        return true;
      }

      PackageSymbol enclosingPackage = ((JCCompilationUnit) path.getCompilationUnit()).packge;
      Set<Modifier> modifiers = var.getModifiers();
      // If we're in the same package where var was defined, we can access package-private fields
      // (JLS 6.6.1).
      if (Objects.equals(enclosingPackage, ASTHelpers.enclosingPackage(var))) {
        return !modifiers.contains(Modifier.PRIVATE);
      }

      // Otherwise we can only access public and protected fields (JLS 6.6.1, plus the fact
      // that the only enum constants and fields usable by simple name are either defined
      // in the enclosing class or a superclass).
      return modifiers.contains(Modifier.PUBLIC) || modifiers.contains(Modifier.PROTECTED);
    case PARAMETER:
    case LOCAL_VARIABLE:
      // If we are in an anonymous inner class, lambda, or local class, any local variable or
      // method parameter we access that is defined outside the anonymous class/lambda must be
      // final or effectively final (JLS 8.1.3).
      if (lowerThan(
          path,
          (curr, parent) ->
              curr.getKind() == Kind.LAMBDA_EXPRESSION
                  || (curr.getKind() == Kind.NEW_CLASS
                      && ((NewClassTree) curr).getClassBody() != null)
                  || (curr.getKind() == Kind.CLASS && parent.getKind() == Kind.BLOCK),
          (curr, unused) -> Objects.equals(var.owner, ASTHelpers.getSymbol(curr)))) {
        if ((var.flags() & (Flags.FINAL | Flags.EFFECTIVELY_FINAL)) == 0) {
          return false;
        }
      }
      return true;
    case EXCEPTION_PARAMETER:
    case RESOURCE_VARIABLE:
      return true;
    default:
      throw new IllegalArgumentException("Unexpected variable type: " + var.getKind());
  }
}
 
开发者ID:google,项目名称:error-prone,代码行数:75,代码来源:FindIdentifiers.java

示例6: inStaticContext

import com.sun.tools.javac.code.Symbol.VarSymbol; //导入方法依赖的package包/类
/** Returns true iff the leaf node of the {@code path} occurs in a JLS 8.3.1 static context. */
private static boolean inStaticContext(TreePath path) {
  Tree prev = path.getLeaf();
  path = path.getParentPath();

  ClassSymbol enclosingClass =
      ASTHelpers.getSymbol(ASTHelpers.findEnclosingNode(path, ClassTree.class));
  ClassSymbol directSuperClass = (ClassSymbol) enclosingClass.getSuperclass().tsym;

  for (Tree tree : path) {
    switch (tree.getKind()) {
      case METHOD:
        return ASTHelpers.getSymbol(tree).isStatic();
      case BLOCK: // static initializer
        if (((BlockTree) tree).isStatic()) {
          return true;
        }
        break;
      case VARIABLE: // variable initializer of static variable
        VariableTree variableTree = (VariableTree) tree;
        VarSymbol variableSym = ASTHelpers.getSymbol(variableTree);
        if (variableSym.getKind() == ElementKind.FIELD) {
          return Objects.equals(variableTree.getInitializer(), prev) && variableSym.isStatic();
        }
        break;
      case METHOD_INVOCATION: // JLS 8.8.7.1 explicit constructor invocation
        MethodSymbol methodSym = ASTHelpers.getSymbol((MethodInvocationTree) tree);
        if (methodSym == null) {
          // sometimes javac can't resolve the symbol. In this case just assume that we are
          // in a static context - this is a safe approximation in our context (checking
          // visibility)
          return true;
        }
        if (methodSym.isConstructor()
            && (Objects.equals(methodSym.owner, enclosingClass)
                || Objects.equals(methodSym.owner, directSuperClass))) {
          return true;
        }
        break;
      default:
        break;
    }
    prev = tree;
  }
  return false;
}
 
开发者ID:google,项目名称:error-prone,代码行数:47,代码来源:FindIdentifiers.java


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