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; }
@Override public void endVisit(SwitchStatement node) { if (skipNode(node)) { return; } SwitchData data = createSwitchData(node); IRegion[] ranges = data.getRanges(); for (int i = 0; i < ranges.length; i++) { IRegion range = ranges[i]; if (fSelection.coveredBy(range)) { GenericSequentialFlowInfo info = createSequential(); setFlowInfo(node, info); info.merge(getFlowInfo(node.getExpression()), fFlowContext); info.merge(data.getInfo(i), fFlowContext); info.removeLabel(null); return; } } super.endVisit(node, data); }
private Statement getParentLoopBody(ASTNode node) { Statement stmt = null; ASTNode start = node; while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) { start = start.getParent(); } if (start instanceof ForStatement) { stmt = ((ForStatement) start).getBody(); } else if (start instanceof DoStatement) { stmt = ((DoStatement) start).getBody(); } else if (start instanceof WhileStatement) { stmt = ((WhileStatement) start).getBody(); } else if (start instanceof EnhancedForStatement) { stmt = ((EnhancedForStatement) start).getBody(); } if (start != null && start.getParent() instanceof LabeledStatement) { LabeledStatement labeledStatement = (LabeledStatement) start.getParent(); fEnclosingLoopLabel = labeledStatement.getLabel(); } return stmt; }
@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; }
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; }
@Override public void endVisit(SwitchStatement node) { if (skipNode(node)) return; SwitchData data = createSwitchData(node); IRegion[] ranges = data.getRanges(); for (int i = 0; i < ranges.length; i++) { IRegion range = ranges[i]; if (fSelection.coveredBy(range)) { GenericSequentialFlowInfo info = createSequential(); setFlowInfo(node, info); info.merge(getFlowInfo(node.getExpression()), fFlowContext); info.merge(data.getInfo(i), fFlowContext); info.removeLabel(null); return; } } super.endVisit(node, data); }
@Override public void endVisit(SwitchStatement node) { if (skipNode(node)) return; SwitchData data= createSwitchData(node); IRegion[] ranges= data.getRanges(); for (int i= 0; i < ranges.length; i++) { IRegion range= ranges[i]; if (fSelection.coveredBy(range)) { GenericSequentialFlowInfo info= createSequential(); setFlowInfo(node, info); info.merge(getFlowInfo(node.getExpression()), fFlowContext); info.merge(data.getInfo(i), fFlowContext); info.removeLabel(null); return; } } super.endVisit(node, data); }
private Statement getParentLoopBody(ASTNode node) { Statement stmt= null; ASTNode start= node; while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) { start= start.getParent(); } if (start instanceof ForStatement) { stmt= ((ForStatement)start).getBody(); } else if (start instanceof DoStatement) { stmt= ((DoStatement)start).getBody(); } else if (start instanceof WhileStatement) { stmt= ((WhileStatement)start).getBody(); } else if (start instanceof EnhancedForStatement) { stmt= ((EnhancedForStatement)start).getBody(); } if (start != null && start.getParent() instanceof LabeledStatement) { LabeledStatement labeledStatement= (LabeledStatement)start.getParent(); fEnclosingLoopLabel= labeledStatement.getLabel(); } return stmt; }
@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; }
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; } } } }
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); } }
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; }
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, 4, image); proposals.add(proposal); break; } } } }
public boolean visit(SwitchStatement node) { this.buffer.append("switch (");//$NON-NLS-1$ node.getExpression().accept(this); this.buffer.append(") ");//$NON-NLS-1$ this.buffer.append("{\n");//$NON-NLS-1$ this.indent++; for (Iterator it = node.statements().iterator(); it.hasNext(); ) { Statement s = (Statement) it.next(); s.accept(this); this.indent--; // incremented in visit(SwitchCase) } this.indent--; printIndent(); this.buffer.append("}\n");//$NON-NLS-1$ return false; }
@Override protected void repairBug(ASTRewrite rewrite, CompilationUnit workingUnit, BugInstance bug) throws BugResolutionException { ASTNode node = getASTNode(workingUnit, bug.getPrimarySourceLineAnnotation()); node = TraversalUtil.backtrackToBlock(node); DefaultCaseVisitor visitor = new DefaultCaseVisitor(); node.accept(visitor); if (visitor.badSwitchStatement != null) { ListRewrite statementsRewrite = rewrite.getListRewrite(visitor.badSwitchStatement, SwitchStatement.STATEMENTS_PROPERTY); SwitchCase defaultCase = rewrite.getAST().newSwitchCase(); defaultCase.setExpression(null); statementsRewrite.insertLast(defaultCase, null); statementsRewrite.insertLast(rewrite.getAST().newBreakStatement(), null); } }
@SuppressWarnings("unchecked") @Override public boolean visit(SwitchStatement node) { if (badSwitchStatement != null) { return false; } List<Statement> switchStatements = node.statements(); for (Statement statement : switchStatements) { if (statement instanceof SwitchCase) { if (((SwitchCase) statement).getExpression() == null) { return true; // this one has a default case...skip it } } } badSwitchStatement = node; return false; }
@Override public boolean visit(SwitchCase node) { //System.out.println("Found: " + node.getClass()); if (node.isDefault()) { println("default:"); } else { print("case "); node.getExpression().accept(this); println(":"); } // Case fallthrough SwitchStatement ss = (SwitchStatement)node.getParent(); List statements = ss.statements(); if (statements.indexOf(node) < statements.size() - 1) { Object nextSibling = statements.get(statements.indexOf(node) + 1); if (nextSibling instanceof SwitchCase) { //indent++; println("goto " + (((SwitchCase)nextSibling).isDefault() ? "default;" : "case;" )); //indent--; return false; } } // TODO Get indentation right //indent++; return false; }
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; }
protected void endVisit(SwitchStatement node, SwitchData data) { SwitchFlowInfo switchFlowInfo = createSwitch(); setFlowInfo(node, switchFlowInfo); switchFlowInfo.mergeTest(getFlowInfo(node.getExpression()), fFlowContext); FlowInfo[] cases = data.getInfos(); for (int i = 0; i < cases.length; i++) { switchFlowInfo.mergeCase(cases[i], fFlowContext); } switchFlowInfo.mergeDefault(data.hasDefaultCase(), fFlowContext); switchFlowInfo.removeLabel(null); }
@Override public void endVisit(SwitchStatement node) { if (skipNode(node)) { return; } endVisit(node, createSwitchData(node)); }
@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); }
private static List<SwitchCase> getSwitchCases(SwitchStatement node) { List<SwitchCase> result = new ArrayList<>(); for (Iterator<Statement> iter = node.statements().iterator(); iter.hasNext();) { Object element = iter.next(); if (element instanceof SwitchCase) { result.add((SwitchCase) element); } } return result; }
private void insertAt(ASTNode target, Statement declaration) { ASTRewrite rewrite = fCURewrite.getASTRewrite(); TextEditGroup groupDescription = fCURewrite.createGroupDescription( RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable); ASTNode parent = target.getParent(); StructuralPropertyDescriptor locationInParent = target.getLocationInParent(); while (locationInParent != Block.STATEMENTS_PROPERTY && locationInParent != SwitchStatement.STATEMENTS_PROPERTY) { if (locationInParent == IfStatement.THEN_STATEMENT_PROPERTY || locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY || locationInParent == ForStatement.BODY_PROPERTY || locationInParent == EnhancedForStatement.BODY_PROPERTY || locationInParent == DoStatement.BODY_PROPERTY || locationInParent == WhileStatement.BODY_PROPERTY) { // create intermediate block if target was the body property of a control statement: Block replacement = rewrite.getAST().newBlock(); ListRewrite replacementRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY); replacementRewrite.insertFirst(declaration, null); replacementRewrite.insertLast(rewrite.createMoveTarget(target), null); rewrite.replace(target, replacement, groupDescription); return; } target = parent; parent = parent.getParent(); locationInParent = target.getLocationInParent(); } ListRewrite listRewrite = rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) locationInParent); listRewrite.insertBefore(declaration, target, groupDescription); }
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; }
public boolean visit(SwitchStatement ss) { Expression expr = ss.getExpression(); if(expr != null){ writeSimpleMarker("switch", null, ss, expr); } return true; }
/** {@inheritDoc} */ @Override public void endVisit(SwitchStatement node) { // TODO-JRO Implement method endVisit logger.warn("Method endVisit not implemented!"); super.endVisit(node); }
/** * Determines if this ASTNode is a conditional statement. * Conditional statements include: if,do,while,for,switch * @param node * @return */ public static boolean isConditional(ASTNode node){ if(node instanceof IfStatement) return true; if(node instanceof DoStatement) return true; if(node instanceof EnhancedForStatement) return true; if(node instanceof ForStatement) return true; if(node instanceof SwitchStatement) return true; if(node instanceof WhileStatement) return true; return false; }
/** * We don't really need anything in particular from this statement, * but since it has an expression and a body, we only want to * investigate the expression part to determine if it needs to * be in the slice. */ public boolean visit(SwitchStatement node){ if(this.options.contains(Slicer.Options.CONTROL_EXPRESSIONS_ONLY)){ /* Visit the expression part. */ node.getExpression().accept(this); /* Don't visit the children. */ return false; } else return true; }
/** * We want to track the variables from the expression only. */ public boolean visit(SwitchStatement node){ /* Visit the expression part. */ node.getExpression().accept(this); /* Don't visit the children. */ return false; }