KickJava   Java API By Example, From Geeks To Geeks.

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


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

42     public boolean requireAST(ICompilationUnit unit) throws CoreException {
43         boolean usePrentheses= isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
44         if (!usePrentheses)
45             return false;
46         
47         return isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS) ||
48                isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
49     }
50     
51     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
52         if (compilationUnit == null)
53             return null;
54         
55         boolean usePrentheses= isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
56         if (!usePrentheses)
57             return null;
58         
59         return ExpressionsFix.createCleanUp(compilationUnit,
60                 isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS),
61                 isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER));
62     }
63     
64     /**
65      * {@inheritDoc}
66      */

67     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
68         return createFix(compilationUnit);
69     }
70
71     public Map JavaDoc getRequiredOptions() {
72         return null;
73     }
74     
75     /**
76      * {@inheritDoc}
77      */

78     public String JavaDoc[] getDescriptions() {
79         List JavaDoc result= new ArrayList JavaDoc();
80         if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS))
81             result.add(MultiFixMessages.ExpressionsCleanUp_addParanoiac_description);
82         
83         if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER))
84             result.add(MultiFixMessages.ExpressionsCleanUp_removeUnnecessary_description);
85         
86         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
87     }
88     
89     public String JavaDoc getPreview() {
90         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
91         
92         if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS)) {
93             buf.append("boolean b= (((i > 0) && (i < 10)) || (i == 50));\n"); //$NON-NLS-1$
94
} else if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER)) {
95             buf.append("boolean b= i > 0 && i < 10 || i == 50;\n"); //$NON-NLS-1$
96
} else {
97             buf.append("boolean b= (i > 0 && i < 10 || i == 50);\n"); //$NON-NLS-1$
98
}
99         
100         return buf.toString();
101     }
102
103     /**
104      * {@inheritDoc}
105      */

106     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
107         if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS)) {
108             IFix fix= ExpressionsFix.createAddParanoidalParenthesisFix(compilationUnit, new ASTNode[] {problem.getCoveredNode(compilationUnit)});
109             if (fix != null)
110                 return true;
111         }
112         if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER)) {
113             IFix fix= ExpressionsFix.createRemoveUnnecessaryParenthesisFix(compilationUnit, new ASTNode[] {problem.getCoveredNode(compilationUnit)});
114             if (fix != null)
115                 return true;
116         }
117         return false;
118     }
119
120     /**
121      * {@inheritDoc}
122      */

123     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
124         return -1;
125     }
126 }
127
Popular Tags