KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > delegates > DelegateFieldCreator


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.refactoring.delegates;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jdt.core.JavaModelException;
16 import org.eclipse.jdt.core.dom.ASTNode;
17 import org.eclipse.jdt.core.dom.BodyDeclaration;
18 import org.eclipse.jdt.core.dom.ChildPropertyDescriptor;
19 import org.eclipse.jdt.core.dom.Expression;
20 import org.eclipse.jdt.core.dom.FieldAccess;
21 import org.eclipse.jdt.core.dom.FieldDeclaration;
22 import org.eclipse.jdt.core.dom.IBinding;
23 import org.eclipse.jdt.core.dom.MemberRef;
24 import org.eclipse.jdt.core.dom.SimpleName;
25 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
26
27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
28 import org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor;
29
30 /**
31  * Delegate creator for static fields. Note that this implementation assumes a
32  * field <strong>with only one fragment</strong>. See
33  * {@link MoveStaticMembersProcessor#getASTMembers(org.eclipse.ltk.core.refactoring.RefactoringStatus)}
34  * for more information.
35  *
36  * @since 3.2
37  */

38 public class DelegateFieldCreator extends DelegateCreator {
39
40     private VariableDeclarationFragment fOldFieldFragment;
41
42     protected void initialize() {
43         
44         Assert.isTrue(getDeclaration() instanceof FieldDeclaration);
45         Assert.isTrue(((FieldDeclaration) getDeclaration()).fragments().size() == 1);
46         
47         fOldFieldFragment= (VariableDeclarationFragment) ((FieldDeclaration) getDeclaration()).fragments().get(0);
48         if (getNewElementName() == null)
49             setNewElementName(fOldFieldFragment.getName().getIdentifier());
50         
51         setInsertBefore(false); // delegate must be inserted after the original field that is referenced in the initializer
52
}
53
54     protected ASTNode createBody(BodyDeclaration fd) throws JavaModelException {
55         FieldDeclaration result= (FieldDeclaration) fd;
56         Expression initializer= createDelegateFieldInitializer(result);
57         return initializer;
58     }
59
60     protected ASTNode createDocReference(BodyDeclaration declaration) {
61         MemberRef ref= getAst().newMemberRef();
62         ref.setName(getAst().newSimpleName(getNewElementName()));
63
64         if (isMoveToAnotherFile())
65             ref.setQualifier(createDestinationTypeName());
66         return ref;
67     }
68     
69     protected ASTNode getBodyHead(BodyDeclaration result) {
70         return fOldFieldFragment;
71     }
72     
73     protected ChildPropertyDescriptor getJavaDocProperty() {
74         return FieldDeclaration.JAVADOC_PROPERTY;
75     }
76
77     protected ChildPropertyDescriptor getBodyProperty() {
78         return VariableDeclarationFragment.INITIALIZER_PROPERTY;
79     }
80
81     protected IBinding getDeclarationBinding() {
82         return fOldFieldFragment.resolveBinding();
83     }
84
85     protected String JavaDoc getTextEditGroupLabel() {
86         return RefactoringCoreMessages.DelegateFieldCreator_text_edit_group_label;
87     }
88
89     // ******************* INTERNAL HELPERS ***************************
90

91     private Expression createDelegateFieldInitializer(final FieldDeclaration declaration) throws JavaModelException {
92         Assert.isNotNull(declaration);
93
94         Expression qualification= getAccess();
95         if (qualification != null) {
96             FieldAccess access= getAst().newFieldAccess();
97             access.setExpression(qualification);
98             access.setName(getAst().newSimpleName(getNewElementName()));
99             return access;
100         } else {
101             SimpleName access= getAst().newSimpleName(getNewElementName());
102             return access;
103         }
104     }
105 }
106
Popular Tags