Java 类org.eclipse.jdt.core.dom.CastExpression 实例源码

项目:evosuite    文件:CodeGenerator.java   
@SuppressWarnings("unchecked")
private void createUnobservedInitStmt(final int logRecNo, final Block methodBlock, final AST ast)
{
    // NOTE: PLAIN INIT: has always one non-null param
    // TODO: use primitives
    final int    oid     = this.log.objectIds.get(logRecNo);
    final String type    = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    final Object value   = this.log.params.get(logRecNo)[0];
    this.isXStreamNeeded = true;

    final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
    // handling because there must always be a new instantiation statement for pseudo inits
    this.oidToVarMapping.remove(oid);
    vd.setName(ast.newSimpleName(this.createNewVarName(oid, type)));

    final MethodInvocation methodInvocation = ast.newMethodInvocation();
    final Name name = ast.newSimpleName("XSTREAM");
    methodInvocation.setExpression(name);
    methodInvocation.setName(ast.newSimpleName("fromXML")); 

    final StringLiteral xmlParam = ast.newStringLiteral();
    xmlParam.setLiteralValue((String) value);
    methodInvocation.arguments().add(xmlParam);

    final CastExpression castExpr = ast.newCastExpression();
    castExpr.setType(this.createAstType(type, ast));
    castExpr.setExpression(methodInvocation);

    vd.setInitializer(castExpr);

    final VariableDeclarationStatement vs = ast.newVariableDeclarationStatement(vd);
    vs.setType(this.createAstType(type, ast));

    methodBlock.statements().add(vs);
}
项目:eclipse.jdt.ls    文件:CastCorrectionProposal.java   
private Type getNewCastTypeNode(ASTRewrite rewrite, ImportRewrite importRewrite) {
    AST ast= rewrite.getAST();

    ImportRewriteContext context= new ContextSensitiveImportRewriteContext((CompilationUnit) fNodeToCast.getRoot(), fNodeToCast.getStartPosition(), importRewrite);

    if (fCastType != null) {
        return importRewrite.addImport(fCastType, ast,context, TypeLocation.CAST);
    }

    ASTNode node= fNodeToCast;
    ASTNode parent= node.getParent();
    if (parent instanceof CastExpression) {
        node= parent;
        parent= parent.getParent();
    }
    while (parent instanceof ParenthesizedExpression) {
        node= parent;
        parent= parent.getParent();
    }
    if (parent instanceof MethodInvocation) {
        MethodInvocation invocation= (MethodInvocation) node.getParent();
        if (invocation.getExpression() == node) {
            IBinding targetContext= ASTResolving.getParentMethodOrTypeBinding(node);
            ITypeBinding[] bindings= ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext);
            if (bindings.length > 0) {
                ITypeBinding first= getCastFavorite(bindings, fNodeToCast.resolveTypeBinding());

                Type newTypeNode= importRewrite.addImport(first, ast, context, TypeLocation.CAST);
                return newTypeNode;
            }
        }
    }
    Type newCastType= ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
    return newCastType;
}
项目:RefDiff    文件:ASTVisitorAtomicChange.java   
public boolean visit(CastExpression node) {
    if (mtbStack.isEmpty()) // not part of a method
        return true;

    Expression expression = node.getExpression();
    ITypeBinding type = node.getType().resolveBinding();
    IMethodBinding mtb = mtbStack.peek();
    String exprStr = expression.toString();
    String typeStr = getQualifiedName(type);
    String methodStr = getQualifiedName(mtb);

    exprStr = edit_str(exprStr);

    facts.add(Fact.makeCastFact(exprStr, typeStr, methodStr));

    return true;
}
项目:eclipse.jdt.ls    文件:UnusedCodeFix.java   
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {

    TextEditGroup group= createTextEditGroup(FixMessages.UnusedCodeFix_RemoveCast_description, cuRewrite);

    ASTRewrite rewrite= cuRewrite.getASTRewrite();

    CastExpression cast= fCast;
    Expression expression= cast.getExpression();
    if (expression instanceof ParenthesizedExpression) {
        Expression childExpression= ((ParenthesizedExpression) expression).getExpression();
        if (NecessaryParenthesesChecker.needsParentheses(childExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
            expression= childExpression;
        }
    }

    replaceCast(cast, expression, rewrite, group);
}
项目:eclipse.jdt.ls    文件:UnusedCodeFix.java   
public static UnusedCodeFix createRemoveUnusedCastFix(CompilationUnit compilationUnit, IProblemLocation problem) {
    if (problem.getProblemId() != IProblem.UnnecessaryCast) {
        return null;
    }

    ASTNode selectedNode= problem.getCoveringNode(compilationUnit);

    ASTNode curr= selectedNode;
    while (curr instanceof ParenthesizedExpression) {
        curr= ((ParenthesizedExpression) curr).getExpression();
    }

    if (!(curr instanceof CastExpression)) {
        return null;
    }

    return new UnusedCodeFix(FixMessages.UnusedCodeFix_RemoveCast_description, compilationUnit, new CompilationUnitRewriteOperation[] {new RemoveCastOperation((CastExpression)curr)});
}
项目:eclipse.jdt.ls    文件:UnusedCodeFix.java   
private static void replaceCast(CastExpression castExpression, Expression replacement, ASTRewrite rewrite, TextEditGroup group) {
    boolean castEnclosedInNecessaryParentheses= castExpression.getParent() instanceof ParenthesizedExpression
            && NecessaryParenthesesChecker.needsParentheses(castExpression, castExpression.getParent().getParent(), castExpression.getParent().getLocationInParent());

    ASTNode toReplace= castEnclosedInNecessaryParentheses ? castExpression.getParent() : castExpression;
    ASTNode move;
    if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
        if (replacement.getParent() instanceof ParenthesizedExpression) {
            move= rewrite.createMoveTarget(replacement.getParent());
        } else if (castEnclosedInNecessaryParentheses) {
            toReplace= castExpression;
            move= rewrite.createMoveTarget(replacement);
        } else {
            ParenthesizedExpression parentheses= replacement.getAST().newParenthesizedExpression();
            parentheses.setExpression((Expression) rewrite.createMoveTarget(replacement));
            move= parentheses;
        }
    } else {
        move= rewrite.createMoveTarget(replacement);
    }
    rewrite.replace(toReplace, move, group);
}
项目:Ref-Finder    文件:ASTVisitorAtomicChange.java   
public boolean visit(CastExpression node)
{
  if (this.mtbStack.isEmpty()) {
    return true;
  }
  Expression expression = node.getExpression();
  ITypeBinding type = node.getType().resolveBinding();
  IMethodBinding mtb = (IMethodBinding)this.mtbStack.peek();
  String exprStr = expression.toString();
  String typeStr = getQualifiedName(type);
  String methodStr = getQualifiedName(mtb);

  exprStr = edit_str(exprStr);

  this.facts.add(Fact.makeCastFact(exprStr, typeStr, methodStr));

  return true;
}
项目:Ref-Finder    文件:ASTVisitorAtomicChange.java   
public boolean visit(CastExpression node) {
    if (mtbStack.isEmpty()) // not part of a method
        return true;

    Expression expression = node.getExpression();
    ITypeBinding type = node.getType().resolveBinding();
    IMethodBinding mtb = mtbStack.peek();
    String exprStr = expression.toString();
    String typeStr = getQualifiedName(type);
    String methodStr = getQualifiedName(mtb);

    exprStr = edit_str(exprStr);

    facts.add(Fact.makeCastFact(exprStr, typeStr, methodStr));

    return true;
}
项目:Ref-Finder    文件:ASTVisitorAtomicChange.java   
public boolean visit(CastExpression node)
{
  if (this.mtbStack.isEmpty()) {
    return true;
  }
  Expression expression = node.getExpression();
  ITypeBinding type = node.getType().resolveBinding();
  IMethodBinding mtb = (IMethodBinding)this.mtbStack.peek();
  String exprStr = expression.toString();
  String typeStr = getQualifiedName(type);
  String methodStr = getQualifiedName(mtb);

  exprStr = edit_str(exprStr);

  this.facts.add(Fact.makeCastFact(exprStr, typeStr, methodStr));

  return true;
}
项目:che    文件:UnusedCodeFix.java   
/** {@inheritDoc} */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model)
    throws CoreException {

  TextEditGroup group =
      createTextEditGroup(FixMessages.UnusedCodeFix_RemoveCast_description, cuRewrite);

  ASTRewrite rewrite = cuRewrite.getASTRewrite();

  CastExpression cast = fCast;
  Expression expression = cast.getExpression();
  if (expression instanceof ParenthesizedExpression) {
    Expression childExpression = ((ParenthesizedExpression) expression).getExpression();
    if (NecessaryParenthesesChecker.needsParentheses(
        childExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
      expression = childExpression;
    }
  }

  replaceCast(cast, expression, rewrite, group);
}
项目:che    文件:UnusedCodeFix.java   
public static UnusedCodeFix createRemoveUnusedCastFix(
    CompilationUnit compilationUnit, IProblemLocation problem) {
  if (problem.getProblemId() != IProblem.UnnecessaryCast) return null;

  ASTNode selectedNode = problem.getCoveringNode(compilationUnit);

  ASTNode curr = selectedNode;
  while (curr instanceof ParenthesizedExpression) {
    curr = ((ParenthesizedExpression) curr).getExpression();
  }

  if (!(curr instanceof CastExpression)) return null;

  return new UnusedCodeFix(
      FixMessages.UnusedCodeFix_RemoveCast_description,
      compilationUnit,
      new CompilationUnitRewriteOperation[] {new RemoveCastOperation((CastExpression) curr)});
}
项目:che    文件:FullConstraintCreator.java   
@Override
public ITypeConstraint[] create(CastExpression castExpression) {
  Expression expression = castExpression.getExpression();
  Type type = castExpression.getType();
  ITypeConstraint[] definesConstraint =
      fTypeConstraintFactory.createDefinesConstraint(
          fConstraintVariableFactory.makeExpressionOrTypeVariable(castExpression, getContext()),
          fConstraintVariableFactory.makeTypeVariable(castExpression.getType()));
  if (isClassBinding(expression.resolveTypeBinding()) && isClassBinding(type.resolveBinding())) {
    ConstraintVariable expressionVariable =
        fConstraintVariableFactory.makeExpressionOrTypeVariable(expression, getContext());
    ConstraintVariable castExpressionVariable =
        fConstraintVariableFactory.makeExpressionOrTypeVariable(castExpression, getContext());
    ITypeConstraint[] c2 =
        createOrOrSubtypeConstraint(expressionVariable, castExpressionVariable);
    if (definesConstraint.length == 0) {
      return c2;
    } else {
      ITypeConstraint c1 = definesConstraint[0];
      Collection<ITypeConstraint> constraints = new ArrayList<ITypeConstraint>();
      constraints.add(c1);
      constraints.addAll(Arrays.asList(c2));
      return constraints.toArray(new ITypeConstraint[constraints.size()]);
    }
  } else return definesConstraint;
}
项目:Eclipse-Postfix-Code-Completion    文件:FullConstraintCreator.java   
@Override
public ITypeConstraint[] create(CastExpression castExpression){
    Expression expression= castExpression.getExpression();
    Type type= castExpression.getType();
    ITypeConstraint[] definesConstraint= fTypeConstraintFactory.createDefinesConstraint(fConstraintVariableFactory.makeExpressionOrTypeVariable(castExpression, getContext()),
                                                                                    fConstraintVariableFactory.makeTypeVariable(castExpression.getType()));
    if (isClassBinding(expression.resolveTypeBinding()) && isClassBinding(type.resolveBinding())){
        ConstraintVariable expressionVariable= fConstraintVariableFactory.makeExpressionOrTypeVariable(expression, getContext());
        ConstraintVariable castExpressionVariable= fConstraintVariableFactory.makeExpressionOrTypeVariable(castExpression, getContext());
        ITypeConstraint[] c2 = createOrOrSubtypeConstraint(expressionVariable, castExpressionVariable);
        if (definesConstraint.length == 0){
            return c2;
        } else {
            ITypeConstraint c1 = definesConstraint[0];
            Collection<ITypeConstraint> constraints= new ArrayList<ITypeConstraint>();
            constraints.add(c1);
            constraints.addAll(Arrays.asList(c2));
            return constraints.toArray(new ITypeConstraint[constraints.size()]);
        }
    } else
        return definesConstraint;
}
项目:Eclipse-Postfix-Code-Completion    文件:GenerateHashCodeEqualsOperation.java   
private Expression createShiftAssignment(Expression shift1, Expression shift2) {
    // (int)(element ^ (element >>> 32));
    // see implementation in Arrays.hashCode(), Double.hashCode() and
    // Long.hashCode()
    CastExpression ce= fAst.newCastExpression();
    ce.setType(fAst.newPrimitiveType(PrimitiveType.INT));

    InfixExpression unsignedShiftRight= fAst.newInfixExpression();
    unsignedShiftRight.setLeftOperand(shift1);
    unsignedShiftRight.setRightOperand(fAst.newNumberLiteral("32")); //$NON-NLS-1$
    unsignedShiftRight.setOperator(Operator.RIGHT_SHIFT_UNSIGNED);

    InfixExpression xor= fAst.newInfixExpression();
    xor.setLeftOperand(shift2);
    xor.setRightOperand(parenthesize(unsignedShiftRight));
    xor.setOperator(InfixExpression.Operator.XOR);

    ce.setExpression(parenthesize(xor));
    return ce;
}
项目:Eclipse-Postfix-Code-Completion    文件:UnusedCodeFix.java   
/**
 * {@inheritDoc}
 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {

    TextEditGroup group= createTextEditGroup(FixMessages.UnusedCodeFix_RemoveCast_description, cuRewrite);

    ASTRewrite rewrite= cuRewrite.getASTRewrite();

    CastExpression cast= fCast;
    Expression expression= cast.getExpression();
    if (expression instanceof ParenthesizedExpression) {
        Expression childExpression= ((ParenthesizedExpression) expression).getExpression();
        if (NecessaryParenthesesChecker.needsParentheses(childExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
            expression= childExpression;
        }
    }

    replaceCast(cast, expression, rewrite, group);
}
项目:Eclipse-Postfix-Code-Completion    文件:UnusedCodeFix.java   
public static UnusedCodeFix createRemoveUnusedCastFix(CompilationUnit compilationUnit, IProblemLocation problem) {
    if (problem.getProblemId() != IProblem.UnnecessaryCast)
        return null;

    ASTNode selectedNode= problem.getCoveringNode(compilationUnit);

    ASTNode curr= selectedNode;
    while (curr instanceof ParenthesizedExpression) {
        curr= ((ParenthesizedExpression) curr).getExpression();
    }

    if (!(curr instanceof CastExpression))
        return null;

    return new UnusedCodeFix(FixMessages.UnusedCodeFix_RemoveCast_description, compilationUnit, new CompilationUnitRewriteOperation[] {new RemoveCastOperation((CastExpression)curr)});
}
项目:Eclipse-Postfix-Code-Completion    文件:UnusedCodeFix.java   
private static void replaceCast(CastExpression castExpression, Expression replacement, ASTRewrite rewrite, TextEditGroup group) {
    boolean castEnclosedInNecessaryParentheses= castExpression.getParent() instanceof ParenthesizedExpression
            && NecessaryParenthesesChecker.needsParentheses(castExpression, castExpression.getParent().getParent(), castExpression.getParent().getLocationInParent());

    ASTNode toReplace= castEnclosedInNecessaryParentheses ? castExpression.getParent() : castExpression;
    ASTNode move;
    if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
        if (replacement.getParent() instanceof ParenthesizedExpression) {
            move= rewrite.createMoveTarget(replacement.getParent());
        } else if (castEnclosedInNecessaryParentheses) {
            toReplace= castExpression;
            move= rewrite.createMoveTarget(replacement);
        } else {
            ParenthesizedExpression parentheses= replacement.getAST().newParenthesizedExpression();
            parentheses.setExpression((Expression) rewrite.createMoveTarget(replacement));
            move= parentheses;
        }
    } else {
        move= rewrite.createMoveTarget(replacement);
    }
    rewrite.replace(toReplace, move, group);
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:FullConstraintCreator.java   
@Override
public ITypeConstraint[] create(CastExpression castExpression){
    Expression expression= castExpression.getExpression();
    Type type= castExpression.getType();
    ITypeConstraint[] definesConstraint= fTypeConstraintFactory.createDefinesConstraint(fConstraintVariableFactory.makeExpressionOrTypeVariable(castExpression, getContext()),
                                                                                    fConstraintVariableFactory.makeTypeVariable(castExpression.getType()));
    if (isClassBinding(expression.resolveTypeBinding()) && isClassBinding(type.resolveBinding())){
        ConstraintVariable expressionVariable= fConstraintVariableFactory.makeExpressionOrTypeVariable(expression, getContext());
        ConstraintVariable castExpressionVariable= fConstraintVariableFactory.makeExpressionOrTypeVariable(castExpression, getContext());
        ITypeConstraint[] c2 = createOrOrSubtypeConstraint(expressionVariable, castExpressionVariable);
        if (definesConstraint.length == 0){
            return c2;
        } else {
            ITypeConstraint c1 = definesConstraint[0];
            Collection<ITypeConstraint> constraints= new ArrayList<ITypeConstraint>();
            constraints.add(c1);
            constraints.addAll(Arrays.asList(c2));
            return constraints.toArray(new ITypeConstraint[constraints.size()]);
        }
    } else
        return definesConstraint;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:GenerateHashCodeEqualsOperation.java   
private Expression createShiftAssignment(Expression shift1, Expression shift2) {
    // (int)(element ^ (element >>> 32));
    // see implementation in Arrays.hashCode(), Double.hashCode() and
    // Long.hashCode()
    CastExpression ce= fAst.newCastExpression();
    ce.setType(fAst.newPrimitiveType(PrimitiveType.INT));

    InfixExpression unsignedShiftRight= fAst.newInfixExpression();
    unsignedShiftRight.setLeftOperand(shift1);
    unsignedShiftRight.setRightOperand(fAst.newNumberLiteral("32")); //$NON-NLS-1$
    unsignedShiftRight.setOperator(Operator.RIGHT_SHIFT_UNSIGNED);

    InfixExpression xor= fAst.newInfixExpression();
    xor.setLeftOperand(shift2);
    xor.setRightOperand(parenthesize(unsignedShiftRight));
    xor.setOperator(InfixExpression.Operator.XOR);

    ce.setExpression(parenthesize(xor));
    return ce;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:UnusedCodeFix.java   
/**
 * {@inheritDoc}
 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {

    TextEditGroup group= createTextEditGroup(FixMessages.UnusedCodeFix_RemoveCast_description, cuRewrite);

    ASTRewrite rewrite= cuRewrite.getASTRewrite();

    CastExpression cast= fCast;
    Expression expression= cast.getExpression();
    if (expression instanceof ParenthesizedExpression) {
        Expression childExpression= ((ParenthesizedExpression) expression).getExpression();
        if (NecessaryParenthesesChecker.needsParentheses(childExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
            expression= childExpression;
        }
    }

    replaceCast(cast, expression, rewrite, group);
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:UnusedCodeFix.java   
public static UnusedCodeFix createRemoveUnusedCastFix(CompilationUnit compilationUnit, IProblemLocation problem) {
    if (problem.getProblemId() != IProblem.UnnecessaryCast)
        return null;

    ASTNode selectedNode= problem.getCoveringNode(compilationUnit);

    ASTNode curr= selectedNode;
    while (curr instanceof ParenthesizedExpression) {
        curr= ((ParenthesizedExpression) curr).getExpression();
    }

    if (!(curr instanceof CastExpression))
        return null;

    return new UnusedCodeFix(FixMessages.UnusedCodeFix_RemoveCast_description, compilationUnit, new CompilationUnitRewriteOperation[] {new RemoveCastOperation((CastExpression)curr)});
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:UnusedCodeFix.java   
private static void replaceCast(CastExpression castExpression, Expression replacement, ASTRewrite rewrite, TextEditGroup group) {
    boolean castEnclosedInNecessaryParentheses= castExpression.getParent() instanceof ParenthesizedExpression
            && NecessaryParenthesesChecker.needsParentheses(castExpression, castExpression.getParent().getParent(), castExpression.getParent().getLocationInParent());

    ASTNode toReplace= castEnclosedInNecessaryParentheses ? castExpression.getParent() : castExpression;
    ASTNode move;
    if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
        if (replacement.getParent() instanceof ParenthesizedExpression) {
            move= rewrite.createMoveTarget(replacement.getParent());
        } else if (castEnclosedInNecessaryParentheses) {
            toReplace= castExpression;
            move= rewrite.createMoveTarget(replacement);
        } else {
            ParenthesizedExpression parentheses= replacement.getAST().newParenthesizedExpression();
            parentheses.setExpression((Expression) rewrite.createMoveTarget(replacement));
            move= parentheses;
        }
    } else {
        move= rewrite.createMoveTarget(replacement);
    }
    rewrite.replace(toReplace, move, group);
}
项目:eclipse.jdt.ls    文件:FlowAnalyzer.java   
@Override
public void endVisit(CastExpression node) {
    if (skipNode(node)) {
        return;
    }
    processSequential(node, node.getType(), node.getExpression());
}
项目:eclipse.jdt.ls    文件:OperatorPrecedence.java   
/**
 * 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;
}
项目:che    文件:GetterSetterUtil.java   
/**
 * Checks if the assignment needs a downcast and inserts it if necessary
 *
 * @param expression the right hand-side
 * @param expressionType the type of the right hand-side. Can be null
 * @param ast the AST
 * @param variableType the Type of the variable the expression will be assigned to
 * @param is50OrHigher if <code>true</code> java 5.0 code will be assumed
 * @return the casted expression if necessary
 */
private static Expression createNarrowCastIfNessecary(
    Expression expression,
    ITypeBinding expressionType,
    AST ast,
    ITypeBinding variableType,
    boolean is50OrHigher) {
  PrimitiveType castTo = null;
  if (variableType.isEqualTo(expressionType)) return expression; // no cast for same type
  if (is50OrHigher) {
    if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) // $NON-NLS-1$
    castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
    if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) // $NON-NLS-1$
    castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
    if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) // $NON-NLS-1$
    castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
  }
  if (ast.resolveWellKnownType("char").isEqualTo(variableType)) // $NON-NLS-1$
  castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
  if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) // $NON-NLS-1$
  castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
  if (ast.resolveWellKnownType("short").isEqualTo(variableType)) // $NON-NLS-1$
  castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
  if (castTo != null) {
    CastExpression cast = ast.newCastExpression();
    if (NecessaryParenthesesChecker.needsParentheses(
        expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
      ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
      parenthesized.setExpression(expression);
      cast.setExpression(parenthesized);
    } else cast.setExpression(expression);
    cast.setType(castTo);
    return cast;
  }
  return expression;
}
项目:che    文件:UnusedCodeFix.java   
private static void replaceCast(
    CastExpression castExpression,
    Expression replacement,
    ASTRewrite rewrite,
    TextEditGroup group) {
  boolean castEnclosedInNecessaryParentheses =
      castExpression.getParent() instanceof ParenthesizedExpression
          && NecessaryParenthesesChecker.needsParentheses(
              castExpression,
              castExpression.getParent().getParent(),
              castExpression.getParent().getLocationInParent());

  ASTNode toReplace =
      castEnclosedInNecessaryParentheses ? castExpression.getParent() : castExpression;
  ASTNode move;
  if (NecessaryParenthesesChecker.needsParentheses(
      replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
    if (replacement.getParent() instanceof ParenthesizedExpression) {
      move = rewrite.createMoveTarget(replacement.getParent());
    } else if (castEnclosedInNecessaryParentheses) {
      toReplace = castExpression;
      move = rewrite.createMoveTarget(replacement);
    } else {
      ParenthesizedExpression parentheses = replacement.getAST().newParenthesizedExpression();
      parentheses.setExpression((Expression) rewrite.createMoveTarget(replacement));
      move = parentheses;
    }
  } else {
    move = rewrite.createMoveTarget(replacement);
  }
  rewrite.replace(toReplace, move, group);
}
项目:che    文件:InferTypeArgumentsRefactoring.java   
private static ASTNode rewriteCastVariable(
    CastVariable2 castCv,
    CompilationUnitRewrite rewrite,
    InferTypeArgumentsTCModel tCModel) { // , List positionGroups) {
  ASTNode node = castCv.getRange().getNode(rewrite.getRoot());

  ConstraintVariable2 expressionVariable = castCv.getExpressionVariable();
  ConstraintVariable2 methodReceiverCv = tCModel.getMethodReceiverCv(expressionVariable);
  if (methodReceiverCv != null) {
    TType chosenReceiverType =
        InferTypeArgumentsConstraintsSolver.getChosenType(methodReceiverCv);
    if (chosenReceiverType == null) return null;
    else if (!InferTypeArgumentsTCModel.isAGenericType(chosenReceiverType)) return null;
    else if (hasUnboundElement(methodReceiverCv, tCModel)) return null;
  }

  CastExpression castExpression = (CastExpression) node;
  Expression expression = castExpression.getExpression();
  ASTNode nodeToReplace;
  if (castExpression.getParent() instanceof ParenthesizedExpression)
    nodeToReplace = castExpression.getParent();
  else nodeToReplace = castExpression;

  Expression newExpression = (Expression) rewrite.getASTRewrite().createMoveTarget(expression);
  rewrite
      .getASTRewrite()
      .replace(
          nodeToReplace,
          newExpression,
          rewrite.createGroupDescription(
              RefactoringCoreMessages.InferTypeArgumentsRefactoring_removeCast));
  rewrite.getImportRemover().registerRemovedNode(nodeToReplace);
  return newExpression;
}
项目:che    文件:InferTypeArgumentsTCModel.java   
public CastVariable2 makeCastVariable(
    CastExpression castExpression, ConstraintVariable2 expressionCv) {
  ITypeBinding typeBinding = castExpression.resolveTypeBinding();
  ICompilationUnit cu = RefactoringASTParser.getCompilationUnit(castExpression);
  CompilationUnitRange range = new CompilationUnitRange(cu, castExpression);
  CastVariable2 castCv = new CastVariable2(createTType(typeBinding), range, expressionCv);
  fCastVariables.add(castCv);
  return castCv;
}
项目:che    文件:OperatorPrecedence.java   
/**
 * 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;
}
项目:tassal    文件:TypenameScopeExtractor.java   
@Override
public boolean visit(CastExpression node) {
    final String type = node.getType().toString();
    if (topMethod != null && methodsAsRoot) {
        types.put(topMethod, type);
    } else if (topClass != null) {
        types.put(topClass, type);
    }
    return super.visit(node);
}
项目:Eclipse-Postfix-Code-Completion    文件:SuperTypeConstraintsModel.java   
/**
 * Creates a cast variable.
 *
 * @param expression the cast expression
 * @param variable the associated constraint variable
 * @return the created cast variable, or <code>null</code>
 */
public final ConstraintVariable2 createCastVariable(final CastExpression expression, final ConstraintVariable2 variable) {
    ITypeBinding binding= expression.resolveTypeBinding();
    if (binding.isArray())
        binding= binding.getElementType();
    if (isConstrainedType(binding)) {
        final CastVariable2 result= new CastVariable2(createTType(binding), new CompilationUnitRange(RefactoringASTParser.getCompilationUnit(expression), expression), variable);
        fCastVariables.add(result);
        return result;
    }
    return null;
}
项目:Eclipse-Postfix-Code-Completion    文件:SuperTypeConstraintsCreator.java   
@Override
public final void endVisit(final CastExpression node) {
    final ConstraintVariable2 first= (ConstraintVariable2) node.getType().getProperty(PROPERTY_CONSTRAINT_VARIABLE);
    if (first != null) {
        node.setProperty(PROPERTY_CONSTRAINT_VARIABLE, first);
        final ConstraintVariable2 second= (ConstraintVariable2) node.getExpression().getProperty(PROPERTY_CONSTRAINT_VARIABLE);
        if (second != null)
            fModel.createCastVariable(node, second);
    }
}
项目:Eclipse-Postfix-Code-Completion    文件:InferTypeArgumentsRefactoring.java   
private static ASTNode rewriteCastVariable(CastVariable2 castCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel) {//, List positionGroups) {
    ASTNode node= castCv.getRange().getNode(rewrite.getRoot());

    ConstraintVariable2 expressionVariable= castCv.getExpressionVariable();
    ConstraintVariable2 methodReceiverCv= tCModel.getMethodReceiverCv(expressionVariable);
    if (methodReceiverCv != null) {
        TType chosenReceiverType= InferTypeArgumentsConstraintsSolver.getChosenType(methodReceiverCv);
        if (chosenReceiverType == null)
            return null;
        else if (! InferTypeArgumentsTCModel.isAGenericType(chosenReceiverType))
            return null;
        else if (hasUnboundElement(methodReceiverCv, tCModel))
            return null;
    }

    CastExpression castExpression= (CastExpression) node;
    Expression expression= castExpression.getExpression();
    ASTNode nodeToReplace;
    if (castExpression.getParent() instanceof ParenthesizedExpression)
        nodeToReplace= castExpression.getParent();
    else
        nodeToReplace= castExpression;

    Expression newExpression= (Expression) rewrite.getASTRewrite().createMoveTarget(expression);
    rewrite.getASTRewrite().replace(nodeToReplace, newExpression, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_removeCast));
    rewrite.getImportRemover().registerRemovedNode(nodeToReplace);
    return newExpression;
}
项目:Eclipse-Postfix-Code-Completion    文件:InferTypeArgumentsTCModel.java   
public CastVariable2 makeCastVariable(CastExpression castExpression, ConstraintVariable2 expressionCv) {
    ITypeBinding typeBinding= castExpression.resolveTypeBinding();
    ICompilationUnit cu= RefactoringASTParser.getCompilationUnit(castExpression);
    CompilationUnitRange range= new CompilationUnitRange(cu, castExpression);
    CastVariable2 castCv= new CastVariable2(createTType(typeBinding), range, expressionCv);
    fCastVariables.add(castCv);
    return castCv;
}
项目:Eclipse-Postfix-Code-Completion    文件:ExtractTempRefactoring.java   
private Type createTempType() throws CoreException {
    Expression expression= getSelectedExpression().getAssociatedExpression();

    Type resultingType= null;
    ITypeBinding typeBinding= expression.resolveTypeBinding();

    ASTRewrite rewrite= fCURewrite.getASTRewrite();
    AST ast= rewrite.getAST();

    if (expression instanceof ClassInstanceCreation && (typeBinding == null || typeBinding.getTypeArguments().length == 0)) {
        resultingType= (Type) rewrite.createCopyTarget(((ClassInstanceCreation) expression).getType());
    } else if (expression instanceof CastExpression) {
        resultingType= (Type) rewrite.createCopyTarget(((CastExpression) expression).getType());
    } else {
        if (typeBinding == null) {
            typeBinding= ASTResolving.guessBindingForReference(expression);
        }
        if (typeBinding != null) {
            typeBinding= Bindings.normalizeForDeclarationUse(typeBinding, ast);
            ImportRewrite importRewrite= fCURewrite.getImportRewrite();
            ImportRewriteContext context= new ContextSensitiveImportRewriteContext(expression, importRewrite);
            resultingType= importRewrite.addImport(typeBinding, ast, context);
        } else {
            resultingType= ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
        }
    }
    if (fLinkedProposalModel != null) {
        LinkedProposalPositionGroup typeGroup= fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
        typeGroup.addPosition(rewrite.track(resultingType), false);
        if (typeBinding != null) {
            ITypeBinding[] relaxingTypes= ASTResolving.getNarrowingTypes(ast, typeBinding);
            for (int i= 0; i < relaxingTypes.length; i++) {
                typeGroup.addProposal(relaxingTypes[i], fCURewrite.getCu(), relaxingTypes.length - i);
            }
        }
    }
    return resultingType;
}
项目:Eclipse-Postfix-Code-Completion    文件:OperatorPrecedence.java   
/**
 * 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;
}
项目:Eclipse-Postfix-Code-Completion    文件:GetterSetterUtil.java   
/**
 * Checks if the assignment needs a downcast and inserts it if necessary
 *
 * @param expression the right hand-side
 * @param expressionType the type of the right hand-side. Can be null
 * @param ast the AST
 * @param variableType the Type of the variable the expression will be assigned to
 * @param is50OrHigher if <code>true</code> java 5.0 code will be assumed
 * @return the casted expression if necessary
 */
private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
    PrimitiveType castTo= null;
    if (variableType.isEqualTo(expressionType))
        return expression; //no cast for same type
    if (is50OrHigher) {
        if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) //$NON-NLS-1$
            castTo= ast.newPrimitiveType(PrimitiveType.CHAR);
        if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) //$NON-NLS-1$
            castTo= ast.newPrimitiveType(PrimitiveType.BYTE);
        if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) //$NON-NLS-1$
            castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
    }
    if (ast.resolveWellKnownType("char").isEqualTo(variableType)) //$NON-NLS-1$
        castTo= ast.newPrimitiveType(PrimitiveType.CHAR);
    if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) //$NON-NLS-1$
        castTo= ast.newPrimitiveType(PrimitiveType.BYTE);
    if (ast.resolveWellKnownType("short").isEqualTo(variableType)) //$NON-NLS-1$
        castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
    if (castTo != null) {
        CastExpression cast= ast.newCastExpression();
        if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
            ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
            parenthesized.setExpression(expression);
            cast.setExpression(parenthesized);
        } else
            cast.setExpression(expression);
        cast.setType(castTo);
        return cast;
    }
    return expression;
}
项目:Eclipse-Postfix-Code-Completion    文件:ExceptionOccurrencesFinder.java   
@Override
public boolean visit(CastExpression node) {
    if ("java.lang.ClassCastException".equals(fException.getQualifiedName())) { //$NON-NLS-1$
        Type type= node.getType();
        fResult.add(new OccurrenceLocation(type.getStartPosition(), type.getLength(), 0, fDescription));
    }
    return super.visit(node);
}
项目:Eclipse-Postfix-Code-Completion    文件:UnresolvedElementsSubProcessor.java   
private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression, Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes, Collection<ICommandAccess> proposals) {
    ITypeBinding castType= expression.getType().resolveBinding();
    if (castType == null) {
        return false;
    }
    if (paramTypes != null) {
        if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) {
            return false;
        }
    } else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
        return false;
    }
    ITypeBinding bindingToCast= accessExpression.resolveTypeBinding();
    if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
        return false;
    }

    IMethodBinding res= Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
    if (res != null) {
        AST ast= expression.getAST();
        ASTRewrite rewrite= ASTRewrite.create(ast);
        CastExpression newCast= ast.newCastExpression();
        newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
        newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
        ParenthesizedExpression parents= ast.newParenthesizedExpression();
        parents.setExpression(newCast);

        ASTNode node= rewrite.createCopyTarget(expression.getExpression());
        rewrite.replace(expression, node, null);
        rewrite.replace(accessExpression, parents, null);

        String label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
        Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
        ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_PARENTHESES_AROUND_CAST, image);
        proposals.add(proposal);
        return true;
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion    文件:CastCorrectionProposal.java   
private Type getNewCastTypeNode(ASTRewrite rewrite, ImportRewrite importRewrite) {
    AST ast= rewrite.getAST();

    ImportRewriteContext context= new ContextSensitiveImportRewriteContext((CompilationUnit) fNodeToCast.getRoot(), fNodeToCast.getStartPosition(), importRewrite);

    if (fCastType != null) {
        return importRewrite.addImport(fCastType, ast,context);
    }

    ASTNode node= fNodeToCast;
    ASTNode parent= node.getParent();
    if (parent instanceof CastExpression) {
        node= parent;
        parent= parent.getParent();
    }
    while (parent instanceof ParenthesizedExpression) {
        node= parent;
        parent= parent.getParent();
    }
    if (parent instanceof MethodInvocation) {
        MethodInvocation invocation= (MethodInvocation) node.getParent();
        if (invocation.getExpression() == node) {
            IBinding targetContext= ASTResolving.getParentMethodOrTypeBinding(node);
            ITypeBinding[] bindings= ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext);
            if (bindings.length > 0) {
                ITypeBinding first= getCastFavorite(bindings, fNodeToCast.resolveTypeBinding());

                Type newTypeNode= importRewrite.addImport(first, ast, context);
                addLinkedPosition(rewrite.track(newTypeNode), true, "casttype"); //$NON-NLS-1$
                for (int i= 0; i < bindings.length; i++) {
                    addLinkedPositionProposal("casttype", bindings[i]); //$NON-NLS-1$
                }
                return newTypeNode;
            }
        }
    }
    Type newCastType= ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
    addLinkedPosition(rewrite.track(newCastType), true, "casttype"); //$NON-NLS-1$
    return newCastType;
}