KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > plugins > ExtractInterfaceRefactoringPlugin


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.java.plugins;
20
21 import org.netbeans.modules.refactoring.api.Problem;
22 import org.netbeans.modules.refactoring.java.api.ExtractInterfaceRefactoring;
23 import org.netbeans.modules.refactoring.java.plugins.JavaRefactoringPlugin;
24 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
25
26 public class ExtractInterfaceRefactoringPlugin extends JavaRefactoringPlugin {
27     
28     private ExtractInterfaceRefactoring refactoring;
29     
30     ExtractInterfaceRefactoringPlugin(ExtractInterfaceRefactoring refactoring) {
31         this.refactoring = refactoring;
32     }
33
34     public Problem preCheck() {
35         //TODO: implement me
36
return null;
37     }
38
39     public Problem checkParameters() {
40         //TODO: implement me
41
return null;
42     }
43
44     public Problem fastCheckParameters() {
45         //TODO: implement me
46
return null;
47     }
48
49     public Problem prepare(RefactoringElementsBag refactoringElements) {
50         //TODO: implement me
51
return null;
52     }
53
54 }
55 ///** Plugin that implements the core functionality of Extract Interface refactoring.
56
// *
57
// * @author Martin Matula
58
// */
59
//public class ExtractInterfaceRefactoringPlugin extends JavaRefactoringPlugin {
60
// /** Reference to the parent refactoring instance */
61
// private final ExtractInterfaceRefactoring refactoring;
62
//
63
// /** Creates a new instance of ExtractInterfaceRefactoringPlugin
64
// * @param refactoring Parent refactoring instance.
65
// */
66
// ExtractInterfaceRefactoringPlugin(ExtractInterfaceRefactoring refactoring) {
67
// this.refactoring = refactoring;
68
// }
69
//
70
// /** Checks pre-conditions of the refactoring.
71
// * @return Problems found or <code>null</code>.
72
// */
73
// public Problem preCheck() {
74
// // fire operation start on the registered progress listeners (1 step)
75
// fireProgressListenerStart(AbstractRefactoring.PRE_CHECK, 1);
76
// try {
77
// JavaClass sourceType = refactoring.getSourceType();
78
//
79
// // check whether the element is valid
80
// Problem result = isElementAvail(sourceType);
81
// if (result != null) {
82
// // fatal error -> don't continue with further checks
83
// return result;
84
// }
85
// if (!CheckUtils.isElementInOpenProject(sourceType)) {
86
// return new Problem(true, NbBundle.getMessage(JavaRefactoringPlugin.class, "ERR_ProjectNotOpened"));
87
// }
88
//
89
// // check whether the element is an unresolved class
90
// if (sourceType instanceof UnresolvedClass) {
91
// // fatal error -> return
92
// return new Problem(true, NbBundle.getMessage(JavaRefactoringPlugin.class, "ERR_ElementNotAvailable")); // NOI18N
93
// }
94
//
95
// // increase progress (step 1)
96
// fireProgressListenerStep();
97
//
98
// // all checks passed -> return null
99
// return null;
100
// } finally {
101
// // fire operation end on the registered progress listeners
102
// fireProgressListenerStop();
103
// }
104
// }
105
//
106
// public Problem fastCheckParameters() {
107
// Problem result = null;
108
//
109
// JavaClass sourceType = refactoring.getSourceType();
110
// String oldName = sourceType.getSimpleName();
111
// String newName = refactoring.getIfcName();
112
//
113
// if (!org.openide.util.Utilities.isJavaIdentifier(newName)) {
114
// result = createProblem(result, true, NbBundle.getMessage(RenameRefactoring.class, "ERR_InvalidIdentifier", newName)); // NOI18N
115
// return result;
116
// }
117
//
118
// Resource resource = sourceType.getResource();
119
// FileObject primFile = JavaModel.getFileObject(resource);
120
// FileObject folder = primFile.getParent();
121
// FileObject[] children = folder.getChildren();
122
// for (int x = 0; x < children.length; x++) {
123
// if (!children[x].isVirtual() && children[x].getName().equals(newName) && "java".equals(children[x].getExt())) { // NOI18N
124
// result = createProblem(result, true, NbBundle.getMessage(RenameRefactoring.class, "ERR_ClassClash", newName, resource.getPackageName())); // NOI18N
125
// return result;
126
// }
127
// }
128
//
129
// return null;
130
// }
131
//
132
// public Problem checkParameters() {
133
// // TODO: check whether the selected members are public and non-static in case of methods, static in other cases
134
// // TODO: check whether all members belong to the source type
135
// return null;
136
// }
137
//
138
// public Problem prepare(RefactoringElementsBag refactoringElements) {
139
// NamedElement[] members = refactoring.getMembers();
140
//
141
// List typeParams = findUsedGenericTypes(members, refactoring.getSourceType());
142
//
143
// CreateIfcElement createIfcElement = new CreateIfcElement(refactoring.getSourceType().getResource(), refactoring.getIfcName(), typeParams);
144
// refactoringElements.add(refactoring, createIfcElement);
145
// refactoringElements.add(refactoring, new AddImplementsElement(refactoring.getSourceType(), refactoring.getIfcName(), typeParams));
146
// for (int i = 0; i < members.length; i++) {
147
// refactoringElements.add(refactoring, new GenerateMemberElement(members[i], createIfcElement));
148
// }
149
//
150
// return null;
151
// }
152
//
153
// /**
154
// * Returns list of TypeParameters from javaClass that are used by the passed members elements.
155
// */
156
// static List findUsedGenericTypes(NamedElement[] members, JavaClass javaClass) {
157
// List typePars = javaClass.getTypeParameters();
158
// if (typePars.isEmpty())
159
// return Collections.EMPTY_LIST;
160
// Map nameToType = new HashMap();
161
// for (Iterator iter = typePars.iterator(); iter.hasNext(); ) {
162
// TypeParameter tp = (TypeParameter) iter.next();
163
// nameToType.put(tp.getName(), tp);
164
// }
165
// Set set = new HashSet();
166
// Set redefined = new HashSet();
167
// for (int x = 0; x < members.length; x++) {
168
// findUsedGenericTypes(members[x], set, redefined, nameToType);
169
// }
170
// List result = new ArrayList(set.size());
171
// for (Iterator iter = typePars.iterator(); iter.hasNext(); ) {
172
// TypeParameter tp = (TypeParameter) iter.next();
173
// if (set.contains(tp.getName()))
174
// result.add(tp);
175
// }
176
// return result;
177
// }
178
//
179
// private static void findUsedGenericTypes(Element elem, Set result, Set hidden, Map nameToType) {
180
// if (elem instanceof GenericElement) {
181
// GenericElement gelem = (GenericElement) elem;
182
// List tpList = gelem.getTypeParameters();
183
// if (!tpList.isEmpty()) {
184
// boolean extendedHiddenSetCreated = false;
185
// for (Iterator iter = tpList.iterator(); iter.hasNext(); ) {
186
// TypeParameter tp = (TypeParameter) iter.next();
187
// String name = tp.getName();
188
// if (nameToType.get(name) != null && !hidden.contains(name)) {
189
// if (!extendedHiddenSetCreated) {
190
// hidden = new HashSet(hidden);
191
// extendedHiddenSetCreated = true;
192
// }
193
// hidden.add(name);
194
// }
195
// } // for
196
// } // if
197
// } // if
198
// if (elem instanceof MultipartId) {
199
// MultipartId id = (MultipartId) elem;
200
// if (id.getElement() instanceof TypeParameter) {
201
// String tpName = id.getName();
202
// if (nameToType.get(tpName) != null && !hidden.contains(tpName))
203
// result.add(tpName);
204
// }
205
// }
206
// for (Iterator iter = elem.getChildren().iterator(); iter.hasNext(); ) {
207
// findUsedGenericTypes((Element) iter.next(), result, hidden, nameToType);
208
// }
209
// }
210
//
211
// // --- REFACTORING ELEMENTS ------------------------------------------------
212
//
213
// private static class CreateIfcElement extends SimpleRefactoringElementImpl {
214
// private final String ifcName;
215
// private final Resource source;
216
// private final String text;
217
// private final List typeParams;
218
//
219
// private JavaClass newIfc = null;
220
//
221
// CreateIfcElement(Resource source, String ifcName, List typeParams) {
222
// this.source = source;
223
// this.ifcName = ifcName;
224
// this.typeParams = typeParams;
225
// this.text = NbBundle.getMessage(ExtractInterfaceRefactoringPlugin.class, "TXT_ExtractInterface_CreateIfc", ifcName); // NOI18N
226
// }
227
//
228
// public void performChange() {
229
// ExternalChange ec = new ExternalChange() {
230
// private FileSystem fs;
231
// private String newIfcName;
232
// private String folderName;
233
//
234
// public void performExternalChange() {
235
// try {
236
// FileObject tempFO = Repository.getDefault().getDefaultFileSystem().findResource("Templates/Classes/Interface.java"); // NOI18N
237
//
238
// FileObject folderFO;
239
// if (fs == null) {
240
// FileObject sourceFO = JavaModel.getFileObject(source);
241
// folderFO = sourceFO.getParent();
242
// folderName = folderFO.getPath();
243
// fs = folderFO.getFileSystem();
244
// } else {
245
// folderFO = fs.findResource(folderName);
246
// }
247
//
248
// DataFolder folder = (DataFolder) DataObject.find(folderFO);
249
// DataObject template = DataObject.find(tempFO);
250
// DataObject newIfcDO = template.createFromTemplate(folder, ifcName);
251
// UndoWatcher.watch(newIfcDO);
252
// FileObject newIfcFO = newIfcDO.getPrimaryFile();
253
// newIfcName = newIfcFO.getPath();
254
// newIfc = (JavaClass) JavaMetamodel.getManager().getResource(newIfcFO).getClassifiers().iterator().next();
255
// } catch (DataObjectNotFoundException e) {
256
// ErrorManager.getDefault().notify(e);
257
// } catch (IOException e) {
258
// ErrorManager.getDefault().notify(e);
259
// }
260
// }
261
//
262
// public void undoExternalChange() {
263
// try {
264
// FileObject newIfcFO = fs.findResource(newIfcName);
265
// DataObject newIfcDO = DataObject.find(newIfcFO);
266
// newIfcDO.delete();
267
// } catch (DataObjectNotFoundException e) {
268
// ErrorManager.getDefault().notify(e);
269
// } catch (IOException e) {
270
// ErrorManager.getDefault().notify(e);
271
// }
272
// }
273
// };
274
// ec.performExternalChange();
275
// JavaMetamodel.getManager().registerUndoElement(ec);
276
//
277
// List ifcTypeParams = newIfc.getTypeParameters();
278
// for (Iterator iter = typeParams.iterator(); iter.hasNext(); ) {
279
// ifcTypeParams.add(((TypeParameter) iter.next()).duplicate());
280
// }
281
// }
282
//
283
// JavaClass getNewInterface() {
284
// return newIfc;
285
// }
286
//
287
// public String getText() {
288
// return text;
289
// }
290
//
291
// public String getDisplayText() {
292
// return text;
293
// }
294
//
295
// public FileObject getParentFile() {
296
// return null;
297
// }
298
//
299
// public Element getJavaElement() {
300
// return (Element) source.refImmediateComposite();
301
// }
302
//
303
// public PositionBounds getPosition() {
304
// return null;
305
// }
306
// }
307
//
308
// private static class AddImplementsElement extends SimpleRefactoringElementImpl {
309
// private final JavaClass sourceType;
310
// private final String ifcName;
311
// private final List typeParams;
312
// private final String text;
313
//
314
// AddImplementsElement(JavaClass sourceType, String ifcName, List typeParams) {
315
// this.sourceType = sourceType;
316
// this.ifcName = ifcName;
317
// this.typeParams = typeParams;
318
// this.text = NbBundle.getMessage(ExtractInterfaceRefactoringPlugin.class, "TXT_ExtractInterface_AddImplements", ifcName); // NOI18N
319
// }
320
//
321
// public void performChange() {
322
// JavaModelPackage extent = (JavaModelPackage) sourceType.refImmediatePackage();
323
// MultipartIdClass idProxy = extent.getMultipartId();
324
// MultipartId mpi = idProxy.createMultipartId(ifcName, null, null);
325
// if (typeParams != null && typeParams.size() > 0) {
326
// List typeArgs = mpi.getTypeArguments();
327
// for (Iterator iter = typeParams.iterator(); iter.hasNext(); ) {
328
// typeArgs.add(idProxy.createMultipartId(((TypeParameter) iter.next()).getName(), null, null));
329
// }
330
// }
331
// sourceType.getInterfaceNames().add(mpi);
332
// }
333
//
334
// public String getText() {
335
// return text;
336
// }
337
//
338
// public String getDisplayText() {
339
// return text;
340
// }
341
//
342
// public FileObject getParentFile() {
343
// return JavaMetamodel.getManager().getFileObject(sourceType.getResource());
344
// }
345
//
346
// public Element getJavaElement() {
347
// return sourceType;
348
// }
349
//
350
// public PositionBounds getPosition() {
351
// return JavaMetamodel.getManager().getElementPosition(sourceType);
352
// }
353
// }
354
//
355
// private static class GenerateMemberElement extends SimpleRefactoringElementImpl {
356
// private final NamedElement elementToGenerate;
357
// private final CreateIfcElement cie;
358
// private final String text;
359
//
360
// GenerateMemberElement(NamedElement elementToGenerate, CreateIfcElement element) {
361
// this.elementToGenerate = elementToGenerate;
362
// this.cie = element;
363
// this.text = NbBundle.getMessage(ExtractInterfaceRefactoringPlugin.class, getBundleKey(elementToGenerate), UIUtilities.getDisplayText(elementToGenerate));
364
// }
365
//
366
// private static String getBundleKey(NamedElement element) {
367
// if (element instanceof Method) {
368
// Object comp = element.refImmediateComposite();
369
// if (comp instanceof JavaClass && ((JavaClass) comp).isInterface()) {
370
// return "TXT_ExtractInterface_MoveMethod"; // NOI18N
371
// } else {
372
// return "TXT_ExtractInterface_Method"; // NOI18N
373
// }
374
// } else if (element instanceof Field) {
375
// return "TXT_ExtractInterface_Field"; // NOI18N
376
// } else if (element instanceof JavaClass) {
377
// return "TXT_ExtractInterface_Class"; // NOI18N
378
// } else if (element instanceof MultipartId) {
379
// return "TXT_ExtractInterface_Implements"; // NOI18N
380
// }
381
// throw new IllegalArgumentException("Wrong type of element: " + element.getClass().getName()); // NOI18N
382
// }
383
//
384
// public void performChange() {
385
// if (cie.getNewInterface() == null) return;
386
//
387
// if (elementToGenerate instanceof Method) {
388
// JavaModelPackage extent = (JavaModelPackage) cie.getNewInterface().refImmediatePackage();
389
// Method oldMethod = (Method) elementToGenerate;
390
// Method method = extent.getMethod().createMethod(
391
// oldMethod.getName(),
392
// Utilities.duplicateList(oldMethod.getAnnotations(), extent),
393
// 0,
394
// oldMethod.getJavadocText(),
395
// null,
396
// null,
397
// null,
398
// Utilities.duplicateList(oldMethod.getTypeParameters(), extent),
399
// Utilities.duplicateList(oldMethod.getParameters(), extent),
400
// Utilities.duplicateList(oldMethod.getExceptionNames(), extent),
401
// (TypeReference) oldMethod.getTypeName().duplicate(),
402
// oldMethod.getDimCount()
403
// );
404
// cie.getNewInterface().getFeatures().add(method);
405
// ((MetadataElement) method).fixImports(cie.getNewInterface(), elementToGenerate);
406
//
407
// Object comp = oldMethod.refImmediateComposite();
408
// if (comp instanceof JavaClass) {
409
// JavaClass jc = (JavaClass)comp;
410
// if (jc.isInterface()) {
411
// jc.replaceChild(oldMethod, null);
412
// }
413
// }
414
// } else {
415
// // remove element from the original type
416
// Element parent = (Element) elementToGenerate.refImmediateComposite();
417
// parent.replaceChild(elementToGenerate, null);
418
// // add the element to the interface
419
// if (elementToGenerate instanceof MultipartId) {
420
// cie.getNewInterface().getInterfaceNames().add(JavaModelUtil.resolveImportsForClass(cie.getNewInterface(), (JavaClass) ((MultipartId) elementToGenerate).getElement()));
421
// elementToGenerate.refDelete();
422
// } else {
423
// Feature feature = (Feature) elementToGenerate;
424
// feature.setModifiers(feature.getModifiers() & ~(Modifier.FINAL | Modifier.STATIC | Modifier.PUBLIC));
425
// cie.getNewInterface().getContents().add(elementToGenerate);
426
// ((MetadataElement) elementToGenerate).fixImports(cie.getNewInterface(), elementToGenerate);
427
// }
428
// }
429
// }
430
//
431
// public String getText() {
432
// return text;
433
// }
434
//
435
// public String getDisplayText() {
436
// return text;
437
// }
438
//
439
// public FileObject getParentFile() {
440
// return JavaMetamodel.getManager().getFileObject(elementToGenerate.getResource());
441
// }
442
//
443
// public Element getJavaElement() {
444
// return (Element) JavaModelUtil.getDeclaringFeature(elementToGenerate);
445
// }
446
//
447
// public PositionBounds getPosition() {
448
// return JavaMetamodel.getManager().getElementPosition(elementToGenerate);
449
// }
450
// }
451
//}
452
Popular Tags