Java Code Examples for org.eclipse.jdt.core.dom.SwitchStatement#statements()

The following examples show how to use org.eclipse.jdt.core.dom.SwitchStatement#statements() . 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: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static void addCasesOmittedProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
	ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
		AST ast = selectedNode.getAST();
		SwitchStatement parent = (SwitchStatement) selectedNode.getParent();

		for (Statement statement : (List<Statement>) parent.statements()) {
			if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {

				// insert //$CASES-OMITTED$:
				ASTRewrite rewrite = ASTRewrite.create(ast);
				rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
				ListRewrite listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
				ASTNode casesOmittedComment = rewrite.createStringPlaceholder("//$CASES-OMITTED$", ASTNode.EMPTY_STATEMENT); //$NON-NLS-1$
				listRewrite.insertBefore(casesOmittedComment, statement, null);

				String label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_cases_omitted;
				ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_CASES_OMITTED);
				proposals.add(proposal);
				break;
			}
		}
	}
}
 
Example 2
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void addCasesOmittedProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
		AST ast= selectedNode.getAST();
		SwitchStatement parent= (SwitchStatement) selectedNode.getParent();
		
		for (Statement statement : (List<Statement>) parent.statements()) {
			if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
				
				// insert //$CASES-OMITTED$:
				ASTRewrite rewrite= ASTRewrite.create(ast);
				rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
				ListRewrite listRewrite= rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
				ASTNode casesOmittedComment= rewrite.createStringPlaceholder("//$CASES-OMITTED$", ASTNode.EMPTY_STATEMENT); //$NON-NLS-1$
				listRewrite.insertBefore(casesOmittedComment, statement, null);
				
				String label= CorrectionMessages.LocalCorrectionsSubProcessor_insert_cases_omitted;
				Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
				ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_CASES_OMITTED, image);
				proposals.add(proposal);
				break;
			}
		}
	}
}
 
Example 3
Source File: SwitchControlStructure.java    From JDeodorant with MIT License 6 votes vote down vote up
private List<AbstractControlCase> createSwitchCases(SwitchStatement switchStatement)
{
	List<AbstractControlCase> returnList  = new ArrayList<AbstractControlCase>();
	List<AbstractControlCase> tempList    = new ArrayList<AbstractControlCase>();
	List<Statement> switchGroupStatements = switchStatement.statements();
	for (Statement currentStatement : switchGroupStatements)
	{
		if (currentStatement instanceof SwitchCase)
		{
			Expression caseValue = ((SwitchCase)currentStatement).getExpression();
			SwitchControlCase newCase = new SwitchControlCase(this.variable, caseValue, new ArrayList<Statement>());
			tempList.add(newCase);
			addToAll((SwitchCase)currentStatement, tempList);
		}
		else if (currentStatement instanceof BreakStatement || currentStatement instanceof ReturnStatement || currentStatement instanceof ContinueStatement)
		{
			addToAll(currentStatement, tempList);
			returnList.addAll(tempList);
			tempList = new ArrayList<AbstractControlCase>();
		}
	}
	return returnList;
}
 
Example 4
Source File: CodeBlock.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private SwitchStmt visit(SwitchStatement node) {
	int startLine = _cunit.getLineNumber(node.getStartPosition());
	int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
	SwitchStmt switchStmt = new SwitchStmt(startLine, endLine, node);
	
	Expr expression = (Expr) process(node.getExpression());
	expression.setParent(switchStmt);
	switchStmt.setExpression(expression);
	
	SwCase lastSW = null;
	List<Stmt> statements = new ArrayList<>();
	for(Object object : node.statements()){
		Stmt stmt = (Stmt) process((ASTNode) object);
		stmt.setParent(switchStmt);
		if (stmt instanceof SwCase) {
			lastSW = (SwCase) stmt;
			statements.add(stmt);
		} else if(lastSW != null){
			lastSW.addSibling(stmt);
		} else {
			statements.add(stmt);
		}
	}
	switchStmt.setStatements(statements);
	
	return switchStmt;
}
 
Example 5
Source File: ImplementationSmellDetector.java    From DesigniteJava with Apache License 2.0 5 votes vote down vote up
private boolean switchIsMissingDefault(SwitchStatement switchStatement) {
	List<Statement> statetmentsOfSwitch = switchStatement.statements();
	for(Statement stm : statetmentsOfSwitch) {
		if ((stm instanceof SwitchCase) && ((SwitchCase)stm).isDefault()) {
			return true;
		}
	}
	return false;			
}
 
Example 6
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected SwitchData createSwitchData(SwitchStatement node) {
	SwitchData result = new SwitchData();
	List<Statement> statements = node.statements();
	if (statements.isEmpty()) {
		return result;
	}

	int start = -1, end = -1;
	GenericSequentialFlowInfo info = null;

	for (Iterator<Statement> iter = statements.iterator(); iter.hasNext();) {
		Statement statement = iter.next();
		if (statement instanceof SwitchCase) {
			SwitchCase switchCase = (SwitchCase) statement;
			if (switchCase.isDefault()) {
				result.setHasDefaultCase();
			}
			if (info == null) {
				info = createSequential();
				start = statement.getStartPosition();
			} else {
				if (info.isReturn() || info.isPartialReturn() || info.branches()) {
					result.add(new Region(start, end - start + 1), info);
					info = createSequential();
					start = statement.getStartPosition();
				}
			}
		} else {
			info.merge(getFlowInfo(statement), fFlowContext);
		}
		end = statement.getStartPosition() + statement.getLength() - 1;
	}
	result.add(new Region(start, end - start + 1), info);
	return result;
}
 
Example 7
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static void getMissingEnumConstantCaseProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
	for (ChangeCorrectionProposal proposal : proposals) {
		if (CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description.equals(proposal.getName())) {
			return;
		}
	}

	ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Expression) {
		StructuralPropertyDescriptor locationInParent = selectedNode.getLocationInParent();
		ASTNode parent = selectedNode.getParent();
		ITypeBinding binding;
		List<Statement> statements;

		if (locationInParent == SwitchStatement.EXPRESSION_PROPERTY) {
			SwitchStatement statement = (SwitchStatement) parent;
			binding = statement.getExpression().resolveTypeBinding();
			statements = statement.statements();
		} else if (locationInParent == SwitchExpression.EXPRESSION_PROPERTY) {
			SwitchExpression switchExpression = (SwitchExpression) parent;
			binding = switchExpression.getExpression().resolveTypeBinding();
			statements = switchExpression.statements();
		} else {
			return;
		}

		if (binding == null || !binding.isEnum()) {
			return;
		}

		ArrayList<String> missingEnumCases = new ArrayList<>();
		boolean hasDefault = evaluateMissingSwitchCases(binding, statements, missingEnumCases);
		if (missingEnumCases.size() == 0 && hasDefault) {
			return;
		}

		createMissingCaseProposals(context, parent, missingEnumCases, proposals);
	}
}
 
Example 8
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected SwitchData createSwitchData(SwitchStatement node) {
	SwitchData result= new SwitchData();
	List<Statement> statements= node.statements();
	if (statements.isEmpty())
		return result;

	int start= -1, end= -1;
	GenericSequentialFlowInfo info= null;

	for (Iterator<Statement> iter= statements.iterator(); iter.hasNext(); ) {
		Statement statement= iter.next();
		if (statement instanceof SwitchCase) {
			SwitchCase switchCase= (SwitchCase)statement;
			if (switchCase.isDefault()) {
				result.setHasDefaultCase();
			}
			if (info == null) {
				info= createSequential();
				start= statement.getStartPosition();
			} else {
				if (info.isReturn() || info.isPartialReturn() || info.branches()) {
					result.add(new Region(start, end - start + 1), info);
					info= createSequential();
					start= statement.getStartPosition();
				}
			}
		} else {
			info.merge(getFlowInfo(statement), fFlowContext);
		}
		end= statement.getStartPosition() + statement.getLength() - 1;
	}
	result.add(new Region(start, end - start + 1), info);
	return result;
}
 
Example 9
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void addMissingDefaultCaseProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
		SwitchStatement switchStatement= (SwitchStatement) selectedNode.getParent();
		for (Statement statement : (List<Statement>) switchStatement.statements()) {
			if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
				return;
			}
		}
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		createMissingDefaultProposal(context, switchStatement, image, proposals);
	}
}
 
Example 10
Source File: TypeCheckElimination.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean isTypeCheckMethodStateSetter() {
	InheritanceTree tree = null;
	if(existingInheritanceTree != null)
		tree = existingInheritanceTree;
	else if(inheritanceTreeMatchingWithStaticTypes != null)
		tree = inheritanceTreeMatchingWithStaticTypes;
	if(tree != null) {
		DefaultMutableTreeNode root = tree.getRootNode();
		DefaultMutableTreeNode leaf = root.getFirstLeaf();
		List<String> subclassNames = new ArrayList<String>();
		while(leaf != null) {
			subclassNames.add((String)leaf.getUserObject());
			leaf = leaf.getNextLeaf();
		}
		Block typeCheckMethodBody = typeCheckMethod.getBody();
		List<Statement> statements = typeCheckMethodBody.statements();
		if(statements.size() > 0 && statements.get(0) instanceof SwitchStatement) {
			SwitchStatement switchStatement = (SwitchStatement)statements.get(0);
			List<Statement> statements2 = switchStatement.statements();
			ExpressionExtractor expressionExtractor = new ExpressionExtractor();
			int matchCounter = 0;
			for(Statement statement2 : statements2) {
				if(!(statement2 instanceof SwitchCase) && !(statement2 instanceof BreakStatement)) {
					List<Expression> classInstanceCreations = expressionExtractor.getClassInstanceCreations(statement2);
					if(classInstanceCreations.size() == 1) {
						ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)classInstanceCreations.get(0);
						Type classInstanceCreationType = classInstanceCreation.getType();
						if(subclassNames.contains(classInstanceCreationType.resolveBinding().getQualifiedName())) {
							matchCounter++;
						}
					}
				}
			}
			if(matchCounter == subclassNames.size())
				return true;
		}
	}
	return false;
}
 
Example 11
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static void createMissingDefaultProposal(IInvocationContext context, ASTNode parent, Collection<ChangeCorrectionProposal> proposals) {
	List<Statement> statements;
	Expression expression;
	if (parent instanceof SwitchStatement) {
		SwitchStatement switchStatement = (SwitchStatement) parent;
		statements = switchStatement.statements();
		expression = switchStatement.getExpression();
	} else if (parent instanceof SwitchExpression) {
		SwitchExpression switchExpression = (SwitchExpression) parent;
		statements = switchExpression.statements();
		expression = switchExpression.getExpression();
	} else {
		return;
	}
	AST ast = parent.getAST();
	ASTRewrite astRewrite = ASTRewrite.create(ast);
	ListRewrite listRewrite;
	if (parent instanceof SwitchStatement) {
		listRewrite = astRewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
	} else {
		listRewrite = astRewrite.getListRewrite(parent, SwitchExpression.STATEMENTS_PROPERTY);
	}
	String label = CorrectionMessages.LocalCorrectionsSubProcessor_add_default_case_description;
	LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_DEFAULT_CASE);

	SwitchCase newSwitchCase = ast.newSwitchCase();
	listRewrite.insertLast(newSwitchCase, null);

	if (ASTHelper.isSwitchCaseExpressionsSupportedInAST(ast)) {
		if (statements.size() > 0) {
			Statement firstStatement = statements.get(0);
			SwitchCase switchCase = (SwitchCase) firstStatement;
			boolean isArrow = switchCase.isSwitchLabeledRule();
			newSwitchCase.setSwitchLabeledRule(isArrow);
			if (isArrow || parent instanceof SwitchExpression) {
				ThrowStatement newThrowStatement = getThrowForUnexpectedDefault(expression, ast, astRewrite);
				listRewrite.insertLast(newThrowStatement, null);
				proposal.addLinkedPosition(astRewrite.track(newThrowStatement), true, null);
			} else {
				listRewrite.insertLast(ast.newBreakStatement(), null);
			}
		} else {
			listRewrite.insertLast(ast.newBreakStatement(), null);
		}
	} else {
		newSwitchCase.setExpression(null);
		listRewrite.insertLast(ast.newBreakStatement(), null);
	}

	proposals.add(proposal);
}
 
Example 12
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 4 votes vote down vote up
private boolean typeObjectGetterMethodAlreadyExists() {
	InheritanceTree tree = typeCheckElimination.getInheritanceTreeMatchingWithStaticTypes();
	if(tree != null) {
		MethodDeclaration[] contextMethods = sourceTypeDeclaration.getMethods();
		DefaultMutableTreeNode rootNode = tree.getRootNode();
		String rootClassName = (String)rootNode.getUserObject();
		DefaultMutableTreeNode leaf = rootNode.getFirstLeaf();
		List<String> subclassNames = new ArrayList<String>();
		while(leaf != null) {
			subclassNames.add((String)leaf.getUserObject());
			leaf = leaf.getNextLeaf();
		}
		for(MethodDeclaration contextMethod : contextMethods) {
			Type returnType = contextMethod.getReturnType2();
			if(returnType != null) {
				if(returnType.resolveBinding().getQualifiedName().equals(rootClassName)) {
					Block contextMethodBody = contextMethod.getBody();
					if(contextMethodBody != null) {
						List<Statement> statements = contextMethodBody.statements();
						if(statements.size() > 0 && statements.get(0) instanceof SwitchStatement) {
							SwitchStatement switchStatement = (SwitchStatement)statements.get(0);
							List<Statement> statements2 = switchStatement.statements();
							int matchCounter = 0;
							for(Statement statement2 : statements2) {
								if(statement2 instanceof ReturnStatement) {
									ReturnStatement returnStatement = (ReturnStatement)statement2;
									Expression returnStatementExpression = returnStatement.getExpression();
									if(returnStatementExpression instanceof ClassInstanceCreation) {
										ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)returnStatementExpression;
										Type classInstanceCreationType = classInstanceCreation.getType();
										if(subclassNames.contains(classInstanceCreationType.resolveBinding().getQualifiedName())) {
											matchCounter++;
										}
									}
								}
							}
							if(matchCounter == subclassNames.size())
								return true;
						}
					}
				}
			}
		}
	}
	return false;
}