/** * Can the parentheses be removed from the parenthesized expression ? * * <p> * <b>Note:</b> The parenthesized expression must not be an unparented node. * </p> * * @param expression the parenthesized expression * @return <code>true</code> if parentheses can be removed, <code>false</code> otherwise. */ public static boolean canRemoveParentheses(Expression expression) { return canRemoveParentheses(expression, expression.getParent(), expression.getLocationInParent()); }
/** * Can the parentheses be removed from the parenthesized expression ? * * <p> * <b>Note:</b> The parenthesized expression must not be an unparented node. * </p> * * @param expression the parenthesized expression * @return <code>true</code> if parentheses can be removed, <code>false</code> otherwise. */ public static boolean canRemoveParentheses(Expression expression) { return canRemoveParentheses(expression, expression.getParent(), expression.getLocationInParent()); }
/** * 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; }
/** * 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; }
private boolean isResourceInTry(Expression node) { return getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED && getFirstSelectedNode() == node && node.getLocationInParent() == TryStatement.RESOURCES2_PROPERTY; } }
private RefactoringStatus checkExpression() { //TODO: adjust error messages (or generalize for all refactorings on expression-selections?) Expression selectedExpression= fSelectedExpression; if (selectedExpression instanceof Name && selectedExpression.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new); //TODO: let's just take the CIC automatically (no ambiguity -> no problem -> no dialog ;-) 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 (selectedExpression.getParent() instanceof Expression) 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 (selectedExpression.getParent() instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || selectedExpression.getParent() instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression); } return null; }
private boolean isAutoUnBoxing(Expression node) { if (isAutoUnBoxingExpression(node)) { return true; } // special cases: the autoboxing conversions happens at a // location that is not mapped directly to a simple name // or a literal, but can still be mapped somehow // A) expressions StructuralPropertyDescriptor desc = node.getLocationInParent(); if (desc == ArrayAccess.ARRAY_PROPERTY || desc == InfixExpression.LEFT_OPERAND_PROPERTY || desc == InfixExpression.RIGHT_OPERAND_PROPERTY || desc == ConditionalExpression.THEN_EXPRESSION_PROPERTY || desc == PrefixExpression.OPERAND_PROPERTY || desc == CastExpression.EXPRESSION_PROPERTY || desc == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) { ASTNode parent = node.getParent(); if (parent instanceof Expression) { return isAutoUnBoxingExpression((Expression) parent); } } // B) constructor invocations if (desc == QualifiedName.NAME_PROPERTY) { node = (Expression) node.getParent(); desc = node.getLocationInParent(); } if (desc == SimpleType.NAME_PROPERTY || desc == NameQualifiedType.NAME_PROPERTY) { ASTNode parent = node.getParent(); if (parent != null && parent.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { parent = parent.getParent(); return isAutoUnBoxingExpression((ClassInstanceCreation) parent); } } return false; }
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); } } 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; }
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; }
private RefactoringStatus checkExpression() { //TODO: adjust error messages (or generalize for all refactorings on expression-selections?) Expression selectedExpression= fSelectedExpression; if (selectedExpression instanceof Name && selectedExpression.getParent() instanceof ClassInstanceCreation) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new); //TODO: let's just take the CIC automatically (no ambiguity -> no problem -> no dialog ;-) 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 (selectedExpression.getParent() instanceof Expression) 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 (selectedExpression.getParent() instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || selectedExpression.getParent() instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression); } return null; }
private RefactoringStatus checkExpression() throws JavaModelException { RefactoringStatus result= new RefactoringStatus(); result.merge(checkExpressionBinding()); if(result.hasFatalError()) return result; checkAllStaticFinal(); IExpressionFragment selectedExpression= getSelectedExpression(); Expression associatedExpression= selectedExpression.getAssociatedExpression(); if (associatedExpression instanceof NullLiteral) result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals)); else if (!ConstantChecks.isLoadTimeConstant(selectedExpression)) result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant)); else if (associatedExpression instanceof SimpleName) { if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression); } return result; }
private RefactoringStatus checkExpression() throws JavaModelException { RefactoringStatus result= new RefactoringStatus(); result.merge(checkExpressionBinding()); if(result.hasFatalError()) return result; checkAllStaticFinal(); IExpressionFragment selectedExpression= getSelectedExpression(); Expression associatedExpression= selectedExpression.getAssociatedExpression(); if (associatedExpression instanceof NullLiteral) result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals)); else if (!ConstantChecks.isLoadTimeConstant(selectedExpression)) result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant)); else if (associatedExpression instanceof SimpleName) { if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression); } return result; }
private RefactoringStatus checkExpression() throws JavaModelException { RefactoringStatus result= new RefactoringStatus(); result.merge(checkExpressionBinding()); if(result.hasFatalError()) return result; checkAllStaticFinal(); IExpressionFragment selectedExpression= getSelectedExpression(); Expression associatedExpression= selectedExpression.getAssociatedExpression(); if (associatedExpression instanceof NullLiteral) result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals)); else if (!ConstantChecks.isLoadTimeConstant(selectedExpression)) result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant)); else if (associatedExpression instanceof SimpleName) { if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression); } return result; }
private RefactoringStatus checkExpression() throws JavaModelException { RefactoringStatus result = new RefactoringStatus(); result.merge(checkExpressionBinding()); if (result.hasFatalError()) { return result; } checkAllStaticFinal(); IExpressionFragment selectedExpression = getSelectedExpression(); Expression associatedExpression = selectedExpression.getAssociatedExpression(); if (associatedExpression instanceof NullLiteral) { result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals)); } else if (!ConstantChecks.isLoadTimeConstant(selectedExpression)) { result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant)); } else if (associatedExpression instanceof SimpleName) { if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression); } } return result; }
private static Expression getAssignedValue(ProposalParameter context) { ASTNode parent= context.accessNode.getParent(); ASTRewrite astRewrite= context.astRewrite; IJavaProject javaProject= context.compilationUnit.getJavaProject(); IMethodBinding getter= findGetter(context); Expression getterExpression= null; if (getter != null) { getterExpression= astRewrite.getAST().newSimpleName("placeholder"); //$NON-NLS-1$ } ITypeBinding type= context.variableBinding.getType(); boolean is50OrHigher= JavaModelUtil.is50OrHigher(javaProject); Expression result= GetterSetterUtil.getAssignedValue(parent, astRewrite, getterExpression, type, is50OrHigher); if (result != null && getterExpression != null && getterExpression.getParent() != null) { getterExpression.getParent().setStructuralProperty(getterExpression.getLocationInParent(), createMethodInvocation(context, getter, null)); } return result; }
private static Expression getAssignedValue(ProposalParameter context) { ASTNode parent = context.accessNode.getParent(); ASTRewrite astRewrite = context.astRewrite; IJavaProject javaProject = context.compilationUnit.getJavaProject(); IMethodBinding getter = findGetter(context); Expression getterExpression = null; if (getter != null) { getterExpression = astRewrite.getAST().newSimpleName("placeholder"); //$NON-NLS-1$ } ITypeBinding type = context.variableBinding.getType(); boolean is50OrHigher = JavaModelUtil.is50OrHigher(javaProject); Expression result = GetterSetterUtil.getAssignedValue(parent, astRewrite, getterExpression, type, is50OrHigher); if (result != null && getterExpression != null && getterExpression.getParent() != null) { getterExpression.getParent().setStructuralProperty(getterExpression.getLocationInParent(), createMethodInvocation(context, getter, null)); } return result; }
private static Expression getAssignedValue(ProposalParameter context) { ASTNode parent= context.accessNode.getParent(); ASTRewrite astRewrite= context.astRewrite; IJavaProject javaProject= context.compilationUnit.getJavaProject(); IMethodBinding getter= findGetter(context); Expression getterExpression= null; if (getter != null) { getterExpression= astRewrite.getAST().newSimpleName("placeholder"); //$NON-NLS-1$ } ITypeBinding type= context.variableBinding.getType(); boolean is50OrHigher= JavaModelUtil.is50OrHigher(javaProject); Expression result= GetterSetterUtil.getAssignedValue(parent, astRewrite, getterExpression, type, is50OrHigher); if (result != null && getterExpression != null && getterExpression.getParent() != null) { getterExpression.getParent().setStructuralProperty(getterExpression.getLocationInParent(), createMethodInvocation(context, getter, null)); } return result; }
private static String getBaseNameFromLocationInParent(Expression assignedExpression) { StructuralPropertyDescriptor location= assignedExpression.getLocationInParent(); if (location == MethodInvocation.ARGUMENTS_PROPERTY) { MethodInvocation parent= (MethodInvocation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveMethodBinding()); } else if (location == ClassInstanceCreation.ARGUMENTS_PROPERTY) { ClassInstanceCreation parent= (ClassInstanceCreation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveConstructorBinding()); } else if (location == SuperMethodInvocation.ARGUMENTS_PROPERTY) { SuperMethodInvocation parent= (SuperMethodInvocation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveMethodBinding()); } else if (location == ConstructorInvocation.ARGUMENTS_PROPERTY) { ConstructorInvocation parent= (ConstructorInvocation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveConstructorBinding()); } else if (location == SuperConstructorInvocation.ARGUMENTS_PROPERTY) { SuperConstructorInvocation parent= (SuperConstructorInvocation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveConstructorBinding()); } return null; }
private static String getBaseNameFromLocationInParent(Expression assignedExpression) { StructuralPropertyDescriptor location= assignedExpression.getLocationInParent(); if (location == MethodInvocation.ARGUMENTS_PROPERTY) { MethodInvocation parent= (MethodInvocation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveMethodBinding()); } else if (location == ClassInstanceCreation.ARGUMENTS_PROPERTY) { ClassInstanceCreation parent= (ClassInstanceCreation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveConstructorBinding()); } else if (location == SuperMethodInvocation.ARGUMENTS_PROPERTY) { SuperMethodInvocation parent= (SuperMethodInvocation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveMethodBinding()); } else if (location == ConstructorInvocation.ARGUMENTS_PROPERTY) { ConstructorInvocation parent= (ConstructorInvocation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveConstructorBinding()); } else if (location == SuperConstructorInvocation.ARGUMENTS_PROPERTY) { SuperConstructorInvocation parent= (SuperConstructorInvocation)assignedExpression.getParent(); return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveConstructorBinding()); } return null; }