|
| 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