KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > fix > AbstractSerialVersionOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.corext.fix;
12
13 import java.util.List JavaDoc;
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 /**
42  * Partial implementation of a serial version correction proposal.
43  *
44  * @since 3.1
45  */

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

51     /** The default serial value */
52     public static final long SERIAL_VALUE= 1;
53
54     /** The default serial id expression */
55     protected static final String JavaDoc DEFAULT_EXPRESSION= SERIAL_VALUE + LONG_SUFFIX;
56
57     /** The name of the serial version field */
58     protected static final String JavaDoc NAME_FIELD= "serialVersionUID"; //$NON-NLS-1$
59

60     /** The originally selected node */
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     /**
70      * Adds an initializer to the specified variable declaration fragment.
71      *
72      * @param fragment the variable declaration fragment to add an initializer
73      * @return false if no id could be calculated
74      * @throws CoreException
75      */

76     protected abstract boolean addInitializer(final VariableDeclarationFragment fragment, final ASTNode declarationNode) throws CoreException;
77     
78     /**
79      * Adds the necessary linked positions for the specified fragment.
80      *
81      * @param rewrite the ast rewrite to operate on
82      * @param fragment the fragment to add linked positions to
83      * @param positionGroups the list of {@link LinkedProposalPositionGroup}s
84      */

85     protected abstract void addLinkedPositions(final ASTRewrite rewrite, final VariableDeclarationFragment fragment, final LinkedProposalModel positionGroups);
86     
87     /**
88      * {@inheritDoc}
89      */

90     public void rewriteAST(CompilationUnitRewrite cuRewrite, List JavaDoc 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 JavaDoc 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