下面列出了怎么用org.eclipse.jdt.core.dom.AssertStatement的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;
}
public boolean visit(AssertStatement stmnt) {
/*
* assert Expression [ : Expression ] ;
*/
styledString.append("assert", new StyledStringStyler(keywordStyle));
appendSpace();
handleExpression((Expression) stmnt.getExpression());
if (stmnt.getMessage() != null) {
appendSpace();
appendColon();
appendSpace();
handleExpression((Expression) stmnt.getMessage());
}
appendSemicolon();
return false;
}
public boolean visit(AssertStatement node) {
int start = _unit.getLineNumber(node.getStartPosition());
if(start == _extendedLine){
_extendedStatement = node;
return false;
}
return true;
}
/************************** visit start : Statement ***********************/
private AssertStmt visit(AssertStatement node) {
int start = _cunit.getLineNumber(node.getStartPosition());
int end = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
AssertStmt assertStmt = new AssertStmt(start, end, node);
return assertStmt;
}
@Override
public void endVisit(AssertStatement node) {
if (skipNode(node)) {
return;
}
IfFlowInfo info = new IfFlowInfo();
setFlowInfo(node, info);
info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext);
info.merge(getFlowInfo(node.getMessage()), null, fFlowContext);
}
private static Expression getBooleanExpression(ASTNode node) {
if (!(node instanceof Expression)) {
return null;
}
// check if the node is a location where it can be negated
StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
if (locationInParent == QualifiedName.NAME_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
Expression expression = (Expression) node;
if (!isBoolean(expression)) {
return null;
}
if (expression.getParent() instanceof InfixExpression) {
return expression;
}
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY
|| locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY
|| locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
return expression;
}
return null;
}
@Override
public boolean visit(final AssertStatement node) {
this.appendToBuffer("if(!(");
node.getExpression().accept(this);
this.appendToBuffer(")) {");
this.appendToBuffer("throw new AssertionError(");
Expression _message = node.getMessage();
boolean _tripleNotEquals = (_message != null);
if (_tripleNotEquals) {
node.getMessage().accept(this);
}
this.appendToBuffer(")}");
return false;
}
@Override
public void endVisit(AssertStatement node) {
if (skipNode(node))
return;
IfFlowInfo info= new IfFlowInfo();
setFlowInfo(node, info);
info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext);
info.merge(getFlowInfo(node.getMessage()), null, fFlowContext);
}
private static Expression getBooleanExpression(ASTNode node) {
if (!(node instanceof Expression)) {
return null;
}
// check if the node is a location where it can be negated
StructuralPropertyDescriptor locationInParent= node.getLocationInParent();
if (locationInParent == QualifiedName.NAME_PROPERTY) {
node= node.getParent();
locationInParent= node.getLocationInParent();
}
while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
node= node.getParent();
locationInParent= node.getLocationInParent();
}
Expression expression= (Expression) node;
if (!isBoolean(expression)) {
return null;
}
if (expression.getParent() instanceof InfixExpression) {
return expression;
}
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY
|| locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY
|| locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.ARGUMENTS_PROPERTY
|| locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY
|| locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY || locationInParent == ConditionalExpression.EXPRESSION_PROPERTY
|| locationInParent == PrefixExpression.OPERAND_PROPERTY) {
return expression;
}
return null;
}
@Override
public boolean visit(AssertStatement node) {
asserts.add(node);
return super.visit(node);
}
@Override
public boolean visit(AssertStatement node) {
if (node.subtreeMatch(fMatcher, fNodeToMatch))
return matches(node);
return super.visit(node);
}
@Override
public boolean visit(AssertStatement node) {
add(fCreator.create(node));
return true;
}
@Override
public void endVisit(AssertStatement node) {
endVisitNode(node);
}
@Override
public boolean visit(AssertStatement node) {
return visitNode(node);
}
public boolean visit(AssertStatement node) {
statementList.add(node);
return false;
}
public boolean visit(AssertStatement node) {
statementsList.add(node);
return false;
}
/**
* This method allows to get all the Blocks for the Class on which it is;
*
* @return a List of all Blocks;
*/
public Collection<AssertStatement> getAsserts() {
return asserts;
}
/**
* @param node the AST node
* @return array of type constraints, may be empty
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.AssertStatement)
*/
public ITypeConstraint[] create(AssertStatement node) {
return EMPTY_ARRAY;
}