@@ -24,6 +24,13 @@ public static Optional<FuncLink> calcIterator(StmtForIn forEach) {
24
24
// find the 'hasNext' function without parameters
25
25
if (!iteratorFunc .isPresent ()) {
26
26
forEach .getIn ().addError ("For loop target " + itrType + " doesn't provide a iterator() function" );
27
+ } else {
28
+ FuncLink f = iteratorFunc .get ();
29
+ if (f .isStatic () && !itrType .isStaticRef ()) {
30
+ iterationTarget .addError ("Cannot use static iterator method from " + itrType + " on dynamic value." );
31
+ } else if (!f .isStatic () && itrType .isStaticRef ()) {
32
+ iterationTarget .addError ("Cannot use dynamic iterator method from " + itrType + " without a dynamic value." );
33
+ }
27
34
}
28
35
return iteratorFunc ;
29
36
}
@@ -36,6 +43,11 @@ public static Optional<FuncLink> calcHasNext(StmtForEach forEach) {
36
43
Optional <FuncLink > nextFunc = hasNext .stream ().filter (nl -> nl .getParameterTypes ().isEmpty ()).findFirst ();
37
44
if (!nextFunc .isPresent ()) {
38
45
forEach .getIn ().addError ("For loop iterator doesn't provide a hasNext() function that returns boolean" );
46
+ } else {
47
+ FuncLink f = nextFunc .get ();
48
+ if (f .isStatic ()) {
49
+ forEach .addError ("Cannot use a foreach loop here, because the 'hasNext' method " + iteratorType + " is static." );
50
+ }
39
51
}
40
52
return nextFunc ;
41
53
}
@@ -49,6 +61,11 @@ public static Optional<FuncLink> calcGetNext(StmtForEach forEach) {
49
61
Optional <FuncLink > nextFunc = next .stream ().filter (nl -> nl .getParameterTypes ().isEmpty ()).findFirst ();
50
62
if (!nextFunc .isPresent ()) {
51
63
forEach .getIn ().addError ("Target of for-loop '" + forEach .getIn ().attrTyp ().getName () + "' doesn't provide a proper next() function" );
64
+ } else {
65
+ FuncLink f = nextFunc .get ();
66
+ if (f .isStatic ()) {
67
+ forEach .addError ("Cannot use a foreach loop here, because the 'next' method " + iteratorType + " is static." );
68
+ }
52
69
}
53
70
return nextFunc ;
54
71
}
@@ -65,6 +82,11 @@ public static Optional<FuncLink> calcClose(StmtForEach forEach) {
65
82
Optional <FuncLink > closeFunc = close .stream ().filter (nl -> nl .getParameterTypes ().isEmpty ()).findFirst ();
66
83
if (!closeFunc .isPresent ()) {
67
84
forEach .getIn ().addError ("Target of for-loop <" + forEach .getIn ().attrTyp ().getName () + " doesn't provide a proper close() function" );
85
+ } else {
86
+ FuncLink f = closeFunc .get ();
87
+ if (f .isStatic ()) {
88
+ forEach .addError ("Cannot use a foreach loop here, because the 'close' method " + iteratorType + " is static." );
89
+ }
68
90
}
69
91
return closeFunc ;
70
92
}
0 commit comments