KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.internal.ui.text.correction;
13
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.core.runtime.CoreException;
16
17 import org.eclipse.jdt.core.ICompilationUnit;
18 import org.eclipse.jdt.core.dom.AST;
19 import org.eclipse.jdt.core.dom.ASTNode;
20 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
21 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
22 import org.eclipse.jdt.core.dom.Block;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24 import org.eclipse.jdt.core.dom.FieldDeclaration;
25 import org.eclipse.jdt.core.dom.IBinding;
26 import org.eclipse.jdt.core.dom.ITypeBinding;
27 import org.eclipse.jdt.core.dom.IVariableBinding;
28 import org.eclipse.jdt.core.dom.MethodDeclaration;
29 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
30 import org.eclipse.jdt.core.dom.Type;
31 import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
32 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
33 import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
34 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
35 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
36 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
37
38 import org.eclipse.jdt.internal.corext.dom.Bindings;
39 import org.eclipse.jdt.internal.corext.util.Messages;
40
41 import org.eclipse.jdt.ui.JavaElementLabels;
42
43 import org.eclipse.jdt.internal.ui.JavaPluginImages;
44 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
45
46 public class TypeChangeCompletionProposal extends LinkedCorrectionProposal {
47
48     private IBinding fBinding;
49     private CompilationUnit fAstRoot;
50     private ITypeBinding fNewType;
51     private boolean fOfferSuperTypeProposals;
52
53     public TypeChangeCompletionProposal(ICompilationUnit targetCU, IBinding binding, CompilationUnit astRoot, ITypeBinding newType, boolean offerSuperTypeProposals, int relevance) {
54         super("", targetCU, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)); //$NON-NLS-1$
55

56         Assert.isTrue(binding != null && (binding.getKind() == IBinding.METHOD || binding.getKind() == IBinding.VARIABLE) && Bindings.isDeclarationBinding(binding));
57
58         fBinding= binding; // must be generic method or (generic) variable
59
fAstRoot= astRoot;
60         fNewType= newType;
61         fOfferSuperTypeProposals= offerSuperTypeProposals;
62
63         String JavaDoc typeName= BindingLabelProvider.getBindingLabel(newType, JavaElementLabels.ALL_DEFAULT);
64         if (binding.getKind() == IBinding.VARIABLE) {
65             IVariableBinding varBinding= (IVariableBinding) binding;
66
67             String JavaDoc[] args= { varBinding.getName(), typeName};
68             if (varBinding.isField()) {
69                 setDisplayName(Messages.format(CorrectionMessages.TypeChangeCompletionProposal_field_name, args));
70             } else if (astRoot.findDeclaringNode(binding) instanceof SingleVariableDeclaration) {
71                 setDisplayName(Messages.format(CorrectionMessages.TypeChangeCompletionProposal_param_name, args));
72             } else {
73                 setDisplayName(Messages.format(CorrectionMessages.TypeChangeCompletionProposal_variable_name, args));
74             }
75         } else {
76             String JavaDoc[] args= { binding.getName(), typeName };
77             setDisplayName(Messages.format(CorrectionMessages.TypeChangeCompletionProposal_method_name, args));
78         }
79     }
80
81     protected ASTRewrite getRewrite() throws CoreException {
82         ASTNode boundNode= fAstRoot.findDeclaringNode(fBinding);
83         ASTNode declNode= null;
84         CompilationUnit newRoot= fAstRoot;
85         if (boundNode != null) {
86             declNode= boundNode; // is same CU
87
} else {
88             newRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
89             declNode= newRoot.findDeclaringNode(fBinding.getKey());
90         }
91         if (declNode != null) {
92             AST ast= declNode.getAST();
93             ASTRewrite rewrite= ASTRewrite.create(ast);
94             ImportRewrite imports= createImportRewrite(newRoot);
95
96             Type type= imports.addImport(fNewType, ast);
97
98             if (declNode instanceof MethodDeclaration) {
99                 MethodDeclaration methodDecl= (MethodDeclaration) declNode;
100                 rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
101                 rewrite.set(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS_PROPERTY, new Integer JavaDoc(0), null);
102             } else if (declNode instanceof AnnotationTypeMemberDeclaration) {
103                 AnnotationTypeMemberDeclaration methodDecl= (AnnotationTypeMemberDeclaration) declNode;
104                 rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.TYPE_PROPERTY, type, null);
105             } else if (declNode instanceof VariableDeclarationFragment) {
106                 ASTNode parent= declNode.getParent();
107                 if (parent instanceof FieldDeclaration) {
108                     FieldDeclaration fieldDecl= (FieldDeclaration) parent;
109                     if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) { // split
110
VariableDeclarationFragment placeholder= (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
111                         FieldDeclaration newField= ast.newFieldDeclaration(placeholder);
112                         newField.setType(type);
113                         AbstractTypeDeclaration typeDecl= (AbstractTypeDeclaration) fieldDecl.getParent();
114
115                         ListRewrite listRewrite= rewrite.getListRewrite(typeDecl, typeDecl.getBodyDeclarationsProperty());
116                         if (fieldDecl.fragments().indexOf(declNode) == 0) { // if it as the first in the list-> insert before
117
listRewrite.insertBefore(newField, parent, null);
118                         } else {
119                             listRewrite.insertAfter(newField, parent, null);
120                         }
121                     } else {
122                         rewrite.set(fieldDecl, FieldDeclaration.TYPE_PROPERTY, type, null);
123                         rewrite.set(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS_PROPERTY, new Integer JavaDoc(0), null);
124                     }
125                 } else if (parent instanceof VariableDeclarationStatement) {
126                     VariableDeclarationStatement varDecl= (VariableDeclarationStatement) parent;
127                     if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) { // split
128
VariableDeclarationFragment placeholder= (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
129                         VariableDeclarationStatement newStat= ast.newVariableDeclarationStatement(placeholder);
130                         newStat.setType(type);
131
132                         ListRewrite listRewrite= rewrite.getListRewrite(varDecl.getParent(), Block.STATEMENTS_PROPERTY);
133                         if (varDecl.fragments().indexOf(declNode) == 0) { // if it as the first in the list-> insert before
134
listRewrite.insertBefore(newStat, parent, null);
135                         } else {
136                             listRewrite.insertAfter(newStat, parent, null);
137                         }
138                     } else {
139                         rewrite.set(varDecl, VariableDeclarationStatement.TYPE_PROPERTY, type, null);
140                         rewrite.set(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS_PROPERTY, new Integer JavaDoc(0), null);
141                     }
142                 } else if (parent instanceof VariableDeclarationExpression) {
143                     VariableDeclarationExpression varDecl= (VariableDeclarationExpression) parent;
144
145                     rewrite.set(varDecl, VariableDeclarationExpression.TYPE_PROPERTY, type, null);
146                     rewrite.set(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS_PROPERTY, new Integer JavaDoc(0), null);
147                 }
148             } else if (declNode instanceof SingleVariableDeclaration) {
149                 SingleVariableDeclaration variableDeclaration= (SingleVariableDeclaration) declNode;
150                 rewrite.set(variableDeclaration, SingleVariableDeclaration.TYPE_PROPERTY, type, null);
151                 rewrite.set(variableDeclaration, SingleVariableDeclaration.EXTRA_DIMENSIONS_PROPERTY, new Integer JavaDoc(0), null);
152             }
153
154             // set up linked mode
155
final String JavaDoc KEY_TYPE= "type"; //$NON-NLS-1$
156
addLinkedPosition(rewrite.track(type), true, KEY_TYPE);
157             if (fOfferSuperTypeProposals) {
158                 ITypeBinding[] typeProposals= ASTResolving.getRelaxingTypes(ast, fNewType);
159                 for (int i= 0; i < typeProposals.length; i++) {
160                     addLinkedPositionProposal(KEY_TYPE, typeProposals[i]);
161                 }
162             }
163             return rewrite;
164         }
165         return null;
166     }
167
168
169 }
170
Popular Tags