List of usage examples for org.eclipse.jdt.core.dom VariableDeclarationStatement getParent
public final ASTNode getParent()
null
if this is the root node. From source file:edu.buffalo.cse.green.relationship.dependency.DependencyLRemover.java
License:Open Source License
/** * @see edu.buffalo.cse.green.relationships.RelationshipRemover#finish() *//* www .jav a 2s. co m*/ protected void finish() { for (VariableDeclarationStatement vds : lVDS) { Block block = (Block) vds.getParent(); vds.delete(); processAddInvocations(block); } }
From source file:egovframework.mgt.fit.library.parser.visitor.StatementParsingVisitor.java
License:Apache License
/** * ? ? ? ? .//from w w w .j av a 2 s . c o m * @return */ @Override public boolean visit(VariableDeclarationStatement node) { if (node.getParent().getNodeType() == parentType) { addSingleStatement(node.toString(), StatementLine.VARIABLE); } return super.visit(node); }
From source file:org.eclipse.wb.internal.core.model.variable.LocalReuseVariableSupport.java
License:Open Source License
/** * This method should be used if this variable is initialized in * {@link VariableDeclarationFragment} and we want split it on separate * {@link VariableDeclarationStatement} and assignment. There is such quick assist in Eclipse. *//* ww w . j av a2 s . co m*/ private void splitVariable() throws Exception { AstEditor editor = m_javaInfo.getEditor(); // prepare current information VariableDeclaration oldFragment = m_declaration; VariableDeclarationStatement oldStatement = getDeclarationStatement(); // add new VariableDeclarationStatement before oldStatement // we should create new variable because "m_variable" is place of _assignment_ { int position = oldStatement.getStartPosition(); String source = ""; // add type Type newType; { Type oldType = oldStatement.getType(); newType = editor.getParser().parseType(position, oldType); source += editor.getSource(oldType) + " "; } // add variable SimpleName newVariable; { String identifier = AstNodeUtils.getVariableName(m_variable); newVariable = editor.getParser().parseVariable(position + source.length(), identifier, null, AstNodeUtils.getTypeBinding(newType), false, Modifier.NONE); source += identifier; } // create fragment VariableDeclarationFragment newFragment; { newFragment = m_variable.getAST().newVariableDeclarationFragment(); newFragment.setName(newVariable); AstNodeUtils.copySourceRange(newFragment, newVariable); } // create statement VariableDeclarationStatement newStatement; { newStatement = m_variable.getAST().newVariableDeclarationStatement(newFragment); newStatement.setType(newType); AstNodeUtils.setSourceRange(newStatement, newType, newFragment, 1); source += ";"; } // modify source { String prefix = editor.getWhitespaceToLeft(position, false); source += editor.getGeneration().getEndOfLine() + prefix; editor.replaceSubstring(position, 0, source); } // add new statement to AST { List<Statement> statements = DomGenerics.statements((Block) oldStatement.getParent()); int index = statements.indexOf(oldStatement); statements.add(index, newStatement); } } // convert old declaration to assignment replaceDeclarationWithAssignment(editor, oldFragment); rememberDeclaration(); }
From source file:org.eclipse.wb.internal.core.model.variable.LocalVariableSupport.java
License:Open Source License
/** * Replaces given {@link VariableDeclarationStatement} with {@link ExpressionStatement} with * {@link Assignment} to the same variable. *///from w ww .ja v a 2 s . c o m protected final void replaceDeclarationWithAssignment(AstEditor editor, VariableDeclaration oldFragment) throws Exception { VariableDeclarationStatement oldStatement = (VariableDeclarationStatement) oldFragment.getParent(); Assert.isTrue(oldStatement.fragments().size() == 1); int oldStart = oldStatement.getStartPosition(); // prepare ExpressionStatement with assignment of existing expression to existing variable ExpressionStatement newStatement; { AST ast = m_variable.getAST(); // prepare assignment Assignment newAssignment; { newAssignment = ast.newAssignment(); // reuse variable { oldFragment.setName(ast.newSimpleName("__foo")); newAssignment.setLeftHandSide(m_variable); } // reuse initializer { Expression initializer = oldFragment.getInitializer(); oldFragment.setInitializer(null); newAssignment.setRightHandSide(initializer); } // source range AstNodeUtils.setSourceRange(newAssignment, newAssignment.getLeftHandSide(), newAssignment.getRightHandSide()); } // prepare ExpressionStatement newStatement = ast.newExpressionStatement(newAssignment); AstNodeUtils.setSourceRange(newStatement, newAssignment, 1); } // replace old expression in AST { List<Statement> oldStatements = DomGenerics.statements((Block) oldStatement.getParent()); int index = oldStatements.indexOf(oldStatement); oldStatements.set(index, newStatement); } // patch source to make it same as changed AST editor.replaceSubstring(oldStart, m_variable.getStartPosition() - oldStart, ""); }