List of usage examples for org.eclipse.jdt.core.dom SimpleName getParent
public final ASTNode getParent()
null
if this is the root node. From source file:ast.StructureParse.java
private void enclose(String body, DefaultMutableTreeNode parentNode) { ASTParser parser = ASTParser.newParser(AST.JLS2); /*//from w w w .java 2s .c om * make parse result into OPP structure, because AST wanna be that. */ String setClass = "class fo{\nvoid foo(){ \n" + body + "\n}\n}"; parser.setSource(setClass.toCharArray()); //parser.setSource("/*abc*/".toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); //ASTNode node = parser.createAST(null); parser.setResolveBindings(true); try { final CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(new ASTVisitor() { Set names2 = new HashSet(); @Override public boolean visit(SimpleName node) { if (this.names2.contains(node.getIdentifier())) { parentNode.add(new DefaultMutableTreeNode(node.toString())); System.out.println("SimpleNode : " + node.toString()); } return true; } @Override public boolean visit(EnhancedForStatement node) { _for = new DefaultMutableTreeNode( "for (" + node.getParameter() + " : " + node.getExpression() + ")"); parentNode.add(_for); System.out.println("for (" + node.getParameter() + " : " + node.getExpression() + ")"); enclose(node.getBody().toString(), _for); return false; } @Override public boolean visit(ForStatement node) { /* * because initial may more than 1. */ String initial = ""; for (int i = 0; i < node.initializers().size(); i++) { initial += node.initializers().get(i); if (node.initializers().size() - 1 != i) initial += ", "; } /* * because increment may more than 1 */ String inc = ""; for (int i = 0; i < node.updaters().size(); i++) { inc += node.updaters().get(i); if (node.updaters().size() - 1 != i) inc += ", "; } _for = new DefaultMutableTreeNode( "for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); parentNode.add(_for); System.out.println("for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); enclose(node.getBody().toString(), _for); return false; } @Override public boolean visit(IfStatement node) { String elseExist = ""; _if = new DefaultMutableTreeNode("if (" + node.getExpression() + ")"); System.out.println("if (+" + node.getExpression() + ")"); parentNode.add(_if); enclose(node.getThenStatement().toString(), _if); elseExist = node.getElseStatement() + ""; if (!(elseExist.equals("") || (elseExist.equals("null")))) { _else = new DefaultMutableTreeNode("else"); System.out.println("else"); parentNode.add(_else); enclose(node.getElseStatement().toString(), _else); } return false; } @Override public boolean visit(VariableDeclarationFragment node) { if (node.getParent() instanceof FieldDeclaration) { FieldDeclaration declaration = ((FieldDeclaration) node.getParent()); _class.add(new DefaultMutableTreeNode(declaration.getType().toString())); } else { System.out.println("VariableDeclarationFragment : " + node.toString()); parentNode.add(new DefaultMutableTreeNode(node.toString())); } return false; // do not continue to avoid usage info } @Override public boolean visit(ReturnStatement node) { parentNode.add(new DefaultMutableTreeNode(node.toString())); System.out.println("Return : " + node.toString()); return false; } @Override public boolean visit(SuperConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node); parentNode.add(_constructorCall); System.out.println("SuperConstructorInvocation : " + node); return false; } @Override public boolean visit(MethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); parentNode.add(_methodCall); System.out.println("MethodInvocation : " + node); return true; } @Override public boolean visit(SuperMethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); parentNode.add(_methodCall); System.out.println("SuperMethodInvocation : " + node); return false; } @Override public boolean visit(WhileStatement node) { _while = new DefaultMutableTreeNode("while " + node.getExpression()); parentNode.add(_while); System.out.println("WhileStatement : " + node.getExpression()); enclose(node.getBody().toString(), _while); return false; } @Override public boolean visit(DoStatement node) { _do = new DefaultMutableTreeNode("do"); parentNode.add(_do); System.out.println("do"); enclose(node.getBody().toString(), _do); _while = new DefaultMutableTreeNode("while(" + node.getExpression() + ")"); parentNode.add(_while); return false; } @Override public boolean visit(TryStatement node) { String ada = ""; _try = new DefaultMutableTreeNode("try"); parentNode.add(_try); System.out.println("try"); enclose(node.getBody().toString(), _try); ada = node.getFinally() + ""; if (!(ada.equals("") || (ada.equals("null")))) { _final = new DefaultMutableTreeNode("finally"); parentNode.add(_final); System.out.println("finally"); enclose(node.getFinally().toString(), _final); } return false; } @Override public boolean visit(CatchClause node) { _catch = new DefaultMutableTreeNode("catch (" + node.getException() + ")"); parentNode.add(_catch); System.out.println("catch : " + node.getException()); enclose(node.getBody().toString(), _catch); return false; } @Override public boolean visit(Assignment node) { _assignment = new DefaultMutableTreeNode(node.toString()); parentNode.add(_assignment); System.out.println("Assignment : " + node.toString()); return false; } @Override public boolean visit(ConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); parentNode.add(_constructorCall); System.out.println(node.toString()); return false; } @Override public boolean visit(AnonymousClassDeclaration node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); parentNode.add(_constructorCall); System.out.println("AnonymousClassDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayAccess node) { _class = new DefaultMutableTreeNode(node.toString()); parentNode.add(_class); System.out.println("AbstrackTypeDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayCreation node) { _array = new DefaultMutableTreeNode(node.toString()); _method.add(_array); System.out.println("ArrayCreation : " + node.toString()); return false; } @Override public boolean visit(ArrayInitializer node) { _array = new DefaultMutableTreeNode(node.toString()); System.out.println("ArrayInitialize : " + node.toString()); parentNode.add(_array); return false; } @Override public boolean visit(AssertStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("AssertStatement : " + node.toString()); parentNode.add(_statement); return false; } @Override public boolean visit(ContinueStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("ContinueStatement : " + node.toString()); parentNode.add(_statement); return false; } @Override public boolean visit(SwitchStatement node) { _switch = new DefaultMutableTreeNode("switch (" + node.getExpression() + ")"); System.out.println("switch (" + node.getExpression() + ")"); parentNode.add(_switch); List getStatement = node.statements(); for (Object st : getStatement) { Matcher _caseMatch = Pattern.compile("^case\\s+.+\\:").matcher(st.toString()); if (_caseMatch.find()) { _case = new DefaultMutableTreeNode(_caseMatch.group()); _switch.add(_case); } enclose(st.toString(), _case); Matcher _breakMatch = Pattern.compile("^break\\s*.*;").matcher(st.toString()); if (_breakMatch.find()) { _break = new DefaultMutableTreeNode(_breakMatch.group()); _case.add(_break); } } return false; } @Override public boolean visit(ClassInstanceCreation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); System.out.println("ClassInstanceCreation : " + node.toString()); parentNode.add(_constructorCall); return false; } }); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java
License:Open Source License
/** * <p>/*from w ww . ja v a 2 s . com*/ * Returns the name + its qualifier if any. Does not return the suffix of * the FQN. * </p> * <ol> * <li>A : returns A</li> * <li>A.B : returns A</li> * <li>C.A : return C.A</li> * <li>C.A.B : returns C.A</li> * </ol> * * @param name * @return */ public static String getQualifierPlusName(SimpleName name) { String qualifierPlusName = name.getFullyQualifiedName(); ASTNode parent = name.getParent(); if (parent != null && parent instanceof QualifiedName) { QualifiedName qName = (QualifiedName) parent; if (qName.getName().equals(name)) { qualifierPlusName = qName.getFullyQualifiedName(); } } return qualifierPlusName; }
From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java
License:Open Source License
/** * Return true if 1) this is a method invocation and 2) one of the parameter * is not full./*from www.j av a 2s . co m*/ * * Always return false if this is a method invocation from Object and that * is final. * * @param node * @return */ public static boolean isUnsafeMethod(SimpleName node) { boolean isUnsafe = false; ASTNode parent = node.getParent(); if (parent instanceof MethodInvocation) { MethodInvocation mi = (MethodInvocation) parent; if (mi.getName() == node) { if (!checkObjectFinalMethod(mi)) { for (Object arg : mi.arguments()) { Expression exp = (Expression) arg; if (PPABindingsUtil .getSafetyValue(PPABindingsUtil.getTypeBinding(exp)) < PPABindingsUtil.FULL_TYPE) { isUnsafe = true; break; } } if (!isUnsafe) { ITypeBinding containerType = PPABindingsUtil.getTypeBinding(getContainer(mi)); isUnsafe = PPABindingsUtil.getSafetyValue(containerType) < PPABindingsUtil.FULL_TYPE; } } } } return isUnsafe; }
From source file:ca.mcgill.cs.swevo.ppa.ui.NameMapVisitor.java
License:Open Source License
private boolean isDeclaration(SimpleName name) { boolean isDeclaration = false; ASTNode declaration = getDeclarationParent(name.getParent()); if (declaration != null) { if (declaration instanceof AbstractTypeDeclaration) { isDeclaration = name == ((AbstractTypeDeclaration) declaration).getName(); } else if (declaration instanceof MethodDeclaration) { isDeclaration = name == ((MethodDeclaration) declaration).getName(); } else if (declaration instanceof EnumConstantDeclaration) { isDeclaration = name == ((EnumConstantDeclaration) declaration).getName(); } else if (declaration instanceof AnnotationTypeMemberDeclaration) { isDeclaration = name == ((AnnotationTypeMemberDeclaration) declaration).getName(); } else if (declaration instanceof VariableDeclarationFragment) { // Covers FieldDeclaration too! isDeclaration = name == ((VariableDeclarationFragment) declaration).getName(); } else if (declaration instanceof SingleVariableDeclaration) { isDeclaration = name == ((SingleVariableDeclaration) declaration).getName(); }//w w w. j a v a 2s. co m } return isDeclaration; }
From source file:com.flamefire.importsmalinames.astutils.RenameVariablesVisitor.java
License:Open Source License
@Override public boolean visit(SimpleName node) { // We have to be inside a method if (curMethod == null) return false; // Only replace variables IBinding binding = node.resolveBinding(); if (binding == null) { if ((node.getParent() instanceof LabeledStatement) && ((LabeledStatement) node.getParent()).getLabel().equals(node)) return false; if ((node.getParent() instanceof BreakStatement) && ((BreakStatement) node.getParent()).getLabel().equals(node)) return false; if (node.getParent() instanceof QualifiedName) return false; // This may happen System.err.println("Detected SimpleName without binding: " + node + "; Parent:" + node.getParent() + "\nThis may happen if there are compile errors"); // return false; } else if (binding.getKind() != IBinding.VARIABLE) return false; // Check if we need to add a "this" // Do this if current node is a field and we may replace a variable with // its name//from w ww.j a v a 2 s . co m AST ast = node.getAST(); IVariableBinding vBinding = (IVariableBinding) binding; // Check for field acceses if (vBinding != null && vBinding.isField()) { // Add this if necessary if (renaming.containsValue(node.toString()) && !(node.getParent() instanceof FieldAccess) && !(node.getParent() instanceof QualifiedName)) { FieldAccess fa = ast.newFieldAccess(); fa.setExpression(ast.newThisExpression()); fa.setName(ast.newSimpleName(node.toString())); astRewrite.replace(node, fa, null); } return false; } String newName = renaming.get(node.toString()); if (newName == null || newName == "") return false; astRewrite.replace(node, ast.newSimpleName(newName), null); return false; }
From source file:com.google.dart.java2dart.SyntaxTranslator.java
License:Open Source License
@Override public boolean visit(org.eclipse.jdt.core.dom.SimpleName node) { IBinding binding = node.resolveBinding(); SimpleIdentifier result = identifier(node.getIdentifier()); putReference(binding, result);//from w w w . j av a 2 s . c om // may be statically imported field, generate PropertyAccess { org.eclipse.jdt.core.dom.StructuralPropertyDescriptor locationInParent = node.getLocationInParent(); if (binding instanceof IVariableBinding) { org.eclipse.jdt.core.dom.IVariableBinding variableBinding = (org.eclipse.jdt.core.dom.IVariableBinding) binding; org.eclipse.jdt.core.dom.ASTNode parent = node.getParent(); if (locationInParent == org.eclipse.jdt.core.dom.EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == org.eclipse.jdt.core.dom.ClassInstanceCreation.ARGUMENTS_PROPERTY || locationInParent == org.eclipse.jdt.core.dom.MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == org.eclipse.jdt.core.dom.ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == org.eclipse.jdt.core.dom.SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == org.eclipse.jdt.core.dom.Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == org.eclipse.jdt.core.dom.SwitchCase.EXPRESSION_PROPERTY || parent instanceof org.eclipse.jdt.core.dom.InfixExpression || parent instanceof org.eclipse.jdt.core.dom.ConditionalExpression || parent instanceof org.eclipse.jdt.core.dom.ReturnStatement) { ITypeBinding declaringBinding = variableBinding.getDeclaringClass(); ITypeBinding enclosingBinding = getEnclosingTypeBinding(node); if (declaringBinding != null && enclosingBinding != declaringBinding && org.eclipse.jdt.core.dom.Modifier.isStatic(variableBinding.getModifiers())) { SimpleIdentifier target = identifier(declaringBinding.getName()); putReference(declaringBinding, target); return done(propertyAccess(target, result)); } } } } // may be statically imported method, generate PrefixedIdentifier { org.eclipse.jdt.core.dom.StructuralPropertyDescriptor locationInParent = node.getLocationInParent(); if (binding instanceof IMethodBinding) { IMethodBinding methodBinding = (IMethodBinding) binding; if (locationInParent == org.eclipse.jdt.core.dom.MethodInvocation.NAME_PROPERTY && ((org.eclipse.jdt.core.dom.MethodInvocation) node.getParent()).getExpression() == null) { ITypeBinding declaringBinding = methodBinding.getDeclaringClass(); ITypeBinding enclosingBinding = getEnclosingTypeBinding(node); if (declaringBinding != null && enclosingBinding != declaringBinding && org.eclipse.jdt.core.dom.Modifier.isStatic(methodBinding.getModifiers())) { SimpleIdentifier prefix = identifier(declaringBinding.getName()); putReference(declaringBinding, prefix); return done(identifier(prefix, result)); } } } } // done return done(result); }
From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java
License:Open Source License
private boolean isProperty(SimpleName name) { IVariableBinding var = Types.getVariableBinding(name); if (!var.isField() || Modifier.isStatic(var.getModifiers())) { return false; }/*ww w . j av a2 s . c o m*/ int parentNodeType = name.getParent().getNodeType(); if (parentNodeType == ASTNode.QUALIFIED_NAME && name == ((QualifiedName) name.getParent()).getQualifier()) { // This case is for arrays, with property.length references. return true; } return parentNodeType != ASTNode.FIELD_ACCESS && parentNodeType != ASTNode.QUALIFIED_NAME; }
From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java
License:Open Source License
public static void getMethodProposals(IInvocationContext context, IProblemLocation problem, boolean isOnlyParameterMismatch, Map<String, Map> missingMethodsMap) throws CoreException { ICompilationUnit cu = context.getCompilationUnit(); CompilationUnit astRoot = context.getASTRoot(); ASTNode selectedNode = problem.getCoveringNode(astRoot); if (!(selectedNode instanceof SimpleName)) { return;//from ww w . java2 s .com } SimpleName nameNode = (SimpleName) selectedNode; List<Expression> arguments; Expression sender; boolean isSuperInvocation; ASTNode invocationNode = nameNode.getParent(); if (invocationNode instanceof MethodInvocation) { MethodInvocation methodImpl = (MethodInvocation) invocationNode; arguments = methodImpl.arguments(); sender = methodImpl.getExpression(); isSuperInvocation = false; } else if (invocationNode instanceof SuperMethodInvocation) { SuperMethodInvocation methodImpl = (SuperMethodInvocation) invocationNode; arguments = methodImpl.arguments(); sender = methodImpl.getQualifier(); isSuperInvocation = true; } else { return; } String methodName = nameNode.getIdentifier(); // new method addNewMethodProposals(cu, astRoot, sender, arguments, isSuperInvocation, invocationNode, methodName, missingMethodsMap); // if (!isOnlyParameterMismatch && !isSuperInvocation && sender != null) { // addMissingCastParentsProposal(cu, (MethodInvocation) invocationNode, proposals); // } }
From source file:com.kodebeagle.javaparser.TypeResolver.java
License:Apache License
/** * Visits {@link SimpleName} AST nodes. Resolves the binding of the simple * name and looks for it in the {variableScope} map. If the binding * is found, this is a reference to a variable. * * @param node//w ww. jav a2s . c o m * the node to visit */ @Override public boolean visit(final SimpleName node) { if (node.getParent().getNodeType() == ASTNode.METHOD_INVOCATION) { final MethodInvocation invocation = (MethodInvocation) node.getParent(); if (invocation.getName() == node) { return true; } } // method declaration can have same name as variable but this does not mean it is binding to that variable // added particularly for enum if (node.getParent().getNodeType() == ASTNode.METHOD_DECLARATION) { return true; } addBindingData(node.getIdentifier(), node, nodeScopes.get(node)); return true; }
From source file:de.ahoehma.jdt.quickfix.DeleteNodeCorrectionProposal.java
License:Open Source License
@Override protected void addEdits(final IDocument doc, final TextEdit root) throws CoreException { // build a full AST final CompilationUnit unit = SharedASTProvider.getAST(getCompilationUnit(), SharedASTProvider.WAIT_YES, null);/*from w w w. ja v a 2 s . c o m*/ final ASTNode name = NodeFinder.perform(unit, fOffset, fLength); if (name instanceof SimpleName) { final SimpleName[] names = LinkedNodeFinder.findByProblems(unit, (SimpleName) name); if (names != null) { for (int i = 0; i < names.length; i++) { final SimpleName curr = names[i]; final ASTNode parent = curr.getParent(); root.addChild(new DeleteEdit(parent.getStartPosition(), parent.getLength())); } return; } } root.addChild(new DeleteEdit(fOffset, fLength)); }