KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > fix > VariableDeclarationCleanUp


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 package org.eclipse.jdt.internal.ui.fix;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.dom.CompilationUnit;
21
22 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
23 import org.eclipse.jdt.internal.corext.fix.IFix;
24 import org.eclipse.jdt.internal.corext.fix.VariableDeclarationFix;
25
26 import org.eclipse.jdt.ui.text.java.IProblemLocation;
27
28 public class VariableDeclarationCleanUp extends AbstractCleanUp {
29
30     public VariableDeclarationCleanUp(Map JavaDoc options) {
31         super(options);
32     }
33     
34     public VariableDeclarationCleanUp() {
35         super();
36     }
37     
38     /**
39      * {@inheritDoc}
40      */

41     public boolean requireAST(ICompilationUnit unit) throws CoreException {
42         boolean addFinal= isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL);
43         if (!addFinal)
44             return false;
45         
46         return isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS) ||
47                 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS) ||
48                 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES);
49     }
50
51     /**
52      * {@inheritDoc}
53      */

54     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
55         if (compilationUnit == null)
56             return null;
57         
58         boolean addFinal= isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL);
59         if (!addFinal)
60             return null;
61         
62         return VariableDeclarationFix.createCleanUp(compilationUnit,
63                 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS),
64                 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS),
65                 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES));
66     }
67
68     /**
69      * {@inheritDoc}
70      */

71     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
72         //No warnings generated by the compiler
73
return null;
74     }
75
76     /**
77      * {@inheritDoc}
78      */

79     public Map JavaDoc getRequiredOptions() {
80         return null;
81     }
82
83     /**
84      * {@inheritDoc}
85      */

86     public String JavaDoc[] getDescriptions() {
87         List JavaDoc result= new ArrayList JavaDoc();
88         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS))
89             result.add(MultiFixMessages.VariableDeclarationCleanUp_AddFinalField_description);
90         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS))
91             result.add(MultiFixMessages.VariableDeclarationCleanUp_AddFinalParameters_description);
92         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES))
93             result.add(MultiFixMessages.VariableDeclarationCleanUp_AddFinalLocals_description);
94         
95         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
96     }
97     
98     public String JavaDoc getPreview() {
99         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
100         
101         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS)) {
102             buf.append("private final int i= 0;\n"); //$NON-NLS-1$
103
} else {
104             buf.append("private int i= 0;\n"); //$NON-NLS-1$
105
}
106         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS)) {
107             buf.append("public void foo(final int j) {\n"); //$NON-NLS-1$
108
} else {
109             buf.append("public void foo(int j) {\n"); //$NON-NLS-1$
110
}
111         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES)) {
112             buf.append(" final int k;\n"); //$NON-NLS-1$
113
buf.append(" int h;\n"); //$NON-NLS-1$
114
buf.append(" h= 0;\n"); //$NON-NLS-1$
115
} else {
116             buf.append(" int k, h;\n"); //$NON-NLS-1$
117
buf.append(" h= 0;\n"); //$NON-NLS-1$
118
}
119         buf.append("}\n"); //$NON-NLS-1$
120

121         return buf.toString();
122     }
123
124     /**
125      * {@inheritDoc}
126      */

127     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
128         return false;
129     }
130
131     /**
132      * {@inheritDoc}
133      */

134     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
135         return -1;
136     }
137 }
138
Popular Tags