Search in sources :

Example 71 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class JavassistUtils method parseTypeParameters.

private static List<ResolvedType> parseTypeParameters(String signature, TypeSolver typeSolver, Context invokationContext) {
    String originalSignature = signature;
    if (signature.contains("<")) {
        signature = signature.substring(signature.indexOf('<') + 1);
        if (!signature.endsWith(">")) {
            throw new IllegalArgumentException();
        }
        signature = signature.substring(0, signature.length() - 1);
        if (signature.contains(",")) {
            throw new UnsupportedOperationException();
        }
        if (signature.startsWith("?")) {
            // TODO: check bounds
            List<ResolvedType> types = new ArrayList<>();
            types.add(ResolvedWildcard.UNBOUNDED);
            return types;
        }
        List<ResolvedType> typeParameters = parseTypeParameters(signature, typeSolver, invokationContext);
        if (signature.contains("<")) {
            signature = signature.substring(0, signature.indexOf('<'));
        }
        if (signature.contains(">")) {
            throw new UnsupportedOperationException();
        }
        ResolvedType type = new SymbolSolver(typeSolver).solveTypeUsage(signature, invokationContext);
        if (type.isReferenceType() && typeParameters.size() > 0) {
            type = type.asReferenceType().transformTypeParameters(tp -> typeParameters.remove(0));
        }
        List<ResolvedType> types = new ArrayList<>();
        types.add(type);
        return types;
    } else {
        return Collections.emptyList();
    }
}
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) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver)

Example 72 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver 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 73 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class MyObjectProvider method byName.

@Override
public ResolvedReferenceType byName(String qualifiedName) {
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(qualifiedName);
    if (!typeDeclaration.getTypeParameters().isEmpty()) {
        throw new UnsupportedOperationException();
    }
    return new ReferenceTypeImpl(typeDeclaration, typeSolver);
}
Also used : ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)

Example 74 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class Issue116 method arrayTypeIsNotPartOfTheTree.

@Test
public void arrayTypeIsNotPartOfTheTree() {
    CompilationUnit cu = parseSample("Issue116");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
    MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "foo");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    com.github.javaparser.ast.type.Type typeNode = methodDeclaration.getParameters().get(0).getType();
    ResolvedType type = javaParserFacade.convert(typeNode, typeNode);
    assertEquals("java.lang.String[]", type.describe());
    ExpressionStmt expressionStmt = (ExpressionStmt) methodDeclaration.getBody().get().getStatements().get(0);
    Expression argRef = expressionStmt.getExpression();
    assertEquals("java.lang.String[]", javaParserFacade.getType(argRef).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Expression(com.github.javaparser.ast.expr.Expression) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 75 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class Issue156 method testFieldAccessThroughClassAndThis.

@Test
public void testFieldAccessThroughClassAndThis() {
    CompilationUnit cu = parseSample("Issue156");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Issue156");
    List<MethodCallExpr> methods = clazz.getChildNodes().get(2).getChildNodes().get(1).findAll(MethodCallExpr.class);
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    assertEquals("char", javaParserFacade.getType(methods.get(0)).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) 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) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)114 Test (org.junit.Test)89 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)76 CompilationUnit (com.github.javaparser.ast.CompilationUnit)59 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)46 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)40 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)33 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)32 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)30 MethodUsage (com.github.javaparser.resolution.MethodUsage)27 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)25 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)21 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)20 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)18 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)18 Collectors (java.util.stream.Collectors)17 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)16 ResolvedClassDeclaration (com.github.javaparser.resolution.declarations.ResolvedClassDeclaration)13 java.util (java.util)13 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)12