/* bd is a static field declaration or static initializer */ private static boolean depends(IExpressionFragment selected, BodyDeclaration bd) { /* We currently consider selected to depend on bd only if db includes a declaration * of a static field on which selected depends. * * A more accurate strategy might be to also check if bd contains (or is) a * static initializer containing code which changes the value of a static field on * which selected depends. However, if a static is written to multiple times within * during class initialization, it is difficult to predict which value should be used. * This would depend on which value is used by expressions instances for which the new * constant will be substituted, and there may be many of these; in each, the * static field in question may have taken on a different value (if some of these uses * occur within static initializers). */ if (bd instanceof FieldDeclaration) { FieldDeclaration fieldDecl = (FieldDeclaration) bd; for (Iterator fragments = fieldDecl.fragments().iterator(); fragments.hasNext(); ) { VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.next(); SimpleName staticFieldName = fragment.getName(); if (selected.getSubFragmentsMatching( ASTFragmentFactory.createFragmentForFullSubtree(staticFieldName)) .length != 0) return true; } } return false; }
// !! - 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; }
/* * @see ASTVisitor#visit(VariableDeclarationFragment) */ public boolean visit(VariableDeclarationFragment node) { node.getName().accept(this); for (int i = 0; i < node.getExtraDimensions(); i++) { this.fBuffer.append("[]"); // $NON-NLS-1$ } if (node.getInitializer() != null) { this.fBuffer.append("="); // $NON-NLS-1$ node.getInitializer().accept(this); } return false; }
/* * @see ASTVisitor#visit(VariableDeclarationExpression) */ public boolean visit(VariableDeclarationExpression node) { if (node.getAST().apiLevel() == AST.JLS2) { printModifiers(node.getModifiers()); } if (node.getAST().apiLevel() >= AST.JLS3) { printModifiers(node.modifiers()); } // node.getType().accept(this); this.fBuffer.append(" "); // $NON-NLS-1$ for (Iterator it = node.fragments().iterator(); it.hasNext(); ) { VariableDeclarationFragment f = (VariableDeclarationFragment) it.next(); f.accept(this); if (it.hasNext()) { this.fBuffer.append(", "); // $NON-NLS-1$ } } return false; }
private void createConstantDeclaration() throws CoreException { Type type = getConstantType(); IExpressionFragment fragment = getSelectedExpression(); String initializerSource = fCu.getBuffer().getText(fragment.getStartPosition(), fragment.getLength()); AST ast = fCuRewrite.getAST(); VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment(); variableDeclarationFragment.setName(ast.newSimpleName(fConstantName)); variableDeclarationFragment.setInitializer( (Expression) fCuRewrite .getASTRewrite() .createStringPlaceholder(initializerSource, ASTNode.SIMPLE_NAME)); FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment); fieldDeclaration.setType(type); Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility); if (accessModifier != null) fieldDeclaration.modifiers().add(ast.newModifier(accessModifier)); fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD)); fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaScriptProject()) .createComments; if (createComments) { String comment = CodeGeneration.getFieldComment( fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu)); if (comment != null && comment.length() > 0) { JSdoc doc = (JSdoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JSDOC); fieldDeclaration.setJavadoc(doc); } } AbstractTypeDeclaration parent = getContainingTypeDeclarationNode(); ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty()); TextEditGroup msg = fCuRewrite.createGroupDescription( RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant); if (insertFirst()) { listRewrite.insertFirst(fieldDeclaration, msg); } else { listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg); } if (fLinkedProposalModel != null) { ASTRewrite rewrite = fCuRewrite.getASTRewrite(); LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true); nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true); String[] nameSuggestions = guessConstantNames(); if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) { nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1); } for (int i = 0; i < nameSuggestions.length; i++) { nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i); } LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true); typeGroup.addPosition(rewrite.track(type), true); ITypeBinding typeBinding = fragment.getAssociatedExpression().resolveTypeBinding(); 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); } } ModifierCorrectionSubProcessor.installLinkedVisibilityProposals( fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), false); } }