private boolean deepVerifyIdentifier2MemberSelect(TreePath identifier, TreePath memberSelect) { for (TreePath thisPath : prepareThis(identifier)) { State origState = State.copyOf(bindState); try { MemberSelectTree t = (MemberSelectTree) memberSelect.getLeaf(); if (scan(thisPath.getLeaf(), t.getExpression(), memberSelect) == Boolean.TRUE) { return true; } } finally { bindState = origState; } } return false; }
@Override public Void visitImport(ImportTree node, Stack<Tree> p) { if (node.isStatic() && toFind.getModifiers().contains(Modifier.STATIC)) { Tree qualIdent = node.getQualifiedIdentifier(); if (qualIdent.getKind() == Kind.MEMBER_SELECT) { MemberSelectTree mst = (MemberSelectTree) qualIdent; if (toFind.getSimpleName().contentEquals(mst.getIdentifier())) { Element el = info.getTrees().getElement(new TreePath(getCurrentPath(), mst.getExpression())); if (el != null && el.equals(toFind.getEnclosingElement())) { Token<JavaTokenId> t = Utilities.getToken(info, doc, new TreePath(getCurrentPath(), mst)); if (t != null) usages.add(t); } } } } return super.visitImport(node, p); }
@Override public Void visitMemberSelect(MemberSelectTree node, Void p) { Element el = info.getTrees().getElement(getCurrentPath()); if (toFind.equals(el)) { try { int[] span = treeUtils.findNameSpan(node); if(span != null) { MutablePositionRegion region = createRegion(doc, span[0], span[1]); usages.add(region); } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } return super.visitMemberSelect(node, p); }
@Override public Void visitImport(ImportTree node, Void p) { if (node.isStatic() && toFind.getModifiers().contains(Modifier.STATIC)) { Tree qualIdent = node.getQualifiedIdentifier(); if (qualIdent.getKind() == Kind.MEMBER_SELECT) { MemberSelectTree mst = (MemberSelectTree) qualIdent; if (toFind.getSimpleName().contentEquals(mst.getIdentifier())) { Element el = info.getTrees().getElement(new TreePath(getCurrentPath(), mst.getExpression())); if (el != null && el.equals(toFind.getEnclosingElement())) { try { int[] span = treeUtils.findNameSpan(mst); if(span != null) { MutablePositionRegion region = createRegion(doc, span[0], span[1]); usages.add(region); } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } } } } return super.visitImport(node, p); }
@Override public Void visitMemberSelect(MemberSelectTree node, Void p) { int s = (int) wc.getTrees().getSourcePositions().getStartPosition(wc.getCompilationUnit(), node); int e = (int) wc.getTrees().getSourcePositions().getEndPosition(wc.getCompilationUnit(), node); if (s >= start && e <= end) { Element el = wc.getTrees().getElement(getCurrentPath()); if (el != null && (el.getKind().isClass() || el.getKind().isInterface()) && ((TypeElement) el).asType().getKind() != TypeKind.ERROR) { wc.rewrite(node, wc.getTreeMaker().QualIdent(el)); return null; } } return super.visitMemberSelect(node, p); }
@Hint(displayName = "#DN_Imports_EXCLUDED", description = "#DESC_Imports_EXCLUDED", category="imports", id="Imports_EXCLUDED", options=Options.QUERY) @TriggerTreeKind(Kind.IMPORT) public static ErrorDescription exlucded(HintContext ctx) throws IOException { ImportTree it = (ImportTree) ctx.getPath().getLeaf(); if (it.isStatic() || !(it.getQualifiedIdentifier() instanceof MemberSelectTree)) { return null; // XXX } MemberSelectTree ms = (MemberSelectTree) it.getQualifiedIdentifier(); String pkg = ms.getExpression().toString(); String klass = ms.getIdentifier().toString(); String exp = pkg + "." + (!klass.equals("*") ? klass : ""); //NOI18N if (Utilities.isExcluded(exp)) { return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), NbBundle.getMessage(Imports.class, "DN_Imports_EXCLUDED")); } return null; }
@Override protected void performRewrite(JavaFix.TransformationContext ctx) throws Exception { Tree t = ctx.getPath().getLeaf(); if (t.getKind() != Tree.Kind.METHOD_INVOCATION) { return; } MethodInvocationTree mi = (MethodInvocationTree)t; if (mi.getMethodSelect().getKind() != Tree.Kind.MEMBER_SELECT) { return; } MemberSelectTree selector = ((MemberSelectTree)mi.getMethodSelect()); TreeMaker maker = ctx.getWorkingCopy().getTreeMaker(); ExpressionTree ms = maker.MemberSelect(maker.QualIdent("java.util.Arrays"), deep ? "deepHashCode" : "hashCode"); // NOI18N Tree nue = maker.MethodInvocation( Collections.<ExpressionTree>emptyList(), ms, Collections.singletonList(selector.getExpression()) ); ctx.getWorkingCopy().rewrite(t, nue); }
@Override public Object visitMemberSelect(MemberSelectTree node, Object p) { if (wc.getTrees().getElement(new TreePath(getCurrentPath(), node.getExpression())) != varElement) { return super.visitMemberSelect(node, p); } Element target = wc.getTrees().getElement(getCurrentPath()); if (target != null && target.getKind() == ElementKind.METHOD) { Tree x = getCurrentPath().getParentPath().getLeaf(); Tree.Kind k = x.getKind(); if (k == Tree.Kind.METHOD_INVOCATION) { if (node.getIdentifier().contentEquals("toString")) { // NOI18N // rewrite the node to just the variable, which is going to change the type gu.copyComments(x, node.getExpression(), true); gu.copyComments(x, node.getExpression(), false); wc.rewrite(x, node.getExpression()); } } } return super.visitMemberSelect(node, p); }
@Override public Void visitMethodInvocation(MethodInvocationTree node, Void p) { if (!node.getArguments().isEmpty()) { return null; } final ExpressionTree et = node.getMethodSelect(); if (et.getKind() != Tree.Kind.MEMBER_SELECT) { return null; } final MemberSelectTree mst = (MemberSelectTree) et; if (!FINALIZE.contentEquals(mst.getIdentifier())) { return null; } if (mst.getExpression().getKind() != Tree.Kind.IDENTIFIER) { return null; } if (!SUPER.contentEquals(((IdentifierTree)mst.getExpression()).getName())) { return null; } found = true; return null; }
@Override public Object visitMemberSelect(MemberSelectTree node, Object p) { String exp = node.getExpression().toString(); // ClassName.this if (exp.equals("this") || exp.endsWith(".this")) { // NOI18N addInstanceForType(findType(node.getExpression())); } else if (exp.equals("super")) { // NOI18N // reference to superclass of this type addSuperInstance(enclosingType); } else if (exp.endsWith(".super")) { // NOI18N // this is a reference to the superclass of some enclosing type. if (node.getExpression().getKind() == Tree.Kind.MEMBER_SELECT) { Tree t = ((MemberSelectTree)node.getExpression()).getExpression(); addSuperInstance(findType(t)); } } else if (node.getIdentifier().contentEquals("this")) { // NOI18N // reference to this addInstanceForType(findType(node.getExpression())); } else { // references to Clazz.super are invalid, references to Clazz.super.whatever() must be // a pert of a broader memberSelect, which will be caught one level up. return super.visitMemberSelect(node, p); } return null; }
@Override public Void visitMethodInvocation(MethodInvocationTree node, Void p) { if (node.getArguments().isEmpty()) { if (node.getMethodSelect().getKind() == Kind.MEMBER_SELECT) { MemberSelectTree mst = (MemberSelectTree) node.getMethodSelect(); Element e = info.getTrees().getElement(new TreePath(new TreePath(getCurrentPath(), mst), mst.getExpression())); if (parameter.equals(e) && mst.getIdentifier().contentEquals("getClass")) { // NOI18N throw new Found(); } } else if (node.getMethodSelect().getKind() == Kind.IDENTIFIER) { IdentifierTree it = (IdentifierTree) node.getMethodSelect(); if (it.getName().contentEquals("getClass")) { // NOI18N throw new Found(); } } } return super.visitMethodInvocation(node, p); }
private static boolean invocationOnThis(MethodInvocationTree mit) { Tree select = mit.getMethodSelect(); switch (select.getKind()) { case IDENTIFIER: return true; case MEMBER_SELECT: if (((MemberSelectTree) select).getExpression().getKind() == Kind.IDENTIFIER) { IdentifierTree ident = (IdentifierTree) ((MemberSelectTree) select).getExpression(); return ident.getName().contentEquals("this"); } } return false; }
private String getBundleName(MethodInvocationTree n, int index, String bfn) { if (n.getArguments().size() <= index) { return null; } ExpressionTree t = n.getArguments().get(index); // recognize just string literals + .class references if (t.getKind() == Tree.Kind.STRING_LITERAL) { Object o = ((LiteralTree)t).getValue(); return o == null ? null : o.toString(); } else if (t.getKind() == Tree.Kind.MEMBER_SELECT) { MemberSelectTree mst = (MemberSelectTree)t; if (!mst.getIdentifier().contentEquals("class")) { return null; } return bundleFileFromClass(new TreePath(getCurrentPath(), mst.getExpression()), bfn); } return null; }
@Override public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { if (!matchWithinClass) { return Description.NO_MATCH; } Symbol symbol = ASTHelpers.getSymbol(tree); // some checks for cases where we know it is not // a null dereference if (symbol == null || symbol.getSimpleName().toString().equals("class") || symbol.isEnum()) { return Description.NO_MATCH; } Description badDeref = matchDereference(tree.getExpression(), tree, state); if (!badDeref.equals(Description.NO_MATCH)) { return badDeref; } // if we're accessing a field of this, make sure we're not reading the field before init if (tree.getExpression() instanceof IdentifierTree && ((IdentifierTree) tree.getExpression()).getName().toString().equals("this")) { return checkForReadBeforeInit(tree, state); } return Description.NO_MATCH; }
@SuppressWarnings("unused") private int depth(ExpressionTree expression) { switch (expression.getKind()) { case MEMBER_SELECT: MemberSelectTree selectTree = (MemberSelectTree) expression; return 1 + depth(selectTree.getExpression()); case METHOD_INVOCATION: MethodInvocationTree invTree = (MethodInvocationTree) expression; return depth(invTree.getMethodSelect()); case IDENTIFIER: IdentifierTree varTree = (IdentifierTree) expression; Symbol symbol = ASTHelpers.getSymbol(varTree); return (symbol.getKind().equals(ElementKind.FIELD)) ? 2 : 1; default: return 0; } }
/** * Helper method for import declarations, names, and qualified names. */ private void visitName(Tree node) { Deque<Name> stack = new ArrayDeque<>(); for (; node instanceof MemberSelectTree; node = ((MemberSelectTree) node).getExpression()) { stack.addFirst(((MemberSelectTree) node).getIdentifier()); } stack.addFirst(((IdentifierTree) node).getName()); boolean first = true; for (Name name : stack) { if (!first) { token("."); } token(name.toString()); first = false; } }
private void dotExpressionUpToArgs(ExpressionTree expression, Optional<BreakTag> tyargTag) { expression = getArrayBase(expression); switch (expression.getKind()) { case MEMBER_SELECT: MemberSelectTree fieldAccess = (MemberSelectTree) expression; visit(fieldAccess.getIdentifier()); break; case METHOD_INVOCATION: MethodInvocationTree methodInvocation = (MethodInvocationTree) expression; if (!methodInvocation.getTypeArguments().isEmpty()) { builder.open(plusFour); addTypeArguments(methodInvocation.getTypeArguments(), ZERO); // TODO(jdd): Should indent the name -4. builder.breakOp(Doc.FillMode.UNIFIED, "", ZERO, tyargTag); builder.close(); } visit(getMethodName(methodInvocation)); break; case IDENTIFIER: visit(((IdentifierTree) expression).getName()); break; default: scan(expression, null); break; } }
@Override public boolean matches(ExpressionTree tree, VisitorState state) { if (!(tree instanceof MethodInvocationTree)) { return false; } MethodInvocationTree invTree = (MethodInvocationTree) tree; final MemberSelectTree memberTree = (MemberSelectTree) invTree.getMethodSelect(); if (!memberTree.getIdentifier().contentEquals(TO)) { return false; } for (MethodMatchers.MethodNameMatcher nameMatcher : METHOD_NAME_MATCHERS) { if (nameMatcher.matches(invTree, state)) { ExpressionTree arg = invTree.getArguments().get(0); final Type scoper = state.getTypeFromString("com.uber.autodispose.Scoper"); return ASTHelpers.isSubtype(ASTHelpers.getType(arg), scoper, state); } } return false; }
@Override public Tree visitMemberSelect(MemberSelectTree tree, Void p) { MemberSelectTree n = make.MemberSelect(tree.getExpression(), tree.getIdentifier()); model.setElement(n, model.getElement(tree)); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
private static Name getSimpleName(Tree t) { if (t.getKind() == Kind.IDENTIFIER) { return ((IdentifierTree) t).getName(); } if (t.getKind() == Kind.MEMBER_SELECT) { return ((MemberSelectTree) t).getIdentifier(); } throw new UnsupportedOperationException(); }
public static boolean isPureMemberSelect(Tree mst, boolean allowVariables) { switch (mst.getKind()) { case IDENTIFIER: return allowVariables || ((IdentifierTree) mst).getName().charAt(0) != '$'; case MEMBER_SELECT: return isPureMemberSelect(((MemberSelectTree) mst).getExpression(), allowVariables); default: return false; } }
@Override public Object visitIdentifier(IdentifierTree node, Object p) { if (!fieldNames.contains(node.getName())) { return null; } // TODO do not track dependencies for method refs boolean ok = false; Tree parent = getCurrentPath().getParentPath().getLeaf(); switch (parent.getKind()) { case MEMBER_SELECT: // dependency is only introduced by dereferencing the identifier. ok = ((MemberSelectTree)parent).getExpression() != node; break; case ASSIGNMENT: ok = ((AssignmentTree)parent).getVariable() == node; break; } if (ok) { return null; } if (!ok) { if (collectNames) { if (node.getName().equals(insertedName)) { revDependencies.add(member); } } else { addDependency(node.getName()); } } return null; }
@Override public Object visitMemberSelect(MemberSelectTree node, Object p) { Tree parent = getCurrentPath().getParentPath().getLeaf(); if (fieldNames.contains(node.getIdentifier())) { // NOTE: because of JLS 8.3.3, forward reference which is NOT a simple name (even this.id, or MyClassname.this.id !!) // is NOT illegal, so I will not count the appearance as a dependency } Object o = super.visitMemberSelect(node, p); return o; }
private static String name(Tree tree) { switch (tree.getKind()) { case VARIABLE: return ((VariableTree)tree).getName().toString(); case METHOD: return ((MethodTree)tree).getName().toString(); case CLASS: return ((ClassTree)tree).getSimpleName().toString(); case IDENTIFIER: return ((IdentifierTree)tree).getName().toString(); case MEMBER_SELECT: return name(((MemberSelectTree)tree).getExpression()) + '.' + ((MemberSelectTree)tree).getIdentifier(); } return ""; //NOI18N }
public Void visitMemberSelect(MemberSelectTree node, Void p) { TypeElement e = info.getElements().getTypeElement(node.toString()); if (e != null) { translateMap.put(node, make.QualIdent(e)); return null; } else { return super.visitMemberSelect(node, p); } }
public Void visitMemberSelect(MemberSelectTree node, Object p) { System.err.println("visitMemberSelect: " + node.getIdentifier()); super.visitMemberSelect(node, p); MemberSelectTree copy = make.setLabel(node, node.getIdentifier() + "0"); this.copy.rewrite(node, copy); return null; }
private boolean isStar(ImportTree tree) { Tree qualIdent = tree.getQualifiedIdentifier(); if (qualIdent == null || qualIdent.getKind() == Kind.IDENTIFIER) { return false; } return ((MemberSelectTree) qualIdent).getIdentifier().contentEquals("*"); }
private static Token<JavaTokenId> findIdentifierSpanImpl(CompilationInfo info, MemberSelectTree tree, CompilationUnitTree cu, SourcePositions positions) { int start = (int)positions.getStartPosition(cu, tree); int endPosition = (int)positions.getEndPosition(cu, tree); if (start == (-1) || endPosition == (-1)) return null; String member = tree.getIdentifier().toString(); TokenHierarchy<?> th = info.getTokenHierarchy(); TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()); if (ts.move(endPosition) == Integer.MAX_VALUE) { return null; } if (ts.moveNext()) { while (ts.offset() >= start) { Token<JavaTokenId> t = ts.token(); if (t.id() == JavaTokenId.IDENTIFIER && member.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString())) { return t; } if (!ts.movePrevious()) break; } } return null; }
private static Token<JavaTokenId> createHighlightImpl(CompilationInfo info, Document doc, TreePath tree) { Tree leaf = tree.getLeaf(); SourcePositions positions = info.getTrees().getSourcePositions(); CompilationUnitTree cu = info.getCompilationUnit(); //XXX: do not use instanceof: if (leaf instanceof MethodTree || leaf instanceof VariableTree || leaf instanceof ClassTree || leaf instanceof MemberSelectTree || leaf instanceof AnnotatedTypeTree || leaf instanceof MemberReferenceTree) { return findIdentifierSpan(info, doc, tree); } int start = (int) positions.getStartPosition(cu, leaf); int end = (int) positions.getEndPosition(cu, leaf); if (start == Diagnostic.NOPOS || end == Diagnostic.NOPOS) { return null; } TokenHierarchy<?> th = info.getTokenHierarchy(); TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()); if (ts.move(start) == Integer.MAX_VALUE) { return null; } if (ts.moveNext()) { Token<JavaTokenId> token = ts.token(); if (ts.offset() == start && token != null) { final JavaTokenId id = token.id(); if (id == JavaTokenId.IDENTIFIER) { return token; } if (id == JavaTokenId.THIS || id == JavaTokenId.SUPER) { return ts.offsetToken(); } } } return null; }
public static boolean isKeyword(Tree tree) { if (tree.getKind() == Kind.IDENTIFIER) { return keywords.contains(((IdentifierTree) tree).getName().toString()); } if (tree.getKind() == Kind.MEMBER_SELECT) { return keywords.contains(((MemberSelectTree) tree).getIdentifier().toString()); } return false; }
public static boolean isNonCtorKeyword(Tree tree) { if (tree.getKind() == Kind.IDENTIFIER) { return nonCtorKeywords.contains(((IdentifierTree) tree).getName().toString()); } if (tree.getKind() == Kind.MEMBER_SELECT) { return nonCtorKeywords.contains(((MemberSelectTree) tree).getIdentifier().toString()); } return false; }
@Override public Number visitMemberSelect(MemberSelectTree node, Void p) { Element e = info.getTrees().getElement(getCurrentPath()); if (e != null && (e.getKind() != ElementKind.CLASS || ((TypeElement) e).asType().getKind() != TypeKind.ERROR)) { //check correct dependency: checkDependency(info, e, canShowUI); if (isStaticElement(e) && !inImport) { rewrite(node, make.QualIdent(e)); return null; } } MemberSelectTree nue = node; String selectedName = node.getIdentifier().toString(); if (selectedName.startsWith("$") && parameterNames.get(selectedName) != null) { nue = make.MemberSelect(node.getExpression(), parameterNames.get(selectedName)); } if (nue.getExpression().getKind() == Kind.IDENTIFIER) { String name = ((IdentifierTree) nue.getExpression()).getName().toString(); if (name.startsWith("$") && parameters.get(name) == null) { //XXX: unbound variable, use identifier instead of member select - may cause problems? rewrite(node, make.Identifier(nue.getIdentifier())); return null; } } if (nue != node) { rewrite(node, nue); } return super.visitMemberSelect(node, p); }
@Override public Tree visitMemberSelect(MemberSelectTree node, ElementHandle p) { assert cc != null; Element e = p.resolve(cc); addIfMatch(getCurrentPath(), node, e); return super.visitMemberSelect(node, p); }
private static List<? extends TypeMirror> computeMemberSelect(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { //class or field: MemberSelectTree ms = (MemberSelectTree) parent.getLeaf(); final TypeElement jlObject = info.getElements().getTypeElement("java.lang.Object"); if ( jlObject != null //may happen if the platform is broken && !"class".equals(ms.getIdentifier().toString())) {//we obviously should not propose "Create Field" for unknown.class: types.add(ElementKind.FIELD); types.add(ElementKind.CLASS); return Collections.singletonList(jlObject.asType()); } return null; }
private static String simpleName(Tree t) { switch (t.getKind()) { case PARAMETERIZED_TYPE: return simpleName(((ParameterizedTypeTree) t).getType()); case IDENTIFIER: return ((IdentifierTree) t).getName().toString(); case MEMBER_SELECT: return ((MemberSelectTree) t).getIdentifier().toString(); default: return "";//XXX } }
@Override public Void visitMemberSelect(MemberSelectTree tree, List<Node> d) { List<Node> below = new ArrayList<Node>(); addCorrespondingElement(below); addCorrespondingType(below); addCorrespondingComments(below); super.visitMemberSelect(tree, below); d.add(new TreeNode(info, getCurrentPath(), below)); return null; }
@Override public Tree visitMethodInvocation(MethodInvocationTree methodInvocationTree, Trees trees) { String nameSuggestion = org.netbeans.modules.editor.java.Utilities.varNameSuggestion(methodInvocationTree.getMethodSelect()); //check for recursion if (nameSuggestion != null && lambdaMethodTree.getName().contentEquals(nameSuggestion)) { ExpressionTree selector = getSelector(methodInvocationTree); if (selector == null || (org.netbeans.modules.editor.java.Utilities.varNameSuggestion(selector) != null && org.netbeans.modules.editor.java.Utilities.varNameSuggestion(selector).contentEquals("this"))) { foundRecursiveCall = true; } } if (singleStatementLambdaMethodBody == getCurrentPath().getParentPath().getParentPath().getLeaf()) { Tree parent = getCurrentPath().getParentPath().getLeaf(); if (parent.getKind() == Tree.Kind.EXPRESSION_STATEMENT || parent.getKind() == Tree.Kind.RETURN) { boolean check = true; Iterator<? extends VariableTree> paramsIt = lambdaMethodTree.getParameters().iterator(); ExpressionTree methodSelect = methodInvocationTree.getMethodSelect(); if (paramsIt.hasNext() && methodSelect.getKind() == Tree.Kind.MEMBER_SELECT) { ExpressionTree expr = ((MemberSelectTree) methodSelect).getExpression(); if (expr.getKind() == Tree.Kind.IDENTIFIER) { if (!((IdentifierTree)expr).getName().contentEquals(paramsIt.next().getName())) { paramsIt = lambdaMethodTree.getParameters().iterator(); } } } Iterator<? extends ExpressionTree> argsIt = methodInvocationTree.getArguments().iterator(); while (check && argsIt.hasNext() && paramsIt.hasNext()) { ExpressionTree arg = argsIt.next(); if (arg.getKind() != Tree.Kind.IDENTIFIER || !paramsIt.next().getName().contentEquals(((IdentifierTree)arg).getName())) { check = false; } } if (check && !paramsIt.hasNext() && !argsIt.hasNext()) { foundMemberReferenceCandidate = true; } } } return super.visitMethodInvocation(methodInvocationTree, trees); }
private ExpressionTree getSelector(Tree tree) { switch (tree.getKind()) { case MEMBER_SELECT: return ((MemberSelectTree) tree).getExpression(); case METHOD_INVOCATION: return getSelector(((MethodInvocationTree) tree).getMethodSelect()); case NEW_CLASS: return getSelector(((NewClassTree) tree).getIdentifier()); default: return null; } }