1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 16 import org.eclipse.jdt.core.ICompilationUnit; 17 import org.eclipse.jdt.core.dom.AST; 18 import org.eclipse.jdt.core.dom.ASTNode; 19 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 20 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; 21 import org.eclipse.jdt.core.dom.ClassInstanceCreation; 22 import org.eclipse.jdt.core.dom.Expression; 23 import org.eclipse.jdt.core.dom.FieldDeclaration; 24 import org.eclipse.jdt.core.dom.Javadoc; 25 import org.eclipse.jdt.core.dom.Modifier; 26 import org.eclipse.jdt.core.dom.ParameterizedType; 27 import org.eclipse.jdt.core.dom.PrimitiveType; 28 import org.eclipse.jdt.core.dom.Type; 29 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 30 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 31 32 import org.eclipse.jdt.ui.CodeGeneration; 33 34 import org.eclipse.jdt.internal.corext.Assert; 35 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 36 import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory; 37 38 import org.eclipse.jdt.internal.ui.JavaPluginImages; 39 40 45 public abstract class AbstractSerialVersionProposal extends LinkedCorrectionProposal { 46 47 48 protected static final String LONG_SUFFIX= "L"; 50 51 protected static final long SERIAL_VALUE= 1; 52 53 54 protected static final String DEFAULT_EXPRESSION= SERIAL_VALUE + LONG_SUFFIX; 56 57 protected static final String NAME_FIELD= "serialVersionUID"; 59 60 private static final int PROPOSAL_RELEVANCE= 9; 61 62 63 private final ASTNode fNode; 64 65 72 protected AbstractSerialVersionProposal(final String label, final ICompilationUnit unit, final ASTNode node) { 73 super(label, unit, null, PROPOSAL_RELEVANCE, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD)); 74 75 Assert.isNotNull(node); 76 77 fNode= node; 78 } 79 80 85 protected abstract void addInitializer(final VariableDeclarationFragment fragment); 86 87 93 protected abstract void addLinkedPositions(final ASTRewrite rewrite, final VariableDeclarationFragment fragment); 94 95 102 protected abstract Expression computeDefaultExpression(final IProgressMonitor monitor); 103 104 109 protected final AST getAST() { 110 return fNode.getAST(); 111 } 112 113 118 protected final ASTNode getDeclarationNode() { 119 120 ASTNode parent= fNode.getParent(); 121 if (!(parent instanceof AbstractTypeDeclaration)) { 122 123 parent= parent.getParent(); 124 if (parent instanceof ParameterizedType || parent instanceof Type) 125 parent= parent.getParent(); 126 if (parent instanceof ClassInstanceCreation) { 127 128 final ClassInstanceCreation creation= (ClassInstanceCreation) parent; 129 parent= creation.getAnonymousClassDeclaration(); 130 } 131 } 132 return parent; 133 } 134 135 138 protected final ASTRewrite getRewrite() throws CoreException { 139 140 final ASTNode node= getDeclarationNode(); 141 142 final AST ast= node.getAST(); 143 final ASTRewrite rewrite= ASTRewrite.create(ast); 144 145 final VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment(); 146 147 fragment.setName(ast.newSimpleName(NAME_FIELD)); 148 149 final FieldDeclaration declaration= ast.newFieldDeclaration(fragment); 150 declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG)); 151 declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL)); 152 153 addInitializer(fragment); 154 155 if (fragment.getInitializer() != null) { 156 157 if (node instanceof AbstractTypeDeclaration) 158 rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, null); 159 else if (node instanceof AnonymousClassDeclaration) 160 rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, null); 161 else 162 Assert.isTrue(false); 163 164 addLinkedPositions(rewrite, fragment); 165 } 166 167 final String comment= CodeGeneration.getFieldComment(getCompilationUnit(), declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(getCompilationUnit())); 168 if (comment != null && comment.length() > 0) { 169 final Javadoc doc= (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC); 170 declaration.setJavadoc(doc); 171 } 172 return rewrite; 173 } 174 } 175 | Popular Tags |