Java Code Examples for org.eclipse.jdt.core.dom.SwitchCase#setExpression()

The following examples show how to use org.eclipse.jdt.core.dom.SwitchCase#setExpression() . 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 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 2
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 3
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 4
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);
}