KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.swt.graphics.Image;
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.AnnotationTypeDeclaration;
24 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
25 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
26 import org.eclipse.jdt.core.dom.CompilationUnit;
27 import org.eclipse.jdt.core.dom.Expression;
28 import org.eclipse.jdt.core.dom.ITypeBinding;
29 import org.eclipse.jdt.core.dom.MemberValuePair;
30 import org.eclipse.jdt.core.dom.SimpleName;
31 import org.eclipse.jdt.core.dom.Type;
32 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
33 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
34
35 import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
36 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
37
38 /**
39  *
40  */

41 public class NewAnnotationMemberProposal extends LinkedCorrectionProposal {
42     
43     private static final String JavaDoc KEY_NAME= "name"; //$NON-NLS-1$
44
private static final String JavaDoc KEY_TYPE= "type"; //$NON-NLS-1$
45

46     private final ASTNode fInvocationNode;
47     private final ITypeBinding fSenderBinding;
48
49     public NewAnnotationMemberProposal(String JavaDoc label, ICompilationUnit targetCU, ASTNode invocationNode, ITypeBinding binding, int relevance, Image image) {
50         super(label, targetCU, null, relevance, image);
51         fInvocationNode= invocationNode;
52         fSenderBinding= binding;
53     }
54     
55     protected ASTRewrite getRewrite() throws CoreException {
56         CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fInvocationNode);
57         ASTNode typeDecl= astRoot.findDeclaringNode(fSenderBinding);
58         ASTNode newTypeDecl= null;
59         if (typeDecl != null) {
60             newTypeDecl= typeDecl;
61         } else {
62             astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
63             newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey());
64         }
65         createImportRewrite(astRoot);
66         
67         if (newTypeDecl instanceof AnnotationTypeDeclaration) {
68             ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
69
70             AnnotationTypeMemberDeclaration newStub= getStub(rewrite, (AnnotationTypeDeclaration) newTypeDecl);
71
72             ChildListPropertyDescriptor property= ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
73             List JavaDoc members= (List JavaDoc) newTypeDecl.getStructuralProperty(property);
74             int insertIndex= members.size();
75
76             ListRewrite listRewriter= rewrite.getListRewrite(newTypeDecl, property);
77             listRewriter.insertAt(newStub, insertIndex, null);
78
79             return rewrite;
80         }
81         return null;
82     }
83     
84     private AnnotationTypeMemberDeclaration getStub(ASTRewrite rewrite, AnnotationTypeDeclaration targetTypeDecl) throws CoreException {
85         AST ast= targetTypeDecl.getAST();
86         
87         AnnotationTypeMemberDeclaration decl= ast.newAnnotationTypeMemberDeclaration();
88
89         SimpleName newNameNode= getNewName(rewrite);
90
91         decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, evaluateModifiers(targetTypeDecl)));
92         
93         ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(getLinkedProposalModel(), rewrite, decl.modifiers(), true);
94
95         decl.setName(newNameNode);
96
97         Type returnType= getNewType(rewrite);
98         decl.setType(returnType);
99         return decl;
100     }
101
102     
103     private Type getNewType(ASTRewrite rewrite) {
104         AST ast= rewrite.getAST();
105         Type newTypeNode= null;
106         ITypeBinding binding= null;
107         if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
108             Expression value= ((MemberValuePair) fInvocationNode.getParent()).getValue();
109             binding= value.resolveTypeBinding();
110         } else if (fInvocationNode instanceof Expression) {
111             binding= ((Expression) fInvocationNode).resolveTypeBinding();
112         }
113         if (binding != null) {
114             newTypeNode= getImportRewrite().addImport(binding, ast);
115         }
116         if (newTypeNode == null) {
117             newTypeNode= ast.newSimpleType(ast.newSimpleName("String")); //$NON-NLS-1$
118
}
119         addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
120         return newTypeNode;
121     }
122
123     private int evaluateModifiers(AnnotationTypeDeclaration targetTypeDecl) {
124         List JavaDoc methodDecls= targetTypeDecl.bodyDeclarations();
125         for (int i= 0; i < methodDecls.size(); i++) {
126             Object JavaDoc curr= methodDecls.get(i);
127             if (curr instanceof AnnotationTypeMemberDeclaration) {
128                 return ((AnnotationTypeMemberDeclaration) curr).getModifiers();
129             }
130         }
131         return 0;
132     }
133
134     private SimpleName getNewName(ASTRewrite rewrite) {
135         AST ast= rewrite.getAST();
136         String JavaDoc name;
137         if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
138             name= ((SimpleName) fInvocationNode).getIdentifier();
139             if (ast == fInvocationNode.getAST()) {
140                 addLinkedPosition(rewrite.track(fInvocationNode), true, KEY_NAME);
141             }
142         } else {
143             name= "value"; //$NON-NLS-1$
144
}
145         
146
147         SimpleName newNameNode= ast.newSimpleName(name);
148         addLinkedPosition(rewrite.track(newNameNode), false, KEY_NAME);
149         return newNameNode;
150     }
151     
152 }
153
Popular Tags