KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.swt.graphics.Image;
21
22 import org.eclipse.jdt.core.CorrectionEngine;
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.dom.AST;
25 import org.eclipse.jdt.core.dom.ASTNode;
26 import org.eclipse.jdt.core.dom.Annotation;
27 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
28 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
29 import org.eclipse.jdt.core.dom.ArrayInitializer;
30 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
31 import org.eclipse.jdt.core.dom.CompilationUnit;
32 import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
33 import org.eclipse.jdt.core.dom.EnumDeclaration;
34 import org.eclipse.jdt.core.dom.Expression;
35 import org.eclipse.jdt.core.dom.FieldDeclaration;
36 import org.eclipse.jdt.core.dom.ITypeBinding;
37 import org.eclipse.jdt.core.dom.Initializer;
38 import org.eclipse.jdt.core.dom.MemberValuePair;
39 import org.eclipse.jdt.core.dom.MethodDeclaration;
40 import org.eclipse.jdt.core.dom.NormalAnnotation;
41 import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
42 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
43 import org.eclipse.jdt.core.dom.StringLiteral;
44 import org.eclipse.jdt.core.dom.TypeDeclaration;
45 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
46 import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
47 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
48 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
49
50 import org.eclipse.jdt.internal.corext.util.Messages;
51
52 import org.eclipse.jdt.ui.text.java.IInvocationContext;
53 import org.eclipse.jdt.ui.text.java.IProblemLocation;
54
55 import org.eclipse.jdt.internal.ui.JavaPlugin;
56 import org.eclipse.jdt.internal.ui.JavaPluginImages;
57
58 /**
59  *
60  */

61 public class SuppressWarningsSubProcessor {
62     
63     private static final String JavaDoc ADD_SUPPRESSWARNINGS_ID= "org.eclipse.jdt.ui.correction.addSuppressWarnings"; //$NON-NLS-1$
64

65     public static final boolean hasSuppressWarningsProposal(int problemId) {
66         return CorrectionEngine.getWarningToken(problemId) != null; // Suppress warning annotations
67
}
68     
69     
70     public static void addSuppressWarningsProposals(IInvocationContext context, IProblemLocation problem, Collection JavaDoc proposals) {
71         if (problem.isError()) {
72             return;
73         }
74         String JavaDoc warningToken= CorrectionEngine.getWarningToken(problem.getProblemId());
75         if (warningToken == null) {
76             return;
77         }
78         for (Iterator JavaDoc iter= proposals.iterator(); iter.hasNext();) {
79             Object JavaDoc element= iter.next();
80             if (element instanceof SuppressWarningsProposal && warningToken.equals(((SuppressWarningsProposal) element).getWarningToken())) {
81                 return; // only one at a time
82
}
83         }
84         
85         ASTNode node= problem.getCoveringNode(context.getASTRoot());
86         if (node == null) {
87             return;
88         }
89         if (node.getLocationInParent() == VariableDeclarationFragment.NAME_PROPERTY) {
90             ASTNode parent= node.getParent();
91             if (parent.getLocationInParent() == VariableDeclarationStatement.FRAGMENTS_PROPERTY) {
92                 addSuppressWarningsProposal(context.getCompilationUnit(), parent.getParent(), warningToken, -2, proposals);
93                 return;
94             }
95         } else if (node.getLocationInParent() == SingleVariableDeclaration.NAME_PROPERTY) {
96             addSuppressWarningsProposal(context.getCompilationUnit(), node.getParent(), warningToken, -2, proposals);
97             return;
98         } else if (node.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
99             node= ASTResolving.findParentBodyDeclaration(node);
100             if (node instanceof FieldDeclaration) {
101                 node= node.getParent();
102             }
103         }
104         
105         ASTNode target= ASTResolving.findParentBodyDeclaration(node);
106         if (target instanceof Initializer) {
107             target= ASTResolving.findParentBodyDeclaration(target.getParent());
108         }
109         if (target != null) {
110             addSuppressWarningsProposal(context.getCompilationUnit(), target, warningToken, -3, proposals);
111         }
112     }
113     
114     private static String JavaDoc getFirstFragmentName(List JavaDoc fragments) {
115         if (fragments.size() > 0) {
116             return ((VariableDeclarationFragment) fragments.get(0)).getName().getIdentifier();
117         }
118         return new String JavaDoc();
119     }
120     
121     
122     private static class SuppressWarningsProposal extends ASTRewriteCorrectionProposal {
123         
124         private final String JavaDoc fWarningToken;
125         private final ASTNode fNode;
126         private final ChildListPropertyDescriptor fProperty;
127
128         public SuppressWarningsProposal(String JavaDoc warningToken, String JavaDoc label, ICompilationUnit cu, ASTNode node, ChildListPropertyDescriptor property, int relevance) {
129             super(label, cu, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_OBJS_ANNOTATION));
130             fWarningToken= warningToken;
131             fNode= node;
132             fProperty= property;
133             setCommandId(ADD_SUPPRESSWARNINGS_ID);
134         }
135         
136         /**
137          * @return Returns the warningToken.
138          */

139         public String JavaDoc getWarningToken() {
140             return fWarningToken;
141         }
142         
143         /* (non-Javadoc)
144          * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
145          */

146         protected ASTRewrite getRewrite() throws CoreException {
147             AST ast= fNode.getAST();
148             ASTRewrite rewrite= ASTRewrite.create(ast);
149             
150             StringLiteral newStringLiteral= ast.newStringLiteral();
151             newStringLiteral.setLiteralValue(fWarningToken);
152             
153             Annotation existing= findExistingAnnotation((List JavaDoc) fNode.getStructuralProperty(fProperty));
154             if (existing == null) {
155                 ListRewrite listRewrite= rewrite.getListRewrite(fNode, fProperty);
156                 
157                 SingleMemberAnnotation newAnnot= ast.newSingleMemberAnnotation();
158                 String JavaDoc importString= createImportRewrite((CompilationUnit) fNode.getRoot()).addImport("java.lang.SuppressWarnings"); //$NON-NLS-1$
159
newAnnot.setTypeName(ast.newName(importString));
160
161                 newAnnot.setValue(newStringLiteral);
162                 
163                 listRewrite.insertFirst(newAnnot, null);
164             } else if (existing instanceof SingleMemberAnnotation) {
165                 SingleMemberAnnotation annotation= (SingleMemberAnnotation) existing;
166                 Expression value= annotation.getValue();
167                 if (!addSuppressArgument(rewrite, value, newStringLiteral)) {
168                     rewrite.set(existing, SingleMemberAnnotation.VALUE_PROPERTY, newStringLiteral, null);
169                 }
170             } else if (existing instanceof NormalAnnotation) {
171                 NormalAnnotation annotation= (NormalAnnotation) existing;
172                 Expression value= findValue(annotation.values());
173                 if (!addSuppressArgument(rewrite, value, newStringLiteral)) {
174                     ListRewrite listRewrite= rewrite.getListRewrite(annotation, NormalAnnotation.VALUES_PROPERTY);
175                     MemberValuePair pair= ast.newMemberValuePair();
176                     pair.setName(ast.newSimpleName("value")); //$NON-NLS-1$
177
pair.setValue(newStringLiteral);
178                     listRewrite.insertFirst(pair, null);
179                 }
180             }
181             return rewrite;
182         }
183         
184         private static boolean addSuppressArgument(ASTRewrite rewrite, Expression value, StringLiteral newStringLiteral) {
185             if (value instanceof ArrayInitializer) {
186                 ListRewrite listRewrite= rewrite.getListRewrite(value, ArrayInitializer.EXPRESSIONS_PROPERTY);
187                 listRewrite.insertLast(newStringLiteral, null);
188             } else if (value instanceof StringLiteral) {
189                 ArrayInitializer newArr= rewrite.getAST().newArrayInitializer();
190                 newArr.expressions().add(rewrite.createMoveTarget(value));
191                 newArr.expressions().add(newStringLiteral);
192                 rewrite.replace(value, newArr, null);
193             } else {
194                 return false;
195             }
196             return true;
197         }
198         
199         private static Expression findValue(List JavaDoc keyValues) {
200             for (int i= 0, len= keyValues.size(); i < len; i++) {
201                 MemberValuePair curr= (MemberValuePair) keyValues.get(i);
202                 if ("value".equals(curr.getName().getIdentifier())) { //$NON-NLS-1$
203
return curr.getValue();
204                 }
205             }
206             return null;
207         }
208         
209         private static Annotation findExistingAnnotation(List JavaDoc modifiers) {
210             for (int i= 0, len= modifiers.size(); i < len; i++) {
211                 Object JavaDoc curr= modifiers.get(i);
212                 if (curr instanceof NormalAnnotation || curr instanceof SingleMemberAnnotation) {
213                     Annotation annotation= (Annotation) curr;
214                     ITypeBinding typeBinding= annotation.resolveTypeBinding();
215                     if (typeBinding != null) {
216                         if ("java.lang.SuppressWarnings".equals(typeBinding.getQualifiedName())) { //$NON-NLS-1$
217
return annotation;
218                         }
219                     } else {
220                         String JavaDoc fullyQualifiedName= annotation.getTypeName().getFullyQualifiedName();
221                         if ("SuppressWarnings".equals(fullyQualifiedName) || "java.lang.SuppressWarnings".equals(fullyQualifiedName)) { //$NON-NLS-1$ //$NON-NLS-2$
222
return annotation;
223                         }
224                     }
225                 }
226             }
227             return null;
228         }
229     }
230     
231     private static void addSuppressWarningsProposal(ICompilationUnit cu, ASTNode node, String JavaDoc warningToken, int relevance, Collection JavaDoc proposals) {
232
233         ChildListPropertyDescriptor property= null;
234         String JavaDoc name;
235         switch (node.getNodeType()) {
236             case ASTNode.SINGLE_VARIABLE_DECLARATION:
237                 property= SingleVariableDeclaration.MODIFIERS2_PROPERTY;
238                 name= ((SingleVariableDeclaration) node).getName().getIdentifier();
239                 break;
240             case ASTNode.VARIABLE_DECLARATION_STATEMENT:
241                 property= VariableDeclarationStatement.MODIFIERS2_PROPERTY;
242                 name= getFirstFragmentName(((VariableDeclarationStatement) node).fragments());
243                 break;
244             case ASTNode.TYPE_DECLARATION:
245                 property= TypeDeclaration.MODIFIERS2_PROPERTY;
246                 name= ((TypeDeclaration) node).getName().getIdentifier();
247                 break;
248             case ASTNode.ANNOTATION_TYPE_DECLARATION:
249                 property= AnnotationTypeDeclaration.MODIFIERS2_PROPERTY;
250                 name= ((AnnotationTypeDeclaration) node).getName().getIdentifier();
251                 break;
252             case ASTNode.ENUM_DECLARATION:
253                 property= EnumDeclaration.MODIFIERS2_PROPERTY;
254                 name= ((EnumDeclaration) node).getName().getIdentifier();
255                 break;
256             case ASTNode.FIELD_DECLARATION:
257                 property= FieldDeclaration.MODIFIERS2_PROPERTY;
258                 name= getFirstFragmentName(((FieldDeclaration) node).fragments());
259                 break;
260             case ASTNode.INITIALIZER:
261                 property= Initializer.MODIFIERS2_PROPERTY;
262                 name= CorrectionMessages.SuppressWarningsSubProcessor_suppress_warnings_initializer_label;
263                 break;
264             case ASTNode.METHOD_DECLARATION:
265                 property= MethodDeclaration.MODIFIERS2_PROPERTY;
266                 name= ((MethodDeclaration) node).getName().getIdentifier() + "()"; //$NON-NLS-1$
267
break;
268             case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
269                 property= AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY;
270                 name= ((AnnotationTypeMemberDeclaration) node).getName().getIdentifier() + "()"; //$NON-NLS-1$
271
break;
272             case ASTNode.ENUM_CONSTANT_DECLARATION:
273                 property= EnumConstantDeclaration.MODIFIERS2_PROPERTY;
274                 name= ((EnumConstantDeclaration) node).getName().getIdentifier();
275                 break;
276             default:
277                 JavaPlugin.logErrorMessage("SuppressWarning quick fix: wrong node kind: " + node.getNodeType()); //$NON-NLS-1$
278
return;
279         }
280         
281         String JavaDoc label= Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_suppress_warnings_label, new String JavaDoc[] { warningToken, name });
282         ASTRewriteCorrectionProposal proposal= new SuppressWarningsProposal(warningToken, label, cu, node, property, relevance);
283
284         proposals.add(proposal);
285     }
286
287     public static void addUnknownSuppressWarningProposals(IInvocationContext context, IProblemLocation problem, Collection JavaDoc proposals) {
288
289         ASTNode coveringNode= context.getCoveringNode();
290         if (!(coveringNode instanceof StringLiteral))
291             return;
292         
293         AST ast= coveringNode.getAST();
294         StringLiteral literal= (StringLiteral) coveringNode;
295         
296         String JavaDoc literalValue= literal.getLiteralValue();
297         String JavaDoc[] allWarningTokens= CorrectionEngine.getAllWarningTokens();
298         for (int i= 0; i < allWarningTokens.length; i++) {
299             String JavaDoc curr= allWarningTokens[i];
300             if (NameMatcher.isSimilarName(literalValue, curr)) {
301                 StringLiteral newLiteral= ast.newStringLiteral();
302                 newLiteral.setLiteralValue(curr);
303                 ASTRewrite rewrite= ASTRewrite.create(ast);
304                 rewrite.replace(literal, newLiteral, null);
305                 String JavaDoc label= Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_fix_suppress_token_label, new String JavaDoc[] { curr });
306                 Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
307                 ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, 5, image);
308                 proposals.add(proposal);
309             }
310         }
311     }
312
313 }
314
Popular Tags