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 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; }
/** * Returns whether an expression at the given location needs explicit boxing. * * @param expression the expression * @return <code>true</code> iff an expression at the given location needs explicit boxing * @since 3.6 */ private static boolean needsExplicitBoxing(Expression expression) { StructuralPropertyDescriptor locationInParent= expression.getLocationInParent(); if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) { return needsExplicitBoxing((ParenthesizedExpression) expression.getParent()); } if (locationInParent == ClassInstanceCreation.EXPRESSION_PROPERTY || locationInParent == FieldAccess.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.EXPRESSION_PROPERTY) { return true; } return false; }
@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); }
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)}); }
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); }
public void replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup) { ASTNode groupNode = getGroupRoot(); List<Expression> allOperands = findGroupMembersInOrderFor(getGroupRoot()); if (allOperands.size() == fOperands.size()) { if (replacement instanceof Name && groupNode.getParent() instanceof ParenthesizedExpression) { // replace including the parenthesized expression around it rewrite.replace(groupNode.getParent(), replacement, textEditGroup); } else { rewrite.replace(groupNode, replacement, textEditGroup); } return; } rewrite.replace(fOperands.get(0), replacement, textEditGroup); int first = allOperands.indexOf(fOperands.get(0)); int after = first + fOperands.size(); for (int i = first + 1; i < after; i++) { rewrite.remove(allOperands.get(i), textEditGroup); } }
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; }
/** {@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); }
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)}); }
/** {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { TextEditGroup group = createTextEditGroup( FixMessages.ExpressionsFix_addParanoiacParentheses_description, cuRewrite); ASTRewrite rewrite = cuRewrite.getASTRewrite(); AST ast = cuRewrite.getRoot().getAST(); for (int i = 0; i < fExpressions.length; i++) { // add parenthesis around expression Expression expression = fExpressions[i]; ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression(); parenthesizedExpression.setExpression((Expression) rewrite.createCopyTarget(expression)); rewrite.replace(expression, parenthesizedExpression, group); } }
public static ExpressionsFix createRemoveUnnecessaryParenthesisFix( CompilationUnit compilationUnit, ASTNode[] nodes) { // check sub-expressions in fully covered nodes final ArrayList<ParenthesizedExpression> changedNodes = new ArrayList<ParenthesizedExpression>(); for (int i = 0; i < nodes.length; i++) { ASTNode covered = nodes[i]; if (covered instanceof ParenthesizedExpression || covered instanceof InfixExpression) covered.accept(new UnnecessaryParenthesisVisitor(changedNodes)); } if (changedNodes.isEmpty()) return null; HashSet<ParenthesizedExpression> expressions = new HashSet<ParenthesizedExpression>(changedNodes); RemoveParenthesisOperation op = new RemoveParenthesisOperation(expressions); return new ExpressionsFix( FixMessages.ExpressionsFix_removeUnnecessaryParentheses_description, compilationUnit, new CompilationUnitRewriteOperation[] {op}); }
public static IBinding resolveBinding(Expression expression) { if (expression instanceof Name) return ((Name) expression).resolveBinding(); if (expression instanceof ParenthesizedExpression) return resolveBinding(((ParenthesizedExpression) expression).getExpression()); else if (expression instanceof Assignment) return resolveBinding(((Assignment) expression).getLeftHandSide()); // TODO ??? else if (expression instanceof MethodInvocation) return ((MethodInvocation) expression).resolveMethodBinding(); else if (expression instanceof SuperMethodInvocation) return ((SuperMethodInvocation) expression).resolveMethodBinding(); else if (expression instanceof FieldAccess) return ((FieldAccess) expression).resolveFieldBinding(); else if (expression instanceof SuperFieldAccess) return ((SuperFieldAccess) expression).resolveFieldBinding(); else if (expression instanceof ConditionalExpression) return resolveBinding(((ConditionalExpression) expression).getThenExpression()); return null; }
private RefactoringStatus checkExpression() throws JavaModelException { Expression selectedExpression= getSelectedExpression().getAssociatedExpression(); if (selectedExpression != null) { final ASTNode parent= selectedExpression.getParent(); if (selectedExpression instanceof NullLiteral) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals); } else if (selectedExpression instanceof ArrayInitializer) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer); } else if (selectedExpression instanceof Assignment) { if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression)) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment); else return null; } else if (selectedExpression instanceof SimpleName) { if ((((SimpleName) selectedExpression)).isDeclaration()) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations); if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression); } else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources); } } return null; }
public static IBinding resolveBinding(Expression expression){ if (expression instanceof Name) return ((Name)expression).resolveBinding(); if (expression instanceof ParenthesizedExpression) return resolveBinding(((ParenthesizedExpression)expression).getExpression()); else if (expression instanceof Assignment) return resolveBinding(((Assignment)expression).getLeftHandSide());//TODO ??? else if (expression instanceof MethodInvocation) return ((MethodInvocation)expression).resolveMethodBinding(); else if (expression instanceof SuperMethodInvocation) return ((SuperMethodInvocation)expression).resolveMethodBinding(); else if (expression instanceof FieldAccess) return ((FieldAccess)expression).resolveFieldBinding(); else if (expression instanceof SuperFieldAccess) return ((SuperFieldAccess)expression).resolveFieldBinding(); else if (expression instanceof ConditionalExpression) return resolveBinding(((ConditionalExpression)expression).getThenExpression()); return null; }
private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) { if (existing == null && removeParentheses) { while (originalNode instanceof ParenthesizedExpression) { originalNode= ((ParenthesizedExpression)originalNode).getExpression(); } } Expression newRight= (Expression)rewrite.createMoveTarget(originalNode); if (originalNode instanceof InfixExpression) { ((InfixExpression)newRight).setOperator(((InfixExpression)originalNode).getOperator()); } if (existing == null) { return newRight; } AST ast= rewrite.getAST(); InfixExpression infix= ast.newInfixExpression(); infix.setOperator(operator); infix.setLeftOperand(existing); infix.setRightOperand(newRight); return infix; }
public void replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup) { ASTNode groupNode= getGroupRoot(); List<Expression> allOperands= findGroupMembersInOrderFor(getGroupRoot()); if (allOperands.size() == fOperands.size()) { if (replacement instanceof Name && groupNode.getParent() instanceof ParenthesizedExpression) { // replace including the parenthesized expression around it rewrite.replace(groupNode.getParent(), replacement, textEditGroup); } else { rewrite.replace(groupNode, replacement, textEditGroup); } return; } rewrite.replace(fOperands.get(0), replacement, textEditGroup); int first= allOperands.indexOf(fOperands.get(0)); int after= first + fOperands.size(); for (int i= first + 1; i < after; i++) { rewrite.remove(allOperands.get(i), textEditGroup); } }
private static boolean getRemoveExtraParenthesesProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) { ArrayList<ASTNode> nodes; if (context.getSelectionLength() == 0 && covering instanceof ParenthesizedExpression) { nodes= new ArrayList<ASTNode>(); nodes.add(covering); } else { nodes= coveredNodes; } if (nodes.isEmpty()) return false; IProposableFix fix= ExpressionsFix.createRemoveUnnecessaryParenthesisFix(context.getASTRoot(), nodes.toArray(new ASTNode[nodes.size()])); if (fix == null) return false; if (resultingCollections == null) return true; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_REMOVE); Map<String, String> options= new Hashtable<String, String>(); options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES, CleanUpOptions.TRUE); options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER, CleanUpOptions.TRUE); FixCorrectionProposal proposal= new FixCorrectionProposal(fix, new ExpressionsCleanUp(options), 1, image, context); resultingCollections.add(proposal); return true; }
/** * {@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); }
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)}); }
/** * {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { TextEditGroup group= createTextEditGroup(FixMessages.ExpressionsFix_addParanoiacParentheses_description, cuRewrite); ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); for (int i= 0; i < fExpressions.length; i++) { // add parenthesis around expression Expression expression= fExpressions[i]; ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression(); parenthesizedExpression.setExpression((Expression) rewrite.createCopyTarget(expression)); rewrite.replace(expression, parenthesizedExpression, group); } }
private static boolean getRemoveExtraParenthesesProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) { ArrayList<ASTNode> nodes; if (context.getSelectionLength() == 0 && covering instanceof ParenthesizedExpression) { nodes= new ArrayList<ASTNode>(); nodes.add(covering); } else { nodes= coveredNodes; } if (nodes.isEmpty()) return false; IProposableFix fix= ExpressionsFix.createRemoveUnnecessaryParenthesisFix(context.getASTRoot(), nodes.toArray(new ASTNode[nodes.size()])); if (fix == null) return false; if (resultingCollections == null) return true; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_REMOVE); Map<String, String> options= new Hashtable<String, String>(); options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES, CleanUpOptions.TRUE); options.put(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER, CleanUpOptions.TRUE); FixCorrectionProposal proposal= new FixCorrectionProposal(fix, new ExpressionsCleanUp(options), IProposalRelevance.REMOVE_EXTRA_PARENTHESES, image, context); resultingCollections.add(proposal); return true; }
private static Expression getExpression(ParenthesizedExpression node) { Expression expression= node.getExpression(); while (expression instanceof ParenthesizedExpression) { expression= ((ParenthesizedExpression)expression).getExpression(); } return expression; }
@Override public void endVisit(ParenthesizedExpression node) { if (skipNode(node)) { return; } assignFlowInfo(node, node.getExpression()); }
private void replaceDuplicates(CompilationUnitChange result, int modifiers) { int numberOf = getNumberOfDuplicates(); if (numberOf == 0 || !fReplaceDuplicates) { return; } String label = null; if (numberOf == 1) { label = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_duplicates_single, BasicElementLabels.getJavaElementName(fMethodName)); } else { label = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_duplicates_multi, BasicElementLabels.getJavaElementName(fMethodName)); } TextEditGroup description = new TextEditGroup(label); result.addTextEditGroup(description); for (Match duplicate : fDuplicates) { if (!duplicate.isInvalidNode()) { if (isDestinationReachable(duplicate.getEnclosingMethod())) { ASTNode[] callNodes = createCallNodes(duplicate, modifiers); ASTNode[] duplicateNodes = duplicate.getNodes(); for (int i = 0; i < duplicateNodes.length; i++) { ASTNode parent = duplicateNodes[i].getParent(); if (parent instanceof ParenthesizedExpression) { duplicateNodes[i] = parent; } } new StatementRewrite(fRewriter, duplicateNodes).replace(callNodes, description); } } } }
public static ASTNode stripParenthesizedExpressions(ASTNode node) { if (node != null && node.getNodeType() == ASTNode.PARENTHESIZED_EXPRESSION) { ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) node; return stripParenthesizedExpressions(parenthesizedExpression.getExpression()); } else return node; }
public Expression createCopyTarget(ASTRewrite rewrite, boolean removeSurroundingParenthesis) { Expression node = getAssociatedExpression(); if (removeSurroundingParenthesis && node instanceof ParenthesizedExpression) { node = ((ParenthesizedExpression) node).getExpression(); } return (Expression) rewrite.createCopyTarget(node); }
public void replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup) { if (replacement instanceof Name && fNode.getParent() instanceof ParenthesizedExpression) { // replace including the parenthesized expression around it rewrite.replace(fNode.getParent(), replacement, textEditGroup); } else { rewrite.replace(fNode, replacement, textEditGroup); } }
private static Expression getExpression(ParenthesizedExpression node) { Expression expression = node.getExpression(); while (expression instanceof ParenthesizedExpression) { expression = ((ParenthesizedExpression) expression).getExpression(); } return expression; }
/** * 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; }
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); }