本文整理汇总了Java中com.sun.tools.javac.code.Symbol.VarSymbol.getKind方法的典型用法代码示例。如果您正苦于以下问题:Java VarSymbol.getKind方法的具体用法?Java VarSymbol.getKind怎么用?Java VarSymbol.getKind使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.code.Symbol.VarSymbol
的用法示例。
在下文中一共展示了VarSymbol.getKind方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: matchVariable
import com.sun.tools.javac.code.Symbol.VarSymbol; //导入方法依赖的package包/类
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
Type type = ASTHelpers.getType(tree);
VarSymbol symbol = ASTHelpers.getSymbol(tree);
if (type == null || symbol == null) {
return NO_MATCH;
}
if (symbol.getKind() != ElementKind.PARAMETER) {
return NO_MATCH;
}
if (!isSameType(type, state.getSymtab().iterableType, state)) {
return NO_MATCH;
}
if (type.getTypeArguments().isEmpty()) {
return NO_MATCH;
}
if (!isSameType(
wildBound(getOnlyElement(type.getTypeArguments())),
state.getTypeFromString(Path.class.getName()),
state)) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
if (tree.getType() instanceof ParameterizedTypeTree) {
description.addFix(
SuggestedFix.builder()
.addImport("java.util.Collection")
.replace(((ParameterizedTypeTree) tree.getType()).getType(), "Collection")
.build());
}
return description.build();
}
示例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();
}
示例3: 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());
}
}
示例4: 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;
}