1 11 package org.eclipse.jdt.internal.corext.fix; 12 13 import java.util.List ; 14 15 import org.eclipse.text.edits.TextEditGroup; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.CoreException; 19 20 import org.eclipse.jdt.core.ICompilationUnit; 21 import org.eclipse.jdt.core.dom.AST; 22 import org.eclipse.jdt.core.dom.ASTNode; 23 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 24 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; 25 import org.eclipse.jdt.core.dom.ClassInstanceCreation; 26 import org.eclipse.jdt.core.dom.FieldDeclaration; 27 import org.eclipse.jdt.core.dom.Javadoc; 28 import org.eclipse.jdt.core.dom.Modifier; 29 import org.eclipse.jdt.core.dom.ParameterizedType; 30 import org.eclipse.jdt.core.dom.PrimitiveType; 31 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 32 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 33 34 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 35 import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory; 36 import org.eclipse.jdt.internal.corext.fix.LinkedFix.AbstractLinkedFixRewriteOperation; 37 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; 38 39 import org.eclipse.jdt.ui.CodeGeneration; 40 41 46 public abstract class AbstractSerialVersionOperation extends AbstractLinkedFixRewriteOperation { 47 48 49 protected static final String LONG_SUFFIX= "L"; 51 52 public static final long SERIAL_VALUE= 1; 53 54 55 protected static final String DEFAULT_EXPRESSION= SERIAL_VALUE + LONG_SUFFIX; 56 57 58 protected static final String NAME_FIELD= "serialVersionUID"; 60 61 private final ASTNode[] fNodes; 62 private final ICompilationUnit fUnit; 63 64 protected AbstractSerialVersionOperation(final ICompilationUnit unit, final ASTNode[] node) { 65 fUnit= unit; 66 fNodes= node; 67 } 68 69 76 protected abstract boolean addInitializer(final VariableDeclarationFragment fragment, final ASTNode declarationNode) throws CoreException; 77 78 85 protected abstract void addLinkedPositions(final ASTRewrite rewrite, final VariableDeclarationFragment fragment, final LinkedProposalModel positionGroups); 86 87 90 public void rewriteAST(CompilationUnitRewrite cuRewrite, List textEditGroups, LinkedProposalModel positionGroups) throws CoreException { 91 final ASTRewrite rewrite= cuRewrite.getASTRewrite(); 92 VariableDeclarationFragment fragment= null; 93 for (int i= 0; i < fNodes.length; i++) { 94 final ASTNode node= fNodes[i]; 95 96 final AST ast= node.getAST(); 97 98 fragment= ast.newVariableDeclarationFragment(); 99 fragment.setName(ast.newSimpleName(NAME_FIELD)); 100 101 final FieldDeclaration declaration= ast.newFieldDeclaration(fragment); 102 declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG)); 103 declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL)); 104 105 if (!addInitializer(fragment, node)) 106 continue; 107 108 if (fragment.getInitializer() != null) { 109 110 final TextEditGroup editGroup= createTextEditGroup(FixMessages.SerialVersion_group_description); 111 textEditGroups.add(editGroup); 112 113 if (node instanceof AbstractTypeDeclaration) 114 rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup); 115 else if (node instanceof AnonymousClassDeclaration) 116 rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup); 117 else if (node instanceof ParameterizedType) { 118 final ParameterizedType type= (ParameterizedType) node; 119 final ASTNode parent= type.getParent(); 120 if (parent instanceof ClassInstanceCreation) { 121 final ClassInstanceCreation creation= (ClassInstanceCreation) parent; 122 final AnonymousClassDeclaration anonymous= creation.getAnonymousClassDeclaration(); 123 if (anonymous != null) 124 rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup); 125 } 126 } else 127 Assert.isTrue(false); 128 129 addLinkedPositions(rewrite, fragment, positionGroups); 130 } 131 132 final String comment= CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(fUnit)); 133 if (comment != null && comment.length() > 0) { 134 final Javadoc doc= (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC); 135 declaration.setJavadoc(doc); 136 } 137 } 138 if (fragment == null) 139 return; 140 141 positionGroups.setEndPosition(rewrite.track(fragment)); 142 } 143 144 } 145 | Popular Tags |