private Optional<MethodUsage> solveMethodAsUsage(ResolvedType type, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext) { if (type instanceof ResolvedReferenceType) { return solveMethodAsUsage((ResolvedReferenceType) type, name, argumentsTypes, typeSolver, invokationContext); } else if (type instanceof ResolvedTypeVariable) { return solveMethodAsUsage((ResolvedTypeVariable) type, name, argumentsTypes, typeSolver, invokationContext); } else if (type instanceof ResolvedWildcard) { ResolvedWildcard wildcardUsage = (ResolvedWildcard) type; if (wildcardUsage.isSuper()) { return solveMethodAsUsage(wildcardUsage.getBoundedType(), name, argumentsTypes, typeSolver, invokationContext); } else if (wildcardUsage.isExtends()) { throw new UnsupportedOperationException("extends wildcard"); } else { throw new UnsupportedOperationException("unbounded wildcard"); } } else if (type instanceof ResolvedLambdaConstraintType){ ResolvedLambdaConstraintType constraintType = (ResolvedLambdaConstraintType) type; return solveMethodAsUsage(constraintType.getBound(), name, argumentsTypes, typeSolver, invokationContext); } else if (type instanceof ResolvedArrayType) { // An array inherits methods from Object not from it's component type return solveMethodAsUsage(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver), name, argumentsTypes, typeSolver, invokationContext); } else { throw new UnsupportedOperationException("type usage: " + type.getClass().getCanonicalName()); } }
private Optional<MethodUsage> solveMethodAsUsage(ResolvedTypeVariable tp, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext) { for (ResolvedTypeParameterDeclaration.Bound bound : tp.asTypeParameter().getBounds()) { Optional<MethodUsage> methodUsage = solveMethodAsUsage(bound.getType(), name, argumentsTypes, typeSolver, invokationContext); if (methodUsage.isPresent()) { return methodUsage; } } return Optional.empty(); }
private Optional<MethodUsage> solveMethodAsUsage(ResolvedTypeVariable tp, String name, List<ResolvedType> argumentsTypes, Context invokationContext) { for (ResolvedTypeParameterDeclaration.Bound bound : tp.asTypeParameter().getBounds()) { Optional<MethodUsage> methodUsage = solveMethodAsUsage(bound.getType(), name, argumentsTypes, invokationContext); if (methodUsage.isPresent()) { return methodUsage; } } return Optional.empty(); }
private Optional<MethodUsage> solveMethodAsUsage(ResolvedTypeVariable tp, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext) { for (ResolvedTypeParameterDeclaration.Bound bound : tp.asTypeParameter().getBounds()) { Optional<MethodUsage> methodUsage = solveMethodAsUsage(bound.getType(), name, argumentsTypes, typeSolver, invokationContext); if (methodUsage.isPresent()) { return methodUsage; } } return Optional.empty(); }
private Optional<MethodUsage> solveMethodAsUsage(ResolvedType type, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext) { if (type instanceof ResolvedReferenceType) { return solveMethodAsUsage((ResolvedReferenceType) type, name, argumentsTypes, typeSolver, invokationContext); } else if (type instanceof ResolvedTypeVariable) { return solveMethodAsUsage((ResolvedTypeVariable) type, name, argumentsTypes, typeSolver, invokationContext); } else if (type instanceof ResolvedWildcard) { ResolvedWildcard wildcardUsage = (ResolvedWildcard) type; if (wildcardUsage.isSuper()) { return solveMethodAsUsage(wildcardUsage.getBoundedType(), name, argumentsTypes, typeSolver, invokationContext); } else if (wildcardUsage.isExtends()) { throw new UnsupportedOperationException("extends wildcard"); } else { throw new UnsupportedOperationException("unbounded wildcard"); } } else if (type instanceof ResolvedLambdaConstraintType){ ResolvedLambdaConstraintType constraintType = (ResolvedLambdaConstraintType) type; return solveMethodAsUsage(constraintType.getBound(), name, argumentsTypes, typeSolver, invokationContext); } else if (type instanceof ResolvedArrayType) { // An array inherits methods from Object not from it's component type return solveMethodAsUsage(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver), name, argumentsTypes, typeSolver, invokationContext); } else { throw new UnsupportedOperationException("type usage: " + type.getClass().getCanonicalName()); } }
private Optional<MethodUsage> solveMethodAsUsage(ResolvedType type, String name, List<ResolvedType> argumentsTypes, Context invokationContext) { if (type instanceof ResolvedReferenceType) { return solveMethodAsUsage((ResolvedReferenceType) type, name, argumentsTypes, invokationContext); } else if (type instanceof ResolvedTypeVariable) { return solveMethodAsUsage((ResolvedTypeVariable) type, name, argumentsTypes, invokationContext); } else if (type instanceof ResolvedWildcard) { ResolvedWildcard wildcardUsage = (ResolvedWildcard) type; if (wildcardUsage.isSuper()) { return solveMethodAsUsage(wildcardUsage.getBoundedType(), name, argumentsTypes, invokationContext); } else if (wildcardUsage.isExtends()) { return solveMethodAsUsage(wildcardUsage.getBoundedType(), name, argumentsTypes, invokationContext); } else { return solveMethodAsUsage(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver), name, argumentsTypes, invokationContext); return solveMethodAsUsage(constraintType.getBound(), name, argumentsTypes, invokationContext); } else if (type instanceof ResolvedArrayType) { return solveMethodAsUsage(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver), name, argumentsTypes, invokationContext); } else if (type instanceof ResolvedUnionType) { Optional<ResolvedReferenceType> commonAncestor = type.asUnionType().getCommonAncestor(); if (commonAncestor.isPresent()) { return solveMethodAsUsage(commonAncestor.get(), name, argumentsTypes, invokationContext); } else { throw new UnsupportedOperationException("no common ancestor available for " + type.describe());
private void assertCanSolveGenericMethodCallMustUseProvidedTypeArgs(String callMethodName) throws ParseException { MethodCallExpr methodCallExpr = getMethodCallExpr("genericMethodTest", callMethodName); CombinedTypeSolver typeSolver = createTypeSolver(); MethodCallExprContext context = new MethodCallExprContext(methodCallExpr, typeSolver); Optional<MethodUsage> ref = context.solveMethodAsUsage(callMethodName, Collections.emptyList(), typeSolver); assertTrue(ref.isPresent()); assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName()); assertEquals(Collections.singletonList("java.lang.Integer"), ref.get().typeParametersMap().getTypes().stream().map(ty -> ty.asReferenceType().describe()).collect(Collectors.toList())); }
return solveMethodAsUsage(typeOfScope, name, argumentsTypes, typeSolver, this); } else { Context parentContext = getParent();
private void assertCanSolveGenericMethodCallCanInferFromArguments(String callMethodName) throws ParseException { MethodCallExpr methodCallExpr = getMethodCallExpr("genericMethodTest", callMethodName); CombinedTypeSolver typeSolver = createTypeSolver(); MethodCallExprContext context = new MethodCallExprContext(methodCallExpr, typeSolver); ResolvedReferenceTypeDeclaration stringType = typeSolver.solveType("java.lang.String"); List<ResolvedType> argumentsTypes = new ArrayList<>(); argumentsTypes.add(new ReferenceTypeImpl(stringType, typeSolver)); Optional<MethodUsage> ref = context.solveMethodAsUsage(callMethodName, argumentsTypes, typeSolver); assertTrue(ref.isPresent()); assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName()); assertEquals(Collections.singletonList("java.lang.String"), ref.get().typeParametersMap().getTypes().stream().map(ty -> ty.asReferenceType().describe()).collect(Collectors.toList())); } }
return solveMethodAsUsage(typeOfScope, name, argumentsTypes, typeSolver, this); } else { Context parentContext = getParent();
return solveMethodAsUsage(typeOfScope, name, argumentsTypes, this); } else { Context parentContext = getParent();