Java Code Examples for org.eclipse.jdt.core.dom.SwitchStatement#EXPRESSION_PROPERTY

The following examples show how to use org.eclipse.jdt.core.dom.SwitchStatement#EXPRESSION_PROPERTY . 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 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 2
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 3
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 4
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 5
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void getMissingEnumConstantCaseProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	for (Iterator<ICommandAccess> iterator= proposals.iterator(); iterator.hasNext();) {
		ICommandAccess proposal= iterator.next();
		if (proposal instanceof ChangeCorrectionProposal) {
			if (CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description.equals(((ChangeCorrectionProposal) proposal).getName())) {
				return;
			}
		}
	}
	
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
		SwitchStatement statement= (SwitchStatement) selectedNode.getParent();
		ITypeBinding binding= statement.getExpression().resolveTypeBinding();
		if (binding == null || !binding.isEnum()) {
			return;
		}

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

		createMissingCaseProposals(context, statement, missingEnumCases, proposals);
	}
}
 
Example 6
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 7
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);
	}
}