-
Notifications
You must be signed in to change notification settings - Fork 86
/
FlowAnalyzer.java
1099 lines (957 loc) · 30.9 KB
/
FlowAnalyzer.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, 2021 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.code.flow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.ArrayAccess;
import org.eclipse.jdt.core.dom.ArrayCreation;
import org.eclipse.jdt.core.dom.ArrayInitializer;
import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.AssertStatement;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.BooleanLiteral;
import org.eclipse.jdt.core.dom.BreakStatement;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.CatchClause;
import org.eclipse.jdt.core.dom.CharacterLiteral;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ConditionalExpression;
import org.eclipse.jdt.core.dom.ConstructorInvocation;
import org.eclipse.jdt.core.dom.ContinueStatement;
import org.eclipse.jdt.core.dom.DoStatement;
import org.eclipse.jdt.core.dom.EmptyStatement;
import org.eclipse.jdt.core.dom.EnhancedForStatement;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionMethodReference;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.Initializer;
import org.eclipse.jdt.core.dom.InstanceofExpression;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.LabeledStatement;
import org.eclipse.jdt.core.dom.LambdaExpression;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.NameQualifiedType;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jdt.core.dom.NullLiteral;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.PostfixExpression;
import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.QualifiedType;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
import org.eclipse.jdt.core.dom.SuperFieldAccess;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.SwitchCase;
import org.eclipse.jdt.core.dom.SwitchExpression;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.SynchronizedStatement;
import org.eclipse.jdt.core.dom.ThisExpression;
import org.eclipse.jdt.core.dom.ThrowStatement;
import org.eclipse.jdt.core.dom.TryStatement;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclarationStatement;
import org.eclipse.jdt.core.dom.TypeLiteral;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.core.dom.WhileStatement;
import org.eclipse.jdt.core.dom.WildcardType;
import org.eclipse.jdt.internal.corext.dom.GenericVisitor;
/**
* Special flow analyzer to determine the return value of the extracted method
* and the variables which have to be passed to the method.
*
* Note: This analyzer doesn't do a full flow analysis. For example it doesn't
* do dead code analysis or variable initialization analysis. It analyses the
* the first access to a variable (read or write) and if all execution paths
* return a value.
*/
abstract class FlowAnalyzer extends GenericVisitor {
static protected class SwitchData {
private boolean fHasDefaultCase;
private List<IRegion> fRanges= new ArrayList<>(4);
private List<FlowInfo> fInfos= new ArrayList<>(4);
public void setHasDefaultCase() {
fHasDefaultCase= true;
}
public boolean hasDefaultCase() {
return fHasDefaultCase;
}
public void add(IRegion range, FlowInfo info) {
fRanges.add(range);
fInfos.add(info);
}
public IRegion[] getRanges() {
return fRanges.toArray(new IRegion[fRanges.size()]);
}
public FlowInfo[] getInfos() {
return fInfos.toArray(new FlowInfo[fInfos.size()]);
}
public FlowInfo getInfo(int index) {
return fInfos.get(index);
}
}
private HashMap<ASTNode, FlowInfo> fData = new HashMap<>(100);
/* package */ FlowContext fFlowContext= null;
public FlowAnalyzer(FlowContext context) {
fFlowContext= context;
}
protected abstract boolean createReturnFlowInfo(ReturnStatement node);
protected abstract boolean traverseNode(ASTNode node);
protected boolean skipNode(ASTNode node) {
return !traverseNode(node);
}
@Override
protected final boolean visitNode(ASTNode node) {
return traverseNode(node);
}
//---- Hooks to create Flow info objects. User may introduce their own infos.
protected ReturnFlowInfo createReturn(ReturnStatement statement) {
return new ReturnFlowInfo(statement);
}
protected ThrowFlowInfo createThrow() {
return new ThrowFlowInfo();
}
protected BranchFlowInfo createBranch(SimpleName label) {
return new BranchFlowInfo(label);
}
protected GenericSequentialFlowInfo createSequential() {
return new GenericSequentialFlowInfo();
}
protected ConditionalFlowInfo createConditional() {
return new ConditionalFlowInfo();
}
protected EnhancedForFlowInfo createEnhancedFor() {
return new EnhancedForFlowInfo();
}
protected ForFlowInfo createFor() {
return new ForFlowInfo();
}
protected TryFlowInfo createTry() {
return new TryFlowInfo();
}
protected WhileFlowInfo createWhile() {
return new WhileFlowInfo();
}
protected IfFlowInfo createIf() {
return new IfFlowInfo();
}
protected DoWhileFlowInfo createDoWhile() {
return new DoWhileFlowInfo();
}
protected SwitchFlowInfo createSwitch() {
return new SwitchFlowInfo();
}
protected BlockFlowInfo createBlock() {
return new BlockFlowInfo();
}
protected MessageSendFlowInfo createMessageSendFlowInfo() {
return new MessageSendFlowInfo();
}
protected FlowContext getFlowContext() {
return fFlowContext;
}
//---- Helpers to access flow analysis objects ----------------------------------------
protected FlowInfo getFlowInfo(ASTNode node) {
return fData.remove(node);
}
protected void setFlowInfo(ASTNode node, FlowInfo info) {
fData.put(node, info);
}
protected FlowInfo assignFlowInfo(ASTNode target, ASTNode source) {
FlowInfo result= getFlowInfo(source);
setFlowInfo(target, result);
return result;
}
protected FlowInfo accessFlowInfo(ASTNode node) {
return fData.get(node);
}
//---- Helpers to process sequential flow infos -------------------------------------
protected GenericSequentialFlowInfo processSequential(ASTNode parent, List<? extends ASTNode> nodes) {
GenericSequentialFlowInfo result= createSequential(parent);
process(result, nodes);
return result;
}
protected GenericSequentialFlowInfo processSequential(ASTNode parent, ASTNode node1) {
GenericSequentialFlowInfo result= createSequential(parent);
if (node1 != null)
result.merge(getFlowInfo(node1), fFlowContext);
return result;
}
protected GenericSequentialFlowInfo processSequential(ASTNode parent, ASTNode node1, ASTNode node2) {
GenericSequentialFlowInfo result= createSequential(parent);
if (node1 != null)
result.merge(getFlowInfo(node1), fFlowContext);
if (node2 != null)
result.merge(getFlowInfo(node2), fFlowContext);
return result;
}
protected GenericSequentialFlowInfo createSequential(ASTNode parent) {
GenericSequentialFlowInfo result= createSequential();
setFlowInfo(parent, result);
return result;
}
protected GenericSequentialFlowInfo createSequential(List<? extends ASTNode> nodes) {
GenericSequentialFlowInfo result= createSequential();
process(result, nodes);
return result;
}
//---- Generic merge methods --------------------------------------------------------
protected void process(GenericSequentialFlowInfo info, List<? extends ASTNode> nodes) {
if (nodes == null)
return;
for (ASTNode node : nodes) {
info.merge(getFlowInfo(node), fFlowContext);
}
}
protected void process(GenericSequentialFlowInfo info, ASTNode node) {
if (node != null)
info.merge(getFlowInfo(node), fFlowContext);
}
protected void process(GenericSequentialFlowInfo info, ASTNode node1, ASTNode node2) {
if (node1 != null)
info.merge(getFlowInfo(node1), fFlowContext);
if (node2 != null)
info.merge(getFlowInfo(node2), fFlowContext);
}
//---- special visit methods -------------------------------------------------------
@Override
public boolean visit(EmptyStatement node) {
// Empty statements aren't of any interest.
return false;
}
@Override
public boolean visit(TryStatement node) {
if (traverseNode(node)) {
fFlowContext.pushExcptions(node);
for (Iterator<Expression> iterator= node.resources().iterator(); iterator.hasNext();) {
iterator.next().accept(this);
}
node.getBody().accept(this);
fFlowContext.popExceptions();
List<CatchClause> catchClauses= node.catchClauses();
for (CatchClause catchClause : catchClauses) {
catchClause.accept(this);
}
Block finallyBlock= node.getFinally();
if (finallyBlock != null) {
finallyBlock.accept(this);
}
}
return false;
}
//---- Helper to process switch statement ----------------------------------------
protected SwitchData createSwitchData(SwitchStatement node) {
return createSwitchData(node.statements());
}
protected SwitchData createSwitchData(SwitchExpression node) {
return createSwitchData(node.statements());
}
protected SwitchData createSwitchData(List<Statement> statements) {
SwitchData result= new SwitchData();
if (statements == null || statements.isEmpty())
return result;
int start= -1, end= -1;
GenericSequentialFlowInfo info= null;
for (Statement statement : statements) {
if (statement instanceof SwitchCase) {
SwitchCase switchCase= (SwitchCase) statement;
if (switchCase.isDefault()) {
result.setHasDefaultCase();
}
if (info == null) {
info= createSequential();
start= statement.getStartPosition();
} else {
if (info.isReturn() || info.isPartialReturn() || info.branches()) {
result.add(new Region(start, end - start + 1), info);
info= createSequential();
start= statement.getStartPosition();
}
}
} else if (info != null) {
info.merge(getFlowInfo(statement), fFlowContext);
} else {
throw new IllegalStateException(statement.getClass().getName());
}
end= statement.getStartPosition() + statement.getLength() - 1;
}
result.add(new Region(start, end - start + 1), info);
return result;
}
protected void endVisit(SwitchStatement node, SwitchData data) {
preEndVisit(node, node.getExpression(), data);
}
protected void endVisit(SwitchExpression node, SwitchData data) {
preEndVisit(node, node.getExpression(), data);
}
private void preEndVisit(ASTNode node, Expression expression, SwitchData data) {
SwitchFlowInfo switchFlowInfo= createSwitch();
setFlowInfo(node, switchFlowInfo);
switchFlowInfo.mergeTest(getFlowInfo(expression), fFlowContext);
for (FlowInfo case1 : data.getInfos())
switchFlowInfo.mergeCase(case1, fFlowContext);
switchFlowInfo.mergeDefault(data.hasDefaultCase(), fFlowContext);
switchFlowInfo.removeLabel(null);
}
//---- concret endVisit methods ---------------------------------------------------
@Override
public void endVisit(AnnotationTypeDeclaration node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.bodyDeclarations());
info.setNoReturn();
}
@Override
public void endVisit(AnnotationTypeMemberDeclaration node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getType(), node.getDefault());
info.setNoReturn();
}
@Override
public void endVisit(AnonymousClassDeclaration node) {
if (skipNode(node))
return;
FlowInfo info= processSequential(node, node.bodyDeclarations());
info.setNoReturn();
}
@Override
public void endVisit(ArrayAccess node) {
if (skipNode(node))
return;
processSequential(node, node.getArray(), node.getIndex());
}
@Override
public void endVisit(ArrayCreation node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getType());
process(info, node.dimensions());
process(info, node.getInitializer());
}
@Override
public void endVisit(ArrayInitializer node) {
if (skipNode(node))
return;
processSequential(node, node.expressions());
}
@Override
public void endVisit(ArrayType node) {
if (skipNode(node))
return;
processSequential(node, node.getElementType());
}
@Override
public void endVisit(AssertStatement node) {
if (skipNode(node))
return;
IfFlowInfo info= new IfFlowInfo();
setFlowInfo(node, info);
info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext);
info.merge(getFlowInfo(node.getMessage()), null, fFlowContext);
}
@Override
public void endVisit(Assignment node) {
if (skipNode(node))
return;
FlowInfo lhs= getFlowInfo(node.getLeftHandSide());
FlowInfo rhs= getFlowInfo(node.getRightHandSide());
if (lhs instanceof LocalFlowInfo) {
LocalFlowInfo llhs= (LocalFlowInfo)lhs;
llhs.setWriteAccess(fFlowContext);
if (node.getOperator() != Assignment.Operator.ASSIGN) {
GenericSequentialFlowInfo tmp= createSequential();
tmp.merge(new LocalFlowInfo(llhs, FlowInfo.READ, fFlowContext), fFlowContext);
tmp.merge(rhs, fFlowContext);
rhs= tmp;
}
}
GenericSequentialFlowInfo info= createSequential(node);
// first process right and side and then left hand side.
info.merge(rhs, fFlowContext);
info.merge(lhs, fFlowContext);
}
@Override
public void endVisit(Block node) {
if (skipNode(node))
return;
BlockFlowInfo info= createBlock();
setFlowInfo(node, info);
process(info, node.statements());
}
@Override
public void endVisit(BooleanLiteral node) {
// Leaf node.
}
@Override
public void endVisit(BreakStatement node) {
if (skipNode(node))
return;
setFlowInfo(node, createBranch(node.getLabel()));
}
@Override
public void endVisit(CastExpression node) {
if (skipNode(node))
return;
processSequential(node, node.getType(), node.getExpression());
}
@Override
public void endVisit(CatchClause node) {
if (skipNode(node))
return;
processSequential(node, node.getException(), node.getBody());
}
@Override
public void endVisit(CharacterLiteral node) {
// Leaf node.
}
@Override
public void endVisit(ClassInstanceCreation node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getExpression());
process(info, node.getType());
process(info, node.arguments());
process(info, node.getAnonymousClassDeclaration());
}
@Override
public void endVisit(CompilationUnit node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.imports());
process(info, node.types());
}
@Override
public void endVisit(ConditionalExpression node) {
if (skipNode(node))
return;
ConditionalFlowInfo info= createConditional();
setFlowInfo(node, info);
info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext);
info.merge(
getFlowInfo(node.getThenExpression()),
getFlowInfo(node.getElseExpression()),
fFlowContext);
}
@Override
public void endVisit(ConstructorInvocation node) {
if (skipNode(node))
return;
processSequential(node, node.arguments());
}
@Override
public void endVisit(ContinueStatement node) {
if (skipNode(node))
return;
setFlowInfo(node, createBranch(node.getLabel()));
}
@Override
public void endVisit(DoStatement node) {
if (skipNode(node))
return;
DoWhileFlowInfo info= createDoWhile();
setFlowInfo(node, info);
info.mergeAction(getFlowInfo(node.getBody()));
info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext);
info.removeLabel(null);
}
@Override
public void endVisit(EmptyStatement node) {
// Leaf node.
}
@Override
public void endVisit(EnhancedForStatement node) {
if (skipNode(node))
return;
EnhancedForFlowInfo forInfo= createEnhancedFor();
setFlowInfo(node, forInfo);
forInfo.mergeParameter(getFlowInfo(node.getParameter()), fFlowContext);
forInfo.mergeExpression(getFlowInfo(node.getExpression()), fFlowContext);
forInfo.mergeAction(getFlowInfo(node.getBody()), fFlowContext);
forInfo.removeLabel(null);
}
@Override
public void endVisit(EnumConstantDeclaration node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.arguments());
process(info, node.getAnonymousClassDeclaration());
}
@Override
public void endVisit(EnumDeclaration node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.superInterfaceTypes());
process(info, node.enumConstants());
process(info, node.bodyDeclarations());
info.setNoReturn();
}
@Override
public void endVisit(ExpressionStatement node) {
if (skipNode(node))
return;
assignFlowInfo(node, node.getExpression());
}
@Override
public void endVisit(FieldAccess node) {
if (skipNode(node))
return;
processSequential(node, node.getExpression(), node.getName());
}
@Override
public void endVisit(FieldDeclaration node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getType());
process(info, node.fragments());
}
@Override
public void endVisit(ForStatement node) {
if (skipNode(node))
return;
ForFlowInfo forInfo= createFor();
setFlowInfo(node, forInfo);
forInfo.mergeInitializer(createSequential(node.initializers()), fFlowContext);
forInfo.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext);
forInfo.mergeAction(getFlowInfo(node.getBody()), fFlowContext);
// Increments are executed after the action.
forInfo.mergeIncrement(createSequential(node.updaters()), fFlowContext);
forInfo.removeLabel(null);
}
@Override
public void endVisit(IfStatement node) {
if (skipNode(node))
return;
IfFlowInfo info= createIf();
setFlowInfo(node, info);
info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext);
info.merge(getFlowInfo(node.getThenStatement()), getFlowInfo(node.getElseStatement()), fFlowContext);
}
@Override
public void endVisit(ImportDeclaration node) {
if (skipNode(node))
return;
assignFlowInfo(node, node.getName());
}
@Override
public void endVisit(InfixExpression node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getLeftOperand(), node.getRightOperand());
process(info, node.extendedOperands());
}
@Override
public void endVisit(InstanceofExpression node) {
if (skipNode(node))
return;
processSequential(node, node.getLeftOperand(), node.getRightOperand());
}
@Override
public void endVisit(Initializer node) {
if (skipNode(node))
return;
assignFlowInfo(node, node.getBody());
}
@Override
public void endVisit(Javadoc node) {
// no influence on flow analysis
}
@Override
public void endVisit(LabeledStatement node) {
if (skipNode(node))
return;
FlowInfo info= assignFlowInfo(node, node.getBody());
if (info != null)
info.removeLabel(node.getLabel());
}
@Override
public void endVisit(LambdaExpression node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= createSequential(node);
process(info, node.parameters());
process(info, node.getBody());
info.setNoReturn();
}
@Override
public void endVisit(MarkerAnnotation node) {
// nothing to do for marker annotations;
}
@Override
public void endVisit(MemberValuePair node) {
if (skipNode(node))
return;
FlowInfo name= getFlowInfo(node.getName());
FlowInfo value= getFlowInfo(node.getValue());
if (name instanceof LocalFlowInfo) {
LocalFlowInfo llhs= (LocalFlowInfo)name;
llhs.setWriteAccess(fFlowContext);
}
GenericSequentialFlowInfo info= createSequential(node);
// first process value and then name.
info.merge(value, fFlowContext);
info.merge(name, fFlowContext);
}
@Override
public void endVisit(MethodDeclaration node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getReturnType2());
process(info, node.parameters());
process(info, node.thrownExceptionTypes());
process(info, node.getBody());
}
@Override
public void endVisit(MethodInvocation node) {
endVisitMethodInvocation(node, node.getExpression(), node.arguments());
}
@Override
public void endVisit(NameQualifiedType node) {
if (skipNode(node))
return;
processSequential(node, node.getQualifier(), node.getName());
}
@Override
public void endVisit(NormalAnnotation node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getTypeName());
process(info, node.values());
}
@Override
public void endVisit(NullLiteral node) {
// Leaf node.
}
@Override
public void endVisit(NumberLiteral node) {
// Leaf node.
}
@Override
public void endVisit(PackageDeclaration node) {
if (skipNode(node))
return;
assignFlowInfo(node, node.getName());
}
@Override
public void endVisit(ParameterizedType node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getType());
process(info, node.typeArguments());
}
@Override
public void endVisit(ParenthesizedExpression node) {
if (skipNode(node))
return;
assignFlowInfo(node, node.getExpression());
}
@Override
public void endVisit(PostfixExpression node) {
endVisitIncDecOperation(node, node.getOperand());
}
@Override
public void endVisit(PrefixExpression node) {
PrefixExpression.Operator op= node.getOperator();
if (PrefixExpression.Operator.INCREMENT.equals(op) || PrefixExpression.Operator.DECREMENT.equals(op)) {
endVisitIncDecOperation(node, node.getOperand());
} else {
assignFlowInfo(node, node.getOperand());
}
}
@Override
public void endVisit(PrimitiveType node) {
// Leaf node
}
@Override
public void endVisit(QualifiedName node) {
if (skipNode(node))
return;
processSequential(node, node.getQualifier(), node.getName());
}
@Override
public void endVisit(QualifiedType node) {
if (skipNode(node))
return;
processSequential(node, node.getQualifier(), node.getName());
}
@Override
public void endVisit(ReturnStatement node) {
if (skipNode(node))
return;
if (createReturnFlowInfo(node)) {
ReturnFlowInfo info= createReturn(node);
setFlowInfo(node, info);
info.merge(getFlowInfo(node.getExpression()));
} else {
assignFlowInfo(node, node.getExpression());
}
}
@Override
public void endVisit(SimpleName node) {
if (skipNode(node) || node.isDeclaration())
return;
IBinding binding= node.resolveBinding();
if (binding instanceof IVariableBinding) {
IVariableBinding variable= (IVariableBinding)binding;
if (!variable.isField()) {
setFlowInfo(node, new LocalFlowInfo(
variable,
FlowInfo.READ,
fFlowContext));
}
} else if (binding instanceof ITypeBinding) {
ITypeBinding type= (ITypeBinding)binding;
if (type.isTypeVariable()) {
setFlowInfo(node, new TypeVariableFlowInfo(type));
}
}
}
@Override
public void endVisit(SimpleType node) {
if (skipNode(node))
return;
assignFlowInfo(node, node.getName());
}
@Override
public void endVisit(SingleMemberAnnotation node) {
if (skipNode(node))
return;
assignFlowInfo(node, node.getValue());
}
@Override
public void endVisit(SingleVariableDeclaration node) {
if (skipNode(node))
return;
IVariableBinding binding= node.resolveBinding();
LocalFlowInfo nameInfo= null;
Expression initializer= node.getInitializer();
if (binding != null && !binding.isField() && initializer != null) {
nameInfo= new LocalFlowInfo(binding, FlowInfo.WRITE, fFlowContext);
}
GenericSequentialFlowInfo info= processSequential(node, node.getType(), initializer);
info.merge(nameInfo, fFlowContext);
}
@Override
public void endVisit(StringLiteral node) {
// Leaf node
}
@Override
public void endVisit(SuperConstructorInvocation node) {
endVisitMethodInvocation(node, node.getExpression(), node.arguments());
}
@Override
public void endVisit(SuperFieldAccess node) {
if (skipNode(node))
return;
processSequential(node, node.getQualifier(), node.getName());
}
@Override
public void endVisit(SuperMethodInvocation node) {
endVisitMethodInvocation(node, node.getQualifier(), node.arguments());
}
@Override
public void endVisit(SwitchCase node) {
endVisitNode(node);
}
@Override
public void endVisit(SwitchStatement node) {
if (skipNode(node))
return;
endVisit(node, createSwitchData(node));
}
@Override
public void endVisit(SwitchExpression node) {
if (skipNode(node))
return;
endVisit(node, createSwitchData(node));
}
@Override
public void endVisit(SynchronizedStatement node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getExpression());
process(info, node.getBody());
}
@Override
public void endVisit(ThisExpression node) {
if (skipNode(node))
return;
assignFlowInfo(node, node.getQualifier());
}
@Override
public void endVisit(ThrowStatement node) {
if (skipNode(node))
return;
ThrowFlowInfo info= createThrow();
setFlowInfo(node, info);
Expression expression= node.getExpression();
info.merge(getFlowInfo(expression));
}
@Override
public void endVisit(TryStatement node) {
if (skipNode(node))
return;
TryFlowInfo info= createTry();
setFlowInfo(node, info);
for (Iterator<Expression> iterator= node.resources().iterator(); iterator.hasNext();) {
info.mergeResources(getFlowInfo(iterator.next()), fFlowContext);
}
info.mergeTry(getFlowInfo(node.getBody()), fFlowContext);
for (Iterator<CatchClause> iter= node.catchClauses().iterator(); iter.hasNext();) {
CatchClause element= iter.next();
info.mergeCatch(getFlowInfo(element), fFlowContext);
}
info.mergeFinally(getFlowInfo(node.getFinally()), fFlowContext);
}
// TODO account for enums and annotations
@Override
public void endVisit(TypeDeclaration node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getSuperclassType());
process(info, node.superInterfaceTypes());
process(info, node.bodyDeclarations());
info.setNoReturn();
}
@Override
public void endVisit(TypeDeclarationStatement node) {
if (skipNode(node))
return;