下面列出了怎么用org.eclipse.jdt.core.dom.ThrowStatement的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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;
}
public StatementObject(Statement statement, StatementType type, AbstractMethodFragment parent) {
super(statement, type, parent);
ExpressionExtractor expressionExtractor = new ExpressionExtractor();
List<Expression> assignments = expressionExtractor.getAssignments(statement);
List<Expression> postfixExpressions = expressionExtractor.getPostfixExpressions(statement);
List<Expression> prefixExpressions = expressionExtractor.getPrefixExpressions(statement);
processVariables(expressionExtractor.getVariableInstructions(statement), assignments, postfixExpressions, prefixExpressions);
processMethodInvocations(expressionExtractor.getMethodInvocations(statement));
processClassInstanceCreations(expressionExtractor.getClassInstanceCreations(statement));
processArrayCreations(expressionExtractor.getArrayCreations(statement));
//processArrayAccesses(expressionExtractor.getArrayAccesses(statement));
processLiterals(expressionExtractor.getLiterals(statement));
if(statement instanceof ThrowStatement) {
processThrowStatement((ThrowStatement)statement);
}
if(statement instanceof ConstructorInvocation) {
processConstructorInvocation((ConstructorInvocation)statement);
}
}
public boolean visit(ThrowStatement node) {
int start = _unit.getLineNumber(node.getStartPosition());
if(start == _extendedLine){
_extendedStatement = node;
return false;
}
return true;
}
private ThrowStmt visit(ThrowStatement node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
ThrowStmt throwStmt = new ThrowStmt(startLine, endLine, node);
Expr expression = (Expr) process(node.getExpression());
expression.setParent(throwStmt);
throwStmt.setExpression(expression);
return throwStmt;
}
@Override
public void endVisit(ThrowStatement node) {
if (skipNode(node)) {
return;
}
ThrowFlowInfo info = createThrow();
setFlowInfo(node, info);
Expression expression = node.getExpression();
info.merge(getFlowInfo(expression), fFlowContext);
}
@Override
public boolean visit(ThrowStatement node) {
ITypeBinding exception = node.getExpression().resolveTypeBinding();
if (exception == null) {
return true;
}
addException(exception, node.getAST());
return true;
}
@Override
public boolean visit(ThrowStatement node) {
ITypeBinding exception = node.getExpression().resolveTypeBinding();
if (!isSelected(node) || exception == null || Bindings.isRuntimeException(exception)) {
return true;
}
addException(exception, node.getAST());
return true;
}
private static ThrowStatement getThrowForUnsupportedCase(Expression switchExpr, AST ast, ASTRewrite astRewrite) {
ThrowStatement newThrowStatement = ast.newThrowStatement();
ClassInstanceCreation newCic = ast.newClassInstanceCreation();
newCic.setType(ast.newSimpleType(ast.newSimpleName("UnsupportedOperationException"))); //$NON-NLS-1$
InfixExpression newInfixExpr = ast.newInfixExpression();
StringLiteral newStringLiteral = ast.newStringLiteral();
newStringLiteral.setLiteralValue("Unimplemented case: "); //$NON-NLS-1$
newInfixExpr.setLeftOperand(newStringLiteral);
newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpr));
newCic.arguments().add(newInfixExpr);
newThrowStatement.setExpression(newCic);
return newThrowStatement;
}
private static ThrowStatement getThrowForUnexpectedDefault(Expression switchExpression, AST ast, ASTRewrite astRewrite) {
ThrowStatement newThrowStatement = ast.newThrowStatement();
ClassInstanceCreation newCic = ast.newClassInstanceCreation();
newCic.setType(ast.newSimpleType(ast.newSimpleName("IllegalArgumentException"))); //$NON-NLS-1$
InfixExpression newInfixExpr = ast.newInfixExpression();
StringLiteral newStringLiteral = ast.newStringLiteral();
newStringLiteral.setLiteralValue("Unexpected value: "); //$NON-NLS-1$
newInfixExpr.setLeftOperand(newStringLiteral);
newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpression));
newCic.arguments().add(newInfixExpr);
newThrowStatement.setExpression(newCic);
return newThrowStatement;
}
@Override
public boolean visit(final ThrowStatement node) {
this.appendToBuffer("throw ");
node.getExpression().accept(this);
ASTNode _parent = node.getParent();
boolean _not = (!(_parent instanceof Block));
if (_not) {
this.appendToBuffer(";");
}
return false;
}
@Override
public boolean visit(ThrowStatement node) {
ITypeBinding binding = node.getExpression().resolveTypeBinding();
if (binding != null) {
ThrownException thrownException = new ThrownException();
Type thrownType = importer.ensureTypeFromTypeBinding(binding);
thrownException.setExceptionClass((com.feenk.jdt2famix.model.famix.Class) thrownType);
thrownException.setDefiningMethod((Method) importer.topOfContainerStack());
importer.repository().add(thrownException);
}
return true;
}
@Override
public void endVisit(ThrowStatement node) {
if (skipNode(node))
return;
ThrowFlowInfo info= createThrow();
setFlowInfo(node, info);
Expression expression= node.getExpression();
info.merge(getFlowInfo(expression), fFlowContext);
}
@Override
public boolean visit(ThrowStatement node) {
ITypeBinding exception= node.getExpression().resolveTypeBinding();
if (exception == null) // Safety net for null bindings when compiling fails.
return true;
addException(exception, node.getAST());
return true;
}
@Override
public ITypeConstraint[] create(ThrowStatement node) {
ConstraintVariable nameVariable= fConstraintVariableFactory.makeExpressionOrTypeVariable(node.getExpression(), getContext());
ITypeBinding throwable= node.getAST().resolveWellKnownType("java.lang.Throwable"); //$NON-NLS-1$
return fTypeConstraintFactory.createSubtypeConstraint(
nameVariable,
fConstraintVariableFactory.makeRawBindingVariable(throwable));
}
@Override
public boolean visit(ThrowStatement node) {
ITypeBinding exception= node.getExpression().resolveTypeBinding();
if (!isSelected(node) || exception == null || Bindings.isRuntimeException(exception)) // Safety net for null bindings when compiling fails.
return true;
addException(exception, node.getAST());
return true;
}
@Override
public boolean visit(ThrowStatement node) {
if (matches(node.getExpression().resolveTypeBinding())) {
// mark 'throw'
fResult.add(new OccurrenceLocation(node.getStartPosition(), 5, 0, fDescription));
}
return super.visit(node);
}
@Override
public boolean visit(ThrowStatement node) {
ITypeBinding exception= node.getExpression().resolveTypeBinding();
if (isExitPoint(exception)) {
// mark 'throw'
fResult.add(new OccurrenceLocation(node.getStartPosition(), 5, 0, fExitDescription));
}
return true;
}
protected void processThrowStatement(ThrowStatement throwStatement) {
Expression expression = throwStatement.getExpression();
if(expression instanceof ClassInstanceCreation) {
ClassInstanceCreation creation = (ClassInstanceCreation)expression;
ITypeBinding typeBinding = creation.getType().resolveBinding();
addExceptionInThrowStatement(typeBinding.getQualifiedName());
}
}
public boolean visit(ThrowStatement stmnt){
/*
* ThrowStatement: throw Expression ;
*/
styledString.append("throw", new StyledStringStyler(keywordStyle));
appendSpace();
handleExpression((Expression) stmnt.getExpression());
appendSemicolon();
return false;
}
public boolean visit(ThrowStatement node) {
throwsException = true;
return true;
}
@Override
public abstract boolean visit(ThrowStatement node);
@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);
}
@Override
public boolean visit(ThrowStatement node) {
throwStatements ++;
throw2 ++;
return true;
}
@Override
public boolean visit(ThrowStatement node) {
if (node.subtreeMatch(fMatcher, fNodeToMatch))
return matches(node);
return super.visit(node);
}
@Override
public boolean visit(ThrowStatement node) {
add(fCreator.create(node));
return true;
}
@Override
public abstract boolean visit(ThrowStatement node);
@Override
public void endVisit(ThrowStatement node) {
endVisitNode(node);
}
@Override
public boolean visit(ThrowStatement node) {
return visitNode(node);
}
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();
}
}
}