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); }
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); }
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); }
@Test public void testGetGenericReturnType() { TypeSolver typeResolver = new ReflectionTypeSolver(); ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver); ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration(); assertEquals(true, put.getReturnType().isTypeVariable()); assertEquals(true, put.getReturnType().asTypeParameter().declaredOnType()); assertEquals("java.util.Map.V", put.getReturnType().asTypeParameter().getQualifiedName()); }
public MethodUsage resolveTypeVariables(Context context, List<ResolvedType> parameterTypes) { ResolvedType returnType = replaceTypeParams(methodDeclaration.getReturnType(), typeSolver, context); List<ResolvedType> params = new ArrayList<>(); for (int i = 0; i < methodDeclaration.getNumberOfParams(); i++) { ResolvedType replaced = replaceTypeParams(methodDeclaration.getParam(i).getType(), typeSolver, context); params.add(replaced); } // We now look at the type parameter for the method which we can derive from the parameter types // and then we replace them in the return type // Map<TypeParameterDeclaration, Type> determinedTypeParameters = new HashMap<>(); InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE); for (int i = 0; i < methodDeclaration.getNumberOfParams() - (methodDeclaration.hasVariadicParameter() ? 1 : 0); i++) { ResolvedType formalParamType = methodDeclaration.getParam(i).getType(); ResolvedType actualParamType = parameterTypes.get(i); inferenceContext.addPair(formalParamType, actualParamType); } returnType = inferenceContext.resolve(inferenceContext.addSingle(returnType)); return new MethodUsage(methodDeclaration, params, returnType); }
public MethodUsage resolveTypeVariables(Context context, List<ResolvedType> parameterTypes) { ResolvedType returnType = replaceTypeParams(methodDeclaration.getReturnType(), typeSolver, context); List<ResolvedType> params = new ArrayList<>(); for (int i = 0; i < methodDeclaration.getNumberOfParams(); i++) { ResolvedType replaced = replaceTypeParams(methodDeclaration.getParam(i).getType(), typeSolver, context); params.add(replaced); } // We now look at the type parameter for the method which we can derive from the parameter types // and then we replace them in the return type // Map<TypeParameterDeclaration, Type> determinedTypeParameters = new HashMap<>(); InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE); for (int i = 0; i < methodDeclaration.getNumberOfParams() - (methodDeclaration.hasVariadicParameter() ? 1 : 0); i++) { ResolvedType formalParamType = methodDeclaration.getParam(i).getType(); ResolvedType actualParamType = parameterTypes.get(i); inferenceContext.addPair(formalParamType, actualParamType); } returnType = inferenceContext.resolve(inferenceContext.addSingle(returnType)); return new MethodUsage(methodDeclaration, params, returnType); }
public MethodUsage resolveTypeVariables(Context context, List<ResolvedType> parameterTypes) { ResolvedType returnType = replaceTypeParams(methodDeclaration.getReturnType(), context); List<ResolvedType> params = new ArrayList<>(); for (int i = 0; i < methodDeclaration.getNumberOfParams(); i++) { ResolvedType replaced = replaceTypeParams(methodDeclaration.getParam(i).getType(), context); params.add(replaced); } // We now look at the type parameter for the method which we can derive from the parameter types // and then we replace them in the return type // Map<TypeParameterDeclaration, Type> determinedTypeParameters = new HashMap<>(); InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE); for (int i = 0; i < methodDeclaration.getNumberOfParams() - (methodDeclaration.hasVariadicParameter() ? 1 : 0); i++) { ResolvedType formalParamType = methodDeclaration.getParam(i).getType(); ResolvedType actualParamType = parameterTypes.get(i); inferenceContext.addPair(formalParamType, actualParamType); } returnType = inferenceContext.resolve(inferenceContext.addSingle(returnType)); return new MethodUsage(methodDeclaration, params, returnType); }
@Test public void resolveMethodDeclaration() throws ParseException { TypeSolver typeSolver = new ReflectionTypeSolver(); CompilationUnit cu = parseSample("SymbolResolverExample"); new JavaSymbolSolver(typeSolver).inject(cu); MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMethods().get(0); ResolvedMethodDeclaration resolvedMethodDeclaration = methodDeclaration.resolve(); assertEquals("foo", resolvedMethodDeclaration.getName()); assertEquals("A[]", resolvedMethodDeclaration.getReturnType().describe()); assertEquals("java.lang.String[]", resolvedMethodDeclaration.getParam(0).getType().describe()); assertEquals("int[]", resolvedMethodDeclaration.getParam(1).getType().describe()); }