-
Notifications
You must be signed in to change notification settings - Fork 86
/
MemberVisibilityAdjustor.java
1317 lines (1227 loc) · 54.5 KB
/
MemberVisibilityAdjustor.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) 2000, 2019 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 - copied to jdt.core.manipulation
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.structure;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup;
import org.eclipse.ltk.core.refactoring.GroupCategory;
import org.eclipse.ltk.core.refactoring.GroupCategorySet;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IInitializer;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
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.corext.dom.ModifierRewrite;
import org.eclipse.jdt.internal.corext.dom.VariableDeclarationRewrite;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringScopeFactory;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2;
import org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup;
import org.eclipse.jdt.internal.corext.refactoring.util.JavaStatusContext;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.LRUMap;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.corext.util.SearchUtils;
import org.eclipse.jdt.internal.ui.util.Progress;
/**
* Helper class to adjust the visibilities of members with respect to a reference element.
*
* @since 3.1
*/
public final class MemberVisibilityAdjustor {
/**
* The visibility group category set.
*
* @since 3.2
*/
public static final GroupCategorySet SET_VISIBILITY_ADJUSTMENTS= new GroupCategorySet(new GroupCategory("org.eclipse.jdt.internal.corext.visibilityAdjustments", //$NON-NLS-1$
RefactoringCoreMessages.MemberVisibilityAdjustor_adjustments_name, RefactoringCoreMessages.MemberVisibilityAdjustor_adjustments_description));
/** Description of a member visibility adjustment */
public static class IncomingMemberVisibilityAdjustment implements IVisibilityAdjustment {
/** The keyword representing the adjusted visibility */
protected final ModifierKeyword fKeyword;
/** The member whose visibility has been adjusted */
protected final IMember fMember;
/** Does the visibility adjustment need rewriting? */
protected boolean fNeedsRewriting= true;
/** The associated refactoring status */
protected final RefactoringStatus fRefactoringStatus;
/**
* Creates a new incoming member visibility adjustment.
*
* @param member the member which is adjusted
* @param keyword the keyword representing the adjusted visibility
* @param status the associated status, or <code>null</code>
*/
public IncomingMemberVisibilityAdjustment(final IMember member, final ModifierKeyword keyword, final RefactoringStatus status) {
Assert.isNotNull(member);
Assert.isTrue(!(member instanceof IInitializer));
Assert.isTrue(isVisibilityKeyword(keyword));
fMember= member;
fKeyword= keyword;
fRefactoringStatus= status;
}
/**
* Returns the visibility keyword.
*
* @return the visibility keyword
*/
public final ModifierKeyword getKeyword() {
return fKeyword;
}
/**
* Returns the adjusted member.
*
* @return the adjusted member
*/
public final IMember getMember() {
return fMember;
}
/**
* Returns the associated refactoring status.
*
* @return the associated refactoring status
*/
public final RefactoringStatus getStatus() {
return fRefactoringStatus;
}
/**
* Does the visibility adjustment need rewriting?
*
* @return <code>true</code> if it needs rewriting, <code>false</code> otherwise
*/
public final boolean needsRewriting() {
return fNeedsRewriting;
}
/**
* Rewrites the visibility adjustment.
*
* @param adjustor the java element visibility adjustor
* @param rewrite the AST rewrite to use
* @param root the root of the AST used in the rewrite
* @param group the text edit group description to use, or <code>null</code>
* @param status the refactoring status, or <code>null</code>
* @throws JavaModelException if an error occurs
*/
protected final void rewriteVisibility(final MemberVisibilityAdjustor adjustor, final ASTRewrite rewrite, final CompilationUnit root, final CategorizedTextEditGroup group, final RefactoringStatus status) throws JavaModelException {
Assert.isNotNull(adjustor);
Assert.isNotNull(rewrite);
Assert.isNotNull(root);
final int visibility= fKeyword != null ? fKeyword.toFlagValue() : Modifier.NONE;
if (fMember instanceof IField && !Flags.isEnum(fMember.getFlags())) {
final VariableDeclarationFragment fragment= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) fMember, root);
final FieldDeclaration declaration= (FieldDeclaration) fragment.getParent();
VariableDeclarationFragment[] fragmentsToChange= new VariableDeclarationFragment[] { fragment };
VariableDeclarationRewrite.rewriteModifiers(declaration, fragmentsToChange, visibility, ModifierRewrite.VISIBILITY_MODIFIERS, rewrite, group);
if (status != null)
adjustor.fStatus.merge(status);
} else if (fMember != null) {
final BodyDeclaration declaration= ASTNodeSearchUtil.getBodyDeclarationNode(fMember, root);
if (declaration != null) {
ModifierRewrite.create(rewrite, declaration).setVisibility(visibility, group);
if (status != null)
adjustor.fStatus.merge(status);
}
}
}
/*
* @see org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IVisibilityAdjustment#rewriteVisibility(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor, org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
public void rewriteVisibility(final MemberVisibilityAdjustor adjustor, final IProgressMonitor monitor) throws JavaModelException {
Assert.isNotNull(adjustor);
Assert.isNotNull(monitor);
try {
monitor.beginTask("", 1); //$NON-NLS-1$
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_adjusting);
if (fNeedsRewriting) {
if (adjustor.fRewrite != null && adjustor.fRoot != null)
rewriteVisibility(adjustor, adjustor.fRewrite, adjustor.fRoot, null, fRefactoringStatus);
else {
final CompilationUnitRewrite rewrite= adjustor.getCompilationUnitRewrite(fMember.getCompilationUnit());
rewriteVisibility(adjustor, rewrite.getASTRewrite(), rewrite.getRoot(), rewrite.createCategorizedGroupDescription(Messages.format(RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility, getLabel(getKeyword())), SET_VISIBILITY_ADJUSTMENTS), fRefactoringStatus);
}
} else if (fRefactoringStatus != null)
adjustor.fStatus.merge(fRefactoringStatus);
monitor.worked(1);
} finally {
monitor.done();
}
}
/**
* Determines whether the visibility adjustment needs rewriting.
*
* @param rewriting <code>true</code> if it needs rewriting, <code>false</code> otherwise
*/
public final void setNeedsRewriting(final boolean rewriting) {
fNeedsRewriting= rewriting;
}
}
/** Interface for visibility adjustments */
public interface IVisibilityAdjustment {
/**
* Rewrites the visibility adjustment.
*
* @param adjustor the java element visibility adjustor
* @param monitor the progress monitor to use
* @throws JavaModelException if an error occurs
*/
void rewriteVisibility(MemberVisibilityAdjustor adjustor, IProgressMonitor monitor) throws JavaModelException;
}
/** Description of an outgoing member visibility adjustment */
public static class OutgoingMemberVisibilityAdjustment extends IncomingMemberVisibilityAdjustment {
/**
* Creates a new outgoing member visibility adjustment.
*
* @param member the member which is adjusted
* @param keyword the keyword representing the adjusted visibility
* @param status the associated status
*/
public OutgoingMemberVisibilityAdjustment(final IMember member, final ModifierKeyword keyword, final RefactoringStatus status) {
super(member, keyword, status);
}
/*
* @see org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IVisibilityAdjustment#rewriteVisibility(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor, org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
public void rewriteVisibility(final MemberVisibilityAdjustor adjustor, final IProgressMonitor monitor) throws JavaModelException {
Assert.isNotNull(adjustor);
Assert.isNotNull(monitor);
try {
monitor.beginTask("", 1); //$NON-NLS-1$
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_adjusting);
if (fNeedsRewriting) {
final CompilationUnitRewrite rewrite= adjustor.getCompilationUnitRewrite(fMember.getCompilationUnit());
rewriteVisibility(adjustor, rewrite.getASTRewrite(), rewrite.getRoot(), rewrite.createCategorizedGroupDescription(Messages.format(RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility, getLabel(getKeyword())), SET_VISIBILITY_ADJUSTMENTS), fRefactoringStatus);
}
monitor.worked(1);
} finally {
monitor.done();
}
}
}
/**
* Returns the label for the specified java element.
*
* @param element the element to get the label for
* @return the label for the element
*/
public static String getLabel(final IJavaElement element) {
Assert.isNotNull(element);
return JavaElementLabelsCore.getElementLabel(element, JavaElementLabelsCore.ALL_FULLY_QUALIFIED | JavaElementLabelsCore.ALL_DEFAULT);
}
/**
* Returns the label for the specified visibility keyword.
*
* @param keyword the keyword to get the label for, or <code>null</code> for default visibility
* @return the label for the keyword
*/
public static String getLabel(final ModifierKeyword keyword) {
Assert.isTrue(isVisibilityKeyword(keyword));
if (keyword == null)
return RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_default;
else if (ModifierKeyword.PUBLIC_KEYWORD.equals(keyword))
return RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_public;
else if (ModifierKeyword.PROTECTED_KEYWORD.equals(keyword))
return RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_protected;
else
return RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_private;
}
/**
* Returns the message string for the specified member.
*
* @param member the member to get the string for
* @return the string for the member
*/
public static String getMessage(final IMember member) {
Assert.isTrue(member instanceof IType || member instanceof IMethod || member instanceof IField);
if (member instanceof IType)
return RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_type_warning;
else if (member instanceof IMethod)
return RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_method_warning;
else
return RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_field_warning;
}
/**
* Do the specified modifiers represent a lower visibility than the required threshold?
*
* @param modifiers the modifiers to test
* @param threshold the visibility threshold to compare with
* @return <code>true</code> if the visibility is lower than required, <code>false</code> otherwise
*/
public static boolean hasLowerVisibility(final int modifiers, final int threshold) {
if (Modifier.isPrivate(threshold))
return false;
else if (Modifier.isPublic(threshold))
return !Modifier.isPublic(modifiers);
else if (Modifier.isProtected(threshold))
return !Modifier.isProtected(modifiers) && !Modifier.isPublic(modifiers);
else
return Modifier.isPrivate(modifiers);
}
/**
* Does the specified modifier keyword represent a lower visibility than the required threshold?
*
* @param keyword the visibility keyword to test, or <code>null</code> for default visibility
* @param threshold the visibility threshold keyword to compare with, or <code>null</code> to compare with default visibility
* @return <code>true</code> if the visibility is lower than required, <code>false</code> otherwise
*/
public static boolean hasLowerVisibility(final ModifierKeyword keyword, final ModifierKeyword threshold) {
Assert.isTrue(isVisibilityKeyword(keyword));
Assert.isTrue(isVisibilityKeyword(threshold));
return hasLowerVisibility(keyword != null ? keyword.toFlagValue() : Modifier.NONE, threshold != null ? threshold.toFlagValue() : Modifier.NONE);
}
/**
* Is the specified severity a refactoring status severity?
*
* @param severity the severity to test
* @return <code>true</code> if it is a refactoring status severity, <code>false</code> otherwise
*/
private static boolean isStatusSeverity(final int severity) {
return severity == RefactoringStatus.ERROR || severity == RefactoringStatus.FATAL || severity == RefactoringStatus.INFO || severity == RefactoringStatus.OK || severity == RefactoringStatus.WARNING;
}
/**
* Is the specified modifier keyword a visibility keyword?
*
* @param keyword the keyword to test, or <code>null</code>
* @return <code>true</code> if it is a visibility keyword, <code>false</code> otherwise
*/
private static boolean isVisibilityKeyword(final ModifierKeyword keyword) {
return keyword == null || ModifierKeyword.PUBLIC_KEYWORD.equals(keyword) || ModifierKeyword.PROTECTED_KEYWORD.equals(keyword) || ModifierKeyword.PRIVATE_KEYWORD.equals(keyword);
}
/**
* Is the specified modifier a visibility modifier?
*
* @param modifier the keyword to test
* @return <code>true</code> if it is a visibility modifier, <code>false</code> otherwise
*/
private static boolean isVisibilityModifier(final int modifier) {
return modifier == Modifier.NONE || modifier == Modifier.PUBLIC || modifier == Modifier.PROTECTED || modifier == Modifier.PRIVATE;
}
/**
* Converts a given modifier keyword into a visibility flag.
*
* @param keyword the keyword to convert
* @return the visibility flag
*/
private static int keywordToVisibility(final ModifierKeyword keyword) {
int visibility= 0;
if (keyword == ModifierKeyword.PUBLIC_KEYWORD)
visibility= Flags.AccPublic;
else if (keyword == ModifierKeyword.PRIVATE_KEYWORD)
visibility= Flags.AccPrivate;
else if (keyword == ModifierKeyword.PROTECTED_KEYWORD)
visibility= Flags.AccProtected;
return visibility;
}
/**
* Does the specified member need further visibility adjustment?
*
* @param member the member to test
* @param threshold the visibility threshold to test for
* @param adjustments the map of members to visibility adjustments
* @return <code>true</code> if the member needs further adjustment, <code>false</code> otherwise
*/
public static boolean needsVisibilityAdjustments(final IMember member, final int threshold, final Map<IMember, IncomingMemberVisibilityAdjustment> adjustments) {
Assert.isNotNull(member);
Assert.isTrue(isVisibilityModifier(threshold));
Assert.isNotNull(adjustments);
final IncomingMemberVisibilityAdjustment adjustment= adjustments.get(member);
if (adjustment != null) {
final ModifierKeyword keyword= adjustment.getKeyword();
return hasLowerVisibility(keyword == null ? Modifier.NONE : keyword.toFlagValue(), threshold);
}
return true;
}
/**
* Does the specified member need further visibility adjustment?
*
* @param member the member to test
* @param threshold the visibility threshold to test for, or <code>null</code> for default visibility
* @param adjustments the map of members to visibility adjustments
* @return <code>true</code> if the member needs further adjustment, <code>false</code> otherwise
*/
public static boolean needsVisibilityAdjustments(final IMember member, final ModifierKeyword threshold, final Map<IMember, IncomingMemberVisibilityAdjustment> adjustments) {
Assert.isNotNull(member);
Assert.isNotNull(adjustments);
final IncomingMemberVisibilityAdjustment adjustment= adjustments.get(member);
if (adjustment != null)
return hasLowerVisibility(adjustment.getKeyword(), threshold);
return true;
}
/** The map of members to visibility adjustments */
private Map<IMember, IncomingMemberVisibilityAdjustment> fAdjustments= new LinkedHashMap<>(); // LinkedHashMap to preserve order of generated warnings
/** Should incoming references be adjusted? */
private boolean fIncoming= true;
/** Should outgoing references be adjusted? */
private boolean fOutgoing= true;
/** The referenced element causing the visibility adjustment */
private final IMember fReferenced;
/** The referencing java element */
private final IJavaElement fReferencing;
/** The AST rewrite to use for reference visibility adjustments, or <code>null</code> to use a compilation unit rewrite */
private ASTRewrite fRewrite= null;
/** The map of compilation units to compilation unit rewrites */
private Map<ICompilationUnit, CompilationUnitRewrite> fRewrites= new HashMap<>(3);
/** The root node of the AST rewrite for reference visibility adjustments, or <code>null</code> to use a compilation unit rewrite */
private CompilationUnit fRoot= null;
/** The incoming search scope */
private IJavaSearchScope fScope;
/** The status of the visibility adjustment */
private RefactoringStatus fStatus= new RefactoringStatus();
/** The type hierarchy cache */
private final Map<IType, ITypeHierarchy> fTypeHierarchies= new LRUMap<>(10);
/** The visibility message severity */
private int fVisibilitySeverity= RefactoringStatus.WARNING;
/** The working copy owner, or <code>null</code> to use none */
private WorkingCopyOwner fOwner= null;
/**
* Creates a new java element visibility adjustor.
*
* @param referencing the referencing element used to compute the visibility
* @param referenced the referenced member which causes the visibility changes
*/
public MemberVisibilityAdjustor(final IJavaElement referencing, final IMember referenced) {
Assert.isTrue(!(referenced instanceof IInitializer));
Assert.isTrue(referencing instanceof ICompilationUnit || referencing instanceof IType || referencing instanceof IPackageFragment);
fScope= RefactoringScopeFactory.createReferencedScope(new IJavaElement[] { referenced}, IJavaSearchScope.REFERENCED_PROJECTS | IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES);
fReferencing= referencing;
fReferenced= referenced;
}
/**
* Adjusts the visibility of the specified member.
*
* @param element the "source" point from which to calculate the visibility
* @param referencedMovedElement the moved element which may be adjusted in visibility
* @param monitor the progress monitor to use
* @throws JavaModelException if the visibility adjustment could not be computed
*/
private void adjustIncomingVisibility(final IJavaElement element, IMember referencedMovedElement, final IProgressMonitor monitor) throws JavaModelException {
final ModifierKeyword threshold= getVisibilityThreshold(element, referencedMovedElement, monitor);
int flags= referencedMovedElement.getFlags();
IType declaring= referencedMovedElement.getDeclaringType();
if (declaring != null && declaring.isInterface() || referencedMovedElement instanceof IField && Flags.isEnum(referencedMovedElement.getFlags()))
return;
if (hasLowerVisibility(flags, threshold == null ? Modifier.NONE : threshold.toFlagValue()) && needsVisibilityAdjustment(referencedMovedElement, threshold))
fAdjustments.put(referencedMovedElement, new IncomingMemberVisibilityAdjustment(referencedMovedElement, threshold, RefactoringStatus.createStatus(fVisibilitySeverity, Messages.format(getMessage(referencedMovedElement), new String[] { getLabel(referencedMovedElement), getLabel(threshold)}), JavaStatusContext.create(referencedMovedElement), null, RefactoringStatusEntry.NO_CODE, null)));
}
/**
* Check whether anyone accesses the members of the moved type from the
* outside. Those may need to have their visibility adjusted.
* @param member the member
* @param monitor the progress monitor to use
* @throws JavaModelException if an error occurs
*/
private void adjustMemberVisibility(final IMember member, final IProgressMonitor monitor) throws JavaModelException {
if (member instanceof IType) {
// recursively check accessibility of member type's members
final IJavaElement[] typeMembers= ((IType) member).getChildren();
for (IJavaElement typeMember : typeMembers) {
if (! (typeMember instanceof IInitializer))
adjustMemberVisibility((IMember) typeMember, monitor);
}
}
if (member.equals(fReferenced) || Modifier.isPublic(member.getFlags()))
return;
for (SearchResultGroup reference : findReferences(member, monitor)) {
for (SearchMatch searchResult : reference.getSearchResults()) {
final IJavaElement referenceToMember= (IJavaElement) searchResult.getElement();
if (fAdjustments.get(member) == null && referenceToMember instanceof IMember && !isInsideMovedMember(referenceToMember)) {
// check whether the member is still visible from the
// destination. As we are moving a type, the destination is
// a package or another type.
adjustIncomingVisibility(fReferencing, member, Progress.subMonitor(monitor, 1));
}
}
}
}
/**
* Is the specified member inside the moved member?
* @param element the element
* @return <code>true</code> if it is inside, <code>false</code> otherwise
*/
private boolean isInsideMovedMember(final IJavaElement element) {
IJavaElement current= element;
while ((current= current.getParent()) != null)
if (current.equals(fReferenced))
return true;
return false;
}
/**
* Finds references to the specified member.
* @param member the member
* @param monitor the progress monitor to use
* @return the search result groups
* @throws JavaModelException if an error occurs during search
*/
private SearchResultGroup[] findReferences(final IMember member, final IProgressMonitor monitor) throws JavaModelException {
SearchPattern pattern= SearchPattern.createPattern(member, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
if (pattern == null) {
return new SearchResultGroup[0];
}
final RefactoringSearchEngine2 engine= new RefactoringSearchEngine2(pattern);
engine.setOwner(fOwner);
engine.setFiltering(true, true);
engine.setScope(RefactoringScopeFactory.create(member));
engine.searchPattern(Progress.subMonitor(monitor, 1));
return (SearchResultGroup[]) engine.getResults();
}
/**
* Adjusts the visibility of the member based on the incoming references
* represented by the specified search result groups.
*
* If there is at least one reference to the moved element from outside the
* moved element, visibility must be increased such that the moved element
* (fReferenced) is still visible at the target from all references. This
* effectively means that the old element (fReferenced) must be visible from
* the new location (fReferencing).
*
* @param groups the search result groups representing the references
* @param monitor the progress monitor to use
* @throws JavaModelException if the java elements could not be accessed
*/
private void adjustIncomingVisibility(final SearchResultGroup[] groups, final IProgressMonitor monitor) throws JavaModelException {
try {
monitor.beginTask("", groups.length); //$NON-NLS-1$
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
boolean adjusted= false;
for (SearchResultGroup group : groups) {
for (SearchMatch match : group.getSearchResults()) {
final Object element= match.getElement();
if (element instanceof IMember && !isInsideMovedMember((IMember) element)) {
// found one reference which is not inside the moved
// element => adjust visibility of the moved element
adjustIncomingVisibility(fReferencing, fReferenced, monitor);
adjusted= true; // one adjustment is enough
break;
}
}
if (adjusted)
break;
monitor.worked(1);
}
} finally {
monitor.done();
}
}
/**
* Adjusts the visibility of the referenced field found in a compilation unit.
*
* @param field the referenced field to adjust
* @param threshold the visibility threshold, or <code>null</code> for default visibility
* @throws JavaModelException if an error occurs
*/
private void adjustOutgoingVisibility(final IField field, final ModifierKeyword threshold) throws JavaModelException {
Assert.isTrue(!field.isBinary() && !field.isReadOnly());
//bug 100555 (moving inner class to top level class; taking private fields with you)
final IType declaring= field.getDeclaringType();
if (declaring != null && declaring.equals(fReferenced)) return;
if (hasLowerVisibility(field.getFlags(), keywordToVisibility(threshold)) && needsVisibilityAdjustment(field, threshold))
adjustOutgoingVisibility(field, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_field_warning);
}
/**
* Adjusts the visibility of the referenced body declaration.
*
* @param member the member where to adjust the visibility
* @param threshold the visibility keyword representing the required visibility, or <code>null</code> for default visibility
* @param template the message template to use
* @throws JavaModelException if an error occurs
*/
private void adjustOutgoingVisibility(final IMember member, final ModifierKeyword threshold, final String template) throws JavaModelException {
Assert.isTrue(!member.isBinary() && !member.isReadOnly());
boolean adjust= true;
final IType declaring= member.getDeclaringType();
if (declaring != null && (JavaModelUtil.isInterfaceOrAnnotation(declaring)
|| (member instanceof IField) && Flags.isEnum(member.getFlags())
|| declaring.equals(fReferenced)))
adjust= false;
if (adjust && hasLowerVisibility(member.getFlags(), keywordToVisibility(threshold)) && needsVisibilityAdjustment(member, threshold))
fAdjustments.put(member, new OutgoingMemberVisibilityAdjustment(member, threshold, RefactoringStatus.createStatus(fVisibilitySeverity, Messages.format(template, new String[] { JavaElementLabelsCore.getTextLabel(member, JavaElementLabelsCore.M_PARAMETER_TYPES | JavaElementLabelsCore.ALL_FULLY_QUALIFIED), getLabel(threshold)}), JavaStatusContext.create(member), null, RefactoringStatusEntry.NO_CODE, null)));
}
/**
* Adjusts the visibilities of the referenced element from the search match found in a compilation unit.
*
* @param match the search match representing the element declaration
* @param monitor the progress monitor to use
* @throws JavaModelException if the visibility could not be determined
*/
private void adjustOutgoingVisibility(final SearchMatch match, final IProgressMonitor monitor) throws JavaModelException {
final Object element= match.getElement();
if (element instanceof IMember) {
final IMember member= (IMember) element;
if (!member.isBinary() && !member.isReadOnly() && !isInsideMovedMember(member)) {
adjustOutgoingVisibilityChain(member, monitor);
}
}
}
private void adjustOutgoingVisibilityChain(final IMember member, final IProgressMonitor monitor) throws JavaModelException {
if (!Modifier.isPublic(member.getFlags())) {
final ModifierKeyword threshold= computeOutgoingVisibilityThreshold(fReferencing, member, monitor);
if (member instanceof IMethod) {
adjustOutgoingVisibility(member, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_method_warning);
} else if (member instanceof IField) {
adjustOutgoingVisibility((IField) member, threshold);
} else if (member instanceof IType) {
adjustOutgoingVisibility(member, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_type_warning);
}
}
if (member.getDeclaringType() != null)
adjustOutgoingVisibilityChain(member.getDeclaringType(), monitor);
}
/**
* Adjusts the visibilities of the outgoing references from the member represented by the specified search result groups.
*
* @param groups the search result groups representing the references
* @param monitor the progress monitor to us
* @throws JavaModelException if the visibility could not be determined
*/
private void adjustOutgoingVisibility(final SearchResultGroup[] groups, final IProgressMonitor monitor) throws JavaModelException {
try {
monitor.beginTask("", groups.length); //$NON-NLS-1$
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
for (SearchResultGroup group : groups) {
IJavaElement element= JavaCore.create(group.getResource());
if (element instanceof ICompilationUnit) {
for (SearchMatch match : group.getSearchResults())
adjustOutgoingVisibility(match, Progress.subMonitor(monitor, 1));
} // else if (element != null)
// fStatus.merge(RefactoringStatus.createStatus(fFailureSeverity, RefactoringCoreMessages.getFormattedString("MemberVisibilityAdjustor.binary.outgoing.project", new String[] { element.getJavaProject().getElementName(), getLabel(fReferenced)}), null, null, RefactoringStatusEntry.NO_CODE, null)); //$NON-NLS-1$
// else if (group.getResource() != null)
// fStatus.merge(RefactoringStatus.createStatus(fFailureSeverity, RefactoringCoreMessages.getFormattedString("MemberVisibilityAdjustor.binary.outgoing.resource", new String[] { group.getResource().getName(), getLabel(fReferenced)}), null, null, RefactoringStatusEntry.NO_CODE, null)); //$NON-NLS-1$
// TW: enable when bug 78387 is fixed
monitor.worked(1);
}
} finally {
monitor.done();
}
}
/**
* Adjusts the visibilities of the referenced and referencing elements.
*
* @param monitor the progress monitor to use
* @throws JavaModelException if an error occurs during search
*/
public void adjustVisibility(final IProgressMonitor monitor) throws JavaModelException {
try {
monitor.beginTask("", 7); //$NON-NLS-1$
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
final RefactoringSearchEngine2 engine= new RefactoringSearchEngine2(SearchPattern.createPattern(fReferenced, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE));
engine.setScope(fScope);
engine.setStatus(fStatus);
engine.setOwner(fOwner);
if (fIncoming) {
// check calls to the referenced (moved) element, adjust element
// visibility if necessary.
engine.searchPattern(Progress.subMonitor(monitor, 1));
adjustIncomingVisibility((SearchResultGroup[]) engine.getResults(), Progress.subMonitor(monitor, 1));
engine.clearResults();
// If the moved element is a type: Adjust visibility of members
// of the type if they are accessed outside of the moved type
if (fReferenced instanceof IType) {
final IType type= (IType) fReferenced;
adjustMemberVisibility(type, Progress.subMonitor(monitor, 1));
}
}
if (fOutgoing) {
/*
* Search for the types, fields, and methods which
* are called/acted upon inside the referenced element (the one
* to be moved) and assure that they are also visible from
* within the referencing element (the destination type (or
* package if move to new type)).
*/
engine.searchReferencedTypes(fReferenced, Progress.subMonitorSupressed(monitor, 1));
engine.searchReferencedFields(fReferenced, Progress.subMonitorSupressed(monitor, 1));
engine.searchReferencedMethods(fReferenced, Progress.subMonitorSupressed(monitor, 1));
adjustOutgoingVisibility((SearchResultGroup[]) engine.getResults(), Progress.subMonitor(monitor, 1));
}
} finally {
monitor.done();
}
}
/**
* Computes the visibility threshold for the referenced element.
*
* @param referencing the referencing element
* @param referenced the referenced element
* @param monitor the progress monitor to use
* @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
* @throws JavaModelException if the java elements could not be accessed
*/
public ModifierKeyword getVisibilityThreshold(final IJavaElement referencing, final IMember referenced, final IProgressMonitor monitor) throws JavaModelException {
Assert.isTrue(!(referencing instanceof IInitializer));
Assert.isTrue(!(referenced instanceof IInitializer));
ModifierKeyword keyword= ModifierKeyword.PUBLIC_KEYWORD;
try {
monitor.beginTask("", 1); //$NON-NLS-1$
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
final int referencingType= referencing.getElementType();
final int referencedType= referenced.getElementType();
switch (referencedType) {
case IJavaElement.TYPE: {
final IType typeReferenced= (IType) referenced;
final ICompilationUnit referencedUnit= typeReferenced.getCompilationUnit();
switch (referencingType) {
case IJavaElement.TYPE: {
keyword= thresholdTypeToType((IType) referencing, typeReferenced, monitor);
break;
}
case IJavaElement.FIELD:
case IJavaElement.METHOD: {
final IMember member= (IMember) referencing;
if (typeReferenced.equals(member.getDeclaringType())
|| (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))) {
keyword= ModifierKeyword.PRIVATE_KEYWORD;
} else if (typeReferenced.getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
keyword= null;
break;
}
case IJavaElement.PACKAGE_FRAGMENT: {
final IPackageFragment fragment= (IPackageFragment) referencing;
if (typeReferenced.getPackageFragment().equals(fragment))
keyword= null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
case IJavaElement.FIELD: {
final IField fieldReferenced= (IField) referenced;
final ICompilationUnit referencedUnit= fieldReferenced.getCompilationUnit();
switch (referencingType) {
case IJavaElement.TYPE: {
keyword= thresholdTypeToField((IType) referencing, fieldReferenced, monitor);
break;
}
case IJavaElement.FIELD:
case IJavaElement.METHOD: {
final IMember member= (IMember) referencing;
if (fieldReferenced.getDeclaringType().equals(member.getDeclaringType())
|| (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))) {
keyword= ModifierKeyword.PRIVATE_KEYWORD;
} else if (fieldReferenced.getDeclaringType().getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
keyword= null;
break;
}
case IJavaElement.PACKAGE_FRAGMENT: {
final IPackageFragment fragment= (IPackageFragment) referencing;
if (fieldReferenced.getDeclaringType().getPackageFragment().equals(fragment))
keyword= null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
case IJavaElement.METHOD: {
final IMethod methodReferenced= (IMethod) referenced;
final ICompilationUnit referencedUnit= methodReferenced.getCompilationUnit();
switch (referencingType) {
case IJavaElement.TYPE: {
keyword= thresholdTypeToMethod((IType) referencing, methodReferenced, monitor);
break;
}
case IJavaElement.FIELD:
case IJavaElement.METHOD: {
final IMember member= (IMember) referencing;
if (methodReferenced.getDeclaringType().equals(member.getDeclaringType())
|| (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))) {
keyword= ModifierKeyword.PRIVATE_KEYWORD;
} else if (methodReferenced.getDeclaringType().getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
keyword= null;
break;
}
case IJavaElement.PACKAGE_FRAGMENT: {
final IPackageFragment fragment= (IPackageFragment) referencing;
if (methodReferenced.getDeclaringType().getPackageFragment().equals(fragment))
keyword= null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
default:
Assert.isTrue(false);
}
} finally {
monitor.done();
}
return keyword;
}
/**
* Computes the visibility threshold for the referenced element.
*
* @param referencing the referencing element
* @param referenced the referenced element
* @param monitor the progress monitor to use
* @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
* @throws JavaModelException if the java elements could not be accessed
*/
private ModifierKeyword computeOutgoingVisibilityThreshold(final IJavaElement referencing, final IMember referenced, final IProgressMonitor monitor) throws JavaModelException {
Assert.isTrue(referencing instanceof ICompilationUnit || referencing instanceof IType || referencing instanceof IPackageFragment);
Assert.isTrue(referenced instanceof IType || referenced instanceof IField || referenced instanceof IMethod);
ModifierKeyword keyword= ModifierKeyword.PUBLIC_KEYWORD;
try {
monitor.beginTask("", 1); //$NON-NLS-1$
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
final int referencingType= referencing.getElementType();
final int referencedType= referenced.getElementType();
switch (referencedType) {
case IJavaElement.TYPE: {
final IType typeReferenced= (IType) referenced;
switch (referencingType) {
case IJavaElement.COMPILATION_UNIT: {
final ICompilationUnit unit= (ICompilationUnit) referencing;
final ICompilationUnit referencedUnit= typeReferenced.getCompilationUnit();
if (referencedUnit != null && referencedUnit.equals(unit))
keyword= ModifierKeyword.PRIVATE_KEYWORD;
else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
keyword= null;
break;
}
case IJavaElement.TYPE: {
keyword= thresholdTypeToType((IType) referencing, typeReferenced, monitor);
break;
}
case IJavaElement.PACKAGE_FRAGMENT: {
final IPackageFragment fragment= (IPackageFragment) referencing;
if (typeReferenced.getPackageFragment().equals(fragment))
keyword= null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
case IJavaElement.FIELD: {
final IField fieldReferenced= (IField) referenced;
final ICompilationUnit referencedUnit= fieldReferenced.getCompilationUnit();
switch (referencingType) {
case IJavaElement.COMPILATION_UNIT: {
final ICompilationUnit unit= (ICompilationUnit) referencing;
if (referencedUnit != null && referencedUnit.equals(unit))
keyword= ModifierKeyword.PRIVATE_KEYWORD;
else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
keyword= null;
break;
}
case IJavaElement.TYPE: {
keyword= thresholdTypeToField((IType) referencing, fieldReferenced, monitor);
break;
}
case IJavaElement.PACKAGE_FRAGMENT: {
final IPackageFragment fragment= (IPackageFragment) referencing;
if (fieldReferenced.getDeclaringType().getPackageFragment().equals(fragment))
keyword= null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
case IJavaElement.METHOD: {
final IMethod methodReferenced= (IMethod) referenced;
final ICompilationUnit referencedUnit= methodReferenced.getCompilationUnit();
switch (referencingType) {
case IJavaElement.COMPILATION_UNIT: {
final ICompilationUnit unit= (ICompilationUnit) referencing;
if (referencedUnit != null && referencedUnit.equals(unit))
keyword= ModifierKeyword.PRIVATE_KEYWORD;
else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
keyword= null;
break;
}
case IJavaElement.TYPE: {
keyword= thresholdTypeToMethod((IType) referencing, methodReferenced, monitor);
break;
}
case IJavaElement.PACKAGE_FRAGMENT: {
final IPackageFragment fragment= (IPackageFragment) referencing;
if (methodReferenced.getDeclaringType().getPackageFragment().equals(fragment))
keyword= null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
default:
Assert.isTrue(false);
}
} finally {
monitor.done();
}
return keyword;
}
/**
* Returns the existing visibility adjustments (element type: Map <IMember, IVisibilityAdjustment>).
*
* @return the visibility adjustments
*/
public Map<IMember, IncomingMemberVisibilityAdjustment> getAdjustments() {
return fAdjustments;
}
/**
* Returns a compilation unit rewrite for the specified compilation unit.
*
* @param unit the compilation unit to get the rewrite for
* @return the rewrite for the compilation unit
*/
private CompilationUnitRewrite getCompilationUnitRewrite(final ICompilationUnit unit) {
CompilationUnitRewrite rewrite= fRewrites.get(unit);
if (rewrite == null) {
if (fOwner == null)
rewrite= new CompilationUnitRewrite(unit);
else
rewrite= new CompilationUnitRewrite(fOwner, unit);
fRewrites.put(unit, rewrite);
}
return rewrite;
}