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; }
/** * Fills instanceof skeleton pieces. * @param instance The instanceof part of the skeleton. * @param parentsOfHoles All nodes that are parents of some hole. * @return The synthesized expressions corresponding to this * skeleton piece and the type constraint representing their types. */ private ExpressionsAndTypeConstraints fillInstanceof(InstanceofExpression instance, Set<ASTNode> parentsOfHoles) { try { codehint.ast.Type rightOperand = ASTConverter.copy(instance.getRightOperand()); IJavaType targetType = EclipseUtils.getType(instance.getRightOperand().toString(), stack, target, typeCache); rightOperand.setStaticType(targetType); ExpressionsAndTypeConstraints exprResult = fillSkeleton(instance.getLeftOperand(), new SameHierarchy(targetType), parentsOfHoles); Map<String, ArrayList<codehint.ast.Expression>> resultExprs = new HashMap<String, ArrayList<codehint.ast.Expression>>(exprResult.getExprs().size()); for (Map.Entry<String, ArrayList<codehint.ast.Expression>> res: exprResult.getExprs().entrySet()) for (codehint.ast.Expression expr: res.getValue()) { IJavaValue exprValue = expressionEvaluator.getValue(expr, Collections.<Effect>emptySet()); Utils.addToListMap(resultExprs, res.getKey(), expressionMaker.makeInstanceOf(expr, rightOperand, booleanType, exprValue == null ? null : valueCache.getBooleanJavaValue(!exprValue.isNull() && subtypeChecker.isSubtypeOf(exprValue.getJavaType(), targetType)))); } return new ExpressionsAndTypeConstraints(resultExprs, new SupertypeBound(booleanType)); } catch (DebugException e) { throw new RuntimeException(e); } }
@Override public void endVisit(InstanceofExpression node) { if (skipNode(node)) { return; } processSequential(node, node.getLeftOperand(), node.getRightOperand()); }
/** * 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; }
/** * 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> * * <p>the precedence order is * * <ul> * <li>3 * <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]; }
@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); } }
@Override public boolean visit(InstanceofExpression node) { node.getLeftOperand().accept(this); this.fBuffer.append(" instanceof ");//$NON-NLS-1$ node.getRightOperand().accept(this); return false; }
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, 10, image); proposals.add(proposal); } }
@Override public boolean visit(InstanceofExpression node) { //System.out.println("Found: " + node.getClass()); print("(cast("); node.getRightOperand().accept(this); print(")("); node.getLeftOperand().accept(this); print(") !is null)"); 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); }
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 boolean visit(InstanceofExpression node) { addNewTypeBinding(node.getRightOperand().resolveBinding()); return true; }
/** {@inheritDoc} */ @Override public void endVisit(InstanceofExpression node) { logger.warn("Method endVisitInstanceofExpression for " + node + " for " + node + " not implemented!"); super.endVisit(node); }
/** {@inheritDoc} */ @Override public boolean visit(InstanceofExpression node) { logger.warn("Method visitInstanceofExpression for " + node + " not implemented!"); return super.visit(node); }
@Override public boolean visit(final InstanceofExpression node) { return false; }
@Override public boolean visit(final InstanceofExpression node) { return this.visitInstrumentable(node); }
@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; }
@Override public boolean visit(InstanceofExpression node) { return visit((Expression)node); }
@Override public void endVisit(InstanceofExpression node) { endVisit((Expression)node); }