-
Notifications
You must be signed in to change notification settings - Fork 82
/
ReorgPolicyFactory.java
4465 lines (3830 loc) · 168 KB
/
ReorgPolicyFactory.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, 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
* Carsten Pfeiffer <carsten.pfeiffer@gebit.de> - [ccp] ReorgPolicies' canEnable() methods return true too often - https://bugs.eclipse.org/bugs/show_bug.cgi?id=303698
* Yves Joan <yves.joan@oracle.com> - [reorg] Copy action should NOT add 'copy of' prefix - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151668
* Red Hat Inc. - [reorg] Move of package should not show binary roots and package roots should not expand - https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/430
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.reorg;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourceAttributes;
import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.NullChange;
import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.CopyArguments;
import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker;
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
import org.eclipse.ltk.core.refactoring.participants.ValidateEditChecker;
import org.eclipse.ltk.core.refactoring.resource.MoveResourceChange;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IImportContainer;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IInitializer;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IModuleDescription;
import org.eclipse.jdt.core.IOpenable;
import org.eclipse.jdt.core.IPackageDeclaration;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
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.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
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.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.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.RefactoringCoreMessages;
import org.eclipse.jdt.internal.corext.refactoring.changes.CopyCompilationUnitChange;
import org.eclipse.jdt.internal.corext.refactoring.changes.CopyPackageChange;
import org.eclipse.jdt.internal.corext.refactoring.changes.CopyPackageFragmentRootChange;
import org.eclipse.jdt.internal.corext.refactoring.changes.CopyResourceChange;
import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange;
import org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange;
import org.eclipse.jdt.internal.corext.refactoring.changes.MovePackageChange;
import org.eclipse.jdt.internal.corext.refactoring.changes.MovePackageFragmentRootChange;
import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgDestinationFactory.JavaElementDestination;
import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgDestinationFactory.ResourceDestination;
import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.refactoring.structure.ImportRewriteUtil;
import org.eclipse.jdt.internal.corext.refactoring.util.Changes;
import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
import org.eclipse.jdt.internal.corext.refactoring.util.QualifiedNameFinder;
import org.eclipse.jdt.internal.corext.refactoring.util.QualifiedNameSearchResult;
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil;
import org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager;
import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil;
import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.ui.refactoring.IRefactoringSaveModes;
import org.eclipse.jdt.internal.ui.util.Progress;
public final class ReorgPolicyFactory {
private static final class ActualSelectionComputer {
private final IJavaElement[] fJavaElements;
private final IResource[] fResources;
public ActualSelectionComputer(IJavaElement[] javaElements, IResource[] resources) {
fJavaElements= javaElements;
fResources= resources;
}
public IJavaElement[] getActualJavaElementsToReorg() throws JavaModelException {
List<IJavaElement> result= new ArrayList<>();
for (IJavaElement element : fJavaElements) {
if (element == null)
continue;
if (element instanceof IType) {
IType type= (IType) element;
ICompilationUnit cu= type.getCompilationUnit();
if (cu != null && type.getDeclaringType() == null && cu.exists() && cu.getTypes().length == 1 && !result.contains(cu))
result.add(cu);
else if (!result.contains(type))
result.add(type);
} else if (!result.contains(element)) {
result.add(element);
}
}
return result.toArray(new IJavaElement[result.size()]);
}
public IResource[] getActualResourcesToReorg() {
Set<IJavaElement> javaElementSet= new HashSet<>(Arrays.asList(fJavaElements));
List<IResource> result= new ArrayList<>();
for (IResource resource : fResources) {
if (resource == null) {
continue;
}
IJavaElement element= JavaCore.create(resource);
if (element == null || !element.exists() || !javaElementSet.contains(element)) {
if (!result.contains(resource)) {
result.add(resource);
}
}
}
return result.toArray(new IResource[result.size()]);
}
}
private static final class CopyFilesFoldersAndCusPolicy extends FilesFoldersAndCusReorgPolicy implements ICopyPolicy {
private static final String POLICY_COPY_RESOURCE= "org.eclipse.jdt.ui.copyResources"; //$NON-NLS-1$
private static Change copyCuToPackage(ICompilationUnit cu, IPackageFragment dest, NewNameProposer nameProposer, INewNameQueries copyQueries) {
// XXX workaround for bug 31998 we will have to disable renaming of
// linked packages (and cus)
IResource res= ReorgUtilsCore.getResource(cu);
if (res != null && res.isLinked()) {
if (ResourceUtil.getResource(dest) instanceof IContainer)
return copyFileToContainer(cu, (IContainer) ResourceUtil.getResource(dest), nameProposer, copyQueries);
}
String newName= nameProposer.createNewName(cu, dest);
Change simpleCopy= new CopyCompilationUnitChange(cu, dest, copyQueries.createStaticQuery(newName));
if (newName == null || newName.equals(cu.getElementName()))
return simpleCopy;
try {
IPath newPath= cu.getResource().getParent().getFullPath().append(JavaModelUtil.getRenamedCUName(cu, newName));
INewNameQuery nameQuery= copyQueries.createNewCompilationUnitNameQuery(cu, newName);
return new CreateCopyOfCompilationUnitChange(newPath, cu.getSource(), cu, nameQuery);
} catch (CoreException e) {
// Using inferred change
return simpleCopy;
}
}
private static Change copyFileToContainer(ICompilationUnit cu, IContainer dest, NewNameProposer nameProposer, INewNameQueries copyQueries) {
IResource resource= ReorgUtilsCore.getResource(cu);
return createCopyResourceChange(resource, nameProposer, copyQueries, dest);
}
private static Change createCopyResourceChange(IResource resource, NewNameProposer nameProposer, INewNameQueries copyQueries, IContainer destination) {
if (resource == null || destination == null)
return new NullChange();
INewNameQuery nameQuery;
String name= nameProposer.createNewName(resource, destination);
if (name == null)
nameQuery= copyQueries.createNullQuery();
else
nameQuery= copyQueries.createNewResourceNameQuery(resource, name);
return new CopyResourceChange(resource, destination, nameQuery);
}
private CopyModifications fModifications;
private ReorgExecutionLog fReorgExecutionLog;
CopyFilesFoldersAndCusPolicy(IFile[] files, IFolder[] folders, ICompilationUnit[] cus) {
super(files, folders, cus);
}
private Change createChange(ICompilationUnit unit, NewNameProposer nameProposer, INewNameQueries copyQueries) {
IPackageFragment pack= getDestinationAsPackageFragment();
if (pack != null)
return copyCuToPackage(unit, pack, nameProposer, copyQueries);
IContainer container= getDestinationAsContainer();
return copyFileToContainer(unit, container, nameProposer, copyQueries);
}
@Override
public Change createChange(IProgressMonitor pm, INewNameQueries copyQueries) {
IFile[] file= getFiles();
IFolder[] folders= getFolders();
ICompilationUnit[] cus= getCus();
pm.beginTask("", cus.length + file.length + folders.length); //$NON-NLS-1$
NewNameProposer nameProposer= new NewNameProposer();
CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_copy);
composite.markAsSynthetic();
for (ICompilationUnit cu : cus) {
composite.add(createChange(cu, nameProposer, copyQueries));
pm.worked(1);
}
if (pm.isCanceled())
throw new OperationCanceledException();
for (IFile f : file) {
composite.add(createChange(f, nameProposer, copyQueries));
pm.worked(1);
}
if (pm.isCanceled())
throw new OperationCanceledException();
for (IFolder folder : folders) {
composite.add(createChange(folder, nameProposer, copyQueries));
pm.worked(1);
}
pm.done();
return composite;
}
private Change createChange(IResource resource, NewNameProposer nameProposer, INewNameQueries copyQueries) {
IContainer dest= getDestinationAsContainer();
return createCopyResourceChange(resource, nameProposer, copyQueries, dest);
}
@Override
protected JavaRefactoringDescriptor createRefactoringDescriptor(JDTRefactoringDescriptorComment comment, Map<String, String> arguments, String description, String project, int flags) {
ReorgExecutionLog log= getReorgExecutionLog();
storeReorgExecutionLog(project, arguments, log);
return RefactoringSignatureDescriptorFactory.createCopyDescriptor(project, description, comment.asString(), arguments, flags);
}
@Override
protected String getDescriptionPlural() {
final int kind= getContentKind();
switch (kind) {
case ONLY_FOLDERS:
return RefactoringCoreMessages.ReorgPolicyFactory_copy_folders;
case ONLY_FILES:
return RefactoringCoreMessages.ReorgPolicyFactory_copy_files;
case ONLY_CUS:
return RefactoringCoreMessages.ReorgPolicyFactory_copy_compilation_units;
}
return RefactoringCoreMessages.ReorgPolicyFactory_copy_description_plural;
}
@Override
protected String getDescriptionSingular() {
final int kind= getContentKind();
switch (kind) {
case ONLY_FOLDERS:
return RefactoringCoreMessages.ReorgPolicyFactory_copy_folder;
case ONLY_FILES:
return RefactoringCoreMessages.ReorgPolicyFactory_copy_file;
case ONLY_CUS:
return RefactoringCoreMessages.ReorgPolicyFactory_copy_compilation_unit;
}
return RefactoringCoreMessages.ReorgPolicyFactory_copy_description_singular;
}
private Object getDestination() {
Object result= getDestinationAsPackageFragment();
if (result != null)
return result;
return getDestinationAsContainer();
}
@Override
protected String getHeaderPatternSingular() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_header_singular;
}
@Override
protected String getHeaderPatternPlural() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_header_plural;
}
@Override
protected RefactoringModifications getModifications() throws CoreException {
if (fModifications != null)
return fModifications;
fModifications= new CopyModifications();
fReorgExecutionLog= new ReorgExecutionLog();
CopyArguments jArgs= new CopyArguments(getDestination(), fReorgExecutionLog);
CopyArguments rArgs= new CopyArguments(getDestinationAsContainer(), fReorgExecutionLog);
for (ICompilationUnit cu : getCus()) {
fModifications.copy(cu, jArgs, rArgs);
}
for (IResource resource : ReorgUtilsCore.union(getFiles(), getFolders())) {
fModifications.copy(resource, rArgs);
}
return fModifications;
}
@Override
public String getPolicyId() {
return POLICY_COPY_RESOURCE;
}
@Override
protected String getProcessorId() {
return IJavaRefactorings.COPY;
}
@Override
protected String getRefactoringId() {
return IJavaRefactorings.COPY;
}
@Override
public ReorgExecutionLog getReorgExecutionLog() {
return fReorgExecutionLog;
}
}
private static final class CopyPackageFragmentRootsPolicy extends PackageFragmentRootsReorgPolicy implements ICopyPolicy {
private static final String POLICY_COPY_ROOTS= "org.eclipse.jdt.ui.copyRoots"; //$NON-NLS-1$
private CopyModifications fModifications;
private ReorgExecutionLog fReorgExecutionLog;
public CopyPackageFragmentRootsPolicy(IPackageFragmentRoot[] roots) {
super(roots);
}
private Change createChange(IPackageFragmentRoot root, IJavaProject destination, NewNameProposer nameProposer, INewNameQueries copyQueries) {
IResource res= root.getResource();
IProject destinationProject= destination.getProject();
String newName= nameProposer.createNewName(res, destinationProject);
INewNameQuery nameQuery;
if (newName == null)
nameQuery= copyQueries.createNullQuery();
else
nameQuery= copyQueries.createNewPackageFragmentRootNameQuery(root, newName);
// TODO fix the query problem
return new CopyPackageFragmentRootChange(root, destinationProject, nameQuery, null);
}
private Change createChange(IPackageFragmentRoot root, IContainer destination, NewNameProposer nameProposer, INewNameQueries copyQueries) {
IResource res= root.getResource();
String newName= nameProposer.createNewName(res, destination);
INewNameQuery nameQuery;
if (newName == null) {
nameQuery= copyQueries.createNullQuery();
} else {
nameQuery= copyQueries.createNewResourceNameQuery(res, newName);
}
return new CopyResourceChange(res, destination, nameQuery);
}
@Override
public Change createChange(IProgressMonitor pm, INewNameQueries copyQueries) {
NewNameProposer nameProposer= new NewNameProposer();
IPackageFragmentRoot[] roots= getPackageFragmentRoots();
pm.beginTask("", roots.length); //$NON-NLS-1$
CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_copy_source_folder);
composite.markAsSynthetic();
IJavaProject destination= getDestinationJavaProject();
for (IPackageFragmentRoot root : roots) {
if (destination == null) {
composite.add(createChange(root, (IContainer) getResourceDestination(), nameProposer, copyQueries));
} else {
composite.add(createChange(root, destination, nameProposer, copyQueries));
}
pm.worked(1);
}
pm.done();
return composite;
}
@Override
protected JavaRefactoringDescriptor createRefactoringDescriptor(JDTRefactoringDescriptorComment comment, Map<String, String> arguments, String description, String project, int flags) {
ReorgExecutionLog log= getReorgExecutionLog();
storeReorgExecutionLog(project, arguments, log);
return RefactoringSignatureDescriptorFactory.createCopyDescriptor(project, description, comment.asString(), arguments, flags);
}
@Override
protected String getDescriptionPlural() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_roots_plural;
}
@Override
protected String getDescriptionSingular() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_roots_singular;
}
@Override
protected String getHeaderPatternSingular() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_roots_header_singular;
}
@Override
protected String getHeaderPatternPlural() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_roots_header_plural;
}
@Override
protected RefactoringModifications getModifications() throws CoreException {
if (fModifications != null)
return fModifications;
fModifications= new CopyModifications();
fReorgExecutionLog= new ReorgExecutionLog();
IJavaProject destination= getDestinationJavaProject();
if (destination == null) {
for (IPackageFragmentRoot root : getRoots()) {
fModifications.copy(root.getResource(), new CopyArguments(getResourceDestination(), fReorgExecutionLog));
}
} else {
CopyArguments javaArgs= new CopyArguments(destination, fReorgExecutionLog);
CopyArguments resourceArgs= new CopyArguments(destination.getProject(), fReorgExecutionLog);
for (IPackageFragmentRoot root : getRoots()) {
fModifications.copy(root, javaArgs, resourceArgs);
}
}
return fModifications;
}
@Override
public String getPolicyId() {
return POLICY_COPY_ROOTS;
}
@Override
protected String getProcessorId() {
return IJavaRefactorings.COPY;
}
@Override
protected String getRefactoringId() {
return IJavaRefactorings.COPY;
}
@Override
public ReorgExecutionLog getReorgExecutionLog() {
return fReorgExecutionLog;
}
}
private static final class CopyPackagesPolicy extends PackagesReorgPolicy implements ICopyPolicy {
private static final String POLICY_COPY_PACKAGES= "org.eclipse.jdt.ui.copyPackages"; //$NON-NLS-1$
private CopyModifications fModifications;
private ReorgExecutionLog fReorgExecutionLog;
public CopyPackagesPolicy(IPackageFragment[] packageFragments) {
super(packageFragments);
}
private Change createChange(IPackageFragment pack, IPackageFragmentRoot destination, NewNameProposer nameProposer, INewNameQueries copyQueries) {
String newName= nameProposer.createNewName(pack, destination);
if (newName == null || JavaConventionsUtil.validatePackageName(newName, destination).getSeverity() < IStatus.ERROR) {
INewNameQuery nameQuery;
if (newName == null)
nameQuery= copyQueries.createNullQuery();
else
nameQuery= copyQueries.createNewPackageNameQuery(pack, newName);
return new CopyPackageChange(pack, destination, nameQuery);
} else {
if (destination.getResource() instanceof IContainer) {
IContainer dest= (IContainer) destination.getResource();
IResource res= pack.getResource();
INewNameQuery nameQuery= copyQueries.createNewResourceNameQuery(res, newName);
return new CopyResourceChange(res, dest, nameQuery);
} else {
return new NullChange();
}
}
}
private Change createChange(IPackageFragment pack, IContainer container, NewNameProposer nameProposer, INewNameQueries copyQueries) {
String newName= nameProposer.createNewName(pack.getResource(), container);
if (newName == null) {
return new CopyResourceChange(pack.getResource(), container, copyQueries.createNullQuery());
} else {
IResource res= pack.getResource();
INewNameQuery nameQuery= copyQueries.createNewResourceNameQuery(res, newName);
return new CopyResourceChange(res, container, nameQuery);
}
}
@Override
public Change createChange(IProgressMonitor pm, INewNameQueries newNameQueries) throws JavaModelException {
NewNameProposer nameProposer= new NewNameProposer();
IPackageFragment[] fragments= getPackages();
pm.beginTask("", fragments.length); //$NON-NLS-1$
CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_copy_package);
composite.markAsSynthetic();
IPackageFragmentRoot root= getDestinationAsPackageFragmentRoot();
for (IPackageFragment fragment : fragments) {
if (root == null) {
composite.add(createChange(fragment, (IContainer) getResourceDestination(), nameProposer, newNameQueries));
} else {
composite.add(createChange(fragment, root, nameProposer, newNameQueries));
}
pm.worked(1);
}
pm.done();
return composite;
}
@Override
protected JavaRefactoringDescriptor createRefactoringDescriptor(JDTRefactoringDescriptorComment comment, Map<String, String> arguments, String description, String project, int flags) {
ReorgExecutionLog log= getReorgExecutionLog();
storeReorgExecutionLog(project, arguments, log);
return RefactoringSignatureDescriptorFactory.createCopyDescriptor(project, description, comment.asString(), arguments, flags);
}
@Override
protected String getDescriptionPlural() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_packages_plural;
}
@Override
protected String getDescriptionSingular() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_package_singular;
}
@Override
protected String getHeaderPatternSingular() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_packages_header_singular;
}
@Override
protected String getHeaderPatternPlural() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_packages_header_plural;
}
@Override
protected RefactoringModifications getModifications() throws CoreException {
if (fModifications != null)
return fModifications;
fModifications= new CopyModifications();
fReorgExecutionLog= new ReorgExecutionLog();
IPackageFragmentRoot destination= getDestinationAsPackageFragmentRoot();
if (destination == null) {
for (IPackageFragment p : getPackages()) {
fModifications.copy(p.getResource(), new CopyArguments(getResourceDestination(), fReorgExecutionLog));
}
} else {
CopyArguments javaArgs= new CopyArguments(destination, fReorgExecutionLog);
CopyArguments resourceArgs= new CopyArguments(destination.getResource(), fReorgExecutionLog);
for (IPackageFragment p : getPackages()) {
fModifications.copy(p, javaArgs, resourceArgs);
}
}
return fModifications;
}
@Override
public String getPolicyId() {
return POLICY_COPY_PACKAGES;
}
@Override
protected String getProcessorId() {
return IJavaRefactorings.COPY;
}
@Override
protected String getRefactoringId() {
return IJavaRefactorings.COPY;
}
@Override
public ReorgExecutionLog getReorgExecutionLog() {
return fReorgExecutionLog;
}
}
private static final class CopySubCuElementsPolicy extends SubCuElementReorgPolicy implements ICopyPolicy {
private static final String POLICY_COPY_MEMBERS= "org.eclipse.jdt.ui.copyMembers"; //$NON-NLS-1$
private CopyModifications fModifications;
private ReorgExecutionLog fReorgExecutionLog;
CopySubCuElementsPolicy(IJavaElement[] javaElements) {
super(javaElements);
}
@Override
public boolean canEnable() throws JavaModelException {
return super.canEnable() && (getSourceCu() != null || getSourceClassFile() != null);
}
@Override
public Change createChange(IProgressMonitor pm, INewNameQueries copyQueries) throws JavaModelException {
try {
CompilationUnit sourceCuNode= createSourceCuNode();
ICompilationUnit targetCu= getEnclosingCompilationUnit(getJavaElementDestination());
CompilationUnitRewrite targetRewriter= new CompilationUnitRewrite(targetCu);
for (IJavaElement javaElement : getJavaElements()) {
copyToDestination(javaElement, targetRewriter, sourceCuNode, targetRewriter.getRoot());
}
return createCompilationUnitChange(targetRewriter);
} catch (JavaModelException e) {
throw e;
} catch (CoreException e) {
throw new JavaModelException(e);
}
}
@Override
protected JavaRefactoringDescriptor createRefactoringDescriptor(JDTRefactoringDescriptorComment comment, Map<String, String> arguments, String description, String project, int flags) {
ReorgExecutionLog log= getReorgExecutionLog();
storeReorgExecutionLog(project, arguments, log);
return RefactoringSignatureDescriptorFactory.createCopyDescriptor(project, description, comment.asString(), arguments, flags);
}
private CompilationUnit createSourceCuNode() {
Assert.isTrue(getSourceCu() != null || getSourceClassFile() != null);
Assert.isTrue(getSourceCu() == null || getSourceClassFile() == null);
ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setBindingsRecovery(true);
parser.setResolveBindings(true);
if (getSourceCu() != null)
parser.setSource(getSourceCu());
else
parser.setSource(getSourceClassFile());
return (CompilationUnit) parser.createAST(null);
}
@Override
public IFile[] getAllModifiedFiles() {
return ReorgUtilsCore.getFiles(new IResource[] { ReorgUtilsCore.getResource(getEnclosingCompilationUnit(getJavaElementDestination()))});
}
@Override
protected String getDescriptionPlural() {
if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.TYPE)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_types;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.FIELD)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_fields;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.METHOD)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_methods;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.INITIALIZER)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_initializers;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.PACKAGE_DECLARATION)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_package_declarations;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.IMPORT_CONTAINER)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_import_containers;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.IMPORT_DECLARATION)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_imports;
} else {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_elements_plural;
}
}
@Override
protected String getDescriptionSingular() {
if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.TYPE)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_type;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.FIELD)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_field;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.METHOD)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_method;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.INITIALIZER)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_initializer;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.PACKAGE_DECLARATION)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_package;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.IMPORT_CONTAINER)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_import_section;
} else if (!ReorgUtilsCore.hasElementsNotOfType(getJavaElements(), IJavaElement.IMPORT_DECLARATION)) {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_import;
} else {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_elements_singular;
}
}
@Override
protected String getHeaderPatternSingular() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_elements_header_singular;
}
@Override
protected String getHeaderPatternPlural() {
return RefactoringCoreMessages.ReorgPolicyFactory_copy_elements_header_plural;
}
@Override
protected RefactoringModifications getModifications() throws CoreException {
if (fModifications != null)
return fModifications;
fModifications= new CopyModifications();
fReorgExecutionLog= new ReorgExecutionLog();
CopyArguments args= new CopyArguments(getJavaElementDestination(), fReorgExecutionLog);
for (IJavaElement javaElement : getJavaElements()) {
fModifications.copy(javaElement, args, null);
}
return fModifications;
}
@Override
public String getPolicyId() {
return POLICY_COPY_MEMBERS;
}
@Override
protected String getProcessorId() {
return IJavaRefactorings.COPY;
}
@Override
protected String getRefactoringId() {
return IJavaRefactorings.COPY;
}
@Override
public ReorgExecutionLog getReorgExecutionLog() {
return fReorgExecutionLog;
}
private IClassFile getSourceClassFile() {
// all have a common parent, so all must be in the same classfile
// we checked before that the array in not null and not empty
return (IClassFile) getJavaElements()[0].getAncestor(IJavaElement.CLASS_FILE);
}
}
private static abstract class FilesFoldersAndCusReorgPolicy extends ReorgPolicy {
protected static final int ONLY_CUS= 2;
protected static final int ONLY_FILES= 1;
protected static final int ONLY_FOLDERS= 0;
private static IContainer getAsContainer(IResource resDest) {
if (resDest instanceof IContainer)
return (IContainer) resDest;
if (resDest instanceof IFile)
return ((IFile) resDest).getParent();
return null;
}
private ICompilationUnit[] fCus;
private IFile[] fFiles;
private IFolder[] fFolders;
public FilesFoldersAndCusReorgPolicy(IFile[] files, IFolder[] folders, ICompilationUnit[] cus) {
fFiles= files;
fFolders= folders;
fCus= cus;
}
@Override
public boolean canChildrenBeDestinations(IJavaElement javaElement) {
switch (javaElement.getElementType()) {
case IJavaElement.JAVA_MODEL:
case IJavaElement.JAVA_PROJECT:
return true;
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
IPackageFragmentRoot root= (IPackageFragmentRoot) javaElement;
return !root.isArchive() && !root.isExternal();
default:
return false;
}
}
@Override
public boolean canChildrenBeDestinations(IResource resource) {
return resource instanceof IContainer;
}
@Override
public boolean canElementBeDestination(IJavaElement javaElement) {
switch (javaElement.getElementType()) {
case IJavaElement.PACKAGE_FRAGMENT:
return true;
default:
return false;
}
}
@Override
public boolean canElementBeDestination(IResource resource) {
return resource instanceof IProject || resource instanceof IFolder;
}
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context, IReorgQueries reorgQueries) throws CoreException {
confirmOverwriting(reorgQueries);
RefactoringStatus status= super.checkFinalConditions(pm, context, reorgQueries);
return status;
}
private void confirmOverwriting(IReorgQueries reorgQueries) {
OverwriteHelper helper= new OverwriteHelper();
helper.setFiles(fFiles);
helper.setFolders(fFolders);
helper.setCus(fCus);
IPackageFragment destPack= getDestinationAsPackageFragment();
if (destPack != null) {
helper.confirmOverwriting(reorgQueries, destPack);
} else {
IContainer destinationAsContainer= getDestinationAsContainer();
if (destinationAsContainer != null)
helper.confirmOverwriting(reorgQueries, destinationAsContainer);
}
fFiles= helper.getFilesWithoutUnconfirmedOnes();
fFolders= helper.getFoldersWithoutUnconfirmedOnes();
fCus= helper.getCusWithoutUnconfirmedOnes();
}
protected boolean containsLinkedResources() {
return ReorgUtilsCore.containsLinkedResources(fFiles) || ReorgUtilsCore.containsLinkedResources(fFolders) || ReorgUtilsCore.containsLinkedResources(fCus);
}
protected abstract JavaRefactoringDescriptor createRefactoringDescriptor(final JDTRefactoringDescriptorComment comment, final Map<String, String> arguments, final String description, final String project, int flags);
protected final int getContentKind() {
final int length= fCus.length + fFiles.length + fFolders.length;
if (length == fCus.length)
return ONLY_CUS;
else if (length == fFiles.length)
return ONLY_FILES;
else if (length == fFolders.length)
return ONLY_FOLDERS;
return -1;
}
protected final ICompilationUnit[] getCus() {
return fCus;
}
private final String getSingleElementName() {
switch (getContentKind()) {
case ONLY_FOLDERS:
return fFolders[0].getName();
case ONLY_FILES:
return fFiles[0].getName();
case ONLY_CUS:
return fCus[0].getElementName();
}
return null;
}
@Override
public final ChangeDescriptor getDescriptor() {
final Map<String, String> arguments= new HashMap<>();
final int length= fFiles.length + fFolders.length + fCus.length;
final String description= length == 1 ? getDescriptionSingular() : getDescriptionPlural();
final IProject resource= getSingleProject();
final String project= resource != null ? resource.getName() : null;
final String header= length == 1 ? Messages.format(getHeaderPatternSingular(), new String[] { getSingleElementName(), getDestinationLabel() }) : Messages.format(
getHeaderPatternPlural(),
new String[] { String.valueOf(length), getDestinationLabel() });
int flags= JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE;
final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header);
arguments.put(ATTRIBUTE_POLICY, getPolicyId());
arguments.put(ATTRIBUTE_FILES, Integer.toString(fFiles.length));
for (int offset= 0; offset < fFiles.length; offset++)
arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + 1), JavaRefactoringDescriptorUtil.resourceToHandle(project, fFiles[offset]));
arguments.put(ATTRIBUTE_FOLDERS, Integer.toString(fFolders.length));
for (int offset= 0; offset < fFolders.length; offset++)
arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + fFiles.length + 1), JavaRefactoringDescriptorUtil.resourceToHandle(project, fFolders[offset]));
arguments.put(ATTRIBUTE_UNITS, Integer.toString(fCus.length));
for (int offset= 0; offset < fCus.length; offset++)
arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + fFolders.length + fFiles.length + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fCus[offset]));
arguments.putAll(getRefactoringArguments(project));
final JavaRefactoringDescriptor descriptor= createRefactoringDescriptor(comment, arguments, description, project, flags);
return new RefactoringChangeDescriptor(descriptor);
}
protected final IContainer getDestinationAsContainer() {
IResource resDest= getResourceDestination();
if (resDest != null)
return getAsContainer(resDest);
IJavaElement jelDest= getJavaElementDestination();
Assert.isNotNull(jelDest);
return getAsContainer(ReorgUtilsCore.getResource(jelDest));
}
protected IPackageFragment getDestinationAsPackageFragment() {
IPackageFragment javaAsPackage= getJavaDestinationAsPackageFragment(getJavaElementDestination());
boolean copyFilesToDefaultPackage= true;
if (javaAsPackage != null) {
IJavaProject jProject= javaAsPackage.getJavaProject();
if (jProject != null && JavaModelUtil.is9OrHigher(jProject) && javaAsPackage.isDefaultPackage()) {
try {
IModuleDescription desc= jProject.getModuleDescription();
if (desc!= null && desc.exists()) {
copyFilesToDefaultPackage= false;
}
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (copyFilesToDefaultPackage) {
return javaAsPackage;
}
}
return getResourceDestinationAsPackageFragment(getResourceDestination());
}
protected final IJavaElement getDestinationContainerAsJavaElement() {
if (getJavaElementDestination() != null)
return getJavaElementDestination();
IContainer destinationAsContainer= getDestinationAsContainer();
if (destinationAsContainer == null)
return null;
IJavaElement je= JavaCore.create(destinationAsContainer);
if (je != null && je.exists())
return je;
return null;