Skip to content

Commit

Permalink
GROOVY-9799
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 10, 2020
1 parent a3fc12b commit c9f1748
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5199,4 +5199,31 @@ public void testCompileStatic9786() {

runConformTest(sources, "B");
}

@Test
public void testCompileStatic9799() {
assumeTrue(isParrotParser());

//@formatter:off
String[] sources = {
"Main.groovy",
"class C {\n" +
" String x\n" +
"}\n" +
"class D {\n" +
" String x\n" +
" static D from(C c) {\n" +
" new D(x: c.x)\n" +
" }\n" +
"}\n" +
"@groovy.transform.CompileStatic\n" +
"void test(C c) {\n" +
" print Optional.of(c).map(D::from).map(D::getX).get()\n" +
"}\n" +
"test(new C(x: 'works'))\n",
};
//@formatter:on

runConformTest(sources, "works");
}
}
3 changes: 1 addition & 2 deletions base/org.codehaus.groovy30/.checkstyle
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
<file-match-pattern match-pattern="groovy/ast/expr/ConstructorCallExpression.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/ast/expr/RangeExpression.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/ast/expr/(Static)?MethodCallExpression.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/ast/tools/ExpressionUtils.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/ast/tools/GenericsUtils.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/ast/tools/(Expression|Generics|Parameter)Utils.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/AnnotationVisitor.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/EnumVisitor.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/ExtendedVerifier.java" include-pattern="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.codehaus.groovy.ast.tools;

import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.Parameter;

import java.util.function.BiPredicate;

public class ParameterUtils {

public static boolean parametersEqual(Parameter[] a, Parameter[] b) {
return parametersEqual(a, b, false);
}

public static boolean parametersEqualWithWrapperType(Parameter[] a, Parameter[] b) {
return parametersEqual(a, b, true);
}

/**
* Checks if two parameter arrays are type-compatible.
*
* each parameter should match the following condition:
* {@code targetParameter.getType().getTypeClass().isAssignableFrom(sourceParameter.getType().getTypeClass())}
*
* @param source source parameters
* @param target target parameters
* @return the check result
* @since 3.0.0
*/
public static boolean parametersCompatible(Parameter[] source, Parameter[] target) {
/* GRECLIPSE edit -- GROOVY-9799
return parametersMatch(source, target, (sourceType, targetType) ->
ClassHelper.getWrapper(targetType).getTypeClass().isAssignableFrom(ClassHelper.getWrapper(sourceType).getTypeClass())
);
*/
return parametersMatch(source, target, org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport::isAssignableTo);
// GRECLIPSE end
}

private static boolean parametersEqual(Parameter[] a, Parameter[] b, boolean wrapType) {
return parametersMatch(a, b, (aType, bType) -> {
if (wrapType) {
aType = ClassHelper.getWrapper(aType);
bType = ClassHelper.getWrapper(bType);
}
return aType.equals(bType);
});
}

private static boolean parametersMatch(Parameter[] a, Parameter[] b, BiPredicate<ClassNode, ClassNode> typeChecker) {
if (a.length == b.length) {
boolean answer = true;
for (int i = 0, n = a.length; i < n; i += 1) {
ClassNode aType = a[i].getType();
ClassNode bType = b[i].getType();

if (!typeChecker.test(aType, bType)) {
answer = false;
break;
}
}
return answer;
}
return false;
}
}
Loading

0 comments on commit c9f1748

Please sign in to comment.