From 6421fd555530187670bc3563e98d399f04357346 Mon Sep 17 00:00:00 2001 From: Federico Tomassetti Date: Wed, 26 Oct 2016 20:31:14 +0200 Subject: [PATCH] remove TypeParametersLogic --- .../JavaParserMethodDeclaration.java | 6 +- .../ReflectionMethodDeclaration.java | 6 +- .../logic/GenericTypeInferenceLogic.java | 51 +++++++++++ .../logic/TypeParametersLogic.java | 84 ------------------- 4 files changed, 57 insertions(+), 90 deletions(-) delete mode 100644 java-symbol-solver-logic/src/main/java/com/github/javaparser/symbolsolver/logic/TypeParametersLogic.java diff --git a/java-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserMethodDeclaration.java b/java-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserMethodDeclaration.java index 01f725e0ac..bb624d2034 100644 --- a/java-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserMethodDeclaration.java +++ b/java-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserMethodDeclaration.java @@ -22,10 +22,10 @@ import com.github.javaparser.symbolsolver.core.resolution.Context; import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory; -import com.github.javaparser.symbolsolver.logic.TypeParametersLogic; +import com.github.javaparser.symbolsolver.logic.GenericTypeInferenceLogic; import com.github.javaparser.symbolsolver.model.declarations.*; -import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.model.methods.MethodUsage; +import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.model.typesystem.Type; import java.util.*; @@ -103,7 +103,7 @@ public MethodUsage resolveTypeVariables(Context context, List parameterTyp for (int i = 0; i < getNumberOfParams(); i++) { Type formalParamType = getParam(i).getType(); Type actualParamType = parameterTypes.get(i); - TypeParametersLogic.determineTypeParameters(determinedTypeParameters, formalParamType, actualParamType, typeSolver); + GenericTypeInferenceLogic.determineTypeParameters(determinedTypeParameters, formalParamType, actualParamType, typeSolver); } Map inferredTypes = new HashMap<>(); diff --git a/java-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/reflectionmodel/ReflectionMethodDeclaration.java b/java-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/reflectionmodel/ReflectionMethodDeclaration.java index 05470858e1..47d76991fe 100644 --- a/java-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/reflectionmodel/ReflectionMethodDeclaration.java +++ b/java-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/reflectionmodel/ReflectionMethodDeclaration.java @@ -18,10 +18,10 @@ import com.github.javaparser.ast.Node; import com.github.javaparser.symbolsolver.core.resolution.Context; -import com.github.javaparser.symbolsolver.logic.TypeParametersLogic; +import com.github.javaparser.symbolsolver.logic.GenericTypeInferenceLogic; import com.github.javaparser.symbolsolver.model.declarations.*; -import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.model.methods.MethodUsage; +import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.model.typesystem.Type; import java.lang.reflect.Method; @@ -125,7 +125,7 @@ public MethodUsage resolveTypeVariables(Context context, List parameterTyp for (int i = 0; i < getNumberOfParams(); i++) { Type formalParamType = getParam(i).getType(); Type actualParamType = parameterTypes.get(i); - TypeParametersLogic.determineTypeParameters(determinedTypeParameters, formalParamType, actualParamType, typeSolver); + GenericTypeInferenceLogic.determineTypeParameters(determinedTypeParameters, formalParamType, actualParamType, typeSolver); } for (TypeParameterDeclaration determinedParam : determinedTypeParameters.keySet()) { diff --git a/java-symbol-solver-logic/src/main/java/com/github/javaparser/symbolsolver/logic/GenericTypeInferenceLogic.java b/java-symbol-solver-logic/src/main/java/com/github/javaparser/symbolsolver/logic/GenericTypeInferenceLogic.java index 6de95df4ed..17f448a1b6 100644 --- a/java-symbol-solver-logic/src/main/java/com/github/javaparser/symbolsolver/logic/GenericTypeInferenceLogic.java +++ b/java-symbol-solver-logic/src/main/java/com/github/javaparser/symbolsolver/logic/GenericTypeInferenceLogic.java @@ -17,13 +17,16 @@ package com.github.javaparser.symbolsolver.logic; import com.github.javaparser.symbolsolver.model.declarations.TypeParameterDeclaration; +import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType; import com.github.javaparser.symbolsolver.model.typesystem.Type; +import com.github.javaparser.symbolsolver.model.typesystem.Wildcard; import javaslang.Tuple2; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * @author Federico Tomassetti @@ -109,4 +112,52 @@ private static void consider(Map map, Type forma } } + public static void determineTypeParameters(Map determinedTypeParameters, + Type formalParamType, Type actualParamType, + TypeSolver typeSolver) { + if (actualParamType.isNull()) { + return; + } + if (actualParamType.isTypeVariable()) { + return; + } + if (formalParamType.isTypeVariable()) { + determinedTypeParameters.put(formalParamType.asTypeParameter(), actualParamType); + return; + } + if (formalParamType instanceof Wildcard) { + return; + } + if (formalParamType.isArray() && actualParamType.isArray()) { + determineTypeParameters( + determinedTypeParameters, + formalParamType.asArrayType().getComponentType(), + actualParamType.asArrayType().getComponentType(), + typeSolver); + return; + } + if (formalParamType.isReferenceType() && actualParamType.isReferenceType() + && !formalParamType.asReferenceType().getQualifiedName().equals(actualParamType.asReferenceType().getQualifiedName())) { + List ancestors = actualParamType.asReferenceType().getAllAncestors(); + final String formalParamTypeQName = formalParamType.asReferenceType().getQualifiedName(); + List correspondingFormalType = ancestors.stream().filter((a) -> a.getQualifiedName().equals(formalParamTypeQName)).collect(Collectors.toList()); + if (correspondingFormalType.isEmpty()) { + throw new IllegalArgumentException(); + } + actualParamType = correspondingFormalType.get(0); + } + if (formalParamType.isReferenceType() && actualParamType.isReferenceType()) { + if (formalParamType.asReferenceType().isRawType() || actualParamType.asReferenceType().isRawType()) { + return; + } + List formalTypeParams = formalParamType.asReferenceType().typeParametersValues(); + List actualTypeParams = actualParamType.asReferenceType().typeParametersValues(); + if (formalTypeParams.size() != actualTypeParams.size()) { + throw new UnsupportedOperationException(); + } + for (int i = 0; i < formalTypeParams.size(); i++) { + determineTypeParameters(determinedTypeParameters, formalTypeParams.get(i), actualTypeParams.get(i), typeSolver); + } + } + } } diff --git a/java-symbol-solver-logic/src/main/java/com/github/javaparser/symbolsolver/logic/TypeParametersLogic.java b/java-symbol-solver-logic/src/main/java/com/github/javaparser/symbolsolver/logic/TypeParametersLogic.java deleted file mode 100644 index 8cdf68cc08..0000000000 --- a/java-symbol-solver-logic/src/main/java/com/github/javaparser/symbolsolver/logic/TypeParametersLogic.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2016 Federico Tomassetti - * - * Licensed 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 com.github.javaparser.symbolsolver.logic; - -import com.github.javaparser.symbolsolver.model.declarations.TypeParameterDeclaration; -import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; -import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType; -import com.github.javaparser.symbolsolver.model.typesystem.Type; -import com.github.javaparser.symbolsolver.model.typesystem.Wildcard; - -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -/** - * @author Federico Tomassetti - */ -public final class TypeParametersLogic { - - private TypeParametersLogic() { - // prevent instantiation - } - - public static void determineTypeParameters(Map determinedTypeParameters, Type formalParamType, Type actualParamType, TypeSolver typeSolver) { - if (actualParamType.isNull()) { - return; - } - if (actualParamType.isTypeVariable()) { - return; - } - if (formalParamType.isTypeVariable()) { - determinedTypeParameters.put(formalParamType.asTypeParameter(), actualParamType); - return; - } - if (formalParamType instanceof Wildcard) { - return; - } - if (formalParamType.isArray() && actualParamType.isArray()) { - determineTypeParameters( - determinedTypeParameters, - formalParamType.asArrayType().getComponentType(), - actualParamType.asArrayType().getComponentType(), - typeSolver); - return; - } - if (formalParamType.isReferenceType() && actualParamType.isReferenceType() - && !formalParamType.asReferenceType().getQualifiedName().equals(actualParamType.asReferenceType().getQualifiedName())) { - List ancestors = actualParamType.asReferenceType().getAllAncestors(); - final String formalParamTypeQName = formalParamType.asReferenceType().getQualifiedName(); - List correspondingFormalType = ancestors.stream().filter((a) -> a.getQualifiedName().equals(formalParamTypeQName)).collect(Collectors.toList()); - if (correspondingFormalType.isEmpty()) { - throw new IllegalArgumentException(); - } - actualParamType = correspondingFormalType.get(0); - } - if (formalParamType.isReferenceType() && actualParamType.isReferenceType()) { - if (formalParamType.asReferenceType().isRawType() || actualParamType.asReferenceType().isRawType()) { - return; - } - List formalTypeParams = formalParamType.asReferenceType().typeParametersValues(); - List actualTypeParams = actualParamType.asReferenceType().typeParametersValues(); - if (formalTypeParams.size() != actualTypeParams.size()) { - throw new UnsupportedOperationException(); - } - for (int i = 0; i < formalTypeParams.size(); i++) { - determineTypeParameters(determinedTypeParameters, formalTypeParams.get(i), actualTypeParams.get(i), typeSolver); - } - } - } -}