Search in sources :

Example 36 with MethodUsage

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;
}
Also used : MethodUsage(com.github.javaparser.resolution.MethodUsage) HashSet(java.util.HashSet) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)

Example 37 with MethodUsage

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();
}
Also used : CtMethod(javassist.CtMethod) java.util(java.util) BadBytecode(javassist.bytecode.BadBytecode) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) Collectors(java.util.stream.Collectors) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) Context(com.github.javaparser.symbolsolver.core.resolution.Context) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) ResolvedTypeParametrizable(com.github.javaparser.resolution.declarations.ResolvedTypeParametrizable) NotFoundException(javassist.NotFoundException) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) MethodUsage(com.github.javaparser.resolution.MethodUsage) CtMethod(javassist.CtMethod)

Example 38 with MethodUsage

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);
}
Also used : MethodUsage(com.github.javaparser.resolution.MethodUsage) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) LinkedList(java.util.LinkedList)

Example 39 with MethodUsage

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;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) MethodUsage(com.github.javaparser.resolution.MethodUsage) HashSet(java.util.HashSet) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)

Example 40 with MethodUsage

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Aggregations

MethodUsage (com.github.javaparser.resolution.MethodUsage)59 Test (org.junit.Test)39 CompilationUnit (com.github.javaparser.ast.CompilationUnit)36 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)33 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)27 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)24 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)20 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)19 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)14 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)12 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)12 Context (com.github.javaparser.symbolsolver.core.resolution.Context)12 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)11 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)7 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)6 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)6 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)6 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)6 InferenceContext (com.github.javaparser.symbolsolver.logic.InferenceContext)6 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)5