Search in sources :

Example 1 with UnsolvedSymbolException

use of com.github.javaparser.resolution.UnsolvedSymbolException in project javaparser by javaparser.

the class MethodCallExprContext method solveMethodAsUsage.

@Override
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) {
    if (wrappedNode.getScope().isPresent()) {
        Expression scope = wrappedNode.getScope().get();
        // Consider static method calls
        if (scope instanceof NameExpr) {
            String className = ((NameExpr) scope).getName().getId();
            SymbolReference<ResolvedTypeDeclaration> ref = solveType(className, typeSolver);
            if (ref.isSolved()) {
                SymbolReference<ResolvedMethodDeclaration> m = MethodResolutionLogic.solveMethodInType(ref.getCorrespondingDeclaration(), name, argumentsTypes, typeSolver);
                if (m.isSolved()) {
                    MethodUsage methodUsage = new MethodUsage(m.getCorrespondingDeclaration());
                    methodUsage = resolveMethodTypeParametersFromExplicitList(typeSolver, methodUsage);
                    methodUsage = resolveMethodTypeParameters(methodUsage, argumentsTypes);
                    return Optional.of(methodUsage);
                } else {
                    throw new UnsolvedSymbolException(ref.getCorrespondingDeclaration().toString(), "Method '" + name + "' with parameterTypes " + argumentsTypes);
                }
            }
        }
        ResolvedType typeOfScope = JavaParserFacade.get(typeSolver).getType(scope);
        // we can replace the parameter types from the scope into the typeParametersValues
        Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
        for (int i = 0; i < argumentsTypes.size(); i++) {
            // by replacing types I can also find new equivalences
            // for example if I replace T=U with String because I know that T=String I can derive that also U equal String
            ResolvedType originalArgumentType = argumentsTypes.get(i);
            ResolvedType updatedArgumentType = usingParameterTypesFromScope(typeOfScope, originalArgumentType, inferredTypes);
            argumentsTypes.set(i, updatedArgumentType);
        }
        for (int i = 0; i < argumentsTypes.size(); i++) {
            ResolvedType updatedArgumentType = applyInferredTypes(argumentsTypes.get(i), inferredTypes);
            argumentsTypes.set(i, updatedArgumentType);
        }
        return solveMethodAsUsage(typeOfScope, name, argumentsTypes, typeSolver, this);
    } else {
        Context parentContext = getParent();
        while (parentContext instanceof MethodCallExprContext) {
            parentContext = parentContext.getParent();
        }
        return parentContext.solveMethodAsUsage(name, argumentsTypes, typeSolver);
    }
}
Also used : UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) Context(com.github.javaparser.symbolsolver.core.resolution.Context) HashMap(java.util.HashMap) NameExpr(com.github.javaparser.ast.expr.NameExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodUsage(com.github.javaparser.resolution.MethodUsage)

Example 2 with UnsolvedSymbolException

use of com.github.javaparser.resolution.UnsolvedSymbolException in project javaparser by javaparser.

the class JavaParserFacade method convertToUsage.

protected ResolvedType convertToUsage(com.github.javaparser.ast.type.Type type, Context context) {
    if (context == null) {
        throw new NullPointerException("Context should not be null");
    }
    if (type instanceof ClassOrInterfaceType) {
        ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType) type;
        String name = qName(classOrInterfaceType);
        SymbolReference<ResolvedTypeDeclaration> ref = context.solveType(name, typeSolver);
        if (!ref.isSolved()) {
            throw new UnsolvedSymbolException(name);
        }
        ResolvedTypeDeclaration typeDeclaration = ref.getCorrespondingDeclaration();
        List<ResolvedType> typeParameters = Collections.emptyList();
        if (classOrInterfaceType.getTypeArguments().isPresent()) {
            typeParameters = classOrInterfaceType.getTypeArguments().get().stream().map((pt) -> convertToUsage(pt, context)).collect(Collectors.toList());
        }
        if (typeDeclaration.isTypeParameter()) {
            if (typeDeclaration instanceof ResolvedTypeParameterDeclaration) {
                return new ResolvedTypeVariable((ResolvedTypeParameterDeclaration) typeDeclaration);
            } else {
                JavaParserTypeVariableDeclaration javaParserTypeVariableDeclaration = (JavaParserTypeVariableDeclaration) typeDeclaration;
                return new ResolvedTypeVariable(javaParserTypeVariableDeclaration.asTypeParameter());
            }
        } else {
            return new ReferenceTypeImpl((ResolvedReferenceTypeDeclaration) typeDeclaration, typeParameters, typeSolver);
        }
    } else if (type instanceof com.github.javaparser.ast.type.PrimitiveType) {
        return ResolvedPrimitiveType.byName(((com.github.javaparser.ast.type.PrimitiveType) type).getType().name());
    } else if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        if (wildcardType.getExtendedType().isPresent() && !wildcardType.getSuperType().isPresent()) {
            // removed (ReferenceTypeImpl)
            return ResolvedWildcard.extendsBound(convertToUsage(wildcardType.getExtendedType().get(), context));
        } else if (!wildcardType.getExtendedType().isPresent() && wildcardType.getSuperType().isPresent()) {
            // removed (ReferenceTypeImpl)
            return ResolvedWildcard.superBound(convertToUsage(wildcardType.getSuperType().get(), context));
        } else if (!wildcardType.getExtendedType().isPresent() && !wildcardType.getSuperType().isPresent()) {
            return ResolvedWildcard.UNBOUNDED;
        } else {
            throw new UnsupportedOperationException(wildcardType.toString());
        }
    } else if (type instanceof com.github.javaparser.ast.type.VoidType) {
        return ResolvedVoidType.INSTANCE;
    } else if (type instanceof com.github.javaparser.ast.type.ArrayType) {
        com.github.javaparser.ast.type.ArrayType jpArrayType = (com.github.javaparser.ast.type.ArrayType) type;
        return new ResolvedArrayType(convertToUsage(jpArrayType.getComponentType(), context));
    } else if (type instanceof UnionType) {
        UnionType unionType = (UnionType) type;
        return new ResolvedUnionType(unionType.getElements().stream().map(el -> convertToUsage(el, context)).collect(Collectors.toList()));
    } else if (type instanceof VarType) {
        Node parent = type.getParentNode().get();
        if (!(parent instanceof VariableDeclarator)) {
            throw new IllegalStateException("Trying to resolve a `var` which is not in a variable declaration.");
        }
        final VariableDeclarator variableDeclarator = (VariableDeclarator) parent;
        return variableDeclarator.getInitializer().map(Expression::calculateResolvedType).orElseThrow(() -> new IllegalStateException("Cannot resolve `var` which has no initializer."));
    } else {
        throw new UnsupportedOperationException(type.getClass().getCanonicalName());
    }
}
Also used : FieldAccessContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.FieldAccessContext) java.util(java.util) Navigator.requireParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode) MethodUsage(com.github.javaparser.resolution.MethodUsage) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) Level(java.util.logging.Level) com.github.javaparser.ast.body(com.github.javaparser.ast.body) Context(com.github.javaparser.symbolsolver.core.resolution.Context) ConstructorResolutionLogic(com.github.javaparser.symbolsolver.resolution.ConstructorResolutionLogic) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) com.github.javaparser.ast.type(com.github.javaparser.ast.type) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) Logger(java.util.logging.Logger) com.github.javaparser.resolution.declarations(com.github.javaparser.resolution.declarations) Collectors(java.util.stream.Collectors) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) com.github.javaparser.symbolsolver.javaparsermodel.declarations(com.github.javaparser.symbolsolver.javaparsermodel.declarations) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) ConsoleHandler(java.util.logging.ConsoleHandler) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) com.github.javaparser.ast.expr(com.github.javaparser.ast.expr) Navigator.requireParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode) Node(com.github.javaparser.ast.Node) com.github.javaparser.ast.type(com.github.javaparser.ast.type) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException)

Example 3 with UnsolvedSymbolException

use of com.github.javaparser.resolution.UnsolvedSymbolException in project javaparser by javaparser.

the class JavaParserEnumDeclaration method getAncestors.

@Override
public List<ResolvedReferenceType> getAncestors() {
    List<ResolvedReferenceType> ancestors = new ArrayList<>();
    ResolvedReferenceType enumClass = ReflectionFactory.typeUsageFor(Enum.class, typeSolver).asReferenceType();
    ResolvedTypeParameterDeclaration eTypeParameter = enumClass.getTypeDeclaration().getTypeParameters().get(0);
    enumClass = enumClass.deriveTypeParameters(new ResolvedTypeParametersMap.Builder().setValue(eTypeParameter, new ReferenceTypeImpl(this, typeSolver)).build());
    ancestors.add(enumClass);
    if (wrappedNode.getImplementedTypes() != null) {
        for (ClassOrInterfaceType implementedType : wrappedNode.getImplementedTypes()) {
            SymbolReference<ResolvedTypeDeclaration> implementedDeclRef = new SymbolSolver(typeSolver).solveTypeInType(this, implementedType.getName().getId());
            if (!implementedDeclRef.isSolved()) {
                throw new UnsolvedSymbolException(implementedType.getName().getId());
            }
            ancestors.add(new ReferenceTypeImpl((ResolvedReferenceTypeDeclaration) implementedDeclRef.getCorrespondingDeclaration(), typeSolver));
        }
    }
    return ancestors;
}
Also used : UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver)

Example 4 with UnsolvedSymbolException

use of com.github.javaparser.resolution.UnsolvedSymbolException in project javaparser by javaparser.

the class JavassistUtils method signatureTypeToType.

static ResolvedType signatureTypeToType(SignatureAttribute.Type signatureType, TypeSolver typeSolver, ResolvedTypeParametrizable typeParametrizable) {
    if (signatureType instanceof SignatureAttribute.ClassType) {
        SignatureAttribute.ClassType classType = (SignatureAttribute.ClassType) signatureType;
        List<ResolvedType> typeArguments = classType.getTypeArguments() == null ? Collections.emptyList() : Arrays.stream(classType.getTypeArguments()).map(ta -> typeArgumentToType(ta, typeSolver, typeParametrizable)).collect(Collectors.toList());
        final String typeName = classType.getDeclaringClass() != null ? classType.getDeclaringClass().getName() + "." + classType.getName() : classType.getName();
        ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(removeTypeArguments(internalNameToCanonicalName(typeName)));
        return new ReferenceTypeImpl(typeDeclaration, typeArguments, typeSolver);
    } else if (signatureType instanceof SignatureAttribute.TypeVariable) {
        SignatureAttribute.TypeVariable typeVariableSignature = (SignatureAttribute.TypeVariable) signatureType;
        Optional<ResolvedTypeParameterDeclaration> typeParameterDeclarationOpt = typeParametrizable.findTypeParameter(typeVariableSignature.getName());
        if (!typeParameterDeclarationOpt.isPresent()) {
            throw new UnsolvedSymbolException("Unable to solve TypeVariable " + typeVariableSignature);
        }
        ResolvedTypeParameterDeclaration typeParameterDeclaration = typeParameterDeclarationOpt.get();
        return new ResolvedTypeVariable(typeParameterDeclaration);
    } else if (signatureType instanceof SignatureAttribute.ArrayType) {
        SignatureAttribute.ArrayType arrayType = (SignatureAttribute.ArrayType) signatureType;
        return new ResolvedArrayType(signatureTypeToType(arrayType.getComponentType(), typeSolver, typeParametrizable));
    } else if (signatureType instanceof SignatureAttribute.BaseType) {
        SignatureAttribute.BaseType baseType = (SignatureAttribute.BaseType) signatureType;
        if (baseType.toString().equals("void")) {
            return ResolvedVoidType.INSTANCE;
        } else {
            return ResolvedPrimitiveType.byName(baseType.toString());
        }
    } else {
        throw new RuntimeException(signatureType.getClass().getCanonicalName());
    }
}
Also used : UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) SignatureAttribute(javassist.bytecode.SignatureAttribute)

Aggregations

UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)4 MethodUsage (com.github.javaparser.resolution.MethodUsage)2 Context (com.github.javaparser.symbolsolver.core.resolution.Context)2 SymbolSolver (com.github.javaparser.symbolsolver.resolution.SymbolSolver)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 Node (com.github.javaparser.ast.Node)1 NodeList (com.github.javaparser.ast.NodeList)1 com.github.javaparser.ast.body (com.github.javaparser.ast.body)1 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)1 com.github.javaparser.ast.expr (com.github.javaparser.ast.expr)1 Expression (com.github.javaparser.ast.expr.Expression)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)1 com.github.javaparser.ast.type (com.github.javaparser.ast.type)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 com.github.javaparser.resolution.declarations (com.github.javaparser.resolution.declarations)1 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)1 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)1 com.github.javaparser.resolution.types (com.github.javaparser.resolution.types)1 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)1