-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
ParentExiter.java
1223 lines (1126 loc) · 45.4 KB
/
ParentExiter.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
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2023 INRIA and contributors
*
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon.support.compiler.jdt;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration;
import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer;
import org.eclipse.jdt.internal.compiler.ast.CaseStatement;
import org.eclipse.jdt.internal.compiler.ast.CastExpression;
import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.ForStatement;
import org.eclipse.jdt.internal.compiler.ast.IfStatement;
import org.eclipse.jdt.internal.compiler.ast.MessageSend;
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.eclipse.jdt.internal.compiler.ast.StringLiteralConcatenation;
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.ast.UnionTypeReference;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import org.jspecify.annotations.Nullable;
import spoon.SpoonException;
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CaseKind;
import spoon.reflect.code.CtArrayAccess;
import spoon.reflect.code.CtArrayRead;
import spoon.reflect.code.CtArrayWrite;
import spoon.reflect.code.CtAssert;
import spoon.reflect.code.CtAssignment;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtBreak;
import spoon.reflect.code.CtCase;
import spoon.reflect.code.CtCasePattern;
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtCatchVariable;
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtDo;
import spoon.reflect.code.CtExecutableReferenceExpression;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFor;
import spoon.reflect.code.CtForEach;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtLoop;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.code.CtPattern;
import spoon.reflect.code.CtRecordPattern;
import spoon.reflect.code.CtResource;
import spoon.reflect.code.CtReturn;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtSuperAccess;
import spoon.reflect.code.CtSwitch;
import spoon.reflect.code.CtSwitchExpression;
import spoon.reflect.code.CtSynchronized;
import spoon.reflect.code.CtTargetedExpression;
import spoon.reflect.code.CtThisAccess;
import spoon.reflect.code.CtThrow;
import spoon.reflect.code.CtTry;
import spoon.reflect.code.CtTryWithResource;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtTypePattern;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.code.CtWhile;
import spoon.reflect.code.CtYieldStatement;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotatedElementType;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtEnum;
import spoon.reflect.declaration.CtEnumValue;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtFormalTypeDeclarer;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtRecord;
import spoon.reflect.declaration.CtRecordComponent;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtIntersectionTypeReference;
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.reference.CtVariableReference;
import spoon.reflect.reference.CtWildcardReference;
import spoon.reflect.visitor.CtInheritanceScanner;
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.filter.TypeFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static spoon.reflect.code.BinaryOperatorKind.INSTANCEOF;
@SuppressWarnings("unchecked")
public class ParentExiter extends CtInheritanceScanner {
private final JDTTreeBuilder jdtTreeBuilder;
private CtElement child;
private ASTNode childJDT;
private ASTPair parentPair;
private Map<CtTypedElement<?>, List<CtAnnotation>> annotationsMap = new HashMap<>();
/**
* @param jdtTreeBuilder
*/
ParentExiter(JDTTreeBuilder jdtTreeBuilder) {
this.jdtTreeBuilder = jdtTreeBuilder;
}
public void exitParent(ASTPair pair) {
this.parentPair = pair;
scan(pair.element);
}
public void setChild(CtElement child) {
this.child = child;
}
public void setChild(ASTNode child) {
this.childJDT = child;
}
@Override
public void scanCtElement(CtElement e) {
if (child instanceof CtAnnotation && this.jdtTreeBuilder.getContextBuilder().annotationValueName.isEmpty()) {
// we check if the current element can have the annotation attached
CtAnnotatedElementType annotatedElementType = CtAnnotation.getAnnotatedElementTypeForCtElement(e);
annotatedElementType = (e instanceof CtTypeParameter || e instanceof CtTypeParameterReference) ? CtAnnotatedElementType.TYPE_USE : annotatedElementType;
// in case of noclasspath, we cannot be 100% sure, so we guess it must be attached...
if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() || (annotatedElementType != null && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, annotatedElementType))) {
e.addAnnotation((CtAnnotation<?>) child);
}
// in this case the annotation should be (also) attached to the type
if (e instanceof CtTypedElement && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, CtAnnotatedElementType.TYPE_USE)) {
List<CtAnnotation> annotations = new ArrayList<>();
if (!annotationsMap.containsKey(e)) {
annotationsMap.put((CtTypedElement<?>) e, annotations);
} else {
annotations = annotationsMap.get(e);
}
annotations.add((CtAnnotation) child.clone());
annotationsMap.put((CtTypedElement<?>) e, annotations);
}
}
}
private void substituteAnnotation(CtTypedElement ele) {
if (annotationsMap.containsKey(ele)) {
List<CtAnnotation> annotations = annotationsMap.get(ele);
for (CtAnnotation annotation : annotations) {
// in case of noclasspath we attached previously the element:
// if we are here, we may have find an element for whom it's a better place
if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() && annotation.isParentInitialized()) {
CtElement parent = annotation.getParent();
parent.removeAnnotation(annotation);
}
if (!ele.getType().getAnnotations().contains(annotation)) {
ele.getType().addAnnotation(annotation.clone());
}
}
annotationsMap.remove(ele);
}
}
@Override
public <R> void scanCtExecutable(CtExecutable<R> e) {
if (child instanceof CtTypeAccess) {
e.addThrownType(((CtTypeAccess) child).getAccessedType());
return;
} else if (child instanceof CtParameter) {
e.addParameter((CtParameter<?>) child);
return;
} else if (child instanceof CtBlock && !(e instanceof CtMethod || e instanceof CtConstructor)) {
e.setBody((CtBlock<R>) child);
return;
}
super.scanCtExecutable(e);
}
@Override
public void scanCtFormalTypeDeclarer(CtFormalTypeDeclarer e) {
if (childJDT instanceof TypeParameter && child instanceof CtTypeParameter) {
e.addFormalCtTypeParameter((CtTypeParameter) child);
}
}
@Override
public void scanCtLoop(CtLoop loop) {
if (loop.getBody() == null && child instanceof CtStatement) {
CtStatement child = (CtStatement) this.child;
if (!(this.child instanceof CtBlock)) {
child = jdtTreeBuilder.getFactory().Code().createCtBlock(child);
child.setImplicit(true);
child.setPosition(this.child.getPosition());
}
loop.setBody(child);
}
super.scanCtLoop(loop);
}
@Override
public <T, E extends CtExpression<?>> void scanCtTargetedExpression(CtTargetedExpression<T, E> targetedExpression) {
if (child instanceof CtExpression) {
targetedExpression.setTarget((E) child);
return;
}
super.scanCtTargetedExpression(targetedExpression);
}
@Override
public <T> void scanCtType(CtType<T> type) {
if (child instanceof CtType && !(child instanceof CtTypeParameter)) {
if (type.getTypeMembers().contains(child)) {
type.removeTypeMember((CtType) child);
}
type.addNestedType((CtType<?>) child);
return;
} else if (child instanceof CtEnumValue && type instanceof CtEnum) {
((CtEnum) type).addEnumValue((CtEnumValue) child);
} else if (child instanceof CtField) {
type.addField((CtField<?>) child);
return;
} else if (child instanceof CtConstructor) {
return;
}
if (child instanceof CtMethod) {
type.addMethod((CtMethod<?>) child);
return;
}
super.scanCtType(type);
}
@Override
public <T> void scanCtVariable(CtVariable<T> v) {
if (childJDT instanceof TypeReference && child instanceof CtTypeAccess) {
v.setType(((CtTypeAccess) child).getAccessedType());
substituteAnnotation((CtTypedElement) v);
return;
} else if (child instanceof CtExpression && hasChildEqualsToDefaultValue(v)) {
v.setDefaultExpression((CtExpression<T>) child);
return;
}
super.scanCtVariable(v);
}
private <T> boolean hasChildEqualsToDefaultValue(CtVariable<T> ctVariable) {
if (jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof AnnotationMethodDeclaration) {
final AnnotationMethodDeclaration parent = (AnnotationMethodDeclaration) jdtTreeBuilder.getContextBuilder().getCurrentNode();
// Default value is equals to the jdt child.
return parent.defaultValue != null && getFinalExpressionFromCast(parent.defaultValue).equals(childJDT)
// Return type not yet initialized.
&& !child.equals(ctVariable.getDefaultExpression());
}
final AbstractVariableDeclaration parent = (AbstractVariableDeclaration) jdtTreeBuilder.getContextBuilder().getCurrentNode();
// Default value is equals to the jdt child.
return parent.initialization != null && getFinalExpressionFromCast(parent.initialization).equals(childJDT)
// Return type not yet initialized.
&& !child.equals(ctVariable.getDefaultExpression());
}
@Override
public <A extends java.lang.annotation.Annotation> void visitCtAnnotation(CtAnnotation<A> annotation) {
if (child instanceof CtExpression) {
annotation.addValue(this.jdtTreeBuilder.getContextBuilder().annotationValueName.peek(), child);
}
super.visitCtAnnotation(annotation);
}
@Override
public <T> void visitCtConstructor(CtConstructor<T> e) {
if (e.getBody() == null && child instanceof CtBlock) {
e.setBody((CtBlock) child);
return;
} else if (child instanceof CtStatement) {
visitCtBlock(e.getBody());
return;
}
super.visitCtConstructor(e);
}
@Override
public <T> void visitCtMethod(CtMethod<T> e) {
if (e.getBody() == null && child instanceof CtBlock) {
e.setBody((CtBlock) child);
return;
} else if (child instanceof CtStatement) {
visitCtBlock(e.getBody());
return;
} else if (child instanceof CtTypeAccess && hasChildEqualsToType(e)) {
e.setType(((CtTypeAccess) child).getAccessedType());
substituteAnnotation(e);
return;
}
super.visitCtMethod(e);
}
private <T> boolean hasChildEqualsToType(CtMethod<T> ctMethod) {
final MethodDeclaration parent = (MethodDeclaration) jdtTreeBuilder.getContextBuilder().getCurrentNode();
// Return type is equals to the jdt child.
return parent.returnType != null && parent.returnType.equals(childJDT)
// Return type not yet initialized.
&& !child.equals(ctMethod.getType());
}
@Override
public <T> void visitCtAnnotationMethod(CtAnnotationMethod<T> annotationMethod) {
if (child instanceof CtExpression && hasChildEqualsToDefaultValue(annotationMethod)) {
annotationMethod.setDefaultExpression((CtExpression) child);
return;
}
super.visitCtAnnotationMethod(annotationMethod);
}
private <T> boolean hasChildEqualsToDefaultValue(CtAnnotationMethod<T> ctAnnotationMethod) {
final AnnotationMethodDeclaration parent = (AnnotationMethodDeclaration) jdtTreeBuilder.getContextBuilder().getCurrentNode();
// Default value is equals to the jdt child.
return parent.defaultValue != null && parent.defaultValue.equals(childJDT)
// Default value not yet initialized.
&& !child.equals(ctAnnotationMethod.getDefaultExpression());
}
@Override
public void visitCtAnonymousExecutable(CtAnonymousExecutable e) {
if (child instanceof CtBlock) {
e.setBody((CtBlock) child);
return;
}
super.visitCtAnonymousExecutable(e);
}
@Override
public <T> void visitCtArrayRead(CtArrayRead<T> arrayRead) {
if (visitArrayAccess(arrayRead)) {
super.visitCtArrayRead(arrayRead);
}
}
@Override
public <T> void visitCtArrayWrite(CtArrayWrite<T> arrayWrite) {
if (visitArrayAccess(arrayWrite)) {
super.visitCtArrayWrite(arrayWrite);
}
}
private <T, E extends CtExpression<?>> boolean visitArrayAccess(CtArrayAccess<T, E> arrayAccess) {
if (child instanceof CtExpression) {
if (arrayAccess.getTarget() == null) {
arrayAccess.setTarget((E) child);
return false;
} else {
arrayAccess.setIndexExpression((CtExpression<Integer>) child);
return false;
}
}
return true;
}
@Override
public <T> void visitCtAssert(CtAssert<T> asserted) {
if (child instanceof CtExpression) {
if (asserted.getAssertExpression() == null) {
asserted.setAssertExpression((CtExpression<Boolean>) child);
return;
} else {
asserted.setExpression((CtExpression<T>) child);
return;
}
}
super.visitCtAssert(asserted);
}
@Override
public <T, A extends T> void visitCtAssignment(CtAssignment<T, A> assignement) {
if (child instanceof CtExpression) {
if (assignement.getAssigned() == null) {
assignement.setAssigned((CtExpression<T>) child);
return;
} else if (assignement.getAssignment() == null) {
assignement.setAssignment((CtExpression<A>) child);
return;
}
}
super.visitCtAssignment(assignement);
}
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
CtElement child = operator.getKind() == INSTANCEOF && operator.getLeftHandOperand() != null
? adjustIfLocalVariableToTypePattern(this.child)
: this.child;
if (child instanceof CtExpression) {
if (operator.getLeftHandOperand() == null) {
operator.setLeftHandOperand((CtExpression<?>) child);
return;
} else if (operator.getRightHandOperand() == null) {
if (child.getPosition().isValidPosition()) {
int childEnd = child.getPosition().getSourceEnd();
SourcePosition oldPos = operator.getPosition();
if (oldPos.isValidPosition() && oldPos.getSourceEnd() < childEnd) {
//fix parent position if right hand expression is `x instanceof List<?>` which has bad sourceEnd ending before `<?>
int[] lineSeparatorPositions = jdtTreeBuilder.getContextBuilder().getCompilationUnitLineSeparatorPositions();
operator.setPosition(operator.getFactory().Core().createSourcePosition(
oldPos.getCompilationUnit(),
oldPos.getSourceStart(), childEnd,
lineSeparatorPositions));
}
}
operator.setRightHandOperand((CtExpression<?>) child);
return;
} else if (jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof StringLiteralConcatenation) {
CtBinaryOperator<?> op = operator.getFactory().Core().createBinaryOperator();
op.setKind(BinaryOperatorKind.PLUS);
op.setLeftHandOperand(operator.getLeftHandOperand());
op.setRightHandOperand(operator.getRightHandOperand());
op.setType(operator.getFactory().Type().stringType());
operator.setLeftHandOperand(op);
operator.setRightHandOperand(((CtExpression<?>) child));
int[] lineSeparatorPositions = jdtTreeBuilder.getContextBuilder().getCompilationUnitLineSeparatorPositions();
SourcePosition leftPosition = op.getLeftHandOperand().getPosition();
SourcePosition rightPosition = op.getRightHandOperand().getPosition();
op.setPosition(op.getFactory().createSourcePosition(leftPosition.getCompilationUnit(), leftPosition.getSourceStart(), rightPosition.getSourceEnd(), lineSeparatorPositions));
return;
}
}
super.visitCtBinaryOperator(operator);
}
/**
* {@return the original element if it is not a local variable, a type pattern containing the local variable otherwise}
*
* @param original the original element
*/
private CtElement adjustIfLocalVariableToTypePattern(CtElement original) {
CtElement child = original;
// check if this is a type pattern, as it needs special treatment
// patterns are only allowed for instanceof and on the right hand
if (child instanceof CtLocalVariable) {
CtTypePattern typePattern = child.getFactory().Core().createTypePattern();
typePattern.setVariable((CtLocalVariable<?>) child);
// as we create the type pattern just here, we need to set its source position - which is luckily the same
typePattern.setPosition(child.getPosition());
child = typePattern; // replace the local variable with a pattern (which is a CtExpression)
}
return child;
}
@Override
public <R> void visitCtBlock(CtBlock<R> block) {
if (child instanceof CtStatement) {
block.addStatement((CtStatement) child);
return;
}
super.visitCtBlock(block);
}
@Override
public void visitCtBreak(CtBreak b) {
super.visitCtBreak(b);
}
@Override
public <E> void visitCtCase(CtCase<E> caseStatement) {
final ASTNode node = jdtTreeBuilder.getContextBuilder().getCurrentNode();
if (node instanceof CaseStatement) {
caseStatement.setCaseKind(((CaseStatement) node).isExpr ? CaseKind.ARROW : CaseKind.COLON);
}
if (node instanceof CaseStatement && ((CaseStatement) node).constantExpressions != null && child instanceof CtExpression
&& caseStatement.getCaseExpressions().size() < ((CaseStatement) node).constantExpressions.length) {
if (child instanceof CtPattern pattern) {
caseStatement.addCaseExpression((CtExpression<E>) jdtTreeBuilder.getFactory().Core().createCasePattern().setPattern(pattern));
} else {
caseStatement.addCaseExpression((CtExpression<E>) child);
}
return;
} else if (child instanceof CtStatement) {
caseStatement.addStatement((CtStatement) child);
return;
}
super.visitCtCase(caseStatement);
}
@Override
public void visitCtCatch(CtCatch catchBlock) {
if (child instanceof CtBlock) {
catchBlock.setBody((CtBlock<?>) child);
return;
} else if (child instanceof CtCatchVariable) {
catchBlock.setParameter((CtCatchVariable<? extends Throwable>) child);
// Catch annotations are processed before actual CtCatchVariable is created and because of that they attach to CtCatch.
// Since annotations cannot be attached to CtCatch itself, we can simply transfer them to CtCatchVariable.
catchBlock.getAnnotations().forEach(a -> { a.setParent(child); child.addAnnotation(a); });
catchBlock.setAnnotations(List.of());
return;
}
super.visitCtCatch(catchBlock);
}
@Override
public <T> void visitCtCatchVariable(CtCatchVariable<T> e) {
if (jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof UnionTypeReference) {
e.addMultiType((CtTypeReference<?>) child);
return;
}
super.visitCtCatchVariable(e);
}
@Override
public void visitCtCasePattern(CtCasePattern casePattern) {
if (child instanceof CtPattern pattern) {
casePattern.setPattern(pattern);
} else if (child instanceof CtExpression<?> guard) {
casePattern.setGuard(guard);
}
super.visitCtCasePattern(casePattern);
}
@Override
public <T> void visitCtClass(CtClass<T> ctClass) {
if (child instanceof CtConstructor) {
CtConstructor<T> constructor = (CtConstructor<T>) child;
ctClass.addConstructor(constructor);
fixJdtEnumConstructorSuperCall(ctClass, constructor);
}
if (child instanceof CtAnonymousExecutable) {
ctClass.addAnonymousExecutable((CtAnonymousExecutable) child);
}
super.visitCtClass(ctClass);
}
private <T> void fixJdtEnumConstructorSuperCall(CtClass<T> ctClass, CtConstructor<T> constructor) {
// For some reason JDT inserts a `super()` call in implicit enum constructors.
// Explicit super calls are forbidden as java.lang.Enum subclasses are permitted by the JLS to delegate
// to the Enum constructor in whatever way they like.
// The constructor is implicit so this isn't *technically* illegal, but it doesn't really make much sense
// as explicit constructors can never contain such a call. Additionally, the Enum class from the standard
// library has a "String, int" constructor, rendering the parameterless supercall semantically invalid.
// We just remove the call to make it a bit more consistent.
// See https://github.com/INRIA/spoon/issues/4758 for more details.
if (!child.isImplicit() || !ctClass.isEnum() || !constructor.getParameters().isEmpty()) {
return;
}
if (constructor.getBody().getStatements().isEmpty()) {
return;
}
if (!(constructor.getBody().getStatement(0) instanceof CtInvocation)) {
return;
}
CtInvocation<?> superCall = constructor.getBody().getStatement(0);
if (superCall.getExecutable().getSimpleName().equals("<init>")) {
constructor.getBody().removeStatement(superCall);
}
}
@Override
public void visitCtTypeParameter(CtTypeParameter typeParameter) {
if (childJDT instanceof TypeReference && child instanceof CtTypeAccess) {
if (typeParameter.getSuperclass() == null) {
typeParameter.setSuperclass(((CtTypeAccess) child).getAccessedType());
} else if (typeParameter.getSuperclass() instanceof CtIntersectionTypeReference) {
typeParameter.getSuperclass().asCtIntersectionTypeReference().addBound(((CtTypeAccess) child).getAccessedType());
} else {
final List<CtTypeReference<?>> refs = new ArrayList<>();
refs.add(typeParameter.getSuperclass());
refs.add(((CtTypeAccess) child).getAccessedType());
typeParameter.setSuperclass(jdtTreeBuilder.getFactory().Type().createIntersectionTypeReferenceWithBounds(refs));
}
return;
}
super.visitCtTypeParameter(typeParameter);
}
@Override
public <T> void visitCtConditional(CtConditional<T> conditional) {
if (child instanceof CtExpression) {
if (conditional.getCondition() == null) {
conditional.setCondition((CtExpression<Boolean>) child);
} else if (conditional.getThenExpression() == null) {
conditional.setThenExpression((CtExpression<T>) child);
} else if (conditional.getElseExpression() == null) {
conditional.setElseExpression((CtExpression<T>) child);
}
}
super.visitCtConditional(conditional);
}
@Override
public void visitCtDo(CtDo doLoop) {
if (doLoop.getBody() != null && child instanceof CtExpression && doLoop.getLoopingExpression() == null) {
doLoop.setLoopingExpression((CtExpression<Boolean>) child);
return;
}
super.visitCtDo(doLoop);
}
@Override
public void visitCtFor(CtFor forLoop) {
if (isContainedInForInit() && child instanceof CtStatement) {
forLoop.addForInit((CtStatement) child);
return;
} else if (isContainedInForUpdate() && child instanceof CtStatement) {
forLoop.addForUpdate((CtStatement) child);
return;
} else if (isContainedInForCondition() && child instanceof CtExpression) {
forLoop.setExpression((CtExpression<Boolean>) child);
return;
}
super.visitCtFor(forLoop);
}
private boolean isContainedInForInit() {
if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ForStatement)) {
return false;
}
final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().getCurrentNode();
if (parent.initializations == null) {
return false;
}
for (Statement initialization : parent.initializations) {
if (initialization != null && initialization.equals(childJDT)) {
return true;
}
}
return false;
}
private boolean isContainedInForUpdate() {
if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ForStatement)) {
return false;
}
final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().getCurrentNode();
if (parent.increments == null) {
return false;
}
for (Statement increment : parent.increments) {
if (increment != null && increment.equals(childJDT)) {
return true;
}
}
return false;
}
private boolean isContainedInForCondition() {
if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ForStatement)) {
return false;
}
final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().getCurrentNode();
return parent.condition != null && parent.condition.equals(childJDT);
}
@Override
public void visitCtForEach(CtForEach foreach) {
if (foreach.getVariable() == null && child instanceof CtLocalVariable<?>) {
foreach.setVariable((CtLocalVariable<?>) child);
} else if (foreach.getExpression() == null && child instanceof CtExpression) {
foreach.setExpression((CtExpression<?>) child);
} else {
super.visitCtForEach(foreach);
}
}
@Override
public void visitCtWhile(CtWhile whileLoop) {
if (whileLoop.getLoopingExpression() == null && child instanceof CtExpression) {
whileLoop.setLoopingExpression((CtExpression<Boolean>) child);
return;
}
super.visitCtWhile(whileLoop);
}
@Override
public void visitCtIf(CtIf ifElement) {
if (ifElement.getCondition() == null && child instanceof CtExpression) {
ifElement.setCondition((CtExpression<Boolean>) child);
return;
} else if (child instanceof CtStatement) {
CtStatement child = (CtStatement) this.child;
// we create implicit blocks everywhere for facilitating transformation
if (!(this.child instanceof CtBlock)) {
child = jdtTreeBuilder.getFactory().Code().createCtBlock(child);
child.setImplicit(true);
child.setPosition(this.child.getPosition());
}
IfStatement ifJDT = (IfStatement) this.parentPair.node;
if (ifJDT.thenStatement == this.childJDT) {
//we are visiting `then` of `if`
ifElement.setThenStatement(child);
return;
} else if (ifJDT.elseStatement == this.childJDT) {
//we are visiting `else` of `if`
ifElement.setElseStatement(child);
return;
} else {
throw new SpoonException("Unexpected call of ParentExiter on CtIf");
}
}
super.visitCtIf(ifElement);
}
@Override
public <T> void visitCtSuperAccess(CtSuperAccess<T> superAccess) {
if (child instanceof CtTypeAccess<?>) {
superAccess.setTarget((CtTypeAccess<?>) child);
return;
}
super.visitCtSuperAccess(superAccess);
}
@Override
public <T> void visitCtInvocation(CtInvocation<T> invocation) {
if (childJDT instanceof TypeReference && child instanceof CtTypeAccess) {
invocation.getExecutable().addActualTypeArgument(((CtTypeAccess) child).getAccessedType());
return;
} else if (child instanceof CtExpression) {
if (hasChildEqualsToReceiver(invocation) || hasChildEqualsToQualification(invocation)) {
if (child instanceof CtThisAccess) {
if (!setTargetFromUnqualifiedAccess(invocation)) {
final CtTypeReference<?> declaringType = invocation.getExecutable().getDeclaringType();
if (declaringType != null && invocation.getExecutable().isStatic() && child.isImplicit()) {
invocation.setTarget(jdtTreeBuilder.getFactory().Code().createTypeAccess(declaringType, true));
} else {
invocation.setTarget((CtThisAccess<?>) child);
}
}
} else {
invocation.setTarget((CtExpression<?>) child);
}
} else {
invocation.addArgument((CtExpression<?>) child);
}
return;
}
super.visitCtInvocation(invocation);
}
private <T> boolean setTargetFromUnqualifiedAccess(CtInvocation<T> invocation) {
// A call to a statically imported method (e.g. assertTrue(false)) is modelled as
// "this.assertTrue(false)" by JDT. We need to unscramble that heuristically and replace the
// "this" reference with the correct type (e.g. org.junit.api.Assertions)
// Additionally, references to methods of enclosing classes are also modelled as "this" by JDT.
// Compare with Test "correctlySetsThisTargetForUnqualifiedCalls".
// We need a MessageSend as the parent to resolve the actualType from the receiver
if (!(parentPair.node instanceof MessageSend)) {
return false;
}
MessageSend messageSend = (MessageSend) parentPair.node;
if (messageSend.actualReceiverType == null || messageSend.receiver.resolvedType == null) {
return false;
}
ReferenceBuilder referenceBuilder = jdtTreeBuilder.getReferencesBuilder();
CtTypeReference<?> actualReceiverType = referenceBuilder.getTypeReference(messageSend.actualReceiverType);
CtTypeReference<?> resolvedReceiverType = referenceBuilder.getTypeReference(messageSend.receiver.resolvedType);
// If they match we have a normal "this" reference
if (actualReceiverType.equals(resolvedReceiverType)) {
return false;
}
if (messageSend.binding() == null || !messageSend.binding().isStatic()) {
// Emulate outer this access
while (resolvedReceiverType != null) {
resolvedReceiverType = resolvedReceiverType.getDeclaringType();
if (actualReceiverType.equals(resolvedReceiverType)) {
invocation.setTarget(jdtTreeBuilder.getFactory().Code().createThisAccess(actualReceiverType, true));
return true;
}
}
// I don't think this can happen but let's be conservative and preserve the previous behaviour
return false;
}
// If not, we probably had a static import/static outer method reference here and should use the actual type
// instead
invocation.setTarget(jdtTreeBuilder.getFactory().Code().createTypeAccess(actualReceiverType, true));
return true;
}
private <T> boolean hasChildEqualsToQualification(CtInvocation<T> ctInvocation) {
if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ExplicitConstructorCall)) {
return false;
}
final ExplicitConstructorCall parent = (ExplicitConstructorCall) jdtTreeBuilder.getContextBuilder().getCurrentNode();
// qualification is equals to the jdt child.
return parent.qualification != null && getFinalExpressionFromCast(parent.qualification).equals(childJDT)
// qualification not yet initialized.
&& !child.equals(ctInvocation.getTarget());
}
private <T> boolean hasChildEqualsToReceiver(CtInvocation<T> ctInvocation) {
if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof MessageSend)) {
return false;
}
final MessageSend parent = (MessageSend) jdtTreeBuilder.getContextBuilder().getCurrentNode();
// Receiver is equals to the jdt child.
return parent.receiver != null && getFinalExpressionFromCast(parent.receiver).equals(childJDT)
// Receiver not yet initialized.
&& !child.equals(ctInvocation.getTarget());
}
private Expression getFinalExpressionFromCast(Expression potentialCase) {
if (!(potentialCase instanceof CastExpression)) {
return potentialCase;
}
return getFinalExpressionFromCast(((CastExpression) potentialCase).expression);
}
@Override
public <T> void visitCtNewArray(CtNewArray<T> newArray) {
if (childJDT instanceof TypeReference && child instanceof CtTypeAccess) {
final ArrayAllocationExpression arrayAlloc = (ArrayAllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode();
newArray.setType((CtArrayTypeReference) jdtTreeBuilder.getFactory().Type().createArrayReference(((CtTypeAccess) child).getAccessedType(), arrayAlloc.dimensions.length));
} else if (child instanceof CtExpression) {
if (isContainedInDimensionExpression()) {
newArray.addDimensionExpression((CtExpression<Integer>) child);
} else if (child instanceof CtNewArray && childJDT instanceof ArrayInitializer && jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ArrayAllocationExpression) {
newArray.setElements(((CtNewArray) child).getElements());
} else {
newArray.addElement((CtExpression) child);
}
}
}
private boolean isContainedInDimensionExpression() {
if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ArrayAllocationExpression)) {
return false;
}
final ArrayAllocationExpression parent = (ArrayAllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode();
if (parent.dimensions == null) {
return false;
}
for (Expression dimension : parent.dimensions) {
if (dimension != null && getFinalExpressionFromCast(dimension).equals(childJDT)) {
return true;
}
}
return false;
}
@Override
public <T> void visitCtConstructorCall(CtConstructorCall<T> ctConstructorCall) {
if (child instanceof CtTypeAccess) {
if (hasChildEqualsToType(ctConstructorCall)) {
ctConstructorCall.getExecutable().setType(((CtTypeAccess) child).getAccessedType());
} else {
ctConstructorCall.addActualTypeArgument(((CtTypeAccess) child).getAccessedType());
}
return;
} else if (child instanceof CtExpression) {
if (hasChildEqualsToEnclosingInstance(ctConstructorCall)) {
ctConstructorCall.setTarget((CtExpression<?>) child);
} else {
ctConstructorCall.addArgument((CtExpression<?>) child);
}
return;
}
super.visitCtConstructorCall(ctConstructorCall);
}
private <T> boolean hasChildEqualsToEnclosingInstance(CtConstructorCall<T> ctConstructorCall) {
if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof QualifiedAllocationExpression)) {
return false;
}
final QualifiedAllocationExpression parent = (QualifiedAllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode();
// Enclosing instance is equals to the jdt child.
return parent.enclosingInstance != null && getFinalExpressionFromCast(parent.enclosingInstance).equals(childJDT)
// Enclosing instance not yet initialized.
&& !child.equals(ctConstructorCall.getTarget());
}
private <T> boolean hasChildEqualsToType(CtConstructorCall<T> ctConstructorCall) {
final AllocationExpression parent = (AllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode();
// Type is equals to the jdt child.
return parent.type != null && parent.type.equals(childJDT);
}
@Override
public <T> void visitCtNewClass(CtNewClass<T> newClass) {
if (child instanceof CtClass) {
newClass.setAnonymousClass((CtClass<?>) child);
final QualifiedAllocationExpression node = (QualifiedAllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode();
final ReferenceBinding[] referenceBindings = node.resolvedType == null ? null : node.resolvedType.superInterfaces();
if (referenceBindings != null && referenceBindings.length > 0) {
//the interface of anonymous class is not printed so it must have no position
//note: the interface is sometimes already assigned so call setSuperInterfaces to replace it
((CtClass<?>) child).setSuperInterfaces(Collections.singleton(cloneAsImplicit(newClass.getType())));
} else if (newClass.getType() != null) {
//the super class of anonymous class is not printed so it must have no position
((CtClass<?>) child).setSuperclass(cloneAsImplicit(newClass.getType()));
}
return;
}
super.visitCtNewClass(newClass);
}
private <T extends CtElement> T cloneAsImplicit(T ele) {
ele = (T) ele.clone();
ele.accept(new CtScanner() {
@Override
protected void enter(CtElement e) {
e.setPosition(SourcePosition.NOPOSITION);
}
});
ele.setImplicit(true);
return ele;
}
@Override
public <T> void visitCtLambda(CtLambda<T> lambda) {
if (child instanceof CtParameter) {
lambda.addParameter((CtParameter<?>) child);
return;
} else if (child instanceof CtBlock) {
lambda.setBody((CtBlock) child);
return;
} else if (child instanceof CtExpression) {
lambda.setExpression((CtExpression<T>) child);
}
super.visitCtLambda(lambda);
}
@Override
public <T, E extends CtExpression<?>> void visitCtExecutableReferenceExpression(CtExecutableReferenceExpression<T, E> expression) {
if (child instanceof CtExpression) {
expression.setTarget((E) child);
} else if (child instanceof CtTypeParameterReference) {
expression.getExecutable().addActualTypeArgument((CtTypeReference<?>) child);
}
super.visitCtExecutableReferenceExpression(expression);
}
@Override
public void visitCtPackage(CtPackage ctPackage) {
if (child instanceof CtType) {
CtType<?> type = (CtType<?>) child;
if (ctPackage.getTypes().contains(type)) {
ctPackage.removeType(type);
}
ctPackage.addType(type);
CompilationUnit cu = type.getPosition().getCompilationUnit();
if (cu != null) {
cu.addDeclaredType(type);
}
return;
}
super.visitCtPackage(ctPackage);
}
@Override
public <R> void visitCtReturn(CtReturn<R> returnStatement) {
if (child instanceof CtExpression) {
returnStatement.setReturnedExpression((CtExpression<R>) child);
return;
}
super.visitCtReturn(returnStatement);
}
@Override
public <E> void visitCtSwitch(CtSwitch<E> switchStatement) {
if (switchStatement.getSelector() == null && child instanceof CtExpression) {
switchStatement.setSelector((CtExpression<E>) child);
return;