Skip to content

Commit be38e3a

Browse files
authored
Merge pull request #8674 from lahodaj/jackpot-rules-resolve-target-typing
Fixing Jackpot rules when target typing fails, and the AST contains error types.
2 parents 53e8866 + f08f21e commit be38e3a

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
$mods$ long $test = $init :: $init instanceof java.lang.String
21+
=>
22+
$mods$ java.lang.String $test = $init
23+
;;
24+
25+
$test = $init :: $test instanceof String && $init instanceof int
26+
=>
27+
$test = String.valueOf($init)
28+
;;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
%%TestCase declaration
20+
package test;
21+
public class Test {
22+
private void test() {
23+
long l = "";
24+
}
25+
}
26+
%%=>
27+
package test;
28+
public class Test {
29+
private void test() {
30+
String l = "";
31+
}
32+
}
33+
%%TestCase assignment
34+
package test;
35+
public class Test {
36+
private void test() {
37+
String t;
38+
t = 0;
39+
}
40+
}
41+
%%=>
42+
package test;
43+
public class Test {
44+
private void test() {
45+
String t;
46+
t = String.valueOf(0);
47+
}
48+
}

java/java.source.base/src/org/netbeans/modules/java/source/matching/CopyFinder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import javax.lang.model.element.TypeElement;
102102
import javax.lang.model.element.VariableElement;
103103
import javax.lang.model.type.DeclaredType;
104+
import javax.lang.model.type.ErrorType;
104105
import javax.lang.model.type.TypeKind;
105106
import javax.lang.model.type.TypeMirror;
106107
import org.netbeans.api.annotations.common.CheckForNull;
@@ -324,6 +325,10 @@ private boolean typeMatches(TreePath currentPath, String placeholderName) {
324325

325326
if (designed != null && designed.getKind() != TypeKind.ERROR) {
326327
TypeMirror real = info.getTrees().getTypeMirror(currentPath);
328+
if (real != null && real.getKind() == TypeKind.ERROR) {
329+
ErrorType et = (ErrorType) real;
330+
real = info.getTrees().getOriginalType(et);
331+
}
327332
if (real != null && !IGNORE_KINDS.contains(real.getKind())) {
328333
// special hack: if the designed type is DECLARED (assuming a boxed primitive) and the real type is
329334
// not DECLARED or is null (assuming a real primitive), do not treat them as assignable.

java/spi.java.hints/test/unit/src/org/netbeans/spi/java/hints/matching/CopyFinderTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,51 @@ public void testInsidePatternMatchingInstanceOf() throws Exception {
13321332
false);
13331333
}
13341334

1335+
public void testTargetTyping1() throws Exception {
1336+
sourceLevel = "17";
1337+
performVariablesTest("""
1338+
package test;
1339+
public class Test {
1340+
boolean test() {
1341+
long l = "";
1342+
}
1343+
}
1344+
""",
1345+
"long $var = $1{java.lang.String};",
1346+
new Pair[] {
1347+
new Pair<String, int[]>("$1", new int[] {72, 74}),
1348+
new Pair<String, int[]>("$var", new int[] {63, 75}),
1349+
},
1350+
new Pair[0],
1351+
new Pair[] {
1352+
new Pair<String, String>("$var", "l"),
1353+
},
1354+
false,
1355+
false);
1356+
}
1357+
1358+
public void testTargetTyping2() throws Exception {
1359+
sourceLevel = "17";
1360+
performVariablesTest("""
1361+
package test;
1362+
public class Test {
1363+
boolean test() {
1364+
long l;
1365+
l = "";
1366+
}
1367+
}
1368+
""",
1369+
"$0{long} = $1{java.lang.String};",
1370+
new Pair[] {
1371+
new Pair<String, int[]>("$1", new int[] {83, 85}),
1372+
new Pair<String, int[]>("$0", new int[] {79, 80}),
1373+
},
1374+
new Pair[0],
1375+
new Pair[0],
1376+
false,
1377+
false);
1378+
}
1379+
13351380
public void testKeepImplicitThis() throws Exception {
13361381
prepareTest("package test; public class Test { void t() { toString(); } }", -1);
13371382

0 commit comments

Comments
 (0)