use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.
the class ReferenceTypeImpl method getDeclaredMethods.
@Override
public Set<MethodUsage> getDeclaredMethods() {
// TODO replace variables
Set<MethodUsage> methods = new HashSet<>();
for (ResolvedMethodDeclaration methodDeclaration : getTypeDeclaration().getDeclaredMethods()) {
MethodUsage methodUsage = new MethodUsage(methodDeclaration);
methods.add(methodUsage);
}
return methods;
}
use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.
the class JavassistUtils method getMethodUsage.
static Optional<MethodUsage> getMethodUsage(CtClass ctClass, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext) {
// TODO avoid bridge and synthetic methods
for (CtMethod method : ctClass.getDeclaredMethods()) {
if (method.getName().equals(name)) {
// TODO check typeParametersValues
MethodUsage methodUsage = new MethodUsage(new JavassistMethodDeclaration(method, typeSolver));
if (argumentsTypes.size() < methodUsage.getNoParams()) {
// this method cannot be a good candidate (except if variadic ?)
continue;
}
try {
if (method.getGenericSignature() != null) {
SignatureAttribute.MethodSignature methodSignature = SignatureAttribute.toMethodSignature(method.getGenericSignature());
List<ResolvedType> parametersOfReturnType = parseTypeParameters(methodSignature.getReturnType().toString(), typeSolver, invokationContext);
ResolvedType newReturnType = methodUsage.returnType();
// consume one parametersOfReturnType at the time
if (newReturnType.isReferenceType() && parametersOfReturnType.size() > 0) {
newReturnType = newReturnType.asReferenceType().transformTypeParameters(tp -> parametersOfReturnType.remove(0));
}
methodUsage = methodUsage.replaceReturnType(newReturnType);
}
return Optional.of(methodUsage);
} catch (BadBytecode e) {
throw new RuntimeException(e);
}
}
}
try {
CtClass superClass = ctClass.getSuperclass();
if (superClass != null) {
Optional<MethodUsage> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
if (ref.isPresent()) {
return ref;
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
try {
for (CtClass interfaze : ctClass.getInterfaces()) {
Optional<MethodUsage> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
if (ref.isPresent()) {
return ref;
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
return Optional.empty();
}
use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.
the class TypeInference method instantiationSetToMethodUsage.
// /
// / Private static methods
// /
private static MethodUsage instantiationSetToMethodUsage(ResolvedMethodDeclaration methodDeclaration, InstantiationSet instantiationSet) {
if (instantiationSet.isEmpty()) {
return new MethodUsage(methodDeclaration);
}
List<ResolvedType> paramTypes = new LinkedList<>();
for (int i = 0; i < methodDeclaration.getNumberOfParams(); i++) {
paramTypes.add(instantiationSet.apply(methodDeclaration.getParam(i).getType()));
}
ResolvedType returnType = instantiationSet.apply(methodDeclaration.getReturnType());
return new MethodUsage(methodDeclaration, paramTypes, returnType);
}
use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.
the class AbstractTypeDeclaration method getAllMethods.
@Override
public final Set<MethodUsage> getAllMethods() {
Set<MethodUsage> methods = new HashSet<>();
Set<String> methodsSignatures = new HashSet<>();
for (ResolvedMethodDeclaration methodDeclaration : getDeclaredMethods()) {
methods.add(new MethodUsage(methodDeclaration));
methodsSignatures.add(methodDeclaration.getSignature());
}
for (ResolvedReferenceType ancestor : getAllAncestors()) {
for (MethodUsage mu : ancestor.getDeclaredMethods()) {
String signature = mu.getDeclaration().getSignature();
if (!methodsSignatures.contains(signature)) {
methodsSignatures.add(signature);
methods.add(mu);
}
}
}
return methods;
}
use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.
the class VariadicResolutionTest method selectMostSpecificVariadic.
@Test
public void selectMostSpecificVariadic() {
CompilationUnit cu = parseSample("MethodCalls");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MethodCalls");
MethodDeclaration method = Navigator.demandMethod(clazz, "variadicTest");
List<MethodCallExpr> calls = method.findAll(MethodCallExpr.class);
File src = adaptPath(new File("src/test/resources"));
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(src));
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
MethodUsage call1 = javaParserFacade.solveMethodAsUsage(calls.get(0));
MethodUsage call2 = javaParserFacade.solveMethodAsUsage(calls.get(1));
assertEquals("int", call1.returnType().describe());
assertEquals("void", call2.returnType().describe());
}
Aggregations