]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/fix/ControlStatementsCleanUp.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / fix / ControlStatementsCleanUp.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.eclipse.core.runtime.CoreException;
18
19 import org.eclipse.jdt.core.dom.CompilationUnit;
20
21 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
22 import org.eclipse.jdt.internal.corext.fix.ControlStatementsFix;
23
24 import org.eclipse.jdt.ui.cleanup.CleanUpContext;
25 import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
26 import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
27
28 public class ControlStatementsCleanUp extends AbstractCleanUp {
29
30         public ControlStatementsCleanUp(Map<String, String> options) {
31                 super(options);
32     }
33
34         public ControlStatementsCleanUp() {
35                 super();
36         }
37
38         /**
39          * {@inheritDoc}
40          */
41         @Override
42         public CleanUpRequirements getRequirements() {
43                 return new CleanUpRequirements(requireAST(), false, false, null);
44         }
45
46         private boolean requireAST() {
47                 boolean useBlocks= isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS);
48
49                 if (!useBlocks)
50                         return false;
51
52                 return isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS) ||
53                        isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER) ||
54                        isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW);
55         }
56
57         /**
58          * {@inheritDoc}
59          */
60         @Override
61         public ICleanUpFix createFix(CleanUpContext context) throws CoreException {
62                 CompilationUnit compilationUnit= context.getAST();
63                 if (compilationUnit == null)
64                         return null;
65
66                 boolean useBlocks= isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS);
67                 if (!useBlocks)
68                         return null;
69
70                 return ControlStatementsFix.createCleanUp(compilationUnit,
71                                 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS),
72                                 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER),
73                                 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW));
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         @Override
80         public String[] getStepDescriptions() {
81                 List<String> result= new ArrayList<String>();
82                 if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS))
83                         result.add(MultiFixMessages.CodeStyleMultiFix_ConvertSingleStatementInControlBodeyToBlock_description);
84                 if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER))
85                         result.add(MultiFixMessages.ControlStatementsCleanUp_RemoveUnnecessaryBlocks_description);
86                 if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW))
87                         result.add(MultiFixMessages.ControlStatementsCleanUp_RemoveUnnecessaryBlocksWithReturnOrThrow_description);
88
89                 return result.toArray(new String[result.size()]);
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         public String getPreview() {
97                 StringBuffer buf= new StringBuffer();
98
99                 if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS)) {
100                         buf.append("if (obj == null) {\n"); //$NON-NLS-1$
101                         buf.append("    throw new IllegalArgumentException();\n"); //$NON-NLS-1$
102                         buf.append("}\n"); //$NON-NLS-1$
103
104                         buf.append("if (ids.length > 0) {\n"); //$NON-NLS-1$
105                         buf.append("    System.out.println(ids[0]);\n"); //$NON-NLS-1$
106                         buf.append("} else {\n"); //$NON-NLS-1$
107                         buf.append("    return;\n"); //$NON-NLS-1$
108                         buf.append("}\n"); //$NON-NLS-1$
109                 } else if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER)){
110                         buf.append("if (obj == null)\n"); //$NON-NLS-1$
111                         buf.append("    throw new IllegalArgumentException();\n"); //$NON-NLS-1$
112                         buf.append("\n"); //$NON-NLS-1$
113
114                         buf.append("if (ids.length > 0)\n"); //$NON-NLS-1$
115                         buf.append("    System.out.println(ids[0]);\n"); //$NON-NLS-1$
116                         buf.append("else\n"); //$NON-NLS-1$
117                         buf.append("    return;\n"); //$NON-NLS-1$
118                         buf.append("\n"); //$NON-NLS-1$
119                 } else if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW)) {
120                         buf.append("if (obj == null)\n"); //$NON-NLS-1$
121                         buf.append("    throw new IllegalArgumentException();\n"); //$NON-NLS-1$
122                         buf.append("\n"); //$NON-NLS-1$
123
124                         buf.append("if (ids.length > 0) {\n"); //$NON-NLS-1$
125                         buf.append("    System.out.println(ids[0]);\n"); //$NON-NLS-1$
126                         buf.append("} else \n"); //$NON-NLS-1$
127                         buf.append("    return;\n"); //$NON-NLS-1$
128                         buf.append("\n"); //$NON-NLS-1$
129                 } else {
130                         buf.append("if (obj == null) {\n"); //$NON-NLS-1$
131                         buf.append("    throw new IllegalArgumentException();\n"); //$NON-NLS-1$
132                         buf.append("}\n"); //$NON-NLS-1$
133
134                         buf.append("if (ids.length > 0) {\n"); //$NON-NLS-1$
135                         buf.append("    System.out.println(ids[0]);\n"); //$NON-NLS-1$
136                         buf.append("} else \n"); //$NON-NLS-1$
137                         buf.append("    return;\n"); //$NON-NLS-1$
138                         buf.append("\n"); //$NON-NLS-1$
139                 }
140
141                 return buf.toString();
142         }
143
144 }