com.sun.source.util.TreePath.getCompilationUnit()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(57)

本文整理了Java中com.sun.source.util.TreePath.getCompilationUnit()方法的一些代码示例,展示了TreePath.getCompilationUnit()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TreePath.getCompilationUnit()方法的具体详情如下:
包路径:com.sun.source.util.TreePath
类名称:TreePath
方法名:getCompilationUnit

TreePath.getCompilationUnit介绍

[英]Get the compilation unit associated with this path.
[中]获取与此路径关联的编译单元。

代码示例

代码示例来源:origin: robolectric/robolectric

private static Set<String> getImports(VisitorState state) {
 Set<String> imports = new HashSet<>();
 for (ImportTree importTree : state.getPath().getCompilationUnit().getImports()) {
  imports.add(importTree.getQualifiedIdentifier().toString());
 }
 return imports;
}

代码示例来源:origin: robolectric/robolectric

void delete() {
  new TreePathScanner<Void, Void>() {
   @Override
   public Void visitVariable(VariableTree variableTree, Void v) {
    if (getSymbol(variableTree).equals(symbol)) {
     possibleFixes.fixByDeleting(variableTree);
    }
    return super.visitVariable(variableTree, v);
   }
  }.scan(state.getPath().getCompilationUnit(), null);
 }
}

代码示例来源:origin: google/error-prone

/** Returns the end position of the node, or -1 if it is not available. */
public int getEndPosition(Tree node) {
 JCCompilationUnit compilationUnit = (JCCompilationUnit) getPath().getCompilationUnit();
 if (compilationUnit.endPositions == null) {
  return -1;
 }
 return ((JCTree) node).getEndPosition(compilationUnit.endPositions);
}

代码示例来源:origin: google/error-prone

static DCDocComment getDocComment(VisitorState state, Tree tree) {
 return ((JCCompilationUnit) state.getPath().getCompilationUnit())
   .docComments.getCommentTree((JCTree) tree);
}

代码示例来源:origin: google/error-prone

private static String getPackageFullName(VisitorState state) {
 JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit();
 return compilationUnit.packge.fullname.toString();
}

代码示例来源:origin: robolectric/robolectric

private void setEndPosition(VisitorState state, JCTree tree, int endPosition) {
 JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit();
 compilationUnit.endPositions.storeEnd(tree, endPosition);
}

代码示例来源:origin: google/error-prone

/**
 * Gets the current source file.
 *
 * @return the source file as a sequence of characters, or null if it is not available
 */
@Nullable
public CharSequence getSourceCode() {
 try {
  return getPath().getCompilationUnit().getSourceFile().getCharContent(false);
 } catch (IOException e) {
  return null;
 }
}

代码示例来源:origin: google/error-prone

@Nullable
 private VariableTree findDeclaration(VisitorState state, Symbol parameter) {
  JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
  TreePath declPath = Trees.instance(javacEnv).getPath(parameter);
  if (declPath != null
    && declPath.getCompilationUnit() == state.getPath().getCompilationUnit()
    && (declPath.getLeaf() instanceof VariableTree)) {
   return (VariableTree) declPath.getLeaf();
  }
  return null;
 }
}

代码示例来源:origin: google/error-prone

@Nullable
private VariableTree findDeclaration(VisitorState state, Symbol field) {
 JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
 TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(field);
 // Skip fields declared in other compilation units since we can't make a fix for them here.
 if (fieldDeclPath != null
   && fieldDeclPath.getCompilationUnit() == state.getPath().getCompilationUnit()
   && (fieldDeclPath.getLeaf() instanceof VariableTree)) {
  return (VariableTree) fieldDeclPath.getLeaf();
 }
 return null;
}

代码示例来源:origin: google/error-prone

static Set<VarSymbol> scan(VisitorState state, Set<Symbol> badAnswers) {
 MockInitializationScanner scanner = new MockInitializationScanner(badAnswers);
 state.getPath().getCompilationUnit().accept(scanner, null);
 return scanner.mockVariables;
}

代码示例来源:origin: google/error-prone

/** Return true if the given symbol is defined in the current package. */
public static boolean inSamePackage(Symbol targetSymbol, VisitorState state) {
 JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit();
 PackageSymbol usePackage = compilationUnit.packge;
 PackageSymbol targetPackage = targetSymbol.packge();
 return targetPackage != null
   && usePackage != null
   && targetPackage.getQualifiedName().equals(usePackage.getQualifiedName());
}

代码示例来源:origin: google/error-prone

private static VisitorState getCheckState(VisitorState state) {
 // Gets the VisitorState to start from when checking how to qualify the name. This won't work
 // correctly in all cases 1) it assumes there is only 1 top level type; 2) it doesn't look for
 // all of the locations where the symbol-to-be-replaced is used in the compilation unit.
 // Really, we should gather all of the usages first, and check them all.
 // It is assumed that this will work sufficiently until proven otherwise.
 CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit();
 if (compilationUnit.getTypeDecls().isEmpty()) {
  return state;
 }
 return state.withPath(TreePath.getPath(compilationUnit, compilationUnit.getTypeDecls().get(0)));
}

代码示例来源:origin: google/error-prone

@Override
public void apply(TreePath path, Context context, DescriptionListener listener) {
 RefasterScanner.create(this, listener)
   .scan(
     path.getLeaf(), prepareContext(context, (JCCompilationUnit) path.getCompilationUnit()));
}

代码示例来源:origin: google/error-prone

public static GuardedBySymbolResolver from(Tree tree, VisitorState visitorState) {
 return GuardedBySymbolResolver.from(
   ASTHelpers.getSymbol(tree).owner.enclClass(),
   visitorState.getPath().getCompilationUnit(),
   visitorState.context,
   tree);
}

代码示例来源:origin: google/error-prone

static int getStartPosition(DocTree docTree, VisitorState state) {
 DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions();
 CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit();
 return (int) positions.getStartPosition(compilationUnitTree, getDocCommentTree(state), docTree);
}

代码示例来源:origin: google/error-prone

static int getEndPosition(DocTree docTree, VisitorState state) {
 DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions();
 CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit();
 return (int) positions.getEndPosition(compilationUnitTree, getDocCommentTree(state), docTree);
}

代码示例来源:origin: google/error-prone

boolean shouldUseGuava(VisitorState state) {
 for (ImportTree importTree : state.getPath().getCompilationUnit().getImports()) {
  Symbol sym = ASTHelpers.getSymbol(importTree.getQualifiedIdentifier());
  if (sym == null) {
   continue;
  }
  if (sym.getQualifiedName().contentEquals("com.google.common.io.Files")) {
   return true;
  }
 }
 return false;
}

代码示例来源:origin: google/error-prone

static SuggestedFix replace(DocTree docTree, String replacement, VisitorState state) {
 DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions();
 CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit();
 int startPos = getStartPosition(docTree, state);
 int endPos =
   (int) positions.getEndPosition(compilationUnitTree, getDocCommentTree(state), docTree);
 return SuggestedFix.replace(startPos, endPos, replacement);
}

代码示例来源:origin: robolectric/robolectric

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
 if (isInShadowClass(state.getPath(), state)) {
  return NO_MATCH;
 }
 final ShadowInliner shadowInliner = new ShadowInliner(
   (JCCompilationUnit) state.getPath().getCompilationUnit());
 shadowInliner.scan(tree, state);
 Fix fix = shadowInliner.possibleFixes.getFix();
 return fix.isEmpty() ? NO_MATCH : describeMatch(tree, fix);
}

代码示例来源:origin: google/error-prone

@Override
 public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
  Symbol baseSym = ASTHelpers.getSymbol(tree.getExpression());
  if (baseSym == null || baseSym.getKind() != ElementKind.TYPE_PARAMETER) {
   return Description.NO_MATCH;
  }
  TreeMaker make =
    TreeMaker.instance(state.context)
      .forToplevel((JCCompilationUnit) state.getPath().getCompilationUnit());
  JCExpression qual = make.QualIdent(ASTHelpers.getSymbol(tree));
  return describeMatch(tree, SuggestedFix.replace(tree, qual.toString()));
 }
}

相关文章

微信公众号

最新文章

更多