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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.fix;
13 import java.util.ArrayList;
14 import java.util.List;
17 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.jdt.core.dom.CompilationUnit;
21 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
22 import org.eclipse.jdt.internal.corext.fix.ControlStatementsFix;
24 import org.eclipse.jdt.ui.cleanup.CleanUpContext;
25 import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
26 import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
28 public class ControlStatementsCleanUp extends AbstractCleanUp {
30 public ControlStatementsCleanUp(Map<String, String> options) {
34 public ControlStatementsCleanUp() {
42 public CleanUpRequirements getRequirements() {
43 return new CleanUpRequirements(requireAST(), false, false, null);
46 private boolean requireAST() {
47 boolean useBlocks= isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS);
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);
61 public ICleanUpFix createFix(CleanUpContext context) throws CoreException {
62 CompilationUnit compilationUnit= context.getAST();
63 if (compilationUnit == null)
66 boolean useBlocks= isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS);
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));
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);
89 return result.toArray(new String[result.size()]);
96 public String getPreview() {
97 StringBuffer buf= new StringBuffer();
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$
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$
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$
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$
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$
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$
141 return buf.toString();