-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3fc12b
commit c9f1748
Showing
5 changed files
with
203 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
base/org.codehaus.groovy30/src/org/codehaus/groovy/ast/tools/ParameterUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.