下面列出了怎么用org.eclipse.jdt.core.dom.InstanceofExpression的API类实例代码及写法,或者点击链接到github查看源代码。
private InstanceofExpr visit(InstanceofExpression node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
InstanceofExpr instanceofExpr = new InstanceofExpr(startLine, endLine, node);
Expr expression = (Expr) process(node.getLeftOperand());
expression.setParent(instanceofExpr);
instanceofExpr.setExpression(expression);
instanceofExpr.setInstanceType(node.getRightOperand());
AST ast = AST.newAST(AST.JLS8);
Type exprType = ast.newPrimitiveType(PrimitiveType.BOOLEAN);
instanceofExpr.setType(exprType);
return instanceofExpr;
}
private boolean needsParentesis(ASTNode node) {
if (!(node.getParent() instanceof InfixExpression))
return false;
if (node instanceof InstanceofExpression)
return true;
if (node instanceof InfixExpression) {
InfixExpression expression = (InfixExpression) node;
InfixExpression.Operator operator = expression.getOperator();
InfixExpression parentExpression = (InfixExpression) node.getParent();
InfixExpression.Operator parentOperator = parentExpression.getOperator();
if (parentOperator == operator)
return false;
return true;
}
return false;
}
protected boolean isTypeHolder(Object o) {
if(o.getClass().equals(MethodInvocation.class) || o.getClass().equals(SuperMethodInvocation.class)
|| o.getClass().equals(NumberLiteral.class) || o.getClass().equals(StringLiteral.class)
|| o.getClass().equals(CharacterLiteral.class) || o.getClass().equals(BooleanLiteral.class)
|| o.getClass().equals(TypeLiteral.class) || o.getClass().equals(NullLiteral.class)
|| o.getClass().equals(ArrayCreation.class)
|| o.getClass().equals(ClassInstanceCreation.class)
|| o.getClass().equals(ArrayAccess.class) || o.getClass().equals(FieldAccess.class)
|| o.getClass().equals(SuperFieldAccess.class) || o.getClass().equals(ParenthesizedExpression.class)
|| o.getClass().equals(SimpleName.class) || o.getClass().equals(QualifiedName.class)
|| o.getClass().equals(CastExpression.class) || o.getClass().equals(InfixExpression.class)
|| o.getClass().equals(PrefixExpression.class) || o.getClass().equals(InstanceofExpression.class)
|| o.getClass().equals(ThisExpression.class) || o.getClass().equals(ConditionalExpression.class))
return true;
return false;
}
@Override
public void endVisit(InstanceofExpression node) {
if (skipNode(node)) {
return;
}
processSequential(node, node.getLeftOperand(), node.getRightOperand());
}
@Override
public boolean visit(final InstanceofExpression node) {
node.getLeftOperand().accept(this);
this.appendToBuffer(" instanceof ");
node.getRightOperand().accept(this);
return false;
}
/**
* Returns the precedence of the expression. Expression
* with higher precedence are executed before expressions
* with lower precedence.
* i.e. in:
* <br><code> int a= ++3--;</code></br>
*
* the precedence order is
* <ul>
* <li>3</li>
* <li>++</li>
* <li>--</li>
* <li>=</li>
* </ul>
* 1. 3 -(++)-> 4<br>
* 2. 4 -(--)-> 3<br>
* 3. 3 -(=)-> a<br>
*
* @param expression the expression to determine the precedence for
* @return the precedence the higher to stronger the binding to its operand(s)
*/
public static int getExpressionPrecedence(Expression expression) {
if (expression instanceof InfixExpression) {
return getOperatorPrecedence(((InfixExpression)expression).getOperator());
} else if (expression instanceof Assignment) {
return ASSIGNMENT;
} else if (expression instanceof ConditionalExpression) {
return CONDITIONAL;
} else if (expression instanceof InstanceofExpression) {
return RELATIONAL;
} else if (expression instanceof CastExpression) {
return TYPEGENERATION;
} else if (expression instanceof ClassInstanceCreation) {
return POSTFIX;
} else if (expression instanceof PrefixExpression) {
return PREFIX;
} else if (expression instanceof FieldAccess) {
return POSTFIX;
} else if (expression instanceof MethodInvocation) {
return POSTFIX;
} else if (expression instanceof ArrayAccess) {
return POSTFIX;
} else if (expression instanceof PostfixExpression) {
return POSTFIX;
}
return Integer.MAX_VALUE;
}
@Override
public ITypeConstraint[] create(InstanceofExpression instanceofExpression){
Expression expression= instanceofExpression.getLeftOperand();
Type type= instanceofExpression.getRightOperand();
if (isClassBinding(expression.resolveTypeBinding()) && isClassBinding(type.resolveBinding())) {
ConstraintVariable expressionVar= fConstraintVariableFactory.makeExpressionOrTypeVariable(expression, getContext());
ConstraintVariable typeVariable= fConstraintVariableFactory.makeTypeVariable(type);
return createOrOrSubtypeConstraint(expressionVar, typeVariable);
} else
return new ITypeConstraint[0];
}
public static void addUnnecessaryInstanceofProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
ASTNode curr= selectedNode;
while (curr instanceof ParenthesizedExpression) {
curr= ((ParenthesizedExpression) curr).getExpression();
}
if (curr instanceof InstanceofExpression) {
AST ast= curr.getAST();
ASTRewrite rewrite= ASTRewrite.create(ast);
InstanceofExpression inst= (InstanceofExpression) curr;
InfixExpression expression= ast.newInfixExpression();
expression.setLeftOperand((Expression) rewrite.createCopyTarget(inst.getLeftOperand()));
expression.setOperator(InfixExpression.Operator.NOT_EQUALS);
expression.setRightOperand(ast.newNullLiteral());
rewrite.replace(inst, expression, null);
String label= CorrectionMessages.LocalCorrectionsSubProcessor_unnecessaryinstanceof_description;
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.UNNECESSARY_INSTANCEOF, image);
proposals.add(proposal);
}
}
public List<InstanceofExpression> getInstanceofExpressions() {
List<InstanceofExpression> expressionList = new ArrayList<InstanceofExpression>();
DefaultMutableTreeNode leaf = root.getFirstLeaf();
while(leaf != null) {
Expression expression = (Expression)leaf.getUserObject();
if(expression instanceof InstanceofExpression) {
InstanceofExpression instanceofExpression = (InstanceofExpression)expression;
expressionList.add(instanceofExpression);
}
leaf = leaf.getNextLeaf();
}
return expressionList;
}
public boolean visit(InstanceofExpression expr) {
/*
* InstanceofExpression: Expression instanceof Type
*/
activateDiffStyle(expr);
handleExpression((Expression) expr.getLeftOperand());
appendSpace();
styledString.append("instanceof", determineDiffStyle(expr, new StyledStringStyler(keywordStyle)));
appendSpace();
handleType((Type) expr.getRightOperand());
deactivateDiffStyle(expr);
return false;
}
@Override
public boolean visit(InstanceofExpression node)
{
Type type = node.getRightOperand();
processType(type, TypeReferenceLocation.INSTANCE_OF, compilationUnit.getLineNumber(node.getStartPosition()),
compilationUnit.getColumnNumber(type.getStartPosition()), type.getLength(), node.toString());
return super.visit(node);
}
public boolean visit(InstanceofExpression node) {
typesInInstanceOf.add(node.getRightOperand());
return true;
}
private void parseExpression(MethodInfo methodInfo, Expression expression) {
if (expression == null) {
return;
}//System.out.println(expression.toString()+" "+Annotation.nodeClassForType(expression.getNodeType()));
if (expression.getNodeType() == ASTNode.ARRAY_INITIALIZER) {
List<Expression> expressions = ((ArrayInitializer) expression).expressions();
for (Expression expression2 : expressions) {
parseExpression(methodInfo, expression2);
}
}
if (expression.getNodeType() == ASTNode.CAST_EXPRESSION) {
parseExpression(methodInfo, ((CastExpression) expression).getExpression());
}
if (expression.getNodeType() == ASTNode.CONDITIONAL_EXPRESSION) {
parseExpression(methodInfo, ((ConditionalExpression) expression).getExpression());
parseExpression(methodInfo, ((ConditionalExpression) expression).getElseExpression());
parseExpression(methodInfo, ((ConditionalExpression) expression).getThenExpression());
}
if (expression.getNodeType() == ASTNode.INFIX_EXPRESSION) {
parseExpression(methodInfo, ((InfixExpression) expression).getLeftOperand());
parseExpression(methodInfo, ((InfixExpression) expression).getRightOperand());
}
if (expression.getNodeType() == ASTNode.INSTANCEOF_EXPRESSION) {
parseExpression(methodInfo, ((InstanceofExpression) expression).getLeftOperand());
}
if (expression.getNodeType() == ASTNode.PARENTHESIZED_EXPRESSION) {
parseExpression(methodInfo, ((ParenthesizedExpression) expression).getExpression());
}
if (expression.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
parseExpression(methodInfo, ((PostfixExpression) expression).getOperand());
}
if (expression.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
parseExpression(methodInfo, ((PrefixExpression) expression).getOperand());
}
if (expression.getNodeType() == ASTNode.THIS_EXPRESSION) {
parseExpression(methodInfo, ((ThisExpression) expression).getQualifier());
}
if (expression.getNodeType() == ASTNode.METHOD_INVOCATION) {
List<Expression> arguments = ((MethodInvocation) expression).arguments();
IMethodBinding methodBinding = ((MethodInvocation) expression).resolveMethodBinding();
if (methodBinding != null)
methodInfo.methodCalls.add(methodBinding);
for (Expression exp : arguments)
parseExpression(methodInfo, exp);
parseExpression(methodInfo, ((MethodInvocation) expression).getExpression());
}
if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
parseExpression(methodInfo, ((Assignment) expression).getLeftHandSide());
parseExpression(methodInfo, ((Assignment) expression).getRightHandSide());
}
if (expression.getNodeType() == ASTNode.QUALIFIED_NAME) {
if (((QualifiedName) expression).getQualifier().resolveTypeBinding() != null) {
String name = ((QualifiedName) expression).getQualifier().resolveTypeBinding().getQualifiedName() + "." + ((QualifiedName) expression).getName().getIdentifier();
methodInfo.fieldUsesSet.add(name);
}
parseExpression(methodInfo, ((QualifiedName) expression).getQualifier());
}
}
@Override
public final void endVisit(final Type node) {
final ASTNode parent= node.getParent();
if (!(parent instanceof AbstractTypeDeclaration) && !(parent instanceof ClassInstanceCreation) && !(parent instanceof TypeLiteral) && (!(parent instanceof InstanceofExpression) || fInstanceOf))
node.setProperty(PROPERTY_CONSTRAINT_VARIABLE, fModel.createTypeVariable(node));
}
@Override
public void endVisit(InstanceofExpression node) {
if (skipNode(node))
return;
processSequential(node, node.getLeftOperand(), node.getRightOperand());
}
@Override
public boolean visit(InstanceofExpression node) {
if (node.subtreeMatch(fMatcher, fNodeToMatch))
return matches(node);
return super.visit(node);
}
@Override
public boolean visit(InstanceofExpression node) {
add(fCreator.create(node));
return true;
}
@Override
public void endVisit(InstanceofExpression node) {
endVisitNode(node);
}
@Override
public boolean visit(InstanceofExpression node) {
return visitNode(node);
}
private static boolean getAddParenthesesForExpressionProposals(IInvocationContext context, ASTNode coveringNode, Collection<ICommandAccess> resultingCollections) {
ASTNode node;
if (context.getSelectionLength() == 0) {
node= coveringNode;
while (node != null && !(node instanceof CastExpression) && !(node instanceof InfixExpression) && !(node instanceof InstanceofExpression) && !(node instanceof ConditionalExpression)) {
node= node.getParent();
}
} else {
node= context.getCoveredNode();
}
String label= null;
if (node instanceof CastExpression) {
label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
} else if (node instanceof InstanceofExpression) {
label= CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_instanceof_description;
} else if (node instanceof InfixExpression) {
InfixExpression infixExpression= (InfixExpression)node;
label= Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_description, infixExpression.getOperator().toString());
} else if (node instanceof ConditionalExpression) {
label= CorrectionMessages.AdvancedQuickAssistProcessor_putConditionalExpressionInParentheses;
} else {
return false;
}
if (node.getParent() instanceof ParenthesizedExpression)
return false;
if (resultingCollections == null)
return true;
AST ast= node.getAST();
ASTRewrite rewrite= ASTRewrite.create(ast);
ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression();
parenthesizedExpression.setExpression((Expression)rewrite.createCopyTarget(node));
rewrite.replace(node, parenthesizedExpression, null);
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_PARENTHESES_FOR_EXPRESSION, image);
resultingCollections.add(proposal);
return true;
}
public boolean instanceOf(Expression expression) {
if(expression instanceof InstanceofExpression)
return true;
else
return false;
}
public boolean visit(InstanceofExpression expr) {
handleExpression(expr.getLeftOperand());
handleType(expr.getRightOperand());
return false;
}
private void handleExpression(Expression expression) {
if (expression instanceof ArrayAccess) {
visit((ArrayAccess) expression);
} else if (expression instanceof ArrayCreation) {
visit((ArrayCreation) expression);
} else if (expression instanceof ArrayInitializer) {
visit((ArrayInitializer) expression);
} else if (expression instanceof Assignment) {
visit((Assignment) expression);
} else if (expression instanceof BooleanLiteral) {
visit((BooleanLiteral) expression);
} else if (expression instanceof CastExpression) {
visit((CastExpression) expression);
} else if (expression instanceof CharacterLiteral) {
visit((CharacterLiteral) expression);
} else if (expression instanceof ClassInstanceCreation) {
visit((ClassInstanceCreation) expression);
} else if (expression instanceof ConditionalExpression) {
visit((ConditionalExpression) expression);
} else if (expression instanceof FieldAccess) {
visit((FieldAccess) expression);
} else if (expression instanceof InfixExpression) {
visit((InfixExpression) expression);
} else if (expression instanceof InstanceofExpression) {
visit((InstanceofExpression) expression);
} else if (expression instanceof MethodInvocation) {
visit((MethodInvocation) expression);
} else if (expression instanceof NullLiteral) {
visit((NullLiteral) expression);
} else if (expression instanceof NumberLiteral) {
visit((NumberLiteral) expression);
} else if (expression instanceof ParenthesizedExpression) {
visit((ParenthesizedExpression) expression);
} else if (expression instanceof PostfixExpression) {
visit((PostfixExpression) expression);
} else if (expression instanceof PrefixExpression) {
visit((PrefixExpression) expression);
} else if ((expression instanceof QualifiedName)) {
visit((QualifiedName) expression);
} else if (expression instanceof SimpleName) {
visit((SimpleName) expression);
} else if (expression instanceof StringLiteral) {
visit((StringLiteral) expression);
} else if (expression instanceof SuperFieldAccess) {
visit((SuperFieldAccess) expression);
} else if (expression instanceof SuperMethodInvocation) {
visit((SuperMethodInvocation) expression);
} else if (expression instanceof ThisExpression) {
visit((ThisExpression) expression);
} else if (expression instanceof TypeLiteral) {
visit((TypeLiteral) expression);
} else if (expression instanceof VariableDeclarationExpression) {
visit((VariableDeclarationExpression) expression);
}
}
private void handleExpression(Expression expression) {
if (expression instanceof ArrayAccess) {
visit((ArrayAccess) expression);
} else if (expression instanceof ArrayCreation) {
visit((ArrayCreation) expression);
} else if (expression instanceof ArrayInitializer) {
visit((ArrayInitializer) expression);
} else if (expression instanceof Assignment) {
visit((Assignment) expression);
} else if (expression instanceof BooleanLiteral) {
visit((BooleanLiteral) expression);
} else if (expression instanceof CastExpression) {
visit((CastExpression) expression);
} else if (expression instanceof CharacterLiteral) {
visit((CharacterLiteral) expression);
} else if (expression instanceof ClassInstanceCreation) {
visit((ClassInstanceCreation) expression);
} else if (expression instanceof ConditionalExpression) {
visit((ConditionalExpression) expression);
} else if (expression instanceof FieldAccess) {
visit((FieldAccess) expression);
} else if (expression instanceof InfixExpression) {
visit((InfixExpression) expression);
} else if (expression instanceof InstanceofExpression) {
visit((InstanceofExpression) expression);
} else if (expression instanceof MethodInvocation) {
visit((MethodInvocation) expression);
} else if (expression instanceof NullLiteral) {
visit((NullLiteral) expression);
} else if (expression instanceof NumberLiteral) {
visit((NumberLiteral) expression);
} else if (expression instanceof ParenthesizedExpression) {
visit((ParenthesizedExpression) expression);
} else if (expression instanceof PostfixExpression) {
visit((PostfixExpression) expression);
} else if (expression instanceof PrefixExpression) {
visit((PrefixExpression) expression);
} else if ((expression instanceof QualifiedName)) {
visit((QualifiedName) expression);
} else if (expression instanceof SimpleName) {
visit((SimpleName) expression);
} else if (expression instanceof StringLiteral) {
visit((StringLiteral) expression);
} else if (expression instanceof SuperFieldAccess) {
visit((SuperFieldAccess) expression);
} else if (expression instanceof SuperMethodInvocation) {
visit((SuperMethodInvocation) expression);
} else if (expression instanceof ThisExpression) {
visit((ThisExpression) expression);
} else if (expression instanceof TypeLiteral) {
visit((TypeLiteral) expression);
} else if (expression instanceof VariableDeclarationExpression) {
visit((VariableDeclarationExpression) expression);
}
}
/**
* @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.InstanceofExpression)
*/
public ITypeConstraint[] create(InstanceofExpression node) {
return EMPTY_ARRAY;
}