JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/resolution/MethodUsage.java
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
22package com.github.javaparser.resolution;
23
24import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
25import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
26import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration;
27import com.github.javaparser.resolution.types.ResolvedType;
28import com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametersMap;
29import com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametrized;
30
31
32import 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 */
40public class MethodUsage implements ResolvedTypeParametrized {
41    private ResolvedMethodDeclaration declaration;
42    private List<ResolvedTypeparamTypes = new ArrayList<>();
43    private List<ResolvedTypeexceptionTypes = 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 = 0i < declaration.getNumberOfParams(); i++) {
51            paramTypes.add(declaration.getParam(i).getType());
52        }
53        for (int i = 0i < declaration.getNumberOfSpecifiedExceptions(); i++) {
54            exceptionTypes.add(declaration.getSpecifiedException(i));
55        }
56        returnType = declaration.getReturnType();
57    }
58
59    public MethodUsage(ResolvedMethodDeclaration declaration,
60                       List<ResolvedTypeparamTypesResolvedType returnType) {
61        this(declarationparamTypesreturnTypedeclaration.getSpecifiedExceptions(),
62                ResolvedTypeParametersMap.empty());
63    }
64
65    public MethodUsage(ResolvedMethodDeclaration declarationList<ResolvedTypeparamTypesResolvedType returnType,
66                       List<ResolvedTypeexceptionTypes) {
67        this(declarationparamTypesreturnTypeexceptionTypesResolvedTypeParametersMap.empty());
68    }
69
70    private MethodUsage(ResolvedMethodDeclaration declarationList<ResolvedTypeparamTypesResolvedType returnType,
71                        List<ResolvedTypeexceptionTypesResolvedTypeParametersMap 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<ResolvedTypegetParamTypes() {
104        return paramTypes;
105    }
106
107    public MethodUsage replaceParamType(int iResolvedType replaced) {
108        if (i < 0 || i >= getNoParams()) {
109            throw new IllegalArgumentException();
110        }
111        if (paramTypes.get(i) == replaced) {
112            return this;
113        }
114        List<ResolvedTypenewParams = new LinkedList<>(paramTypes);
115        newParams.set(ireplaced);
116        return new MethodUsage(declarationnewParamsreturnTypeexceptionTypestypeParametersMap);
117    }
118
119    public MethodUsage replaceExceptionType(int iResolvedType 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<ResolvedTypenewTypes = new LinkedList<>(exceptionTypes);
127        newTypes.set(ireplaced);
128        return new MethodUsage(declarationparamTypesreturnTypenewTypestypeParametersMap);
129    }
130
131    public MethodUsage replaceReturnType(ResolvedType returnType) {
132        if (returnType == this.returnType) {
133            return this;
134        } else {
135            return new MethodUsage(declarationparamTypesreturnTypeexceptionTypestypeParametersMap);
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 typeParameterResolvedType 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(declarationparamTypesreturnTypeexceptionTypes,
160                typeParametersMap.toBuilder().setValue(typeParametertype).build());
161
162        Map<ResolvedTypeParameterDeclarationResolvedTypeinferredTypes = new HashMap<>();
163        for (int i = 0i < paramTypes.size(); i++) {
164            ResolvedType originalParamType = paramTypes.get(i);
165            ResolvedType newParamType = originalParamType.replaceTypeVariables(typeParametertypeinferredTypes);
166            res = res.replaceParamType(inewParamType);
167        }
168        for (int i = 0i < exceptionTypes.size(); i++) {
169            ResolvedType originalType = exceptionTypes.get(i);
170            ResolvedType newType = originalType.replaceTypeVariables(typeParametertypeinferredTypes);
171            res = res.replaceExceptionType(inewType);
172        }
173        ResolvedType oldReturnType = res.returnType;
174        ResolvedType newReturnType = oldReturnType.replaceTypeVariables(typeParametertypeinferredTypes);
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 = 0i < 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<ResolvedTypeexceptionTypes() {
215        return exceptionTypes;
216    }
217}
218
MembersX
MethodUsage:replaceTypeParameter:Block:Block:newParamType
MethodUsage:replaceTypeParameter:Block:inferredTypes
MethodUsage:getDeclaration
MethodUsage:getSignature:Block:Block:type
MethodUsage:paramTypes
MethodUsage:replaceParamType
MethodUsage:replaceTypeParameter
MethodUsage:getSignature:Block:sb
MethodUsage:replaceTypeParameter:Block:Block:originalType
MethodUsage:replaceTypeParameter:Block:Block:newType
MethodUsage:declaration
MethodUsage:replaceExceptionType:Block:newTypes
MethodUsage:typeParametersMap
MethodUsage:getParamTypes
MethodUsage:getNoParams
MethodUsage:replaceExceptionType
MethodUsage:replaceReturnType
MethodUsage:replaceTypeParameter:Block:Block:originalParamType
MethodUsage:getParamType
MethodUsage:exceptionTypes
MethodUsage:getQualifiedSignature
MethodUsage:getSignature
MethodUsage:replaceParamType:Block:newParams
MethodUsage:replaceTypeParameter:Block:newReturnType
MethodUsage:toString
MethodUsage:declaringType
MethodUsage:returnType
MethodUsage:MethodUsage
MethodUsage:replaceTypeParameter:Block:oldReturnType
MethodUsage:getName
MethodUsage:replaceTypeParameter:Block:res
Members
X