Search in sources :

Example 6 with ResolvedArrayType

use of com.github.javaparser.resolution.types.ResolvedArrayType in project javaparser by javaparser.

the class JavaParserSymbolDeclaration method getType.

@Override
public ResolvedType getType() {
    if (wrappedNode instanceof Parameter) {
        Parameter parameter = (Parameter) wrappedNode;
        if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
            int pos = getParamPos(parameter);
            ResolvedType lambdaType = JavaParserFacade.get(typeSolver).getType(requireParentNode(wrappedNode));
            // MethodDeclaration methodCalled = JavaParserFacade.get(typeSolver).solve()
            throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
        } else {
            final ResolvedType rawType;
            if (parameter.getType() instanceof com.github.javaparser.ast.type.PrimitiveType) {
                rawType = ResolvedPrimitiveType.byName(((com.github.javaparser.ast.type.PrimitiveType) parameter.getType()).getType().name());
            } else {
                rawType = JavaParserFacade.get(typeSolver).convertToUsage(parameter.getType(), wrappedNode);
            }
            if (parameter.isVarArgs()) {
                return new ResolvedArrayType(rawType);
            }
            return rawType;
        }
    } else if (wrappedNode instanceof VariableDeclarator) {
        VariableDeclarator variableDeclarator = (VariableDeclarator) wrappedNode;
        if (requireParentNode(wrappedNode) instanceof VariableDeclarationExpr) {
            return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
        } else if (requireParentNode(wrappedNode) instanceof FieldDeclaration) {
            return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
        }
    }
    throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedPrimitiveType(com.github.javaparser.resolution.types.ResolvedPrimitiveType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 7 with ResolvedArrayType

use of com.github.javaparser.resolution.types.ResolvedArrayType in project javaparser by javaparser.

the class ConstructorResolutionLogic method isApplicable.

private static boolean isApplicable(ResolvedConstructorDeclaration constructor, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, boolean withWildcardTolerance) {
    if (constructor.hasVariadicParameter()) {
        int pos = constructor.getNumberOfParams() - 1;
        if (constructor.getNumberOfParams() == argumentsTypes.size()) {
            // check if the last value is directly assignable as an array
            ResolvedType expectedType = constructor.getLastParam().getType();
            ResolvedType actualType = argumentsTypes.get(pos);
            if (!expectedType.isAssignableBy(actualType)) {
                for (ResolvedTypeParameterDeclaration tp : constructor.getTypeParameters()) {
                    expectedType = MethodResolutionLogic.replaceTypeParam(expectedType, tp, typeSolver);
                }
                if (!expectedType.isAssignableBy(actualType)) {
                    if (actualType.isArray() && expectedType.isAssignableBy(actualType.asArrayType().getComponentType())) {
                        argumentsTypes.set(pos, actualType.asArrayType().getComponentType());
                    } else {
                        argumentsTypes = groupVariadicParamValues(argumentsTypes, pos, constructor.getLastParam().getType());
                    }
                }
            }
        // else it is already assignable, nothing to do
        } else {
            if (pos > argumentsTypes.size()) {
                return false;
            }
            argumentsTypes = groupVariadicParamValues(argumentsTypes, pos, constructor.getLastParam().getType());
        }
    }
    if (constructor.getNumberOfParams() != argumentsTypes.size()) {
        return false;
    }
    Map<String, ResolvedType> matchedParameters = new HashMap<>();
    boolean needForWildCardTolerance = false;
    for (int i = 0; i < constructor.getNumberOfParams(); i++) {
        ResolvedType expectedType = constructor.getParam(i).getType();
        ResolvedType actualType = argumentsTypes.get(i);
        if ((expectedType.isTypeVariable() && !(expectedType.isWildcard())) && expectedType.asTypeParameter().declaredOnMethod()) {
            matchedParameters.put(expectedType.asTypeParameter().getName(), actualType);
            continue;
        }
        boolean isAssignableWithoutSubstitution = expectedType.isAssignableBy(actualType) || (constructor.getParam(i).isVariadic() && new ResolvedArrayType(expectedType).isAssignableBy(actualType));
        if (!isAssignableWithoutSubstitution && expectedType.isReferenceType() && actualType.isReferenceType()) {
            isAssignableWithoutSubstitution = MethodResolutionLogic.isAssignableMatchTypeParameters(expectedType.asReferenceType(), actualType.asReferenceType(), matchedParameters);
        }
        if (!isAssignableWithoutSubstitution) {
            List<ResolvedTypeParameterDeclaration> typeParameters = constructor.getTypeParameters();
            typeParameters.addAll(constructor.declaringType().getTypeParameters());
            for (ResolvedTypeParameterDeclaration tp : typeParameters) {
                expectedType = MethodResolutionLogic.replaceTypeParam(expectedType, tp, typeSolver);
            }
            if (!expectedType.isAssignableBy(actualType)) {
                if (actualType.isWildcard() && withWildcardTolerance && !expectedType.isPrimitive()) {
                    needForWildCardTolerance = true;
                    continue;
                }
                if (constructor.hasVariadicParameter() && i == constructor.getNumberOfParams() - 1) {
                    if (new ResolvedArrayType(expectedType).isAssignableBy(actualType)) {
                        continue;
                    }
                }
                return false;
            }
        }
    }
    return !withWildcardTolerance || needForWildCardTolerance;
}
Also used : ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) HashMap(java.util.HashMap) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 8 with ResolvedArrayType

use of com.github.javaparser.resolution.types.ResolvedArrayType in project javaparser by javaparser.

the class PrimitiveTypeTest method setup.

@Before
public void setup() {
    typeSolver = new ReflectionTypeSolver();
    OBJECT = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
    STRING = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
    arrayOfBooleans = new ResolvedArrayType(ResolvedPrimitiveType.BOOLEAN);
    arrayOfListOfA = new ResolvedArrayType(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(List.class, typeSolver), ImmutableList.of(new ResolvedTypeVariable(ResolvedTypeParameterDeclaration.onType("A", "foo.Bar", Collections.emptyList()))), typeSolver));
    booleanBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Boolean.class, typeSolver), typeSolver);
    characterBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Character.class, typeSolver), typeSolver);
    byteBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Byte.class, typeSolver), typeSolver);
    shortBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Short.class, typeSolver), typeSolver);
    integerBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Integer.class, typeSolver), typeSolver);
    longBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Long.class, typeSolver), typeSolver);
    floatBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Float.class, typeSolver), typeSolver);
    doubleBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Double.class, typeSolver), typeSolver);
}
Also used : ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ReflectionInterfaceDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Before(org.junit.Before)

Aggregations

ResolvedArrayType (com.github.javaparser.resolution.types.ResolvedArrayType)8 ReflectionInterfaceDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration)5 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)4 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)4 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)4 Before (org.junit.Before)4 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)3 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)1 ResolvedPrimitiveType (com.github.javaparser.resolution.types.ResolvedPrimitiveType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1