Skip to content

Commit c9f1748

Browse files
committed
GROOVY-9799
1 parent a3fc12b commit c9f1748

5 files changed

Lines changed: 203 additions & 134 deletions

File tree

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5199,4 +5199,31 @@ public void testCompileStatic9786() {
51995199

52005200
runConformTest(sources, "B");
52015201
}
5202+
5203+
@Test
5204+
public void testCompileStatic9799() {
5205+
assumeTrue(isParrotParser());
5206+
5207+
//@formatter:off
5208+
String[] sources = {
5209+
"Main.groovy",
5210+
"class C {\n" +
5211+
" String x\n" +
5212+
"}\n" +
5213+
"class D {\n" +
5214+
" String x\n" +
5215+
" static D from(C c) {\n" +
5216+
" new D(x: c.x)\n" +
5217+
" }\n" +
5218+
"}\n" +
5219+
"@groovy.transform.CompileStatic\n" +
5220+
"void test(C c) {\n" +
5221+
" print Optional.of(c).map(D::from).map(D::getX).get()\n" +
5222+
"}\n" +
5223+
"test(new C(x: 'works'))\n",
5224+
};
5225+
//@formatter:on
5226+
5227+
runConformTest(sources, "works");
5228+
}
52025229
}

base/org.codehaus.groovy30/.checkstyle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
<file-match-pattern match-pattern="groovy/ast/expr/ConstructorCallExpression.java" include-pattern="false" />
3838
<file-match-pattern match-pattern="groovy/ast/expr/RangeExpression.java" include-pattern="false" />
3939
<file-match-pattern match-pattern="groovy/ast/expr/(Static)?MethodCallExpression.java" include-pattern="false" />
40-
<file-match-pattern match-pattern="groovy/ast/tools/ExpressionUtils.java" include-pattern="false" />
41-
<file-match-pattern match-pattern="groovy/ast/tools/GenericsUtils.java" include-pattern="false" />
40+
<file-match-pattern match-pattern="groovy/ast/tools/(Expression|Generics|Parameter)Utils.java" include-pattern="false" />
4241
<file-match-pattern match-pattern="groovy/classgen/AnnotationVisitor.java" include-pattern="false" />
4342
<file-match-pattern match-pattern="groovy/classgen/EnumVisitor.java" include-pattern="false" />
4443
<file-match-pattern match-pattern="groovy/classgen/ExtendedVerifier.java" include-pattern="false" />
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
package org.codehaus.groovy.ast.tools;
20+
21+
import org.codehaus.groovy.ast.ClassHelper;
22+
import org.codehaus.groovy.ast.ClassNode;
23+
import org.codehaus.groovy.ast.Parameter;
24+
25+
import java.util.function.BiPredicate;
26+
27+
public class ParameterUtils {
28+
29+
public static boolean parametersEqual(Parameter[] a, Parameter[] b) {
30+
return parametersEqual(a, b, false);
31+
}
32+
33+
public static boolean parametersEqualWithWrapperType(Parameter[] a, Parameter[] b) {
34+
return parametersEqual(a, b, true);
35+
}
36+
37+
/**
38+
* Checks if two parameter arrays are type-compatible.
39+
*
40+
* each parameter should match the following condition:
41+
* {@code targetParameter.getType().getTypeClass().isAssignableFrom(sourceParameter.getType().getTypeClass())}
42+
*
43+
* @param source source parameters
44+
* @param target target parameters
45+
* @return the check result
46+
* @since 3.0.0
47+
*/
48+
public static boolean parametersCompatible(Parameter[] source, Parameter[] target) {
49+
/* GRECLIPSE edit -- GROOVY-9799
50+
return parametersMatch(source, target, (sourceType, targetType) ->
51+
ClassHelper.getWrapper(targetType).getTypeClass().isAssignableFrom(ClassHelper.getWrapper(sourceType).getTypeClass())
52+
);
53+
*/
54+
return parametersMatch(source, target, org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport::isAssignableTo);
55+
// GRECLIPSE end
56+
}
57+
58+
private static boolean parametersEqual(Parameter[] a, Parameter[] b, boolean wrapType) {
59+
return parametersMatch(a, b, (aType, bType) -> {
60+
if (wrapType) {
61+
aType = ClassHelper.getWrapper(aType);
62+
bType = ClassHelper.getWrapper(bType);
63+
}
64+
return aType.equals(bType);
65+
});
66+
}
67+
68+
private static boolean parametersMatch(Parameter[] a, Parameter[] b, BiPredicate<ClassNode, ClassNode> typeChecker) {
69+
if (a.length == b.length) {
70+
boolean answer = true;
71+
for (int i = 0, n = a.length; i < n; i += 1) {
72+
ClassNode aType = a[i].getType();
73+
ClassNode bType = b[i].getType();
74+
75+
if (!typeChecker.test(aType, bType)) {
76+
answer = false;
77+
break;
78+
}
79+
}
80+
return answer;
81+
}
82+
return false;
83+
}
84+
}

0 commit comments

Comments
 (0)