KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > AbstractSerialVersionProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
41  * Partial implementation of a serial version correction proposal.
42  *
43  * @since 3.1
44  */

45 public abstract class AbstractSerialVersionProposal extends LinkedCorrectionProposal {
46
47     /** The long literal suffix */
48     protected static final String JavaDoc LONG_SUFFIX= "L"; //$NON-NLS-1$
49

50     /** The default serial value */
51     protected static final long SERIAL_VALUE= 1;
52
53     /** The default serial id expression */
54     protected static final String JavaDoc DEFAULT_EXPRESSION= SERIAL_VALUE + LONG_SUFFIX; //$NON-NLS-1$
55

56     /** The name of the serial version field */
57     protected static final String JavaDoc NAME_FIELD= "serialVersionUID"; //$NON-NLS-1$
58

59     /** The proposal relevance */
60     private static final int PROPOSAL_RELEVANCE= 9;
61
62     /** The originally selected node */
63     private final ASTNode fNode;
64
65     /**
66      * Creates a new abstract serial version proposal.
67      *
68      * @param label the label of this proposal
69      * @param unit the compilation unit
70      * @param node the originally selected node
71      */

72     protected AbstractSerialVersionProposal(final String JavaDoc 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     /**
81      * Adds an initializer to the specified variable declaration fragment.
82      *
83      * @param fragment the variable declaration fragment to add an initializer
84      */

85     protected abstract void addInitializer(final VariableDeclarationFragment fragment);
86
87     /**
88      * Adds the necessary linked positions for the specified fragment.
89      *
90      * @param rewrite the ast rewrite to operate on
91      * @param fragment the fragment to add linked positions to
92      */

93     protected abstract void addLinkedPositions(final ASTRewrite rewrite, final VariableDeclarationFragment fragment);
94
95     /**
96      * Computes the default expression to initialize the serial version id with.
97      *
98      * @param monitor the progress monitor to use
99      *
100      * @return the default expression for the serial version id
101      */

102     protected abstract Expression computeDefaultExpression(final IProgressMonitor monitor);
103
104     /**
105      * Returns the AST to operate on.
106      *
107      * @return the AST to operate on
108      */

109     protected final AST getAST() {
110         return fNode.getAST();
111     }
112
113     /**
114      * Returns the declaration node for the originally selected node.
115      *
116      * @return the declaration node
117      */

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     /*
136      * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
137      */

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 JavaDoc 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