1 /*******************************************************************************
2 * Copyright (c) 2007, 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.HashSet;
16 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.templates.Template;
20 import org.eclipse.jface.text.templates.TemplateBuffer;
21 import org.eclipse.jface.text.templates.TemplateException;
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.compiler.IProblem;
25 import org.eclipse.jdt.core.dom.ASTNode;
26 import org.eclipse.jdt.core.dom.CompilationUnit;
28 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
29 import org.eclipse.jdt.internal.corext.fix.UnimplementedCodeFix;
30 import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext;
31 import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType;
33 import org.eclipse.jdt.ui.PreferenceConstants;
34 import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
35 import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
36 import org.eclipse.jdt.ui.text.java.IProblemLocation;
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
40 public class UnimplementedCodeCleanUp extends AbstractMultiFix {
42 public static final String MAKE_TYPE_ABSTRACT= "cleanup.make_type_abstract_if_missing_method"; //$NON-NLS-1$
44 public UnimplementedCodeCleanUp() {
48 public UnimplementedCodeCleanUp(Map<String, String> settings) {
56 public String[] getStepDescriptions() {
57 if (isEnabled(CleanUpConstants.ADD_MISSING_METHODES))
58 return new String[] { MultiFixMessages.UnimplementedCodeCleanUp_AddUnimplementedMethods_description };
60 if (isEnabled(MAKE_TYPE_ABSTRACT))
61 return new String[] { MultiFixMessages.UnimplementedCodeCleanUp_MakeAbstract_description };
70 public String getPreview() {
71 StringBuffer buf= new StringBuffer();
73 if (isEnabled(MAKE_TYPE_ABSTRACT)) {
74 buf.append("public abstract class Face implements IFace {\n"); //$NON-NLS-1$
76 buf.append("public class Face implements IFace {\n"); //$NON-NLS-1$
78 if (isEnabled(CleanUpConstants.ADD_MISSING_METHODES)) {
79 boolean createComments= Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_ADD_COMMENTS, null)).booleanValue();
81 buf.append(indent(getOverridingMethodComment(), " ")); //$NON-NLS-1$
83 buf.append(" @Override\n"); //$NON-NLS-1$
84 buf.append(" public void method() {\n"); //$NON-NLS-1$
85 buf.append(indent(getMethodBody(), " ")); //$NON-NLS-1$
86 buf.append(" }\n"); //$NON-NLS-1$
88 buf.append("}\n"); //$NON-NLS-1$
90 return buf.toString();
97 public CleanUpRequirements getRequirements() {
98 if (!isEnabled(CleanUpConstants.ADD_MISSING_METHODES) && !isEnabled(MAKE_TYPE_ABSTRACT))
99 return super.getRequirements();
101 return new CleanUpRequirements(true, false, false, null);
108 protected ICleanUpFix createFix(CompilationUnit unit) throws CoreException {
109 IProblemLocation[] problemLocations= convertProblems(unit.getProblems());
110 problemLocations= filter(problemLocations, new int[] { IProblem.AbstractMethodMustBeImplemented, IProblem.EnumConstantMustImplementAbstractMethod });
112 return UnimplementedCodeFix.createCleanUp(unit, isEnabled(CleanUpConstants.ADD_MISSING_METHODES), isEnabled(MAKE_TYPE_ABSTRACT), problemLocations);
119 protected ICleanUpFix createFix(CompilationUnit unit, IProblemLocation[] problems) throws CoreException {
120 IProblemLocation[] problemLocations= filter(problems, new int[] { IProblem.AbstractMethodMustBeImplemented, IProblem.EnumConstantMustImplementAbstractMethod });
121 return UnimplementedCodeFix.createCleanUp(unit, isEnabled(CleanUpConstants.ADD_MISSING_METHODES), isEnabled(MAKE_TYPE_ABSTRACT), problemLocations);
127 public boolean canFix(ICompilationUnit compilationUnit, IProblemLocation problem) {
128 int id= problem.getProblemId();
129 if (id == IProblem.AbstractMethodMustBeImplemented || id == IProblem.EnumConstantMustImplementAbstractMethod)
130 return isEnabled(CleanUpConstants.ADD_MISSING_METHODES) || isEnabled(MAKE_TYPE_ABSTRACT);
139 public int computeNumberOfFixes(CompilationUnit compilationUnit) {
140 if (!isEnabled(CleanUpConstants.ADD_MISSING_METHODES) && !isEnabled(MAKE_TYPE_ABSTRACT))
143 IProblemLocation[] locations= filter(convertProblems(compilationUnit.getProblems()), new int[] { IProblem.AbstractMethodMustBeImplemented, IProblem.EnumConstantMustImplementAbstractMethod });
145 HashSet<ASTNode> types= new HashSet<ASTNode>();
146 for (int i= 0; i < locations.length; i++) {
147 ASTNode type= UnimplementedCodeFix.getSelectedTypeNode(compilationUnit, locations[i]);
156 private String getOverridingMethodComment() {
157 String templateName= CodeTemplateContextType.OVERRIDECOMMENT_ID;
159 Template template= getCodeTemplate(templateName);
160 if (template == null)
161 return ""; //$NON-NLS-1$
163 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), null, "\n"); //$NON-NLS-1$
165 return context.generated_5529463810416389532(template, this);
168 private String getMethodBody() {
169 String templateName= CodeTemplateContextType.METHODSTUB_ID;
170 Template template= getCodeTemplate(templateName);
171 if (template == null)
172 return ""; //$NON-NLS-1$
174 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), null, "\n"); //$NON-NLS-1$
175 return context.generated_4985176108266267398(template, this);
178 private static Template getCodeTemplate(String id) {
179 return JavaPlugin.getDefault().getCodeTemplateStore().findTemplateById(id);
182 public String evaluateTemplate(Template template, CodeTemplateContext context) {
183 TemplateBuffer buffer;
185 buffer= context.evaluate(template);
186 } catch (BadLocationException e) {
188 return ""; //$NON-NLS-1$
189 } catch (TemplateException e) {
191 return ""; //$NON-NLS-1$
194 return ""; //$NON-NLS-1$
196 return buffer.getString();
199 private String indent(String code, String indent) {
200 if (code.length() == 0)
203 StringBuffer buf= new StringBuffer();
205 char[] codeArray= code.toCharArray();
206 for (int i= 0; i < codeArray.length; i++) {
207 buf.append(codeArray[i]);
208 if (codeArray[i] == '\n')
211 buf.append("\n"); //$NON-NLS-1$
213 return buf.toString();