Java Code Examples for org.eclipse.jdt.core.dom.Statement#getParent()

The following examples show how to use org.eclipse.jdt.core.dom.Statement#getParent() . 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: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isLastStatementInEnclosingMethodOrLambda(Statement statement) {
	ASTNode currentStructure= statement;
	ASTNode currentParent= statement.getParent();
	while (!(currentParent instanceof MethodDeclaration || currentParent instanceof LambdaExpression)) {
		// should not be in a loop
		if (currentParent instanceof ForStatement || currentParent instanceof EnhancedForStatement
				|| currentParent instanceof WhileStatement || currentParent instanceof DoStatement) {
			return false;
		}
		if (currentParent instanceof Block) {
			Block parentBlock= (Block) currentParent;
			if (parentBlock.statements().indexOf(currentStructure) != parentBlock.statements().size() - 1) { // not last statement in the block
				return false;
			}
		}
		currentStructure= currentParent;
		currentParent= currentParent.getParent();
	}
	return true;
}
 
Example 2
Source File: PDG.java    From JDeodorant with MIT License 6 votes vote down vote up
private PDGNode getLoopNodeUnderLabel(PDGNode node, String label) {
	for(GraphEdge edge : node.incomingEdges) {
		PDGDependence dependence = (PDGDependence)edge;
		if(dependence instanceof PDGControlDependence) {
			PDGControlDependence controlDependence = (PDGControlDependence)dependence;
			PDGNode srcPDGNode = (PDGNode)controlDependence.src;
			CFGNode srcCFGNode = srcPDGNode.getCFGNode();
			if(srcCFGNode instanceof CFGBranchLoopNode || srcCFGNode instanceof CFGBranchDoLoopNode || srcCFGNode instanceof CFGBranchSwitchNode) {
				Statement predicate = srcCFGNode.getASTStatement();
				if(predicate.getParent() instanceof LabeledStatement) {
					LabeledStatement labeled = (LabeledStatement)predicate.getParent();
					if(labeled.getLabel().getIdentifier().equals(label))
						return srcPDGNode;
				}
			}
			return getLoopNodeUnderLabel(srcPDGNode, label);
		}
	}
	return null;
}
 
Example 3
Source File: PDG.java    From JDeodorant with MIT License 6 votes vote down vote up
private PDGNode findParentOfBlockNode(PDGBlockNode blockNode) {
	Statement statement = blockNode.getASTStatement();
	ASTNode parent = statement.getParent();
	while(parent instanceof Block) {
		parent = parent.getParent();
	}
	if(entryNode.getMethod().getMethodDeclaration().equals(parent)) {
		return entryNode;
	}
	for(GraphNode node : nodes) {
		PDGNode pdgNode = (PDGNode)node;
		if(pdgNode.getASTStatement().equals(parent)) {
			return pdgNode;
		}
	}
	return null;
}
 
Example 4
Source File: ExtractMethodFragmentRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
protected ListRewrite createTryStatementIfNeeded(ASTRewrite sourceRewriter, AST ast, ListRewrite bodyRewrite, PDGNode node) {
	Statement statement = node.getASTStatement();
	ASTNode statementParent = statement.getParent();
	if(statementParent != null && statementParent instanceof Block)
		statementParent = statementParent.getParent();
	if(statementParent != null && statementParent instanceof TryStatement) {
		TryStatement tryStatementParent = (TryStatement)statementParent;
		if(tryStatementsToBeRemoved.contains(tryStatementParent) || tryStatementsToBeCopied.contains(tryStatementParent)) {
			if(tryStatementBodyRewriteMap.containsKey(tryStatementParent)) {
				bodyRewrite = tryStatementBodyRewriteMap.get(tryStatementParent);
			}
			else {
				TryStatement newTryStatement = copyTryStatement(sourceRewriter, ast, tryStatementParent);
				Block tryMethodBody = ast.newBlock();
				sourceRewriter.set(newTryStatement, TryStatement.BODY_PROPERTY, tryMethodBody, null);
				ListRewrite tryBodyRewrite = sourceRewriter.getListRewrite(tryMethodBody, Block.STATEMENTS_PROPERTY);
				tryStatementBodyRewriteMap.put(tryStatementParent, tryBodyRewrite);
				bodyRewrite.insertLast(newTryStatement, null);
				bodyRewrite = tryBodyRewrite;
			}
		}
	}
	return bodyRewrite;
}
 
Example 5
Source File: ControlStatementsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void handle(Statement body, ChildPropertyDescriptor bodyProperty) {
	if ((body.getFlags() & ASTNode.RECOVERED) != 0)
		return;
	Statement parent= (Statement)body.getParent();
	if ((parent.getFlags() & ASTNode.RECOVERED) != 0)
		return;

	if (fRemoveUnnecessaryBlocksOnlyWhenReturnOrThrow) {
		if (!(body instanceof Block)) {
			if (body.getNodeType() != ASTNode.IF_STATEMENT && body.getNodeType() != ASTNode.RETURN_STATEMENT && body.getNodeType() != ASTNode.THROW_STATEMENT) {
				fResult.add(new AddBlockOperation(bodyProperty, body, parent));
			}
		} else {
			if (RemoveBlockOperation.satisfiesCleanUpPrecondition(parent, bodyProperty, true)) {
				fResult.add(new RemoveBlockOperation(parent, bodyProperty));
			}
		}
	} else if (fFindControlStatementsWithoutBlock) {
		if (!(body instanceof Block)) {
			fResult.add(new AddBlockOperation(bodyProperty, body, parent));
		}
	} else if (fRemoveUnnecessaryBlocks) {
		if (RemoveBlockOperation.satisfiesCleanUpPrecondition(parent, bodyProperty, false)) {
			fResult.add(new RemoveBlockOperation(parent, bodyProperty));
		}
	}
}
 
Example 6
Source File: ExtractToNullCheckedLocalProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static RearrangeStrategy create(Statement origStmt, ASTRewrite rewrite, TextEditGroup group) {
	ASTNode parent = origStmt.getParent();
	if (parent instanceof Block) {
		Block block= (Block)parent;
		if (origStmt instanceof VariableDeclarationStatement)
			return new ModifyBlockWithLocalDecl(origStmt, block, rewrite, group);
		else
			return new ModifyBlock(origStmt, block, rewrite, group);
	} else {
		return new ReplaceStatement(origStmt, rewrite, group);
	}
}
 
Example 7
Source File: ExtractMethodFragmentRefactoring.java    From JDeodorant with MIT License 5 votes vote down vote up
private LabeledStatement belongsToLabeledStatement(PDGNode pdgNode) {
	Statement statement = pdgNode.getASTStatement();
	if(statement.getParent() instanceof LabeledStatement) {
		return (LabeledStatement) statement.getParent();
	}
	return null;
}
 
Example 8
Source File: BuggyCode.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
public boolean process(Statement statement) {

			// TODO : wait for completing ...
			
			int start = _unit.getLineNumber(statement.getStartPosition());
			int end = _unit.getLineNumber(statement.getStartPosition() + statement.getLength());

			if (start <= _buggyLine && _buggyLine <= end) {
				if (statement instanceof IfStatement || statement instanceof ForStatement
						|| statement instanceof WhileStatement || statement instanceof DoStatement
						|| statement instanceof EnhancedForStatement) {
					_nodes.add(statement);
					return false;
				} else if(statement instanceof Block){
					if(statement.getParent() instanceof IfStatement && (end - start) < Constant.MAX_BLOCK_LINE){
						_nodes.add(statement.getParent());
						return true;
					}
					Block block = (Block) statement;
					for(Object object : block.statements()){
						process((Statement)object);
					}
				} else if(statement instanceof SwitchStatement){
					SwitchStatement switchStmt = (SwitchStatement) statement;
					for(int i = 0; i < switchStmt.statements().size(); i++){
						Statement stmt = (Statement) switchStmt.statements().get(i);
						int s = _unit.getLineNumber(stmt.getStartPosition());
						int e = _unit.getLineNumber(stmt.getStartPosition() + stmt.getLength());
						if(s <= _buggyLine && _buggyLine <= e){
							_nodes.add(stmt);
							if(stmt instanceof SwitchCase){
								for(int j = i + 1 ; j < switchStmt.statements().size(); j++){
									Statement SC = (Statement) switchStmt.statements().get(j);
									if(SC instanceof SwitchCase){
										return false;
									} else {
										_nodes.add(SC);
									}
								}
							} else {
								_nodes.add(stmt);
								return false;
							}
						}
					}
					
				} else {
					_nodes.add(statement);
					return false;
				}
			}

			return true;
		}