org.eclipse.jdt.core.dom.SwitchCase Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.SwitchCase. 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: MissingSwitchDefaultQuickFix.java    From vscode-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(SwitchStatement node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                final SwitchCase defNode = node.getAST().newSwitchCase();
                defNode.setExpression(null);
                node.statements().add(defNode);
                node.statements().add(node.getAST().newBreakStatement());
            }
            return true; // also visit children
        }
    };
}
 
Example #2
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 6 votes vote down vote up
public boolean visit(SwitchCase stmnt){
	/*
	 * case Expression  :
               default :
	 */
	if (stmnt.isDefault()){
		styledString.append("default", new StyledStringStyler(keywordStyle));
		appendColon();
	}
	else {
		styledString.append("case", new StyledStringStyler(keywordStyle));
		appendSpace();
		handleExpression((Expression) stmnt.getExpression());
		appendColon();
	}
	return false;
}
 
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: CFG.java    From JDeodorant with MIT License 6 votes vote down vote up
private CFGNode createNonCompositeNode(StatementObject statement) {
	CFGNode currentNode;
	Statement astStatement = statement.getStatement();
	if(astStatement instanceof ReturnStatement)
		currentNode = new CFGExitNode(statement);
	else if(astStatement instanceof SwitchCase)
		currentNode = new CFGSwitchCaseNode(statement);
	else if(astStatement instanceof BreakStatement)
		currentNode = new CFGBreakNode(statement);
	else if(astStatement instanceof ContinueStatement)
		currentNode = new CFGContinueNode(statement);
	else if(astStatement instanceof ThrowStatement)
		currentNode = new CFGThrowNode(statement);
	else
		currentNode = new CFGNode(statement);
	directlyNestedNodeInBlock(currentNode);
	return currentNode;
}
 
Example #5
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getMissingCaseStatementProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> proposals) {
	if (node instanceof SwitchCase) {
		node= node.getParent();
	}
	if (!(node instanceof SwitchStatement))
		return false;

	SwitchStatement switchStatement= (SwitchStatement)node;
	ITypeBinding expressionBinding= switchStatement.getExpression().resolveTypeBinding();
	if (expressionBinding == null || !expressionBinding.isEnum())
		return false;

	ArrayList<String> missingEnumCases= new ArrayList<String>();
	boolean hasDefault= LocalCorrectionsSubProcessor.evaluateMissingSwitchCases(expressionBinding, switchStatement.statements(), missingEnumCases);
	if (missingEnumCases.size() == 0 && hasDefault)
		return false;

	if (proposals == null)
		return true;

	LocalCorrectionsSubProcessor.createMissingCaseProposals(context, switchStatement, missingEnumCases, proposals);
	return true;
}
 
Example #6
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean evaluateMissingSwitchCases(ITypeBinding enumBindings, List<Statement> switchStatements, ArrayList<String> enumConstNames) {
	IVariableBinding[] fields= enumBindings.getDeclaredFields();
	for (int i= 0; i < fields.length; i++) {
		if (fields[i].isEnumConstant()) {
			enumConstNames.add(fields[i].getName());
		}
	}

	boolean hasDefault=false;
	List<Statement> statements= switchStatements;
	for (int i= 0; i < statements.size(); i++) {
		Statement curr= statements.get(i);
		if (curr instanceof SwitchCase) {
			Expression expression= ((SwitchCase) curr).getExpression();
			if (expression instanceof SimpleName) {
				enumConstNames.remove(((SimpleName) expression).getFullyQualifiedName());
			} else if(expression== null){
				hasDefault=true;
			}
		}
	}
	return hasDefault;
}
 
Example #7
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 #8
Source File: NecessaryParenthesesChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) {
	if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
		// e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ...
		return false;
	}
	if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
			|| locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
			|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY
			|| locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY
			|| locationInParent == ForStatement.EXPRESSION_PROPERTY
			|| locationInParent == WhileStatement.EXPRESSION_PROPERTY
			|| locationInParent == DoStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.MESSAGE_PROPERTY
			|| locationInParent == IfStatement.EXPRESSION_PROPERTY
			|| locationInParent == SwitchStatement.EXPRESSION_PROPERTY
			|| locationInParent == SwitchCase.EXPRESSION_PROPERTY
			|| locationInParent == ArrayAccess.INDEX_PROPERTY
			|| locationInParent == ThrowStatement.EXPRESSION_PROPERTY
			|| locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY
			|| locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
		return false;
	}
	return true;
}
 
Example #9
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(SwitchCase node) {
	// switch on enum allows to use enum constants without qualification
	if (hasFlag(VARIABLES, fFlags) && !node.isDefault() && isInside(node.getExpression())) {
		SwitchStatement switchStatement= (SwitchStatement) node.getParent();
		ITypeBinding binding= switchStatement.getExpression().resolveTypeBinding();
		if (binding != null && binding.isEnum()) {
			IVariableBinding[] declaredFields= binding.getDeclaredFields();
			for (int i= 0; i < declaredFields.length; i++) {
				IVariableBinding curr= declaredFields[i];
				if (curr.isEnumConstant()) {
					fBreak= fRequestor.acceptBinding(curr);
					if (fBreak)
						return false;
				}
			}
		}
	}
	return false;
}
 
Example #10
Source File: ExtractConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node= fragment.getAssociatedNode();
	ASTNode parent= node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf= (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName()))
			return false;
	}
	if (parent instanceof ExpressionStatement)
		return false;
	if (parent instanceof SwitchCase) {
		if (node instanceof Name) {
			Name name= (Name) node;
			ITypeBinding typeBinding= name.resolveTypeBinding();
			if (typeBinding != null) {
				return !typeBinding.isEnum();
			}
		}
	}
	return true;
}
 
Example #11
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node= fragment.getAssociatedNode();
	ASTNode parent= node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf= (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName()))
			return false;
	}
	if (isMethodParameter(node))
		return false;
	if (isThrowableInCatchBlock(node))
		return false;
	if (parent instanceof ExpressionStatement)
		return false;
	if (isLeftValue(node))
		return false;
	if (isReferringToLocalVariableFromFor((Expression) node))
		return false;
	if (isUsedInForInitializerOrUpdater((Expression) node))
		return false;
	if (parent instanceof SwitchCase)
		return false;
	return true;
}
 
Example #12
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 #13
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static void addMissingDefaultCaseProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
	ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Expression) {
		StructuralPropertyDescriptor locationInParent = selectedNode.getLocationInParent();
		ASTNode parent = selectedNode.getParent();
		List<Statement> statements;

		if (locationInParent == SwitchStatement.EXPRESSION_PROPERTY) {
			statements = ((SwitchStatement) parent).statements();
		} else if (locationInParent == SwitchExpression.EXPRESSION_PROPERTY) {
			statements = ((SwitchExpression) parent).statements();
		} else {
			return;
		}

		for (Statement statement : statements) {
			if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
				return;
			}
		}
		createMissingDefaultProposal(context, parent, proposals);
	}
}
 
Example #14
Source File: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node = fragment.getAssociatedNode();
	ASTNode parent = node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName())) {
			return false;
		}
	}
	if (parent instanceof ExpressionStatement) {
		return false;
	}
	if (parent instanceof SwitchCase) {
		if (node instanceof Name) {
			Name name = (Name) node;
			ITypeBinding typeBinding = name.resolveTypeBinding();
			if (typeBinding != null) {
				return !typeBinding.isEnum();
			}
		}
	}
	return true;
}
 
Example #15
Source File: MissingSwitchDefaultQuickfix.java    From eclipse-cs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo,
        final int markerStartOffset) {

  return new ASTVisitor() {

    @SuppressWarnings("unchecked")
    @Override
    public boolean visit(SwitchStatement node) {
      if (containsPosition(lineInfo, node.getStartPosition())) {
        SwitchCase defNode = node.getAST().newSwitchCase();
        defNode.setExpression(null);
        node.statements().add(defNode);
        node.statements().add(node.getAST().newBreakStatement());
      }
      return true; // also visit children
    }
  };
}
 
Example #16
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node = fragment.getAssociatedNode();
	ASTNode parent = node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName())) {
			return false;
		}
	}
	if (isMethodParameter(node)) {
		return false;
	}
	if (isThrowableInCatchBlock(node)) {
		return false;
	}
	if (parent instanceof ExpressionStatement) {
		return false;
	}
	if (parent instanceof LambdaExpression) {
		return false;
	}
	if (isLeftValue(node)) {
		return false;
	}
	if (isReferringToLocalVariableFromFor((Expression) node)) {
		return false;
	}
	if (isUsedInForInitializerOrUpdater((Expression) node)) {
		return false;
	}
	if (parent instanceof SwitchCase) {
		return false;
	}
	return true;
}
 
Example #17
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static SwitchCase[] createSwitchCaseStatements(AST ast, ASTRewrite rewrite, List<Expression> caseExpressions) {
	int len= (caseExpressions.size() == 0) ? 1 : caseExpressions.size();
	SwitchCase[] switchCaseStatements= new SwitchCase[len];
	if (caseExpressions.size() == 0) {
		switchCaseStatements[0]= ast.newSwitchCase();
		switchCaseStatements[0].setExpression(null);
	} else {
		for (int i= 0; i < caseExpressions.size(); i++) {
			ASTNode astNode= caseExpressions.get(i);
			switchCaseStatements[i]= ast.newSwitchCase();
			switchCaseStatements[i].setExpression((Expression) rewrite.createCopyTarget(astNode));
		}
	}
	return switchCaseStatements;
}
 
Example #18
Source File: CodeSearch.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(SwitchCase node) {
	int start = _unit.getLineNumber(node.getStartPosition());
	if(start == _extendedLine){
		_extendedStatement = node;
		return false;
	}
	return true;
}
 
Example #19
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void createMissingDefaultProposal(IInvocationContext context, SwitchStatement switchStatement, Image image, Collection<ICommandAccess> proposals) {
	AST ast= switchStatement.getAST();
	ASTRewrite astRewrite= ASTRewrite.create(ast);
	ListRewrite listRewrite= astRewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);

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

	String label= CorrectionMessages.LocalCorrectionsSubProcessor_add_default_case_description;
	proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_DEFAULT_CASE, image));
}
 
Example #20
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 #21
Source File: CodeBlock.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private SwCase visit(SwitchCase node) {
	int startLine = _cunit.getLineNumber(node.getStartPosition());
	int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
	SwCase swCase = new SwCase(startLine, endLine, node);
	
	if(node.getExpression() != null){
		Expr expression = (Expr) process(node.getExpression());
		expression.setParent(swCase);
		swCase.setExpression(expression);
	}
	
	return swCase;
}
 
Example #22
Source File: MethodControlFlowVisitor.java    From DesigniteJava with Apache License 2.0 5 votes vote down vote up
public boolean visit(SwitchCase node) {
	switchCases.add(node);
	if (!node.isDefault()) {
		switchCasesWitoutDefaults.add(node);
	}
	return true;
}
 
Example #23
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void addFallThroughProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof SwitchCase && selectedNode.getLocationInParent() == SwitchStatement.STATEMENTS_PROPERTY) {
		AST ast= selectedNode.getAST();
		ASTNode parent= selectedNode.getParent();

		// insert break:
		ASTRewrite rewrite= ASTRewrite.create(ast);
		ListRewrite listRewrite= rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
		listRewrite.insertBefore(ast.newBreakStatement(), selectedNode, null);

		String label= CorrectionMessages.LocalCorrectionsSubProcessor_insert_break_statement;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_BREAK_STATEMENT, image);
		proposals.add(proposal);

		// insert //$FALL-THROUGH$:
		rewrite= ASTRewrite.create(ast);
		rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
		listRewrite= rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
		ASTNode fallThroughComment= rewrite.createStringPlaceholder("//$FALL-THROUGH$", ASTNode.EMPTY_STATEMENT); //$NON-NLS-1$
		listRewrite.insertBefore(fallThroughComment, selectedNode, null);

		label= CorrectionMessages.LocalCorrectionsSubProcessor_insert_fall_through;
		image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_FALL_THROUGH, image);
		proposals.add(proposal);
	}
}
 
Example #24
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 #25
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 #26
Source File: StatementAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static List<SwitchCase> getSwitchCases(SwitchStatement node) {
	List<SwitchCase> result= new ArrayList<SwitchCase>();
	for (Iterator<Statement> iter= node.statements().iterator(); iter.hasNext(); ) {
		Object element= iter.next();
		if (element instanceof SwitchCase)
			result.add((SwitchCase) element);
	}
	return result;
}
 
Example #27
Source File: StatementAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(SwitchStatement node) {
	ASTNode[] selectedNodes= getSelectedNodes();
	if (doAfterValidation(node, selectedNodes)) {
		List<SwitchCase> cases= getSwitchCases(node);
		for (int i= 0; i < selectedNodes.length; i++) {
			ASTNode topNode= selectedNodes[i];
			if (cases.contains(topNode)) {
				invalidSelection(RefactoringCoreMessages.StatementAnalyzer_switch_statement);
				break;
			}
		}
	}
	super.endVisit(node);
}
 
Example #28
Source File: CFGSwitchCaseNode.java    From JDeodorant with MIT License 5 votes vote down vote up
public CFGSwitchCaseNode(AbstractStatement statement) {
	super(statement);
	SwitchCase switchCase = (SwitchCase)statement.getStatement();
	if(switchCase.isDefault())
		isDefault = true;
	else
		isDefault = false;
}
 
Example #29
Source File: SwitchControlCase.java    From JDeodorant with MIT License 5 votes vote down vote up
@Override
public boolean match(SwitchControlCase otherCase, ASTNodeMatcher matcher)
{
	if (this.body.size() == otherCase.body.size())
	{
		boolean caseStatementsMatch = true;
		for (int i = 0; i < this.body.size(); i++)
		{
			Statement currentThisStatement = this.body.get(i);
			Statement currentOtherStatement = otherCase.body.get(i);
			boolean switchCaseStatementMatch = false;
			if (currentThisStatement instanceof SwitchCase && currentOtherStatement instanceof SwitchCase)
			{
				SwitchCase currentThisSwitchCase = (SwitchCase)currentThisStatement;
				SwitchCase currentOtherSwitchCase = (SwitchCase)currentOtherStatement;
				if(currentThisSwitchCase.isDefault() && currentOtherSwitchCase.isDefault())
				{
					switchCaseStatementMatch = true;
				}
				else
				{
					switchCaseStatementMatch = matchCaseCondition(currentThisSwitchCase.getExpression(), currentOtherSwitchCase.getExpression());
				}
			}
			boolean endStatementMatch = ((currentThisStatement instanceof BreakStatement && currentOtherStatement instanceof BreakStatement) ||
					(currentThisStatement instanceof ContinueStatement && currentOtherStatement instanceof ContinueStatement) ||
					(currentThisStatement instanceof ReturnStatement && currentOtherStatement instanceof ReturnStatement));
			if (!switchCaseStatementMatch && !endStatementMatch)
			{
				caseStatementsMatch = false;
			}
		}
		return caseStatementsMatch;
	}
	return false;
}
 
Example #30
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void checkExpression(RefactoringStatus status) {
	ASTNode[] nodes= getSelectedNodes();
	if (nodes != null && nodes.length == 1) {
		ASTNode node= nodes[0];
		if (node instanceof Type) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference, JavaStatusContext.create(fCUnit, node));
		} else if (node.getLocationInParent() == SwitchCase.EXPRESSION_PROPERTY) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_switch_case, JavaStatusContext.create(fCUnit, node));
		} else if (node instanceof Annotation || ASTNodes.getParent(node, Annotation.class) != null) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_from_annotation, JavaStatusContext.create(fCUnit, node));
		}
	}
}