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

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

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

TreePath.getPath介绍

[英]Gets a tree path for a tree node within a compilation unit.
[中]获取编译单元中树节点的树路径。

代码示例

代码示例来源: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

static VisitorState makeVisitorState(Tree target, Unifier unifier) {
  Context context = unifier.getContext();
  TreePath path = TreePath.getPath(context.get(JCCompilationUnit.class), target);
  return new VisitorState(context).withPath(path);
 }
}

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

private static Tree findParent(Tree node, VisitorState state) {
 return TreePath.getPath(state.getPath().getCompilationUnit(), node).getParentPath().getLeaf();
}

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

private void maybeReplaceFieldRef(ExpressionTree subject) {
  Symbol symbol = getSymbol(subject);
  if (symbol != null && symbol.getKind() == ElementKind.FIELD) {
   TreePath subjectPath = TreePath.getPath(compilationUnit, subject);
   if (symbol.equals(fieldSymbol) && isPartOfMethodInvocation(subjectPath)) {
    String fieldRef =
      subject.toString().startsWith("this.")
        ? "this." + newFieldName
        : newFieldName;
    JCTree replaceNode = (JCTree) subject;
    Tree container = subjectPath.getParentPath().getLeaf();
    if (container instanceof JCFieldAccess) {
     JCFieldAccess fieldAccess = (JCFieldAccess) container;
     JCMethodInvocation newShadowOfCall =
       createSyntheticShadowAccess(shadowOfCall, newFieldName, symbol, state);
     replaceFieldSelected(fieldAccess, newShadowOfCall, state);
     replaceNode = newShadowOfCall;
    }
    String replaceWith =
      shouldCallDirectlyOnFramework(subjectPath)
        ? fieldRef
        : shadowOfCall.getMethodSelect() + "(" + fieldRef + ")";
    possibleFixes.put(
      replaceNode, possibleFixes.new ReplacementFix(subject, replaceWith));
   }
  }
 }
}.scan(compilationUnit, null);

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

@Override
 public Void visitMethodInvocation(MethodInvocationTree tree, VisitorState state) {
  VisitorState nowState = state.withPath(TreePath.getPath(state.getPath(), tree));
  if (!inShadowClass) {
   for (MethodInvocationMatcher matcher : matchers) {
    if (matcher.matcher().matches(tree, state)) {
     matcher.replace(tree, nowState, fixBuilder, possibleFixes);
     return null;
    }
   }
  }
  return super.visitMethodInvocation(tree, nowState);
 }
}.scan(tree, state);

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

TreePath path = TreePath.getPath(state.getPath(), formatStringTree);
while (path != null && ASTHelpers.getSymbol(path.getLeaf()) != owner) {
 path = path.getParentPath();

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

private static MethodCall getSurroundingMethodCall(Tree node, VisitorState state) {
 TreePath nodePath = TreePath.getPath(state.getPath(), node);
 TreePath parentPath = nodePath.getParentPath();
 if (parentPath.getLeaf().getKind() == Kind.MEMBER_SELECT) {
  Tree grandparentNode = parentPath.getParentPath().getLeaf();
  if (grandparentNode.getKind() == Kind.METHOD_INVOCATION) {
   return new MethodCall((JCMethodInvocation) grandparentNode);
  }
 }
 return null;
}

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

VisitorState state) {
CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit();
TreePath path = TreePath.getPath(compilationUnit, compilationUnit);
IdentifierTree firstFound =
  new TreePathScanner<IdentifierTree, Void>() {

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

@Override
public Void visitIdentifier(IdentifierTree identifierTreeX, Void aVoid) {
 JCIdent identifierTree = (JCIdent) identifierTreeX;
 Symbol symbol = getSymbol(identifierTree);
 if (variableDecl.sym.equals(symbol) && !isLeftSideOfAssignment(identifierTree)) {
  TreePath idPath = TreePath.getPath(compilationUnit, identifierTree);
  Tree parent = idPath.getParentPath().getLeaf();
  boolean callDirectlyOnFramework = shouldCallDirectlyOnFramework(idPath);
  JCTree replaceNode;
  if (parent instanceof JCFieldAccess && !callDirectlyOnFramework) {
   JCFieldAccess fieldAccess = (JCFieldAccess) parent;
   JCMethodInvocation newShadowOfCall =
     createSyntheticShadowAccess(shadowOfCall, newVarName, symbol, state);
   replaceFieldSelected(fieldAccess, newShadowOfCall, state);
   replaceNode = newShadowOfCall;
  } else {
   identifierTree.name = state.getName(newVarName);
   identifierTree.sym.name = state.getName(newVarName);
   replaceNode = identifierTree;
  }
  String replaceWith =
    callDirectlyOnFramework
      ? newVarName
      : shadowOfCall.getMethodSelect() + "(" + newVarName + ")";
  possibleFixes.put(
    replaceNode, possibleFixes.new ReplacementFix(identifierTree, replaceWith));
 }
 return super.visitIdentifier(identifierTree, aVoid);
}

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

@Override
 public Void visitMethodInvocation(MethodInvocationTree tree, VisitorState state) {
  VisitorState nowState = state.withPath(TreePath.getPath(state.getPath(), tree));
  if (!inShadowClass && shadowStaticMatcher.matches(tree, nowState)) {
   // Replace ShadowXxx.method() with Xxx.method() where possible...
   JCFieldAccess methodSelect = (JCFieldAccess) tree.getMethodSelect();
   ClassSymbol owner = (ClassSymbol) methodSelect.sym.owner;
   ClassType shadowedClass = determineShadowedClassName(owner, nowState);
   String shadowedTypeName =
     SuggestedFixes.prettyType(state, possibleFixes.fixBuilder, shadowedClass);
   possibleFixes.fixByReplacing(methodSelect.selected, shadowedTypeName);
  }
  if (!inShadowClass && shadowOfMatcher.matches(tree, nowState)) {
   matchedShadowOf(tree, nowState);
  }
  return super.visitMethodInvocation(tree, nowState);
 }
}

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

TreePath initializerPath = TreePath.getPath(fieldDeclPath, initializer);
UnderlyingAST ast = new UnderlyingAST.CFGStatement(initializerPath.getLeaf(), classTree);
ControlFlowGraph cfg =

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

if (newFieldName.equals(shadowOfArg.toString())) {
 TreePath assignmentPath = TreePath.getPath(compilationUnit, assignment);
 Tree assignmentParent = assignmentPath.getParentPath().getLeaf();
 if (assignmentParent instanceof ExpressionStatementTree) {
    TreePath.getPath(compilationUnit, assignment), ExpressionStatementTree.class);
if (enclosingNode != null) {
 possibleFixes.fixByDeleting(enclosingNode);

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

TreePath initializerPath = TreePath.getPath(fieldDeclPath, initializer);
ClassTree classTree = (ClassTree) fieldDeclPath.getParentPath().getLeaf();
JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(context);

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
  final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
  if (treeTopLevel == null)
    return null;
  return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
}

代码示例来源:origin: sc.fiji/javac

public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
  final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
  if (treeTopLevel == null)
    return null;
  return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
}

代码示例来源:origin: konsoletyper/teavm-javac

public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
  final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
  if (treeTopLevel == null)
    return null;
  return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-jsf

public void run(CompilationController co) throws Exception {
    co.toPhase(JavaSource.Phase.RESOLVED);
    CompilationUnitTree cut = co.getCompilationUnit();
    List<? extends Tree> typeDecls = cut.getTypeDecls();
    if (!typeDecls.isEmpty()){
      treePathHandles.add(TreePathHandle.create(TreePath.getPath(cut, typeDecls.get(0)), co));
    }
  }
}, false);

代码示例来源:origin: com.google.errorprone/error_prone_core

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: org.netbeans.modules/org-netbeans-modules-web-refactoring

public void run(CompilationController co) throws Exception {
    co.toPhase(JavaSource.Phase.RESOLVED);
    CompilationUnitTree cut = co.getCompilationUnit();
    if (cut.getTypeDecls().isEmpty()) {
      return;
    }
    result[0] = TreePathHandle.create(TreePath.getPath(cut, cut.getTypeDecls().get(0)), co);
  }
}, true);

代码示例来源:origin: com.google.errorprone/error_prone_core

static VisitorState makeVisitorState(Tree target, Unifier unifier) {
  Context context = unifier.getContext();
  TreePath path = TreePath.getPath(context.get(JCCompilationUnit.class), target);
  return new VisitorState(context).withPath(path);
 }
}

相关文章

微信公众号

最新文章

更多