-
Notifications
You must be signed in to change notification settings - Fork 82
/
PushDownRefactoringProcessor.java
1115 lines (1018 loc) · 51.3 KB
/
PushDownRefactoringProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2006, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Microsoft Corporation - read formatting options from the compilation unit
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.structure;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.GroupCategory;
import org.eclipse.ltk.core.refactoring.GroupCategorySet;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTRequestor;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IExtendedModifier;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NodeFinder;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.ThisExpression;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
import org.eclipse.jdt.core.refactoring.descriptors.PushDownDescriptor;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore;
import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;
import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.core.manipulation.util.Strings;
import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.BodyDeclarationRewrite;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.dom.ModifierRewrite;
import org.eclipse.jdt.internal.corext.refactoring.Checks;
import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTesterCore;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2;
import org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup;
import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange;
import org.eclipse.jdt.internal.corext.refactoring.rename.MethodChecks;
import org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment;
import org.eclipse.jdt.internal.corext.refactoring.util.JavaStatusContext;
import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil;
import org.eclipse.jdt.internal.corext.refactoring.util.TextEditBasedChangeManager;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.JdtFlags;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.corext.util.SearchUtils;
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
/**
* Refactoring processor for the push down refactoring.
*
* @since 3.2
*/
public final class PushDownRefactoringProcessor extends HierarchyProcessor {
public static class MemberActionInfo implements IMemberActionInfo {
public static final int NO_ACTION= 2;
public static final int PUSH_ABSTRACT_ACTION= 1;
public static final int PUSH_DOWN_ACTION= 0;
private static void assertValidAction(IMember member, int action) {
if (member instanceof IMethod)
Assert.isTrue(action == PUSH_ABSTRACT_ACTION || action == NO_ACTION || action == PUSH_DOWN_ACTION);
else if (member instanceof IField)
Assert.isTrue(action == NO_ACTION || action == PUSH_DOWN_ACTION);
}
public static MemberActionInfo create(IMember member, int action) {
return new MemberActionInfo(member, action);
}
static IMember[] getMembers(MemberActionInfo[] infos) {
IMember[] result= new IMember[infos.length];
for (int i= 0; i < result.length; i++) {
result[i]= infos[i].getMember();
}
return result;
}
private int fAction;
private final IMember fMember;
private MemberActionInfo(IMember member, int action) {
assertValidAction(member, action);
Assert.isTrue(member instanceof IField || member instanceof IMethod);
fMember= member;
fAction= action;
}
boolean copyJavadocToCopiesInSubclasses() {
return isToBeDeletedFromDeclaringClass();
}
public int getAction() {
return fAction;
}
public int[] getAvailableActions() {
if (isFieldInfo())
return new int[] { PUSH_DOWN_ACTION, NO_ACTION };
return new int[] { PUSH_DOWN_ACTION, PUSH_ABSTRACT_ACTION, NO_ACTION };
}
public IMember getMember() {
return fMember;
}
int getNewModifiersForCopyInSubclass(int oldModifiers) throws JavaModelException {
if (isFieldInfo())
return oldModifiers;
if (isToBeDeletedFromDeclaringClass())
return oldModifiers;
int modifiers= oldModifiers;
if (isNewMethodToBeDeclaredAbstract()) {
if (!JdtFlags.isPublic(fMember))
modifiers= Modifier.PROTECTED | JdtFlags.clearAccessModifiers(modifiers);
}
return modifiers;
}
int getNewModifiersForOriginal(int oldModifiers) throws JavaModelException {
if (isFieldInfo())
return oldModifiers;
if (isToBeDeletedFromDeclaringClass())
return oldModifiers;
int modifiers= oldModifiers;
if (isNewMethodToBeDeclaredAbstract()) {
modifiers= JdtFlags.clearFlag(Modifier.FINAL | Modifier.NATIVE, oldModifiers);
modifiers|= Modifier.ABSTRACT;
if (!JdtFlags.isPublic(fMember))
modifiers= Modifier.PROTECTED | JdtFlags.clearAccessModifiers(modifiers);
}
return modifiers;
}
@Override
public boolean isActive() {
return getAction() != NO_ACTION;
}
public boolean isEditable() {
if (isFieldInfo())
return false;
if (getAction() == MemberActionInfo.NO_ACTION)
return false;
return true;
}
boolean isFieldInfo() {
return fMember instanceof IField;
}
boolean isNewMethodToBeDeclaredAbstract() throws JavaModelException {
return !isFieldInfo() && !JdtFlags.isAbstract(fMember) && fAction == PUSH_ABSTRACT_ACTION;
}
boolean isToBeCreatedInSubclassesOfDeclaringClass() {
return fAction != NO_ACTION;
}
boolean isToBeDeletedFromDeclaringClass() {
return isToBePushedDown();
}
public boolean isToBePushedDown() {
return fAction == PUSH_DOWN_ACTION;
}
public void setAction(int action) {
assertValidAction(fMember, action);
if (isFieldInfo())
Assert.isTrue(action != PUSH_ABSTRACT_ACTION);
fAction= action;
}
}
private static final String ATTRIBUTE_ABSTRACT= "abstract"; //$NON-NLS-1$
private static final String ATTRIBUTE_PUSH= "push"; //$NON-NLS-1$
/** The identifier of this processor */
public static final String IDENTIFIER= "org.eclipse.jdt.ui.pushDownProcessor"; //$NON-NLS-1$
/** The push down group category set */
private static final GroupCategorySet SET_PUSH_DOWN= new GroupCategorySet(new GroupCategory("org.eclipse.jdt.internal.corext.pushDown", //$NON-NLS-1$
RefactoringCoreMessages.PushDownRefactoring_category_name, RefactoringCoreMessages.PushDownRefactoring_category_description));
private static MemberActionInfo[] createInfosForAllPushableFieldsAndMethods(IType type) throws JavaModelException {
List<MemberActionInfo> result= new ArrayList<>();
for (IMember pushableMember : RefactoringAvailabilityTesterCore.getPushDownMembers(type)) {
result.add(MemberActionInfo.create(pushableMember, MemberActionInfo.NO_ACTION));
}
return result.toArray(new MemberActionInfo[result.size()]);
}
private static IMember[] getAbstractMembers(IMember[] members) throws JavaModelException {
List<IMember> result= new ArrayList<>(members.length);
for (IMember member : members) {
if (JdtFlags.isAbstract(member))
result.add(member);
}
return result.toArray(new IMember[result.size()]);
}
private static CompilationUnitRewrite getCompilationUnitRewrite(final Map<ICompilationUnit, CompilationUnitRewrite> rewrites, final ICompilationUnit unit) {
Assert.isNotNull(rewrites);
Assert.isNotNull(unit);
CompilationUnitRewrite rewrite= rewrites.get(unit);
if (rewrite == null) {
rewrite= new CompilationUnitRewrite(unit);
rewrites.put(unit, rewrite);
}
return rewrite;
}
private static IJavaElement[] getReferencingElementsFromSameClass(IMember member, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException {
Assert.isNotNull(member);
SearchPattern pattern= SearchPattern.createPattern(member,
IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
if (pattern == null) {
return new IJavaElement[0];
}
final RefactoringSearchEngine2 engine= new RefactoringSearchEngine2(pattern);
engine.setFiltering(true, true);
IType declaringType= member.getDeclaringType();
engine.setScope(SearchEngine.createJavaSearchScope(new IJavaElement[] { declaringType }));
engine.setStatus(status);
engine.searchPattern(pm);
Set<IJavaElement> result= new HashSet<>(3);
ICompilationUnit cu= member.getCompilationUnit();
String source= cu.getSource();
CompilationUnit cuRoot= null;
for (SearchResultGroup group : (SearchResultGroup[]) engine.getResults()) {
outer:
for (SearchMatch searchResult : group.getSearchResults()) {
if (source.charAt(searchResult.getOffset() - 1) == '.') {
if (cuRoot == null) {
cuRoot= getCompilationUnitRoot(cu, source);
}
if (cuRoot != null) {
ASTNode node= NodeFinder.perform(cuRoot, searchResult.getOffset(), searchResult.getLength());
if (node != null && node instanceof MethodInvocation) {
MethodInvocation methodInvocation= (MethodInvocation)node;
Expression expression= methodInvocation.getExpression();
if (expression != null && !(expression instanceof ThisExpression)) {
if (expression instanceof ClassInstanceCreation) {
ClassInstanceCreation cic= (ClassInstanceCreation)expression;
Type t= cic.getType();
if (t.isSimpleType() && !((SimpleType)t).getName().getFullyQualifiedName().equals(declaringType.getElementName())) {
continue;
}
} else if (expression instanceof SimpleName) {
String referenceName= ((SimpleName)expression).getFullyQualifiedName();
final TypeDeclaration thisType= ASTNodes.getFirstAncestorOrNull(methodInvocation, TypeDeclaration.class);
if (thisType != null) {
final String thisTypeName= thisType.getName().getFullyQualifiedName();
for (FieldDeclaration fieldDeclaration : thisType.getFields()) {
List<VariableDeclarationFragment> fragments= fieldDeclaration.fragments();
for (VariableDeclarationFragment fragment : fragments) {
SimpleName name= fragment.getName();
if (name.getFullyQualifiedName().equals(referenceName)) {
Type fieldType= fieldDeclaration.getType();
if (!fieldType.isSimpleType()) {
continue outer;
}
SimpleType simpleType= (SimpleType)fieldType;
if (!simpleType.getName().getFullyQualifiedName().equals(thisTypeName)) {
continue outer;
}
}
}
}
}
}
}
}
}
}
result.add(SearchUtils.getEnclosingJavaElement(searchResult));
}
}
return result.toArray(new IJavaElement[result.size()]);
}
private static CompilationUnit getCompilationUnitRoot(ICompilationUnit cu, String source) {
Map<String, String> options = cu.getJavaProject().getOptions(true);
ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(false);
parser.setSource(source.toCharArray());
parser.setCompilerOptions(options);
parser.setProject(cu.getJavaProject());
CompilationUnit selectionCURoot= (CompilationUnit) parser.createAST(null);
return selectionCURoot;
}
private ITypeHierarchy fCachedClassHierarchy;
private MemberActionInfo[] fMemberInfos;
/**
* Creates a new push down refactoring processor.
*
* @param members
* the members to pull up
*/
public PushDownRefactoringProcessor(IMember[] members) {
super(members, null, false);
if (members != null) {
final IType type= RefactoringAvailabilityTesterCore.getTopLevelType(members);
try {
if (type != null && RefactoringAvailabilityTesterCore.getPushDownMembers(type).length != 0) {
fMembersToMove= new IMember[0];
fCachedDeclaringType= type;
}
} catch (JavaModelException exception) {
JavaManipulationPlugin.log(exception);
}
}
}
/**
* Creates a new push down refactoring processor from refactoring arguments.
*
* @param arguments
* the refactoring arguments
* @param status
* the resulting status
*/
public PushDownRefactoringProcessor(JavaRefactoringArguments arguments, RefactoringStatus status) {
super(null, null, false);
RefactoringStatus initializeStatus= initialize(arguments);
status.merge(initializeStatus);
}
private void addAllRequiredPushableMembers(List<IMember> queue, IMember member, IProgressMonitor monitor) throws JavaModelException {
SubMonitor sub= SubMonitor.convert(monitor,RefactoringCoreMessages.PushDownRefactoring_calculating_required, 2);
IMethod[] requiredMethods= ReferenceFinderUtil.getMethodsReferencedIn(new IJavaElement[] { member }, sub.newChild(1));
for (IMethod method : requiredMethods) {
if (!MethodChecks.isVirtual(method) && (method.getDeclaringType().equals(getDeclaringType()) && !queue.contains(method) && RefactoringAvailabilityTesterCore.isPushDownAvailable(method)))
queue.add(method);
}
for (IField field : ReferenceFinderUtil.getFieldsReferencedIn(new IJavaElement[] { member }, sub.newChild(1))) {
if (field.getDeclaringType().equals(getDeclaringType()) && !queue.contains(field) && RefactoringAvailabilityTesterCore.isPushDownAvailable(field))
queue.add(field);
}
}
private RefactoringStatus checkAbstractMembersInDestinationClasses(IMember[] membersToPushDown, IType[] destinationClassesForAbstract) throws JavaModelException {
RefactoringStatus result= new RefactoringStatus();
IMember[] abstractMembersToPushDown= getAbstractMembers(membersToPushDown);
for (IType type : destinationClassesForAbstract) {
result.merge(MemberCheckUtil.checkMembersInDestinationType(abstractMembersToPushDown, type));
}
return result;
}
private RefactoringStatus checkAccessedFields(IType[] subclasses, IProgressMonitor pm) throws JavaModelException {
RefactoringStatus result= new RefactoringStatus();
IMember[] membersToPushDown= MemberActionInfo.getMembers(getInfosForMembersToBeCreatedInSubclassesOfDeclaringClass());
List<IMember> pushedDownList= Arrays.asList(membersToPushDown);
IField[] accessedFields= ReferenceFinderUtil.getFieldsReferencedIn(membersToPushDown, pm);
for (IType targetClass : subclasses) {
ITypeHierarchy targetSupertypes= targetClass.newSupertypeHierarchy(null);
for (IField field : accessedFields) {
boolean isAccessible= pushedDownList.contains(field) || canBeAccessedFrom(field, targetClass, targetSupertypes) || Flags.isEnum(field.getFlags());
if (!isAccessible) {
String message= Messages.format(RefactoringCoreMessages.PushDownRefactoring_field_not_accessible, new String[] { JavaElementLabelsCore.getTextLabel(field, JavaElementLabelsCore.ALL_FULLY_QUALIFIED), JavaElementLabelsCore.getTextLabel(targetClass, JavaElementLabelsCore.ALL_FULLY_QUALIFIED) });
result.addError(message, JavaStatusContext.create(field));
}
}
}
pm.done();
return result;
}
private RefactoringStatus checkAccessedMethods(IType[] subclasses, IProgressMonitor pm) throws JavaModelException {
RefactoringStatus result= new RefactoringStatus();
IMember[] membersToPushDown= MemberActionInfo.getMembers(getInfosForMembersToBeCreatedInSubclassesOfDeclaringClass());
List<IMember> pushedDownList= Arrays.asList(membersToPushDown);
IMethod[] accessedMethods= ReferenceFinderUtil.getMethodsReferencedIn(membersToPushDown, pm);
for (IType targetClass : subclasses) {
ITypeHierarchy targetSupertypes= targetClass.newSupertypeHierarchy(null);
for (IMethod method : accessedMethods) {
boolean isAccessible= pushedDownList.contains(method) || canBeAccessedFrom(method, targetClass, targetSupertypes);
if (!isAccessible) {
String message= Messages.format(RefactoringCoreMessages.PushDownRefactoring_method_not_accessible, new String[] { JavaElementLabelsCore.getTextLabel(method, JavaElementLabelsCore.ALL_FULLY_QUALIFIED), JavaElementLabelsCore.getTextLabel(targetClass, JavaElementLabelsCore.ALL_FULLY_QUALIFIED) });
result.addError(message, JavaStatusContext.create(method));
}
}
}
pm.done();
return result;
}
private RefactoringStatus checkAccessedTypes(IType[] subclasses, IProgressMonitor pm) throws JavaModelException {
RefactoringStatus result= new RefactoringStatus();
IType[] accessedTypes= getTypesReferencedInMovedMembers(pm);
for (IType targetClass : subclasses) {
ITypeHierarchy targetSupertypes= targetClass.newSupertypeHierarchy(null);
for (IType type : accessedTypes) {
if (!canBeAccessedFrom(type, targetClass, targetSupertypes)) {
String message= Messages.format(RefactoringCoreMessages.PushDownRefactoring_type_not_accessible, new String[] { JavaElementLabelsCore.getTextLabel(type, JavaElementLabelsCore.ALL_FULLY_QUALIFIED), JavaElementLabelsCore.getTextLabel(targetClass, JavaElementLabelsCore.ALL_FULLY_QUALIFIED) });
result.addError(message, JavaStatusContext.create(type));
}
}
}
pm.done();
return result;
}
private RefactoringStatus checkElementsAccessedByModifiedMembers(IProgressMonitor pm) throws JavaModelException {
RefactoringStatus result= new RefactoringStatus();
SubMonitor subMon= SubMonitor.convert(pm, RefactoringCoreMessages.PushDownRefactoring_check_references, 4);
IType[] subclasses= getAbstractDestinations(subMon.newChild(1));
result.merge(checkAccessedTypes(subclasses, subMon.newChild(1)));
result.merge(checkAccessedFields(subclasses, subMon.newChild(1)));
result.merge(checkAccessedMethods(subclasses, subMon.newChild(1)));
pm.done();
return result;
}
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor monitor, CheckConditionsContext context) throws CoreException, OperationCanceledException {
try {
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, 5);
clearCaches();
ICompilationUnit unit= getDeclaringType().getCompilationUnit();
if (fLayer)
unit= unit.findWorkingCopy(fOwner);
resetWorkingCopies(unit);
final RefactoringStatus result= new RefactoringStatus();
result.merge(checkMembersInDestinationClasses(subMon.newChild(1)));
result.merge(checkElementsAccessedByModifiedMembers(subMon.newChild(1)));
result.merge(checkReferencesToPushedDownMembers(subMon.newChild(1)));
if (!JdtFlags.isAbstract(getDeclaringType()) && getAbstractDeclarationInfos().length != 0)
result.merge(checkConstructorCalls(getDeclaringType(), subMon.newChild(1)));
else
subMon.setWorkRemaining(1);
if (result.hasFatalError())
return result;
List<IMember> members= new ArrayList<>(fMemberInfos.length);
for (MemberActionInfo memberInfo : fMemberInfos) {
if (memberInfo.getAction() != MemberActionInfo.NO_ACTION) {
members.add(memberInfo.getMember());
}
}
fMembersToMove= members.toArray(new IMember[members.size()]);
fChangeManager= createChangeManager(subMon.newChild(1), result);
if (result.hasFatalError())
return result;
Checks.addModifiedFilesToChecker(ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits()), context);
return result;
} finally {
monitor.done();
}
}
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
try {
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, 2);
RefactoringStatus status= new RefactoringStatus();
status.merge(checkPossibleSubclasses(subMon.newChild(1)));
if (status.hasFatalError())
return status;
status.merge(checkDeclaringType(subMon.newChild(1)));
if (status.hasFatalError())
return status;
status.merge(checkIfMembersExist());
if (status.hasFatalError())
return status;
fMemberInfos= createInfosForAllPushableFieldsAndMethods(getDeclaringType());
List<IMember> list= Arrays.asList(fMembersToMove);
for (MemberActionInfo info : fMemberInfos) {
if (list.contains(info.getMember()))
info.setAction(MemberActionInfo.PUSH_DOWN_ACTION);
}
return status;
} finally {
monitor.done();
}
}
private RefactoringStatus checkMembersInDestinationClasses(IProgressMonitor monitor) throws JavaModelException {
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, 2);
RefactoringStatus result= new RefactoringStatus();
IMember[] membersToPushDown= MemberActionInfo.getMembers(getInfosForMembersToBeCreatedInSubclassesOfDeclaringClass());
IType[] destinationClassesForNonAbstract= getAbstractDestinations(subMon.newChild(1));
result.merge(checkNonAbstractMembersInDestinationClasses(membersToPushDown, destinationClassesForNonAbstract));
List<IMember> list= Arrays.asList(getAbstractMembers(getAbstractDestinations(subMon.newChild(1))));
IType[] destinationClassesForAbstract= list.toArray(new IType[list.size()]);
result.merge(checkAbstractMembersInDestinationClasses(membersToPushDown, destinationClassesForAbstract));
monitor.done();
return result;
}
private RefactoringStatus checkNonAbstractMembersInDestinationClasses(IMember[] membersToPushDown, IType[] destinationClassesForNonAbstract) throws JavaModelException {
RefactoringStatus result= new RefactoringStatus();
List<IMember> list= new ArrayList<>(Arrays.asList(membersToPushDown)); // Arrays.asList does not support removing
list.removeAll(Arrays.asList(getAbstractMembers(membersToPushDown)));
IMember[] nonAbstractMembersToPushDown= list.toArray(new IMember[list.size()]);
for (IType type : destinationClassesForNonAbstract) {
result.merge(MemberCheckUtil.checkMembersInDestinationType(nonAbstractMembersToPushDown, type));
}
return result;
}
private RefactoringStatus checkPossibleSubclasses(IProgressMonitor pm) throws JavaModelException {
IType[] modifiableSubclasses= getAbstractDestinations(pm);
if (modifiableSubclasses.length == 0) {
String msg= Messages.format(RefactoringCoreMessages.PushDownRefactoring_no_subclasses, new String[] { JavaElementLabelsCore.getTextLabel(getDeclaringType(), JavaElementLabelsCore.ALL_FULLY_QUALIFIED) });
return RefactoringStatus.createFatalErrorStatus(msg);
}
return new RefactoringStatus();
}
private RefactoringStatus checkReferencesToPushedDownMembers(IProgressMonitor monitor) throws JavaModelException {
List<IMember> fields= new ArrayList<>(fMemberInfos.length);
for (MemberActionInfo info : fMemberInfos) {
if (info.isToBePushedDown())
fields.add(info.getMember());
}
IMember[] membersToPush= fields.toArray(new IMember[fields.size()]);
RefactoringStatus result= new RefactoringStatus();
List<IMember> movedMembers= Arrays.asList(MemberActionInfo.getMembers(getInfosForMembersToBeCreatedInSubclassesOfDeclaringClass()));
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_check_references, membersToPush.length);
for (IMember member : membersToPush) {
String label= createLabel(member);
for (IJavaElement element : getReferencingElementsFromSameClass(member, subMon.newChild(1), result)) {
if (movedMembers.contains(element))
continue;
if (!(element instanceof IMember))
continue;
IMember referencingMember= (IMember) element;
Object[] keys= { label, createLabel(referencingMember) };
String msg= Messages.format(RefactoringCoreMessages.PushDownRefactoring_referenced, keys);
result.addError(msg, JavaStatusContext.create(referencingMember));
}
}
return result;
}
public void computeAdditionalRequiredMembersToPushDown(IProgressMonitor monitor) throws JavaModelException {
List<IMember> list= Arrays.asList(getAdditionalRequiredMembers(monitor));
for (MemberActionInfo info : fMemberInfos) {
if (list.contains(info.getMember()))
info.setAction(MemberActionInfo.PUSH_DOWN_ACTION);
}
}
/**
* AST node visitor which performs This check and removes qualifier if needed
*/
public static class ThisVisitor extends ASTVisitor {
private final IType fDeclaringType;
private final ASTRewrite fRewrite;
public ThisVisitor(final ASTRewrite rewrite, final IType declaringType) {
Assert.isNotNull(rewrite);
Assert.isNotNull(declaringType);
fRewrite= rewrite;
fDeclaringType= declaringType;
}
@Override
public final boolean visit(ThisExpression node) {
Name q= node.getQualifier();
if (q == null) {
return false;
}
String qName= q.getFullyQualifiedName();
if (qName.equals(fDeclaringType.getElementName()) || qName.equals(fDeclaringType.getFullyQualifiedName())) {
fRewrite.set(node, ThisExpression.QUALIFIER_PROPERTY, null, null);
}
return false;
}
}
private void copyBodyOfPushedDownMethod(ASTRewrite targetRewrite, IMethod method, MethodDeclaration oldMethod, MethodDeclaration newMethod, TypeVariableMaplet[] mapping) throws JavaModelException {
Block body= oldMethod.getBody();
if (body == null) {
newMethod.setBody(null);
return;
}
try {
final IDocument document= new Document(method.getCompilationUnit().getBuffer().getContents());
final ASTRewrite rewriter= ASTRewrite.create(body.getAST());
final ITrackedNodePosition position= rewriter.track(body);
body.accept(new TypeVariableMapper(rewriter, mapping));
body.accept(new ThisVisitor(rewriter, fCachedDeclaringType));
rewriter.rewriteAST(document, getDeclaringType().getCompilationUnit().getOptions(true)).apply(document, TextEdit.NONE);
String content= document.get(position.getStartPosition(), position.getLength());
String[] lines= Strings.convertIntoLines(content);
Strings.trimIndentation(lines, method.getCompilationUnit(), false);
content= Strings.concatenate(lines, StubUtility.getLineDelimiterUsed(method));
newMethod.setBody((Block) targetRewrite.createStringPlaceholder(content, ASTNode.BLOCK));
} catch (MalformedTreeException | BadLocationException exception) {
JavaManipulationPlugin.log(exception);
}
}
private void copyMembers(Collection<MemberVisibilityAdjustor> adjustors, Map<IMember, IncomingMemberVisibilityAdjustment> adjustments, Map<ICompilationUnit, CompilationUnitRewrite> rewrites, RefactoringStatus status, MemberActionInfo[] infos, IType[] destinations, CompilationUnitRewrite sourceRewriter, CompilationUnitRewrite unitRewriter, IProgressMonitor monitor) throws JavaModelException {
try {
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, destinations.length * infos.length);
IType type= null;
TypeVariableMaplet[] mapping= null;
for (IType destination : destinations) {
type= destination;
mapping= TypeVariableUtil.superTypeToInheritedType(getDeclaringType(), type);
if (unitRewriter.getCu().equals(type.getCompilationUnit())) {
IMember member= null;
MemberVisibilityAdjustor adjustor= null;
AbstractTypeDeclaration declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(type, unitRewriter.getRoot());
ImportRewriteContext context= new ContextSensitiveImportRewriteContext(declaration, unitRewriter.getImportRewrite());
for (int offset= infos.length - 1; offset >= 0; offset--) {
member= infos[offset].getMember();
adjustor= new MemberVisibilityAdjustor(type, member);
if (infos[offset].isNewMethodToBeDeclaredAbstract())
adjustor.setIncoming(false);
adjustor.setRewrite(sourceRewriter.getASTRewrite(), sourceRewriter.getRoot());
adjustor.setRewrites(rewrites);
// TW: set to error if bug 78387 is fixed
adjustor.setFailureSeverity(RefactoringStatus.WARNING);
adjustor.setStatus(status);
adjustor.setAdjustments(adjustments);
adjustor.adjustVisibility(subMon.newChild(1));
adjustments.remove(member);
adjustors.add(adjustor);
status.merge(checkProjectCompliance(getCompilationUnitRewrite(rewrites, getDeclaringType().getCompilationUnit()), type, new IMember[] {infos[offset].getMember()}));
if (infos[offset].isFieldInfo()) {
final VariableDeclarationFragment oldField= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) infos[offset].getMember(), sourceRewriter.getRoot());
if (oldField != null) {
FieldDeclaration newField= createNewFieldDeclarationNode(infos[offset], sourceRewriter.getRoot(), mapping, unitRewriter.getASTRewrite(), oldField);
unitRewriter.getASTRewrite().getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newField, BodyDeclarationRewrite.getInsertionIndex(newField, declaration.bodyDeclarations()), unitRewriter.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PUSH_DOWN));
ImportRewriteUtil.addImports(unitRewriter, context, oldField.getParent(), new HashMap<>(), new HashMap<>(), false);
}
} else {
final MethodDeclaration oldMethod= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) infos[offset].getMember(), sourceRewriter.getRoot());
if (oldMethod != null) {
MethodDeclaration newMethod= createNewMethodDeclarationNode(infos[offset], mapping, unitRewriter, oldMethod);
unitRewriter.getASTRewrite().getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newMethod, BodyDeclarationRewrite.getInsertionIndex(newMethod, declaration.bodyDeclarations()), unitRewriter.createCategorizedGroupDescription(RefactoringCoreMessages.HierarchyRefactoring_add_member, SET_PUSH_DOWN));
ImportRewriteUtil.addImports(unitRewriter, context, oldMethod, new HashMap<>(), new HashMap<>(), false);
}
}
}
}
}
} finally {
monitor.done();
}
}
@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
try {
final Map<String, String> arguments= new HashMap<>();
String project= null;
final IType declaring= getDeclaringType();
final IJavaProject javaProject= declaring.getJavaProject();
if (javaProject != null)
project= javaProject.getElementName();
int flags= JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE;
try {
if (declaring.isLocal() || declaring.isAnonymous())
flags|= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
} catch (JavaModelException exception) {
JavaManipulationPlugin.log(exception);
}
final String description= fMembersToMove.length == 1 ? Messages.format(RefactoringCoreMessages.PushDownRefactoring_descriptor_description_short_multi, BasicElementLabels.getJavaElementName(fMembersToMove[0].getElementName())) : RefactoringCoreMessages.PushDownRefactoring_descriptor_description_short;
final String header= fMembersToMove.length == 1 ? Messages.format(RefactoringCoreMessages.PushDownRefactoring_descriptor_description_full, new String[] { JavaElementLabelsCore.getElementLabel(fMembersToMove[0], JavaElementLabelsCore.ALL_FULLY_QUALIFIED), JavaElementLabelsCore.getElementLabel(declaring, JavaElementLabelsCore.ALL_FULLY_QUALIFIED) }) : Messages.format(RefactoringCoreMessages.PushDownRefactoring_descriptor_description, new String[] { JavaElementLabelsCore.getElementLabel(declaring, JavaElementLabelsCore.ALL_FULLY_QUALIFIED) });
final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header);
final String[] settings= new String[fMembersToMove.length];
for (int index= 0; index < settings.length; index++)
settings[index]= JavaElementLabelsCore.getElementLabel(fMembersToMove[index], JavaElementLabelsCore.ALL_FULLY_QUALIFIED);
comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.PushDownRefactoring_pushed_members_pattern, settings));
addSuperTypeSettings(comment, true);
final PushDownDescriptor descriptor= RefactoringSignatureDescriptorFactory.createPushDownDescriptor(project, description, comment.asString(), arguments, flags);
if (fCachedDeclaringType != null)
arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCachedDeclaringType));
for (int index= 0; index < fMembersToMove.length; index++) {
arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (index + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fMembersToMove[index]));
for (MemberActionInfo memberInfo : fMemberInfos) {
if (memberInfo.getMember().equals(fMembersToMove[index])) {
switch (memberInfo.getAction()) {
case MemberActionInfo.PUSH_ABSTRACT_ACTION:
arguments.put(ATTRIBUTE_ABSTRACT + (index + 1), Boolean.TRUE.toString());
break;
case MemberActionInfo.PUSH_DOWN_ACTION:
arguments.put(ATTRIBUTE_PUSH + (index + 1), Boolean.TRUE.toString());
break;
}
}
}
}
return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.PushDownRefactoring_change_name, fChangeManager.getAllChanges());
} finally {
pm.done();
clearCaches();
}
}
private TextEditBasedChangeManager createChangeManager(final IProgressMonitor monitor, final RefactoringStatus status) throws CoreException {
Assert.isNotNull(monitor);
Assert.isNotNull(status);
try {
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, 6);
final ICompilationUnit source= getDeclaringType().getCompilationUnit();
final CompilationUnitRewrite sourceRewriter= new CompilationUnitRewrite(source);
final Map<ICompilationUnit, CompilationUnitRewrite> rewrites= new HashMap<>(2);
rewrites.put(source, sourceRewriter);
IType[] types= getHierarchyOfDeclaringClass(subMon.newChild(1)).getSubclasses(getDeclaringType());
final Set<ICompilationUnit> result= new HashSet<>(types.length + 1);
for (IType type : types) {
ICompilationUnit cu= type.getCompilationUnit();
if (cu != null) { // subclasses can be in binaries
result.add(cu);
}
}
result.add(source);
final Map<IMember, IncomingMemberVisibilityAdjustment> adjustments= new HashMap<>();
final List<MemberVisibilityAdjustor> adjustors= new ArrayList<>();
CompilationUnitRewrite rewrite= null;
{
final SubMonitor sub= SubMonitor.convert(subMon.newChild(4), result.size() * 4);
for (ICompilationUnit unit : result) {
rewrite= getCompilationUnitRewrite(rewrites, unit);
if (unit.equals(sourceRewriter.getCu())) {
final AbstractTypeDeclaration declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(getDeclaringType(), rewrite.getRoot());
if (!JdtFlags.isAbstract(getDeclaringType()) && getAbstractDeclarationInfos().length != 0)
ModifierRewrite.create(rewrite.getASTRewrite(), declaration).setModifiers((Modifier.ABSTRACT | declaration.getModifiers()), rewrite.createCategorizedGroupDescription(RefactoringCoreMessages.PushDownRefactoring_make_abstract, SET_PUSH_DOWN));
deleteDeclarationNodes(false, rewrite, Arrays.asList(getDeletableMembers()), SET_PUSH_DOWN);
for (MemberActionInfo method : getAbstractDeclarationInfos()) {
declareMethodAbstract(method, sourceRewriter, rewrite);
}
}
final IMember[] members= getAbstractMembers(getAbstractDestinations(sub.newChild(1)));
final IType[] classes= new IType[members.length];
for (int offset= 0; offset < members.length; offset++) {
classes[offset]= (IType) members[offset];
}
copyMembers(adjustors, adjustments, rewrites, status, getAbstractMemberInfos(), classes, sourceRewriter, rewrite, sub.newChild(1));
IType[] abstractDestinations= getAbstractDestinations(sub.newChild(1));
copyMembers(adjustors, adjustments, rewrites, status, getAffectedMemberInfos(), abstractDestinations, sourceRewriter, rewrite, sub.newChild(1));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
}
removeOverrideAnnotation(rewrites);
}
if (!adjustors.isEmpty() && !adjustments.isEmpty()) {
final MemberVisibilityAdjustor adjustor= adjustors.get(0);
adjustor.rewriteVisibility(subMon.newChild(1));
}
final TextEditBasedChangeManager manager= new TextEditBasedChangeManager();
for (Map.Entry<ICompilationUnit, CompilationUnitRewrite> entry : rewrites.entrySet()) {
ICompilationUnit unit= entry.getKey();
rewrite= entry.getValue();
if (rewrite != null)
manager.manage(unit, rewrite.createChange(true));
}
return manager;
} finally {
monitor.done();
}
}
/*
* Check if target method in subclass has '@Override' annotation, and if so removes it.
*/
private void removeOverrideAnnotation(Map<ICompilationUnit, CompilationUnitRewrite> rewrites) throws JavaModelException {
IType[] subclasses= fCachedClassHierarchy.getSubclasses(fCachedDeclaringType);
for (IMember memberActionInfo : fMembersToMove) {
if (memberActionInfo.getElementType() != IJavaElement.METHOD) {
continue;
}
IMethod method= (IMethod) memberActionInfo;
nextClass: for (IType iType : subclasses) {
for (IMethod iMethod : iType.getMethods()) {
if (iMethod.isSimilar(method)) {
CompilationUnitRewrite rewrite= getCompilationUnitRewrite(rewrites, iType.getCompilationUnit());
MethodDeclaration methodDeclarationNode= ASTNodeSearchUtil.getMethodDeclarationNode(iMethod, rewrite.getRoot());
if (methodDeclarationNode == null) {
continue;
}
ListRewrite listRewrite= rewrite.getASTRewrite().getListRewrite(methodDeclarationNode, MethodDeclaration.MODIFIERS2_PROPERTY);
for (IExtendedModifier iExtendedModifier : (List<IExtendedModifier>) methodDeclarationNode.modifiers()) {
if (iExtendedModifier.isAnnotation() &&
"Override".equals(((Annotation) iExtendedModifier).getTypeName().getFullyQualifiedName())) { //$NON-NLS-1$
listRewrite.remove((Annotation) iExtendedModifier, null);
continue nextClass; // resume checking with next target class
}
}
}
}
}
}
}
private FieldDeclaration createNewFieldDeclarationNode(MemberActionInfo info, CompilationUnit declaringCuNode, TypeVariableMaplet[] mapping, ASTRewrite rewrite, VariableDeclarationFragment oldFieldFragment) throws JavaModelException {
Assert.isTrue(info.isFieldInfo());
IField field= (IField) info.getMember();
AST ast= rewrite.getAST();
VariableDeclarationFragment newFragment= ast.newVariableDeclarationFragment();
copyExtraDimensions(oldFieldFragment, newFragment);
Expression initializer= oldFieldFragment.getInitializer();
if (initializer != null) {
Expression newInitializer= null;
if (mapping.length > 0)
newInitializer= createPlaceholderForExpression(initializer, field.getCompilationUnit(), mapping, rewrite);
else
newInitializer= createPlaceholderForExpression(initializer, field.getCompilationUnit(), rewrite);
newFragment.setInitializer(newInitializer);
}
newFragment.setName(ast.newSimpleName(oldFieldFragment.getName().getIdentifier()));
FieldDeclaration newField= ast.newFieldDeclaration(newFragment);
FieldDeclaration oldField= ASTNodeSearchUtil.getFieldDeclarationNode(field, declaringCuNode);
if (info.copyJavadocToCopiesInSubclasses())
copyJavadocNode(rewrite, oldField, newField);
copyAnnotations(oldField, newField);
newField.modifiers().addAll(ASTNodeFactory.newModifiers(ast, info.getNewModifiersForCopyInSubclass(oldField.getModifiers())));
Type oldType= oldField.getType();
ICompilationUnit cu= field.getCompilationUnit();
Type newType= null;
if (mapping.length > 0) {
newType= createPlaceholderForType(oldType, cu, mapping, rewrite);
} else
newType= createPlaceholderForType(oldType, cu, rewrite);
newField.setType(newType);
return newField;
}
private MethodDeclaration createNewMethodDeclarationNode(MemberActionInfo info, TypeVariableMaplet[] mapping, CompilationUnitRewrite rewriter, MethodDeclaration oldMethod) throws JavaModelException {
Assert.isTrue(!info.isFieldInfo());
IMethod method= (IMethod) info.getMember();
ASTRewrite rewrite= rewriter.getASTRewrite();
AST ast= rewrite.getAST();
MethodDeclaration newMethod= ast.newMethodDeclaration();
copyBodyOfPushedDownMethod(rewrite, method, oldMethod, newMethod, mapping);
newMethod.setConstructor(oldMethod.isConstructor());
copyExtraDimensions(oldMethod, newMethod);
if (info.copyJavadocToCopiesInSubclasses())
copyJavadocNode(rewrite, oldMethod, newMethod);
final IJavaProject project= rewriter.getCu().getJavaProject();
if (info.isNewMethodToBeDeclaredAbstract() && JavaModelUtil.is50OrHigher(project) && JavaPreferencesSettings.getCodeGenerationSettings(rewriter.getCu()).overrideAnnotation) {
final MarkerAnnotation annotation= ast.newMarkerAnnotation();
annotation.setTypeName(ast.newSimpleName("Override")); //$NON-NLS-1$
newMethod.modifiers().add(annotation);
}
copyAnnotations(oldMethod, newMethod);
newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, info.getNewModifiersForCopyInSubclass(oldMethod.getModifiers())));
newMethod.setName(ast.newSimpleName(oldMethod.getName().getIdentifier()));
copyReturnType(rewrite, method.getCompilationUnit(), oldMethod, newMethod, mapping);
copyParameters(rewrite, method.getCompilationUnit(), oldMethod, newMethod, mapping);
copyThrownExceptions(oldMethod, newMethod);
copyTypeParameters(oldMethod, newMethod);
return newMethod;
}
private void declareMethodAbstract(MemberActionInfo info, CompilationUnitRewrite sourceRewrite, CompilationUnitRewrite unitRewrite) throws JavaModelException {
Assert.isTrue(!info.isFieldInfo());
IMethod method= (IMethod) info.getMember();
if (JdtFlags.isAbstract(method))
return;
final MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(method, sourceRewrite.getRoot());
unitRewrite.getASTRewrite().remove(declaration.getBody(), null);
sourceRewrite.getImportRemover().registerRemovedNode(declaration.getBody());
ModifierRewrite.create(unitRewrite.getASTRewrite(), declaration).setModifiers(info.getNewModifiersForOriginal(declaration.getModifiers()), null);
}
private MemberActionInfo[] getAbstractDeclarationInfos() throws JavaModelException {
List<MemberActionInfo> result= new ArrayList<>(fMemberInfos.length);
for (MemberActionInfo info : fMemberInfos) {
if (info.isNewMethodToBeDeclaredAbstract())
result.add(info);
}
return result.toArray(new MemberActionInfo[result.size()]);
}
private IType[] getAbstractDestinations(IProgressMonitor monitor) throws JavaModelException {
IType[] allDirectSubclasses= getHierarchyOfDeclaringClass(monitor).getSubclasses(getDeclaringType());
List<IType> result= new ArrayList<>(allDirectSubclasses.length);
for (IType subclass : allDirectSubclasses) {
if (subclass.exists() && !subclass.isBinary() && !subclass.isReadOnly() && subclass.getCompilationUnit() != null && subclass.isStructureKnown())
result.add(subclass);
}
return result.toArray(new IType[result.size()]);
}
private MemberActionInfo[] getAbstractMemberInfos() throws JavaModelException {
List<MemberActionInfo> result= new ArrayList<>(fMemberInfos.length);
for (MemberActionInfo info : fMemberInfos) {
if (info.isToBeCreatedInSubclassesOfDeclaringClass() && JdtFlags.isAbstract(info.getMember()))
result.add(info);
}
return result.toArray(new MemberActionInfo[result.size()]);
}
public IMember[] getAdditionalRequiredMembers(IProgressMonitor monitor) throws JavaModelException {
IMember[] members= MemberActionInfo.getMembers(getInfosForMembersToBeCreatedInSubclassesOfDeclaringClass());
List<IMember> queue= new ArrayList<>(Arrays.asList(members));
SubMonitor submon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_calculating_required, queue.size());
if (queue.isEmpty())
return new IMember[0];
int i= 0;
IMember current;
do {