1 /* 2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 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 MethodUsage(ResolvedMethodDeclaration declaration)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 MethodUsage(ResolvedMethodDeclaration declaration, List<ResolvedType> paramTypes, ResolvedType returnType)59 public MethodUsage(ResolvedMethodDeclaration declaration, 60 List<ResolvedType> paramTypes, ResolvedType returnType) { 61 this(declaration, paramTypes, returnType, declaration.getSpecifiedExceptions(), 62 ResolvedTypeParametersMap.empty()); 63 } 64 MethodUsage(ResolvedMethodDeclaration declaration, List<ResolvedType> paramTypes, ResolvedType returnType, List<ResolvedType> exceptionTypes)65 public MethodUsage(ResolvedMethodDeclaration declaration, List<ResolvedType> paramTypes, ResolvedType returnType, 66 List<ResolvedType> exceptionTypes) { 67 this(declaration, paramTypes, returnType, exceptionTypes, ResolvedTypeParametersMap.empty()); 68 } 69 MethodUsage(ResolvedMethodDeclaration declaration, List<ResolvedType> paramTypes, ResolvedType returnType, List<ResolvedType> exceptionTypes, ResolvedTypeParametersMap typeParametersMap)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 toString()80 public String toString() { 81 return "MethodUsage{" + 82 "declaration=" + declaration + 83 ", paramTypes=" + paramTypes + 84 '}'; 85 } 86 getDeclaration()87 public ResolvedMethodDeclaration getDeclaration() { 88 return declaration; 89 } 90 getName()91 public String getName() { 92 return declaration.getName(); 93 } 94 declaringType()95 public ResolvedReferenceTypeDeclaration declaringType() { 96 return declaration.declaringType(); 97 } 98 returnType()99 public ResolvedType returnType() { 100 return returnType; 101 } 102 getParamTypes()103 public List<ResolvedType> getParamTypes() { 104 return paramTypes; 105 } 106 replaceParamType(int i, ResolvedType replaced)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 replaceExceptionType(int i, ResolvedType replaced)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 replaceReturnType(ResolvedType returnType)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 */ getNoParams()142 public int getNoParams() { 143 return paramTypes.size(); 144 } 145 146 /** 147 * Return the type of the formal argument at the given position. 148 */ getParamType(int i)149 public ResolvedType getParamType(int i) { 150 return paramTypes.get(i); 151 } 152 replaceTypeParameter(ResolvedTypeParameterDeclaration typeParameter, ResolvedType type)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 typeParametersMap()180 public ResolvedTypeParametersMap typeParametersMap() { 181 return typeParametersMap; 182 } 183 getQualifiedSignature()184 public String getQualifiedSignature() { 185 // TODO use the type parameters 186 return this.getDeclaration().getQualifiedSignature(); 187 } 188 exceptionTypes()189 public List<ResolvedType> exceptionTypes() { 190 return exceptionTypes; 191 } 192 } 193