KickJava   Java API By Example, From Geeks To Geeks.

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


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.ControlStatementsFix;
24 import org.eclipse.jdt.internal.corext.fix.IFix;
25
26 import org.eclipse.jdt.ui.text.java.IProblemLocation;
27
28 public class ControlStatementsCleanUp extends AbstractCleanUp {
29     
30     public ControlStatementsCleanUp(Map JavaDoc options) {
31         super(options);
32     }
33     
34     public ControlStatementsCleanUp() {
35         super();
36     }
37     
38     /**
39      * {@inheritDoc}
40      */

41     public boolean requireAST(ICompilationUnit unit) throws CoreException {
42         boolean useBlocks= isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS);
43         
44         if (!useBlocks)
45             return false;
46         
47         return isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS) ||
48                isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER) ||
49                isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW);
50     }
51
52     /**
53      * {@inheritDoc}
54      */

55     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
56         if (compilationUnit == null)
57             return null;
58         
59         boolean useBlocks= isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS);
60         if (!useBlocks)
61             return null;
62         
63         return ControlStatementsFix.createCleanUp(compilationUnit,
64                 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS),
65                 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER),
66                 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW));
67     }
68
69     /**
70      * {@inheritDoc}
71      */

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

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

87     public String JavaDoc[] getDescriptions() {
88         List JavaDoc result= new ArrayList JavaDoc();
89         if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS))
90             result.add(MultiFixMessages.CodeStyleMultiFix_ConvertSingleStatementInControlBodeyToBlock_description);
91         if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER))
92             result.add(MultiFixMessages.ControlStatementsCleanUp_RemoveUnnecessaryBlocks_description);
93         if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW))
94             result.add(MultiFixMessages.ControlStatementsCleanUp_RemoveUnnecessaryBlocksWithReturnOrThrow_description);
95         
96         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
97     }
98     
99     public String JavaDoc getPreview() {
100         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
101         
102         if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS)) {
103             buf.append("if (obj == null) {\n"); //$NON-NLS-1$
104
buf.append(" throw new IllegalArgumentException();\n"); //$NON-NLS-1$
105
buf.append("}\n"); //$NON-NLS-1$
106

107             buf.append("if (ids.length > 0) {\n"); //$NON-NLS-1$
108
buf.append(" System.out.println(ids[0]);\n"); //$NON-NLS-1$
109
buf.append("} else {\n"); //$NON-NLS-1$
110
buf.append(" return;\n"); //$NON-NLS-1$
111
buf.append("}\n"); //$NON-NLS-1$
112
} else if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER)){
113             buf.append("if (obj == null)\n"); //$NON-NLS-1$
114
buf.append(" throw new IllegalArgumentException();\n"); //$NON-NLS-1$
115
buf.append("\n"); //$NON-NLS-1$
116

117             buf.append("if (ids.length > 0)\n"); //$NON-NLS-1$
118
buf.append(" System.out.println(ids[0]);\n"); //$NON-NLS-1$
119
buf.append("else\n"); //$NON-NLS-1$
120
buf.append(" return;\n"); //$NON-NLS-1$
121
buf.append("\n"); //$NON-NLS-1$
122
} else if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW)) {
123             buf.append("if (obj == null)\n"); //$NON-NLS-1$
124
buf.append(" throw new IllegalArgumentException();\n"); //$NON-NLS-1$
125
buf.append("\n"); //$NON-NLS-1$
126

127             buf.append("if (ids.length > 0) {\n"); //$NON-NLS-1$
128
buf.append(" System.out.println(ids[0]);\n"); //$NON-NLS-1$
129
buf.append("} else \n"); //$NON-NLS-1$
130
buf.append(" return;\n"); //$NON-NLS-1$
131
buf.append("\n"); //$NON-NLS-1$
132
} else {
133             buf.append("if (obj == null) {\n"); //$NON-NLS-1$
134
buf.append(" throw new IllegalArgumentException();\n"); //$NON-NLS-1$
135
buf.append("}\n"); //$NON-NLS-1$
136

137             buf.append("if (ids.length > 0) {\n"); //$NON-NLS-1$
138
buf.append(" System.out.println(ids[0]);\n"); //$NON-NLS-1$
139
buf.append("} else \n"); //$NON-NLS-1$
140
buf.append(" return;\n"); //$NON-NLS-1$
141
buf.append("\n"); //$NON-NLS-1$
142
}
143         
144         return buf.toString();
145     }
146
147     /**
148      * {@inheritDoc}
149      */

150     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
151         return false;
152     }
153
154     /**
155      * {@inheritDoc}
156      */

157     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
158         return -1;
159     }
160 }
161
Popular Tags