public static ChildPropertyDescriptor getArrayTypeElementPropertyDescriptor(ArrayType node) { List<?> propertyDescriptors = ArrayType.propertyDescriptors(node.getAST().apiLevel()); for (Object propertyDescriptor : propertyDescriptors) { if (propertyDescriptor instanceof ChildPropertyDescriptor) { ChildPropertyDescriptor childListPropertyDescriptor = (ChildPropertyDescriptor) propertyDescriptor; if (DESCRIPTOR_NAME_AST4.equals(childListPropertyDescriptor.getId())) { if (OptimusLogger.logger.isLoggable(Level.FINE)) { OptimusLogger.logger.fine(String.format(RETURNED_DESCRIPTOR_MESSAGE, DESCRIPTOR_NAME_AST4)); } return childListPropertyDescriptor; } else if (DESCRIPTOR_NAME_AST8.equals(childListPropertyDescriptor.getId())) { if (OptimusLogger.logger.isLoggable(Level.FINE)) { OptimusLogger.logger.fine(String.format(RETURNED_DESCRIPTOR_MESSAGE, DESCRIPTOR_NAME_AST8)); } return childListPropertyDescriptor; } } } return null; }
private void handle(Statement body, ChildPropertyDescriptor bodyProperty) { if ((body.getFlags() & ASTNode.RECOVERED) != 0) return; Statement parent = (Statement) body.getParent(); if ((parent.getFlags() & ASTNode.RECOVERED) != 0) return; if (fRemoveUnnecessaryBlocksOnlyWhenReturnOrThrow) { if (!(body instanceof Block)) { if (body.getNodeType() != ASTNode.IF_STATEMENT && body.getNodeType() != ASTNode.RETURN_STATEMENT && body.getNodeType() != ASTNode.THROW_STATEMENT) { fResult.add(new AddBlockOperation(bodyProperty, body, parent)); } } else { if (RemoveBlockOperation.satisfiesCleanUpPrecondition(parent, bodyProperty, true)) { fResult.add(new RemoveBlockOperation(parent, bodyProperty)); } } } else if (fFindControlStatementsWithoutBlock) { if (!(body instanceof Block)) { fResult.add(new AddBlockOperation(bodyProperty, body, parent)); } } else if (fRemoveUnnecessaryBlocks) { if (RemoveBlockOperation.satisfiesCleanUpPrecondition(parent, bodyProperty, false)) { fResult.add(new RemoveBlockOperation(parent, bodyProperty)); } } }
@Override protected ChildPropertyDescriptor getBodyProperty() { return VariableDeclarationFragment.INITIALIZER_PROPERTY; }
public boolean isDangligIf() { List<Statement> statements = fDeclaration.getBody().statements(); if (statements.size() != 1) return false; ASTNode p = statements.get(0); while (true) { if (p instanceof IfStatement) { return ((IfStatement) p).getElseStatement() == null; } else { ChildPropertyDescriptor childD; if (p instanceof WhileStatement) { childD = WhileStatement.BODY_PROPERTY; } else if (p instanceof ForStatement) { childD = ForStatement.BODY_PROPERTY; } else if (p instanceof EnhancedForStatement) { childD = EnhancedForStatement.BODY_PROPERTY; } else if (p instanceof DoStatement) { childD = DoStatement.BODY_PROPERTY; } else if (p instanceof LabeledStatement) { childD = LabeledStatement.BODY_PROPERTY; } else { return false; } Statement body = (Statement) p.getStructuralProperty(childD); if (body instanceof Block) { return false; } else { p = body; } } } }
public boolean isDangligIf() { List<Statement> statements= fDeclaration.getBody().statements(); if (statements.size() != 1) return false; ASTNode p= statements.get(0); while (true) { if (p instanceof IfStatement) { return ((IfStatement) p).getElseStatement() == null; } else { ChildPropertyDescriptor childD; if (p instanceof WhileStatement) { childD= WhileStatement.BODY_PROPERTY; } else if (p instanceof ForStatement) { childD= ForStatement.BODY_PROPERTY; } else if (p instanceof EnhancedForStatement) { childD= EnhancedForStatement.BODY_PROPERTY; } else if (p instanceof DoStatement) { childD= DoStatement.BODY_PROPERTY; } else if (p instanceof LabeledStatement) { childD= LabeledStatement.BODY_PROPERTY; } else { return false; } Statement body= (Statement) p.getStructuralProperty(childD); if (body instanceof Block) { return false; } else { p= body; } } } }
private void handle(Statement body, ChildPropertyDescriptor bodyProperty) { if ((body.getFlags() & ASTNode.RECOVERED) != 0) return; Statement parent= (Statement)body.getParent(); if ((parent.getFlags() & ASTNode.RECOVERED) != 0) return; if (fRemoveUnnecessaryBlocksOnlyWhenReturnOrThrow) { if (!(body instanceof Block)) { if (body.getNodeType() != ASTNode.IF_STATEMENT && body.getNodeType() != ASTNode.RETURN_STATEMENT && body.getNodeType() != ASTNode.THROW_STATEMENT) { fResult.add(new AddBlockOperation(bodyProperty, body, parent)); } } else { if (RemoveBlockOperation.satisfiesCleanUpPrecondition(parent, bodyProperty, true)) { fResult.add(new RemoveBlockOperation(parent, bodyProperty)); } } } else if (fFindControlStatementsWithoutBlock) { if (!(body instanceof Block)) { fResult.add(new AddBlockOperation(bodyProperty, body, parent)); } } else if (fRemoveUnnecessaryBlocks) { if (RemoveBlockOperation.satisfiesCleanUpPrecondition(parent, bodyProperty, false)) { fResult.add(new RemoveBlockOperation(parent, bodyProperty)); } } }
public static Type getComponentType(ArrayType arrayType) { ChildPropertyDescriptor cpd = ASTArrayTypeHelper.getArrayTypeElementPropertyDescriptor(arrayType); if (cpd != null) { Object structuralProperty = arrayType.getStructuralProperty(cpd); if (structuralProperty instanceof Type) { return (Type) structuralProperty; } } return null; }
public AddBlockOperation( ChildPropertyDescriptor bodyProperty, Statement body, Statement controlStatement) { fBodyProperty = bodyProperty; fBody = body; fControlStatement = controlStatement; }
public RemoveBlockOperation(Statement controlStatement, ChildPropertyDescriptor child) { fStatement = controlStatement; fChild = child; }
public static boolean satisfiesCleanUpPrecondition( Statement controlStatement, ChildPropertyDescriptor childDescriptor, boolean onlyReturnAndThrows) { return satisfiesPrecondition(controlStatement, childDescriptor, onlyReturnAndThrows, true); }
public static boolean satisfiesQuickAssistPrecondition( Statement controlStatement, ChildPropertyDescriptor childDescriptor) { return satisfiesPrecondition(controlStatement, childDescriptor, false, false); }
private static boolean satisfiesPrecondition( Statement controlStatement, ChildPropertyDescriptor childDescriptor, boolean onlyReturnAndThrows, boolean cleanUpCheck) { Object child = controlStatement.getStructuralProperty(childDescriptor); if (!(child instanceof Block)) return false; Block block = (Block) child; List<Statement> list = block.statements(); if (list.size() != 1) return false; ASTNode singleStatement = list.get(0); if (onlyReturnAndThrows) if (!(singleStatement instanceof ReturnStatement) && !(singleStatement instanceof ThrowStatement)) return false; if (controlStatement instanceof IfStatement) { // if (true) { // while (true) // if (false) // ; // } else // ; if (((IfStatement) controlStatement).getThenStatement() != child) return true; // can always remove blocks in else part IfStatement ifStatement = (IfStatement) controlStatement; if (ifStatement.getElseStatement() == null) return true; // can always remove if no else part return !hasUnblockedIf((Statement) singleStatement, onlyReturnAndThrows, cleanUpCheck); } else { // if (true) // while (true) { // if (false) // ; // } // else // ; if (!hasUnblockedIf((Statement) singleStatement, onlyReturnAndThrows, cleanUpCheck)) return true; ASTNode currentChild = controlStatement; ASTNode parent = currentChild.getParent(); while (true) { Statement body = null; if (parent instanceof IfStatement) { body = ((IfStatement) parent).getThenStatement(); if (body == currentChild && ((IfStatement) parent).getElseStatement() != null) // ->currentChild is an unblocked then part return false; } else if (parent instanceof WhileStatement) { body = ((WhileStatement) parent).getBody(); } else if (parent instanceof DoStatement) { body = ((DoStatement) parent).getBody(); } else if (parent instanceof ForStatement) { body = ((ForStatement) parent).getBody(); } else if (parent instanceof EnhancedForStatement) { body = ((EnhancedForStatement) parent).getBody(); } else { return true; } if (body != currentChild) // ->parents child is a block return true; currentChild = parent; parent = currentChild.getParent(); } } }
@Override protected ChildPropertyDescriptor getJavaDocProperty() { return MethodDeclaration.JAVADOC_PROPERTY; }
@Override protected ChildPropertyDescriptor getBodyProperty() { return MethodDeclaration.BODY_PROPERTY; }
@Override protected ChildPropertyDescriptor getJavaDocProperty() { return FieldDeclaration.JAVADOC_PROPERTY; }
public AddBlockOperation(ChildPropertyDescriptor bodyProperty, Statement body, Statement controlStatement) { fBodyProperty= bodyProperty; fBody= body; fControlStatement= controlStatement; }
public RemoveBlockOperation(Statement controlStatement, ChildPropertyDescriptor child) { fStatement= controlStatement; fChild= child; }
public static boolean satisfiesCleanUpPrecondition(Statement controlStatement, ChildPropertyDescriptor childDescriptor, boolean onlyReturnAndThrows) { return satisfiesPrecondition(controlStatement, childDescriptor, onlyReturnAndThrows, true); }
public static boolean satisfiesQuickAssistPrecondition(Statement controlStatement, ChildPropertyDescriptor childDescriptor) { return satisfiesPrecondition(controlStatement, childDescriptor, false, false); }
private static boolean satisfiesPrecondition(Statement controlStatement, ChildPropertyDescriptor childDescriptor, boolean onlyReturnAndThrows, boolean cleanUpCheck) { Object child= controlStatement.getStructuralProperty(childDescriptor); if (!(child instanceof Block)) return false; Block block= (Block)child; List<Statement> list= block.statements(); if (list.size() != 1) return false; ASTNode singleStatement= list.get(0); if (onlyReturnAndThrows) if (!(singleStatement instanceof ReturnStatement) && !(singleStatement instanceof ThrowStatement)) return false; if (controlStatement instanceof IfStatement) { // if (true) { // while (true) // if (false) // ; // } else // ; if (((IfStatement)controlStatement).getThenStatement() != child) return true;//can always remove blocks in else part IfStatement ifStatement= (IfStatement)controlStatement; if (ifStatement.getElseStatement() == null) return true;//can always remove if no else part return !hasUnblockedIf((Statement)singleStatement, onlyReturnAndThrows, cleanUpCheck); } else { //if (true) // while (true) { // if (false) // ; // } //else // ; if (!hasUnblockedIf((Statement)singleStatement, onlyReturnAndThrows, cleanUpCheck)) return true; ASTNode currentChild= controlStatement; ASTNode parent= currentChild.getParent(); while (true) { Statement body= null; if (parent instanceof IfStatement) { body= ((IfStatement)parent).getThenStatement(); if (body == currentChild && ((IfStatement)parent).getElseStatement() != null)//->currentChild is an unblocked then part return false; } else if (parent instanceof WhileStatement) { body= ((WhileStatement)parent).getBody(); } else if (parent instanceof DoStatement) { body= ((DoStatement)parent).getBody(); } else if (parent instanceof ForStatement) { body= ((ForStatement)parent).getBody(); } else if (parent instanceof EnhancedForStatement) { body= ((EnhancedForStatement)parent).getBody(); } else { return true; } if (body != currentChild)//->parents child is a block return true; currentChild= parent; parent= currentChild.getParent(); } } }
public ChildPropertyDescriptor getJavadocDescriptor() { return TypeDeclaration.JAVADOC_PROPERTY; }
public ChildPropertyDescriptor getJavadocDescriptor() { return MethodDeclaration.JAVADOC_PROPERTY; }
@Override protected ChildPropertyDescriptor getJavadocPropertyDescriptor() { return JAVADOC_PROPERTY; }