Java Code Examples for com.sun.source.tree.MemberSelectTree#getExpression()
The following examples show how to use
com.sun.source.tree.MemberSelectTree#getExpression() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NullAway.java From NullAway with MIT License | 6 votes |
@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; }
Example 2
Source File: ResourceStringFoldProvider.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Void visitMemberSelect(MemberSelectTree node, Void p) { Void d = super.visitMemberSelect(node, p); Element el = info.getTrees().getElement(getCurrentPath()); messageMethod = null; if (el == null || el.getKind() != ElementKind.METHOD) { return d; } ExecutableElement ee = (ExecutableElement)el; String sn = ee.getSimpleName().toString(); for (MessagePattern desc : descriptions) { if (!desc.getMethodNamePattern().matcher(sn).matches()) { continue; } // check the defining type el = ee.getEnclosingElement(); if (el == null || !(el.getKind().isClass() || el.getKind().isInterface())) { continue; } TypeElement tel = (TypeElement)el; if (!desc.getOwnerTypePattern().matcher(tel.getQualifiedName().toString()).matches()) { continue; } messageMethod = desc; methodName = sn; methodOwnerPath = new TreePath(getCurrentPath(), node.getExpression()); break; } return d; }
Example 3
Source File: JavacMethodIntrospector.java From FreeBuilder with Apache License 2.0 | 5 votes |
@Override public Name visitMemberSelect(MemberSelectTree node, Void p) { // A member select is an "own method invocation" if the expression is "this", // under the condition that we only hit this case from visitMethodInvocation. ExpressionTree lhs = node.getExpression(); if (lhs.getKind() != Kind.IDENTIFIER) { return null; } if (!((IdentifierTree) lhs).getName().contentEquals("this")) { return null; } return node.getIdentifier(); }
Example 4
Source File: UTemplater.java From Refaster with Apache License 2.0 | 5 votes |
@Override public UExpression visitMemberSelect(MemberSelectTree tree, Void v) { Symbol sym = ASTHelpers.getSymbol(tree); if (sym instanceof ClassSymbol) { return UClassIdent.create((ClassSymbol) sym); } else if (sym.isStatic()) { ExpressionTree selected = tree.getExpression(); checkState(ASTHelpers.getSymbol(selected) instanceof ClassSymbol, "Refaster cannot match static methods used on instances"); return staticMember(sym); } return UMemberSelect.create(template(tree.getExpression()), tree.getIdentifier().toString(), template(sym.type)); }
Example 5
Source File: ImportsTrackerTestHelper.java From buck with Apache License 2.0 | 5 votes |
private static void handleImport(Trees trees, ImportsTracker imports, TreePath importTreePath) { ImportTree importTree = (ImportTree) importTreePath.getLeaf(); MemberSelectTree importedExpression = (MemberSelectTree) importTree.getQualifiedIdentifier(); TreePath importedExpressionPath = new TreePath(importTreePath, importedExpression); Name simpleName = importedExpression.getIdentifier(); boolean isStarImport = simpleName.contentEquals("*"); if (!isStarImport && !importTree.isStatic()) { TypeElement importedType = (TypeElement) trees.getElement(importedExpressionPath); imports.importType(importedType, importedExpressionPath); } else { ExpressionTree containingElementExpression = importedExpression.getExpression(); TreePath containingElementExpressionPath = new TreePath(importedExpressionPath, containingElementExpression); QualifiedNameable containingElement = (QualifiedNameable) trees.getElement(containingElementExpressionPath); if (importTree.isStatic()) { TypeElement containingType = (TypeElement) containingElement; if (isStarImport) { imports.importStaticMembers((TypeElement) containingElement); } else { imports.importStatic(containingType, simpleName); } } else { // Normal star import imports.importMembers(containingElement, containingElementExpressionPath); } } }
Example 6
Source File: TreePathHandle.java From netbeans with Apache License 2.0 | 4 votes |
/** * special handling of static imports * see #196685 * */ private Element getStaticallyImportedElement(TreePath treePath, CompilationInfo info) { if (treePath.getLeaf().getKind() != Tree.Kind.MEMBER_SELECT) return null; MemberSelectTree memberSelectTree = (MemberSelectTree) treePath.getLeaf(); TreePath tp = treePath; while (tp!=null) { Kind treeKind = tp.getLeaf().getKind(); if (treeKind == Tree.Kind.IMPORT) { if (!((ImportTree) tp.getLeaf()).isStatic()) { return null; } break; } else if (treeKind == Tree.Kind.MEMBER_SELECT || treeKind == Tree.Kind.IDENTIFIER) { tp = tp.getParentPath(); continue; } return null; } Name simpleName = memberSelectTree.getIdentifier(); if (simpleName == null) { return null; } TreePath declPath = new TreePath(new TreePath(treePath, memberSelectTree), memberSelectTree.getExpression()); TypeElement decl = (TypeElement) info.getTrees().getElement(declPath); if (decl==null) { return null; } for (Element e : info.getElements().getAllMembers((TypeElement) decl)) { if (!e.getModifiers().contains(Modifier.STATIC)) { continue; } if (!e.getSimpleName().equals(simpleName)) { continue; } return e; } return null; }
Example 7
Source File: JavaElementFoldVisitor.java From netbeans with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings("fallthrough") public Object visitCompilationUnit(CompilationUnitTree node, Object p) { int importsStart = Integer.MAX_VALUE; int importsEnd = -1; TokenHierarchy<?> th = info.getTokenHierarchy(); TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()); IMP: for (ImportTree imp : node.getImports()) { // eliminate invalid imports; erroenous imports at the start/end of import block // will not be folder. Tree qualIdent = imp.getQualifiedIdentifier(); if (qualIdent == null) { continue; } while (qualIdent.getKind() == Tree.Kind.MEMBER_SELECT) { MemberSelectTree mst = (MemberSelectTree)qualIdent; if (mst.getIdentifier().contentEquals("<error>")) { // NOI18N // ignore erroneous imports continue IMP; } qualIdent = mst.getExpression(); } // don't rely on Javac for the end position: in case of missing semicolon the import consumes all whitespace // including comments / javadocs up to the following declaration or text. Rather scan tokens and consume only the import // identifier + semi. int start = (int) sp.getStartPosition(cu, imp); int identPos = (int) sp.getStartPosition(cu, qualIdent); int end = identPos; boolean firstNewline = true; ts.move(identPos); IDENT: while (ts.moveNext()) { Token<JavaTokenId> tukac = ts.token(); switch (tukac.id()) { case IDENTIFIER: case DOT: case STAR: firstNewline = false; end = ts.offset() + tukac.length(); break; case SEMICOLON: end = (int) sp.getEndPosition(cu, imp); break IDENT; case WHITESPACE: { if (firstNewline) { int endl = tukac.text().toString().indexOf("\n"); // NOI18N if (endl > -1) { // remember the first newline after some ident/star/dot content end = ts.offset() + endl; firstNewline = true; } } // fall through } case LINE_COMMENT: case BLOCK_COMMENT: continue; default: break IDENT; } } if (importsStart > start) importsStart = start; if (end > importsEnd) { importsEnd = end; } } if (importsEnd != (-1) && importsStart != (-1)) { if (importsStart < initialCommentStopPos) { initialCommentStopPos = importsStart; } importsStart += 7/*"import ".length()*/; if (importsStart < importsEnd) { addFold(creator.createImportsFold(importsStart, importsEnd), importsStart); } } return super.visitCompilationUnit(node, p); }
Example 8
Source File: UnusedImports.java From netbeans with Apache License 2.0 | 4 votes |
@Override public Void visitImport(ImportTree tree, Void d) { if (parseErrorInImport(tree)) { return super.visitImport(tree, null); } if (tree.getQualifiedIdentifier() == null || tree.getQualifiedIdentifier().getKind() != Tree.Kind.MEMBER_SELECT) { return super.visitImport(tree, null); } MemberSelectTree qualIdent = (MemberSelectTree) tree.getQualifiedIdentifier(); boolean assign = false; // static imports and star imports only use the qualifier part boolean star = isStar(tree); TreePath tp = tree.isStatic() || star ? new TreePath(new TreePath(getCurrentPath(), qualIdent), qualIdent.getExpression()) : new TreePath(getCurrentPath(), tree.getQualifiedIdentifier()); Element decl = info.getTrees().getElement(tp); import2Highlight.put(tree, getCurrentPath()); if (decl != null && !isErroneous(decl)) { if (!tree.isStatic()) { if (star) { List<TypeElement> types = ElementFilter.typesIn(decl.getEnclosedElements()); for (TypeElement te : types) { assign = true; if (!element2Import.containsKey(te)) { element2Import.put(te, tree); } } } else { element2Import.put(decl, tree); importedBySingleImport.add(decl); } } else if (decl.getKind().isClass() || decl.getKind().isInterface()) { Name simpleName = star ? null : qualIdent.getIdentifier(); for (Element e : info.getElements().getAllMembers((TypeElement) decl)) { if (!e.getModifiers().contains(Modifier.STATIC)) continue; if (simpleName != null && !e.getSimpleName().equals(simpleName)) { continue; } if (!star || !element2Import.containsKey(e)) { element2Import.put(e, tree); } assign = true; } } } if (!assign) { if (!tree.isStatic() && star) { unresolvablePackageImports.add(tree); } else { addUnresolvableImport(qualIdent.getIdentifier(), tree); } } super.visitImport(tree, null); return null; }
Example 9
Source File: StaticAccess.java From netbeans with Apache License 2.0 | 4 votes |
protected static Fix computeFixes(CompilationInfo info, TreePath treePath, int[] bounds, int[] kind, String[] simpleName) { if (treePath.getLeaf().getKind() != Kind.MEMBER_SELECT) { return null; } MemberSelectTree mst = (MemberSelectTree)treePath.getLeaf(); Tree expression = mst.getExpression(); TreePath expr = new TreePath(treePath, expression); TypeMirror tm = info.getTrees().getTypeMirror(expr); if (!Utilities.isValidType(tm)) { return null; } Element el = info.getTypes().asElement(tm); if (el == null || (!el.getKind().isClass() && !el.getKind().isInterface())) { return null; } TypeElement type = (TypeElement)el; if (isError(type)) { return null; } Name idName = null; if (expression.getKind() == Kind.MEMBER_SELECT) { MemberSelectTree exprSelect = (MemberSelectTree)expression; idName = exprSelect.getIdentifier(); } if (expression.getKind() == Kind.IDENTIFIER) { IdentifierTree idt = (IdentifierTree)expression; idName = idt.getName(); } if (idName != null) { if (idName.equals(type.getSimpleName())) { return null; } if (idName.equals(type.getQualifiedName())) { return null; } } Element used = info.getTrees().getElement(treePath); if (used == null || !used.getModifiers().contains(Modifier.STATIC)) { return null; } if (isError(used)) { return null; } if (used.getKind().isField()) { kind[0] = 0; } else { if (used.getKind() == ElementKind.METHOD) { kind[0] = 1; } else { kind[0] = 2; } } simpleName[0] = used.getSimpleName().toString(); return new FixImpl(info, expr, type).toEditorFix(); }
Example 10
Source File: TreeConverter.java From j2objc with Apache License 2.0 | 4 votes |
private TreeNode convertFieldAccess(MemberSelectTree node, TreePath parent) { TreePath path = getTreePath(parent, node); String fieldName = node.getIdentifier().toString(); SourcePosition pos = getPosition(node); ExpressionTree selected = node.getExpression(); TreePath selectedPath = getTreePath(path, selected); Element element = getElement(path); TypeMirror typeMirror = getTypeMirror(path); if (fieldName.equals("this")) { return new ThisExpression() .setQualifier((Name) convert(selected, path)) .setTypeMirror(typeMirror); } if ("super".equals(getMemberName(selected))) { SuperFieldAccess newNode = new SuperFieldAccess() .setVariableElement((VariableElement) element) .setTypeMirror(typeMirror); if (selected.getKind() == Kind.MEMBER_SELECT) { newNode.setQualifier( (Name) convert(((MemberSelectTree) selected).getExpression(), selectedPath)); } return newNode; } if (node.getIdentifier().toString().equals("class")) { Type type = convertType(getTypeMirror(selectedPath), pos, false); type.setPosition(getPosition(node)); return new TypeLiteral(typeMirror).setType(type); } if (selected.getKind() == Kind.IDENTIFIER && (!element.getKind().isField() || ElementUtil.isConstant((VariableElement) element))) { if (selected.toString().equals("this")) { // Just return the constant. return new SimpleName(element); } return new QualifiedName() .setName(convertSimpleName(element, typeMirror, pos)) .setQualifier( convertSimpleName(getElement(selectedPath), getTypeMirror(selectedPath), pos)) .setElement(element); } if (selected.getKind() == Kind.MEMBER_SELECT) { TreeNode newSelected = convertFieldAccess((MemberSelectTree) selected, path).setPosition(pos); if (newSelected.getKind() == TreeNode.Kind.QUALIFIED_NAME) { return new QualifiedName() .setName(convertSimpleName(element, typeMirror, pos)) .setQualifier((QualifiedName) newSelected) .setElement(element); } } if (ElementUtil.isConstant((VariableElement) element) && ElementUtil.isStatic(element) && !(selected.getKind() == Kind.METHOD_INVOCATION) && !(selected.getKind() == Kind.MEMBER_SELECT) && !(selected.getKind() == Kind.PARENTHESIZED)) { return new QualifiedName() .setName(convertSimpleName(element, typeMirror, pos)) .setQualifier((Name) convert(selected, path)) .setElement(element); } return new FieldAccess() .setVariableElement((VariableElement) element) .setExpression((Expression) convert(selected, path)) .setName(convertSimpleName(element, typeMirror, pos).setTypeMirror(typeMirror)); }