File tree 3 files changed +42
-0
lines changed
java/ql/test/library-tests/dispatch
3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class CallableViaSummary {
4
+ public interface Element {
5
+ public void handle (String message );
6
+ }
7
+
8
+ public void main (String [] args ) {
9
+ List <Element > elements = new ArrayList <>();
10
+
11
+ List <Element > elements2 = new ArrayList <>();
12
+
13
+ elements .add (new Element () {
14
+ @ Override
15
+ public void handle (String message ) {
16
+ System .out .println (message );
17
+ }
18
+ });
19
+
20
+ elements .add (message -> System .out .println (message ));
21
+
22
+ // This dispatches to the two added elements because
23
+ // the summary of ArrayList causes flow via type tracking.
24
+ elements .get (0 ).handle ("Hello, world!" );
25
+
26
+ // This does not dispatch to anything, showing that the
27
+ // open-world assumption does not apply
28
+ // (and hence that type tracking is necessary above).
29
+ elements2 .get (0 ).handle ("Hello, world!" );
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ | CallableViaSummary.java:24:9:24:47 | handle(...) | CallableViaSummary.java:15:25:15:30 | handle |
2
+ | CallableViaSummary.java:24:9:24:47 | handle(...) | CallableViaSummary.java:20:22:20:59 | handle |
Original file line number Diff line number Diff line change
1
+ import java
2
+ import semmle.code.java.dispatch.VirtualDispatch
3
+
4
+ from MethodAccess ma , Method m
5
+ where
6
+ m = viableImpl ( ma ) and
7
+ m .fromSource ( ) and
8
+ ma .getFile ( ) .toString ( ) = "CallableViaSummary"
9
+ select ma , m
You can’t perform that action at this time.
0 commit comments