private void replaceExpressionsWithConstant() throws JavaScriptModelException { ASTRewrite astRewrite = fCuRewrite.getASTRewrite(); AST ast = astRewrite.getAST(); IASTFragment[] fragmentsToReplace = getFragmentsToReplace(); for (int i = 0; i < fragmentsToReplace.length; i++) { IASTFragment fragment = fragmentsToReplace[i]; SimpleName ref = ast.newSimpleName(fConstantName); Name replacement = ref; if (qualifyReferencesWithDeclaringClassName()) { 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 JavaScriptModelException { 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; } 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) return false; return true; }