private void replaceExpressionsWithConstant() throws JavaModelException { ASTRewrite astRewrite = fCuRewrite.getASTRewrite(); AST ast = astRewrite.getAST(); IASTFragment[] fragmentsToReplace = getFragmentsToReplace(); for (int i = 0; i < fragmentsToReplace.length; i++) { IASTFragment fragment = fragmentsToReplace[i]; ASTNode node = fragment.getAssociatedNode(); boolean inTypeDeclarationAnnotation = isInTypeDeclarationAnnotation(node); if (inTypeDeclarationAnnotation && JdtFlags.VISIBILITY_STRING_PRIVATE == getVisibility()) continue; SimpleName ref = ast.newSimpleName(fConstantName); Name replacement = ref; boolean qualifyReference = qualifyReferencesWithDeclaringClassName(); if (!qualifyReference) { qualifyReference = inTypeDeclarationAnnotation; } if (qualifyReference) { replacement = ast.newQualifiedName(ast.newSimpleName(getContainingTypeBinding().getName()), ref); } TextEditGroup description = fCuRewrite.createGroupDescription( RefactoringCoreMessages.ExtractConstantRefactoring_replace); fragment.replace(astRewrite, replacement, description); if (fLinkedProposalModel != null) fLinkedProposalModel .getPositionGroup(KEY_NAME, true) .addPosition(astRewrite.track(ref), false); } }
private IExpressionFragment getSelectedExpression() throws JavaModelException { if (fSelectedExpression != null) return fSelectedExpression; IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange( new SourceRange(fSelectionStart, fSelectionLength), fCuRewrite.getRoot(), fCu); if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) { fSelectedExpression = (IExpressionFragment) selectedFragment; } if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) { fSelectedExpression = null; } return fSelectedExpression; }
// !! - like one in ExtractTempRefactoring private static boolean canReplace(IASTFragment fragment) { ASTNode node = fragment.getAssociatedNode(); ASTNode parent = node.getParent(); if (parent instanceof VariableDeclarationFragment) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent; if (node.equals(vdf.getName())) return false; } if (parent instanceof ExpressionStatement) return false; if (parent instanceof SwitchCase) { if (node instanceof Name) { Name name = (Name) node; ITypeBinding typeBinding = name.resolveTypeBinding(); if (typeBinding != null) { return !typeBinding.isEnum(); } } } return true; }