]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/fix/UnimplementedCodeCleanUp.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / fix / UnimplementedCodeCleanUp.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.fix;
12
13 import java.util.HashSet;
14 import java.util.Map;
15
16 import org.eclipse.core.runtime.CoreException;
17
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;
22
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;
27
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;
32
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;
37
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39
40 public class UnimplementedCodeCleanUp extends AbstractMultiFix {
41
42         public static final String MAKE_TYPE_ABSTRACT= "cleanup.make_type_abstract_if_missing_method"; //$NON-NLS-1$
43
44         public UnimplementedCodeCleanUp() {
45                 super();
46         }
47
48         public UnimplementedCodeCleanUp(Map<String, String> settings) {
49                 super(settings);
50         }
51
52         /**
53          * {@inheritDoc}
54          */
55         @Override
56         public String[] getStepDescriptions() {
57                 if (isEnabled(CleanUpConstants.ADD_MISSING_METHODES))
58                         return new String[] { MultiFixMessages.UnimplementedCodeCleanUp_AddUnimplementedMethods_description };
59
60                 if (isEnabled(MAKE_TYPE_ABSTRACT))
61                         return new String[] { MultiFixMessages.UnimplementedCodeCleanUp_MakeAbstract_description };
62
63                 return null;
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         @Override
70         public String getPreview() {
71                 StringBuffer buf= new StringBuffer();
72
73                 if (isEnabled(MAKE_TYPE_ABSTRACT)) {
74                         buf.append("public abstract class Face implements IFace {\n"); //$NON-NLS-1$
75                 } else {
76                         buf.append("public class Face implements IFace {\n"); //$NON-NLS-1$
77                 }
78                 if (isEnabled(CleanUpConstants.ADD_MISSING_METHODES)) {
79                         boolean createComments= Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_ADD_COMMENTS, null)).booleanValue();
80                         if (createComments)
81                                 buf.append(indent(getOverridingMethodComment(), "    ")); //$NON-NLS-1$
82
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$
87                 }
88                 buf.append("}\n"); //$NON-NLS-1$
89
90                 return buf.toString();
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         @Override
97         public CleanUpRequirements getRequirements() {
98                 if (!isEnabled(CleanUpConstants.ADD_MISSING_METHODES) && !isEnabled(MAKE_TYPE_ABSTRACT))
99                         return super.getRequirements();
100
101                 return new CleanUpRequirements(true, false, false, null);
102         }
103
104         /**
105          * {@inheritDoc}
106          */
107         @Override
108         protected ICleanUpFix createFix(CompilationUnit unit) throws CoreException {
109                 IProblemLocation[] problemLocations= convertProblems(unit.getProblems());
110                 problemLocations= filter(problemLocations, new int[] { IProblem.AbstractMethodMustBeImplemented, IProblem.EnumConstantMustImplementAbstractMethod });
111
112                 return UnimplementedCodeFix.createCleanUp(unit, isEnabled(CleanUpConstants.ADD_MISSING_METHODES), isEnabled(MAKE_TYPE_ABSTRACT), problemLocations);
113         }
114
115         /**
116          * {@inheritDoc}
117          */
118         @Override
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);
122         }
123
124         /**
125          * {@inheritDoc}
126          */
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);
131
132                 return false;
133         }
134
135         /**
136          * {@inheritDoc}
137          */
138         @Override
139         public int computeNumberOfFixes(CompilationUnit compilationUnit) {
140                 if (!isEnabled(CleanUpConstants.ADD_MISSING_METHODES) && !isEnabled(MAKE_TYPE_ABSTRACT))
141                         return 0;
142
143                 IProblemLocation[] locations= filter(convertProblems(compilationUnit.getProblems()), new int[] { IProblem.AbstractMethodMustBeImplemented, IProblem.EnumConstantMustImplementAbstractMethod });
144
145                 HashSet<ASTNode> types= new HashSet<ASTNode>();
146                 for (int i= 0; i < locations.length; i++) {
147                         ASTNode type= UnimplementedCodeFix.getSelectedTypeNode(compilationUnit, locations[i]);
148                         if (type != null) {
149                                 types.add(type);
150                         }
151                 }
152
153                 return types.size();
154         }
155
156         private String getOverridingMethodComment() {
157                 String templateName= CodeTemplateContextType.OVERRIDECOMMENT_ID;
158
159                 Template template= getCodeTemplate(templateName);
160                 if (template == null)
161                         return ""; //$NON-NLS-1$
162
163                 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), null, "\n"); //$NON-NLS-1$
164
165                 return context.generated_5529463810416389532(template, this);
166         }
167
168         private String getMethodBody() {
169                 String templateName= CodeTemplateContextType.METHODSTUB_ID;
170                 Template template= getCodeTemplate(templateName);
171                 if (template == null)
172                         return ""; //$NON-NLS-1$
173
174                 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), null, "\n"); //$NON-NLS-1$
175                 return context.generated_4985176108266267398(template, this);
176         }
177
178         private static Template getCodeTemplate(String id) {
179                 return JavaPlugin.getDefault().getCodeTemplateStore().findTemplateById(id);
180         }
181
182         public String evaluateTemplate(Template template, CodeTemplateContext context) {
183                 TemplateBuffer buffer;
184                 try {
185                         buffer= context.evaluate(template);
186                 } catch (BadLocationException e) {
187                         JavaPlugin.log(e);
188                         return ""; //$NON-NLS-1$
189                 } catch (TemplateException e) {
190                         JavaPlugin.log(e);
191                         return ""; //$NON-NLS-1$
192                 }
193                 if (buffer == null)
194                         return ""; //$NON-NLS-1$
195
196                 return buffer.getString();
197         }
198
199         private String indent(String code, String indent) {
200                 if (code.length() == 0)
201                         return code;
202
203                 StringBuffer buf= new StringBuffer();
204                 buf.append(indent);
205                 char[] codeArray= code.toCharArray();
206                 for (int i= 0; i < codeArray.length; i++) {
207                         buf.append(codeArray[i]);
208                         if (codeArray[i] == '\n')
209                                 buf.append(indent);
210                 }
211                 buf.append("\n"); //$NON-NLS-1$
212
213                 return buf.toString();
214         }
215
216 }