| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.resolution; |
| 23 | |
| 24 | import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration; |
| 25 | import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration; |
| 26 | import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration; |
| 27 | import com.github.javaparser.resolution.types.ResolvedType; |
| 28 | import com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametersMap; |
| 29 | import com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametrized; |
| 30 | |
| 31 | |
| 32 | import java.util.*; |
| 33 | |
| 34 | /** |
| 35 | * This is basically a MethodDeclaration with some TypeParameters defined. |
| 36 | * The defined TypeParameters can comes from the Method itself or from the surrounding types. |
| 37 | * |
| 38 | * @author Federico Tomassetti |
| 39 | */ |
| 40 | public class MethodUsage implements ResolvedTypeParametrized { |
| 41 | private ResolvedMethodDeclaration declaration; |
| 42 | private List<ResolvedType> paramTypes = new ArrayList<>(); |
| 43 | private List<ResolvedType> exceptionTypes = new ArrayList<>(); |
| 44 | private ResolvedType returnType; |
| 45 | private ResolvedTypeParametersMap typeParametersMap; |
| 46 | |
| 47 | public MethodUsage(ResolvedMethodDeclaration declaration) { |
| 48 | this.typeParametersMap = ResolvedTypeParametersMap.empty(); |
| 49 | this.declaration = declaration; |
| 50 | for (int i = 0; i < declaration.getNumberOfParams(); i++) { |
| 51 | paramTypes.add(declaration.getParam(i).getType()); |
| 52 | } |
| 53 | for (int i = 0; i < declaration.getNumberOfSpecifiedExceptions(); i++) { |
| 54 | exceptionTypes.add(declaration.getSpecifiedException(i)); |
| 55 | } |
| 56 | returnType = declaration.getReturnType(); |
| 57 | } |
| 58 | |
| 59 | public MethodUsage(ResolvedMethodDeclaration declaration, |
| 60 | List<ResolvedType> paramTypes, ResolvedType returnType) { |
| 61 | this(declaration, paramTypes, returnType, declaration.getSpecifiedExceptions(), |
| 62 | ResolvedTypeParametersMap.empty()); |
| 63 | } |
| 64 | |
| 65 | public MethodUsage(ResolvedMethodDeclaration declaration, List<ResolvedType> paramTypes, ResolvedType returnType, |
| 66 | List<ResolvedType> exceptionTypes) { |
| 67 | this(declaration, paramTypes, returnType, exceptionTypes, ResolvedTypeParametersMap.empty()); |
| 68 | } |
| 69 | |
| 70 | private MethodUsage(ResolvedMethodDeclaration declaration, List<ResolvedType> paramTypes, ResolvedType returnType, |
| 71 | List<ResolvedType> exceptionTypes, ResolvedTypeParametersMap typeParametersMap) { |
| 72 | this.declaration = declaration; |
| 73 | this.paramTypes = paramTypes; |
| 74 | this.returnType = returnType; |
| 75 | this.exceptionTypes = exceptionTypes; |
| 76 | this.typeParametersMap = typeParametersMap; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String toString() { |
| 81 | return "MethodUsage{" + |
| 82 | "declaration=" + declaration + |
| 83 | ", paramTypes=" + paramTypes + |
| 84 | '}'; |
| 85 | } |
| 86 | |
| 87 | public ResolvedMethodDeclaration getDeclaration() { |
| 88 | return declaration; |
| 89 | } |
| 90 | |
| 91 | public String getName() { |
| 92 | return declaration.getName(); |
| 93 | } |
| 94 | |
| 95 | public ResolvedReferenceTypeDeclaration declaringType() { |
| 96 | return declaration.declaringType(); |
| 97 | } |
| 98 | |
| 99 | public ResolvedType returnType() { |
| 100 | return returnType; |
| 101 | } |
| 102 | |
| 103 | public List<ResolvedType> getParamTypes() { |
| 104 | return paramTypes; |
| 105 | } |
| 106 | |
| 107 | public MethodUsage replaceParamType(int i, ResolvedType replaced) { |
| 108 | if (i < 0 || i >= getNoParams()) { |
| 109 | throw new IllegalArgumentException(); |
| 110 | } |
| 111 | if (paramTypes.get(i) == replaced) { |
| 112 | return this; |
| 113 | } |
| 114 | List<ResolvedType> newParams = new LinkedList<>(paramTypes); |
| 115 | newParams.set(i, replaced); |
| 116 | return new MethodUsage(declaration, newParams, returnType, exceptionTypes, typeParametersMap); |
| 117 | } |
| 118 | |
| 119 | public MethodUsage replaceExceptionType(int i, ResolvedType replaced) { |
| 120 | if (i < 0 || i >= exceptionTypes.size()) { |
| 121 | throw new IllegalArgumentException(); |
| 122 | } |
| 123 | if (exceptionTypes.get(i) == replaced) { |
| 124 | return this; |
| 125 | } |
| 126 | List<ResolvedType> newTypes = new LinkedList<>(exceptionTypes); |
| 127 | newTypes.set(i, replaced); |
| 128 | return new MethodUsage(declaration, paramTypes, returnType, newTypes, typeParametersMap); |
| 129 | } |
| 130 | |
| 131 | public MethodUsage replaceReturnType(ResolvedType returnType) { |
| 132 | if (returnType == this.returnType) { |
| 133 | return this; |
| 134 | } else { |
| 135 | return new MethodUsage(declaration, paramTypes, returnType, exceptionTypes, typeParametersMap); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Return the number of formal arguments accepted by this method. |
| 141 | */ |
| 142 | public int getNoParams() { |
| 143 | return paramTypes.size(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Return the type of the formal argument at the given position. |
| 148 | */ |
| 149 | public ResolvedType getParamType(int i) { |
| 150 | return paramTypes.get(i); |
| 151 | } |
| 152 | |
| 153 | public MethodUsage replaceTypeParameter(ResolvedTypeParameterDeclaration typeParameter, ResolvedType type) { |
| 154 | if (type == null) { |
| 155 | throw new IllegalArgumentException(); |
| 156 | } |
| 157 | |
| 158 | // TODO if the method declaration has a type param with that name ignore this call |
| 159 | MethodUsage res = new MethodUsage(declaration, paramTypes, returnType, exceptionTypes, |
| 160 | typeParametersMap.toBuilder().setValue(typeParameter, type).build()); |
| 161 | |
| 162 | Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>(); |
| 163 | for (int i = 0; i < paramTypes.size(); i++) { |
| 164 | ResolvedType originalParamType = paramTypes.get(i); |
| 165 | ResolvedType newParamType = originalParamType.replaceTypeVariables(typeParameter, type, inferredTypes); |
| 166 | res = res.replaceParamType(i, newParamType); |
| 167 | } |
| 168 | for (int i = 0; i < exceptionTypes.size(); i++) { |
| 169 | ResolvedType originalType = exceptionTypes.get(i); |
| 170 | ResolvedType newType = originalType.replaceTypeVariables(typeParameter, type, inferredTypes); |
| 171 | res = res.replaceExceptionType(i, newType); |
| 172 | } |
| 173 | ResolvedType oldReturnType = res.returnType; |
| 174 | ResolvedType newReturnType = oldReturnType.replaceTypeVariables(typeParameter, type, inferredTypes); |
| 175 | res = res.replaceReturnType(newReturnType); |
| 176 | return res; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public ResolvedTypeParametersMap typeParametersMap() { |
| 181 | return typeParametersMap; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * The qualified signature of the method. It is composed by the qualified name of the declaring type |
| 186 | * followed by the signature of the method. |
| 187 | */ |
| 188 | public String getQualifiedSignature() { |
| 189 | return getDeclaration().declaringType().getQualifiedName() + "." + getSignature(); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * The signature of the method. |
| 194 | */ |
| 195 | public String getSignature() { |
| 196 | StringBuilder sb = new StringBuilder(); |
| 197 | sb.append(getName()); |
| 198 | sb.append("("); |
| 199 | for (int i = 0; i < getNoParams(); i++) { |
| 200 | if (i != 0) { |
| 201 | sb.append(", "); |
| 202 | } |
| 203 | ResolvedType type = getParamType(i); |
| 204 | if (type.isArray() && getDeclaration().getParam(i).isVariadic()) { |
| 205 | sb.append(type.asArrayType().getComponentType().describe()).append("..."); |
| 206 | } else { |
| 207 | sb.append(type.describe()); |
| 208 | } |
| 209 | } |
| 210 | sb.append(")"); |
| 211 | return sb.toString(); |
| 212 | } |
| 213 | |
| 214 | public List<ResolvedType> exceptionTypes() { |
| 215 | return exceptionTypes; |
| 216 | } |
| 217 | } |
| 218 |
Members