本文整理汇总了Java中com.sun.source.util.TreePath.getPath方法的典型用法代码示例。如果您正苦于以下问题:Java TreePath.getPath方法的具体用法?Java TreePath.getPath怎么用?Java TreePath.getPath使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.util.TreePath
的用法示例。
在下文中一共展示了TreePath.getPath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getChangedTree
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
/**
* Returns tree which was reparsed by an incremental reparse.
* When the source file wasn't parsed yet or the parse was a full parse
* this method returns null.
* <p class="nonnormative">
* Currently the leaf tree is a MethodTree but this may change in the future.
* Client of this method is responsible to check the corresponding TreeKind
* to find out if it may perform on the changed subtree or it needs to
* reprocess the whole tree.
* </p>
* @return {@link TreePath} or null
* @since 0.31
*/
public @CheckForNull @CheckReturnValue TreePath getChangedTree () {
checkConfinement();
if (JavaSource.Phase.PARSED.compareTo (impl.getPhase())>0) {
return null;
}
final Pair<DocPositionRegion,MethodTree> changedTree = impl.getChangedTree();
if (changedTree == null) {
return null;
}
final CompilationUnitTree cu = impl.getCompilationUnit();
if (cu == null) {
return null;
}
return TreePath.getPath(cu, changedTree.second());
}
示例2: main
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return "class BadName { Object o = j; }";
}
};
List<? extends JavaFileObject> files = Arrays.asList(sfo);
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
Iterable<? extends CompilationUnitTree> compUnits = ct.parse();
CompilationUnitTree cu = compUnits.iterator().next();
ClassTree cdef = (ClassTree)cu.getTypeDecls().get(0);
JCVariableDecl vdef = (JCVariableDecl)cdef.getMembers().get(0);
TreePath path = TreePath.getPath(cu, vdef.init);
Trees.instance(ct).getScope(path);
}
示例3: run
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
List<Fix> result = new ArrayList<Fix>();
List<TypeMirror> targetType = new ArrayList<TypeMirror>();
TreePath[] tmTree = new TreePath[1];
ExpressionTree[] expression = new ExpressionTree[1];
Tree[] leaf = new Tree[1];
computeType(info, offset, targetType, tmTree, expression, leaf);
if (!targetType.isEmpty()) {
TreePath expressionPath = TreePath.getPath(info.getCompilationUnit(), expression[0]); //XXX: performance
for (TypeMirror type : targetType) {
if (type.getKind() != TypeKind.NULL) {
result.add(new AddCastFix(info, expressionPath, tmTree[0], type).toEditorFix());
}
}
}
return result;
}
示例4: run
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
@Override
public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
EnumSet<Kind> supportedKinds = EnumSet.of(Kind.ANNOTATION_TYPE, Kind.CLASS, Kind.ENUM, Kind.VARIABLE, Kind.INTERFACE, Kind.METHOD);
boolean isSupported = (supportedKinds.contains(treePath.getLeaf().getKind()));
if (!isSupported) {
return null;
}
String invalidMod = getInvalidModifier(compilationInfo, treePath, CODES);
if (null==invalidMod)
{
return null;
}
//support multiple invalid modifiers
Collection<Modifier> modss=convertToModifiers(invalidMod.split(","));
TreePath modifierTreePath = TreePath.getPath(treePath, getModifierTree(treePath));
Fix removeModifiersFix = FixFactory.removeModifiersFix(compilationInfo, modifierTreePath, new HashSet<>(modss), NbBundle.getMessage(RemoveInvalidModifier.class, "FIX_RemoveInvalidModifier", invalidMod, modss.size()));
return Arrays.asList(removeModifiersFix);
}
示例5: assignmentToCatchBlockParameter
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToCatchBlockParameter", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToCatchBlockParameter", category = "assignment_issues", enabled = false, suppressWarnings = "AssignmentToCatchBlockParameter", options=Options.QUERY) //NOI18N
@TriggerTreeKind(Kind.CATCH)
public static List<ErrorDescription> assignmentToCatchBlockParameter(HintContext context) {
final Trees trees = context.getInfo().getTrees();
final TreePath catchPath = context.getPath();
final Element param = trees.getElement(TreePath.getPath(catchPath, ((CatchTree) catchPath.getLeaf()).getParameter()));
if (param == null || param.getKind() != ElementKind.EXCEPTION_PARAMETER) {
return null;
}
final TreePath block = TreePath.getPath(catchPath, ((CatchTree) catchPath.getLeaf()).getBlock());
final List<TreePath> paths = new LinkedList<TreePath>();
new AssignmentFinder(trees, param).scan(block, paths);
final List<ErrorDescription> ret = new ArrayList<ErrorDescription>(paths.size());
for (TreePath path : paths) {
ret.add(ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_AssignmentToCatchBlockParameter", param.getSimpleName()))); //NOI18N
}
return ret;
}
示例6: visitTypeCast
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
@Override
public void visitTypeCast(JCTypeCast tree) {
print(cs.spaceWithinTypeCastParens() ? "( " : "(");
print(tree.clazz);
print(cs.spaceWithinTypeCastParens() ? " )" : ")");
if (cs.spaceAfterTypeCast())
needSpace();
if (diffContext.origUnit != null && TreePath.getPath(diffContext.origUnit, tree.expr) != null) {
int a = TreeInfo.getStartPos(tree.expr);
int b = TreeInfo.getEndPos(tree.expr, diffContext.origUnit.endPositions);
print(diffContext.origText.substring(a, b));
return;
}
printExpr(tree.expr, TreeInfo.prefixPrec);
}
示例7: visitAssignment
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
@Override
public Void visitAssignment(AssignmentTree node, Void ignored) {
TreePath path = TreePath.getPath(getCurrentPath(), node.getExpression());
if (trees.getTypeMirror(path).getKind() == TypeKind.ERROR)
throw new AssertionError(path.getLeaf());
return null;
}
示例8: isInAnnotation
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
/** Return true iff this tree is a child of some annotation. */
public static boolean isInAnnotation(Env<?> env, JCTree tree) {
TreePath tp = TreePath.getPath(env.toplevel, tree);
if (tp != null) {
for (Tree t : tp) {
if (t.getKind() == Tree.Kind.ANNOTATION)
return true;
}
}
return false;
}
示例9: run
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
@Override
public void run(CompilationController cc) throws Exception {
TreePath selectedElement = null;
cc.toPhase(Phase.RESOLVED);
selectedElement = cc.getTreeUtilities().pathFor(caret);
//workaround for issue 89064
if (selectedElement.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
List<? extends Tree> decls = cc.getCompilationUnit().getTypeDecls();
if (!decls.isEmpty()) {
selectedElement = TreePath.getPath(cc.getCompilationUnit(), decls.get(0));
}
}
ui = delegate.createRefactoringUI(TreePathHandle.create(selectedElement, cc), start, end, cc);
}
示例10: findSelectedClassMemberDeclaration
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
private static TreePath findSelectedClassMemberDeclaration(final TreePath path, final CompilationInfo javac) {
TreePath currentPath = path;
TreePath selection = null;
while (currentPath != null && selection == null) {
switch (currentPath.getLeaf().getKind()) {
case ANNOTATION_TYPE:
case CLASS:
case ENUM:
case INTERFACE:
case NEW_CLASS:
case METHOD:
selection = currentPath;
break;
case VARIABLE:
Element elm = javac.getTrees().getElement(currentPath);
if (elm != null && elm.getKind().isField()) {
selection = currentPath;
}
break;
}
if (selection != null && javac.getTreeUtilities().isSynthetic(selection)) {
selection = null;
}
if (selection == null) {
currentPath = currentPath.getParentPath();
}
}
if (selection == null && path != null) {
List<? extends Tree> typeDecls = path.getCompilationUnit().getTypeDecls();
if (!typeDecls.isEmpty() && typeDecls.get(0).getKind().asInterface() == ClassTree.class) {
selection = TreePath.getPath(path.getCompilationUnit(), typeDecls.get(0));
}
}
return selection;
}
示例11: isRefactorable
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
public Boolean isRefactorable() {
prospectives = this.getListRepresentation(loop.getStatement(), true);
if (prospectives != null && !prospectives.isEmpty()) {
prospectives.get(prospectives.size() - 1).eagerize();
if (this.untrasformable) {
return false;
}
for ( int i = 0; i < prospectives.size() - 1; i++) {
if (!prospectives.get(i).isLazy()) {
return false;
}
}
hasIterable = false;
VariableTree var = loop.getVariable();
TypeElement el = workingCopy.getElements().getTypeElement("java.lang.Iterable"); // NOI18N
if (el != null) {
TreePath path = TreePath.getPath(workingCopy.getCompilationUnit(), loop.getExpression());
TypeMirror m = workingCopy.getTrees().getTypeMirror(path);
Types types = workingCopy.getTypes();
hasIterable =
types.isSubtype(
types.erasure(m),
types.erasure(el.asType())
);
}
prospectives = ProspectiveOperation.mergeIntoComposableOperations(prospectives);
return prospectives != null;
} else {
return false;
}
}
示例12: performRewrite
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
LambdaExpressionTree let = (LambdaExpressionTree) ctx.getPath().getLeaf();
for (VariableTree var : let.getParameters()) {
TreePath typePath = TreePath.getPath(ctx.getPath(), var.getType());
if (ctx.getWorkingCopy().getTreeUtilities().isSynthetic(typePath)) {
Tree imported = ctx.getWorkingCopy().getTreeMaker().Type(ctx.getWorkingCopy().getTrees().getTypeMirror(typePath));
ctx.getWorkingCopy().rewrite(var.getType(), imported);
}
}
}
示例13: getElement
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
private Element getElement(Tree tree) {
TreePath expPath = TreePath.getPath(cut, tree);
Element e = trees.getElement(expPath);
if (e == null) {
if (tree instanceof ParenthesizedTree) {
e = getElement(((ParenthesizedTree) tree).getExpression());
//if (e == null) {
// System.err.println("Have null element for "+tree);
//}
//System.err.println("\nHAVE "+e.asType().toString()+" for ParenthesizedTree "+tree);
}
else if (tree instanceof TypeCastTree) {
e = getElement(((TypeCastTree) tree).getType());
//if (e == null) {
// System.err.println("Have null element for "+tree);
//}
//System.err.println("\nHAVE "+e.asType().toString()+" for TypeCastTree "+tree);
}
else if (tree instanceof AssignmentTree) {
e = getElement(((AssignmentTree) tree).getVariable());
}
else if (tree instanceof ArrayAccessTree) {
e = getElement(((ArrayAccessTree) tree).getExpression());
if (e != null) {
TypeMirror tm = e.asType();
if (tm.getKind() == TypeKind.ARRAY) {
tm = ((ArrayType) tm).getComponentType();
e = types.asElement(tm);
}
}
//System.err.println("ArrayAccessTree = "+((ArrayAccessTree) tree).getExpression()+", element = "+getElement(((ArrayAccessTree) tree).getExpression())+", type = "+getElement(((ArrayAccessTree) tree).getExpression()).asType());
}
}
return e;
}
示例14: importComments
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
static <T extends Tree> T importComments(CompilationInfo info, T original, CompilationUnitTree cut) {
try {
CommentSetImpl comments = CommentHandlerService.instance(info.impl.getJavacTask().getContext()).getComments(original);
if (comments.areCommentsMapped()) {
//optimalization, if comments are already mapped, do not even try to
//map them again, would not be attached anyway:
return original;
}
JCTree.JCCompilationUnit unit = (JCCompilationUnit) cut;
TokenHierarchy<?> tokens = unit.getSourceFile() instanceof AbstractSourceFileObject
? ((AbstractSourceFileObject) unit.getSourceFile()).getTokenHierarchy()
: TokenHierarchy.create(unit.getSourceFile().getCharContent(true), JavaTokenId.language());
TokenSequence<JavaTokenId> seq = tokens.tokenSequence(JavaTokenId.language());
TreePath tp = TreePath.getPath(cut, original);
Tree toMap = original;
Tree mapTarget = null;
if (tp != null && original.getKind() != Kind.COMPILATION_UNIT) {
// find some 'nice' place like method/class/field so the comments get an appropriate contents
// Javadocs or other comments may be assigned inappropriately with wider surrounding contents.
TreePath p2 = tp;
boolean first = true;
B: while (p2 != null) {
Tree.Kind k = p2.getLeaf().getKind();
if (StatementTree.class.isAssignableFrom(k.asInterface())) {
mapTarget = p2.getLeaf();
p2 = p2.getParentPath();
break;
}
switch (p2.getLeaf().getKind()) {
case CLASS: case INTERFACE: case ENUM:
case METHOD:
case BLOCK:
case VARIABLE:
if (mapTarget == null) {
mapTarget = p2.getLeaf();
}
if (first) {
p2 = p2 = p2.getParentPath();
}
break B;
}
first = false;
p2 = p2.getParentPath();
}
if (p2 != null) {
toMap = p2.getLeaf();
}
if (toMap == tp.getLeaf()) {
// go at least one level up in a hope it's sufficient.
toMap = tp.getParentPath().getLeaf();
}
}
if (mapTarget == null) {
mapTarget = original;
}
AssignComments translator = new AssignComments(info, mapTarget, seq, unit);
translator.scan(toMap, null);
return original;
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
return original;
}
示例15: run
import com.sun.source.util.TreePath; //导入方法依赖的package包/类
@Override
public void run(final CompilationController cc) throws Exception {
cc.toPhase(JavaSource.Phase.RESOLVED);
final int c = selection?start:this.caret;
final TreePath[] selectedElement = new TreePath[] {null};
// final boolean[] insideJavadoc = {false};
final Document doc = cc.getDocument();
doc.render(new Runnable() {
@Override
public void run() {
selectedElement[0] = validateSelection(cc, start, end);
if (selectedElement[0] == null) {
TokenSequence<JavaTokenId> ts = SourceUtils.getJavaTokenSequence(cc.getTokenHierarchy(), c);
int adjustedCaret = c;
ts.move(c);
if (ts.moveNext() && ts.token() != null) {
if (ts.token().id() == JavaTokenId.IDENTIFIER) {
adjustedCaret = ts.offset() + ts.token().length() / 2 + 1;
} /*else if (ts.token().id() == JavaTokenId.JAVADOC_COMMENT) {
TokenSequence<JavadocTokenId> jdts = ts.embedded(JavadocTokenId.language());
if (jdts != null && JavadocImports.isInsideReference(jdts, caret)) {
jdts.move(caret);
if (jdts.moveNext() && jdts.token().id() == JavadocTokenId.IDENT) {
adjustedCaret[0] = jdts.offset();
insideJavadoc[0] = true;
}
} else if (jdts != null && JavadocImports.isInsideParamName(jdts, caret)) {
jdts.move(caret);
if (jdts.moveNext()) {
adjustedCaret[0] = jdts.offset();
insideJavadoc[0] = true;
}
}
}*/
}
selectedElement[0] = cc.getTreeUtilities().pathFor(adjustedCaret);
}
}
});
//workaround for issue 89064
if (selectedElement[0].getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
List<? extends Tree> decls = cc.getCompilationUnit().getTypeDecls();
if (!decls.isEmpty()) {
TreePath path = TreePath.getPath(cc.getCompilationUnit(), decls.get(0));
if (path!=null && cc.getTrees().getElement(path)!=null) {
selectedElement[0] = path;
}
} else {
selectedElement[0] = null;
}
}
ui = createRefactoringUI(selectedElement[0] != null ? TreePathHandle.create(selectedElement[0], cc) : null, start, end, cc);
}