|
| 1 | +package spoon.test.reference; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import spoon.reflect.CtModel; |
| 5 | +import spoon.reflect.code.CtLocalVariable; |
| 6 | +import spoon.reflect.code.CtTypePattern; |
| 7 | +import spoon.reflect.reference.CtFieldReference; |
| 8 | +import spoon.reflect.reference.CtLocalVariableReference; |
| 9 | +import spoon.reflect.visitor.filter.TypeFilter; |
| 10 | +import spoon.testing.utils.GitHubIssue; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 14 | +import static spoon.test.SpoonTestHelpers.createModelFromString; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests that references to pattern variables declared using the <code>instanceof</code> operator can be resolved. |
| 18 | + * Pattern matching for instanceof was introduced in Java 16, cf. <a href=https://openjdk.java.net/jeps/394>JEP 394</a>. |
| 19 | + * Variables declared in pattern matches have <a href="https://openjdk.org/projects/amber/design-notes/patterns/pattern-match-semantics">flow scope semantics</a>. |
| 20 | + */ |
| 21 | +public class InstanceOfReferenceTest { |
| 22 | + @Test |
| 23 | + public void testVariableDeclaredInIf() { |
| 24 | + String code = """ |
| 25 | + class X { |
| 26 | + String typePattern(Object obj) { |
| 27 | + boolean someCondition = true; |
| 28 | + if (someCondition && obj instanceof String s) { |
| 29 | + return s; |
| 30 | + } |
| 31 | + return ""; |
| 32 | + } |
| 33 | + } |
| 34 | + """; |
| 35 | + CtModel model = createModelFromString(code, 21); |
| 36 | + CtLocalVariable<?> variable = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 37 | + CtLocalVariableReference<?> ref = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)).get(1); |
| 38 | + var decl = ref.getDeclaration(); |
| 39 | + assertNotNull(decl); |
| 40 | + assertEquals(variable, decl); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testVariableDeclaredInWhileLoop() { |
| 45 | + String code = """ |
| 46 | + class X { |
| 47 | + public void processShapes(List<Object> shapes) { |
| 48 | + var iter = 0; |
| 49 | + while (iter < shapes.size() && shapes.get(iter) instanceof String shape) { |
| 50 | + iter++; |
| 51 | + System.out.println(shape); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + """; |
| 56 | + CtModel model = createModelFromString(code, 21); |
| 57 | + CtLocalVariable<?> variable = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 58 | + CtLocalVariableReference<?> ref = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)).get(3); |
| 59 | + var decl = ref.getDeclaration(); |
| 60 | + assertNotNull(decl); |
| 61 | + assertEquals(variable, decl); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testVariableDeclaredInForLoop() { |
| 66 | + String code = """ |
| 67 | + class X { |
| 68 | + public void processShapes(List<Object> shapes) { |
| 69 | + for (var iter = 0; iter < shapes.size() && shapes.get(iter) instanceof String shape; iter++) { |
| 70 | + System.out.println(shape); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + """; |
| 75 | + CtModel model = createModelFromString(code, 21); |
| 76 | + CtLocalVariable<?> variable = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 77 | + CtLocalVariableReference<?> ref = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)).get(3); |
| 78 | + var decl = ref.getDeclaration(); |
| 79 | + assertNotNull(decl); |
| 80 | + assertEquals(variable, decl); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testDeclaredVariableUsedInSameCondition() { |
| 85 | + String code = """ |
| 86 | + class X { |
| 87 | + public void processShapes(Object obj) { |
| 88 | + if (obj instanceof String s && s.length() > 5) { |
| 89 | + // NOP |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + """; |
| 94 | + CtModel model = createModelFromString(code, 21); |
| 95 | + CtLocalVariable<?> variable = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 96 | + CtLocalVariableReference<?> ref = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)).get(0); |
| 97 | + var decl = ref.getDeclaration(); |
| 98 | + assertNotNull(decl); |
| 99 | + assertEquals(variable, decl); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testDeclaredVariableUsedInSameCondition2() { |
| 104 | + String code = """ |
| 105 | + class X { |
| 106 | + public void hasRightSize(Shape s) throws MyException { |
| 107 | + return s instanceof Circle c && c.getRadius() > 10; |
| 108 | + } |
| 109 | + } |
| 110 | + """; |
| 111 | + CtModel model = createModelFromString(code, 21); |
| 112 | + CtLocalVariable<?> variable = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 113 | + CtLocalVariableReference<?> ref = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)).get(0); |
| 114 | + var decl = ref.getDeclaration(); |
| 115 | + assertNotNull(decl); |
| 116 | + assertEquals(variable, decl); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testFlowScope() { |
| 121 | + String code = """ |
| 122 | + class X { |
| 123 | + public void onlyForStrings(Object o) throws MyException { |
| 124 | + if (!(o instanceof String s)) |
| 125 | + throw new MyException(); |
| 126 | + // s is in scope |
| 127 | + System.out.println(s); |
| 128 | + } |
| 129 | + } |
| 130 | + """; |
| 131 | + CtModel model = createModelFromString(code, 21); |
| 132 | + CtLocalVariable<?> variable = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 133 | + CtLocalVariableReference<?> ref = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)).get(0); |
| 134 | + var decl = ref.getDeclaration(); |
| 135 | + assertNotNull(decl); |
| 136 | + assertEquals(variable, decl); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testFlowScope2() { |
| 141 | + String code = """ |
| 142 | + class X { |
| 143 | + String s = "abc"; |
| 144 | +
|
| 145 | + public void method2(Object o) { |
| 146 | + if (!(o instanceof String s)) { |
| 147 | + System.out.println("not a string"); |
| 148 | + } else { |
| 149 | + System.out.println(s); // The local variable is in scope here! |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + """; |
| 154 | + CtModel model = createModelFromString(code, 21); |
| 155 | + CtLocalVariable<?> variable = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 156 | + CtLocalVariableReference<?> ref = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)).get(0); |
| 157 | + var decl = ref.getDeclaration(); |
| 158 | + assertNotNull(decl); |
| 159 | + assertEquals(variable, decl); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testFlowScope3() { |
| 164 | + String code = """ |
| 165 | + class X { |
| 166 | + String typePattern(Object obj) { |
| 167 | + if (obj instanceof String s) { |
| 168 | + System.out.println("It's a string"); |
| 169 | + } else { |
| 170 | + throw new RuntimeException("It's not a string"); |
| 171 | + } |
| 172 | + return s; // We can still access s here! |
| 173 | + } |
| 174 | + } |
| 175 | + """; |
| 176 | + CtModel model = createModelFromString(code, 21); |
| 177 | + CtLocalVariable<?> variable = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 178 | + CtLocalVariableReference<?> ref = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)).get(0); |
| 179 | + var decl = ref.getDeclaration(); |
| 180 | + assertNotNull(decl); |
| 181 | + assertEquals(variable, decl); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testCorrectScoping() { |
| 186 | + String code = """ |
| 187 | + class Example2 { |
| 188 | + Point p; |
| 189 | +
|
| 190 | + void test2(Object o) { |
| 191 | + if (o instanceof Point p) { |
| 192 | + // p refers to the pattern variable |
| 193 | + System.out.println(p); |
| 194 | + } else { |
| 195 | + // p refers to the field |
| 196 | + System.out.println(p); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + """; |
| 201 | + CtModel model = createModelFromString(code, 21); |
| 202 | + CtLocalVariable<?> variable = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 203 | + var refs = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)); |
| 204 | + assertEquals(1, refs.size()); |
| 205 | + var decl = refs.get(0).getDeclaration(); |
| 206 | + assertNotNull(decl); |
| 207 | + assertEquals(variable, decl); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void testRecordPatterns() { |
| 212 | + String code = """ |
| 213 | + record Point(int x, int y) {} |
| 214 | + record Circle(Point center, int radius) {} |
| 215 | + |
| 216 | + public class Y { |
| 217 | + public void test() { |
| 218 | + Object obj = new Circle(new Point(10, 20), 5); |
| 219 | + if (obj instanceof Circle(Point (int x, int y), int r)) { |
| 220 | + System.out.println("Object is a Circle at center (" + x + ", " + y + ") with radius " + r); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + """; |
| 225 | + CtModel model = createModelFromString(code, 21); |
| 226 | + CtLocalVariable<?> varX = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(0).getVariable(); |
| 227 | + CtLocalVariable<?> varY = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(1).getVariable(); |
| 228 | + CtLocalVariable<?> varR = model.getElements(new TypeFilter<>(CtTypePattern.class)).get(2).getVariable(); |
| 229 | + var refs = model.getElements(new TypeFilter<>(CtLocalVariableReference.class)); |
| 230 | + assertEquals(4, refs.size()); // includes reference to 'obj' |
| 231 | + var declX = refs.get(1).getDeclaration(); |
| 232 | + var declY = refs.get(2).getDeclaration(); |
| 233 | + var declR = refs.get(3).getDeclaration(); |
| 234 | + assertEquals(varX, declX); |
| 235 | + assertEquals(varY, declY); |
| 236 | + assertEquals(varR, declR); |
| 237 | + } |
| 238 | +} |
0 commit comments