@Test public void testDesugarizeAsteriskParamWhichIsNotAReference() { List<FormalParameterNode> formalParameters = ImmutableList.of(fn1, fn2, fd2, fd2); Expression value = createMock(Expression.class); SymbolResolver resolver = createMock(SymbolResolver.class); TypeUsageNode typeOfAsteriskParam = createMock(TypeUsageNode.class); expect(value.calcType()).andReturn(typeOfAsteriskParam); expect(typeOfAsteriskParam.isReference()).andReturn(false); Node parent = new IntLiteral(3); replay(value, resolver, typeOfAsteriskParam); Either<String, List<ActualParam>> res = desugarizeAsteriskParam(formalParameters, value, resolver, parent); assertEquals(true, res.isLeft()); verify(value, resolver, typeOfAsteriskParam); }
public static Either<String, List<ActualParam>> desugarizeAsteriskParam(List<? extends FormalParameter> formalParameters, Expression value, SymbolResolver resolver, Node parent) { TypeUsage type = value.calcType(); if (!type.isReference()) { return Either.left("An asterisk param should be an object"); } List<ActualParam> actualParams = new ArrayList<>(); TypeDefinition typeDefinition = type.asReferenceTypeUsage().getTypeDefinition(); Expression mapCreation = new Creation("turin.collections.MapBuilder", Collections.emptyList()); mapCreation.setParent(parent); for (FormalParameter formalParameter : formalParameters) { String getterName = getterName(formalParameter, resolver); InstanceMethodInvokation instanceMethodInvokation = new InstanceMethodInvokation(value, getterName, Collections.emptyList()); if (typeDefinition.hasMethodFor(getterName, Collections.emptyList(), false)) { if (formalParameter.hasDefaultValue()) { List<ActualParam> params = new ArrayList<>(); params.add(new ActualParam(new StringLiteral(formalParameter.getName()))); params.add(new ActualParam(instanceMethodInvokation)); mapCreation = new InstanceMethodInvokation(mapCreation, "put", params); } else { ActualParam actualParam = new ActualParam(instanceMethodInvokation); actualParam.setParent(parent); actualParams.add(actualParam); } } else { if (!formalParameter.hasDefaultValue()) { return Either.left("the given value has not a getter '" + getterName + "'"); } } } if (hasDefaultParams(formalParameters)) { mapCreation = new InstanceMethodInvokation(mapCreation, "build", ImmutableList.of()); mapCreation.setParent(parent); ActualParam mapForDefaultParams = new ActualParam(mapCreation); mapForDefaultParams.setParent(parent); actualParams.add(mapForDefaultParams); } return Either.right(actualParams); }
ParamUtils { public static Either<String, List<ActualParam>> desugarizeAsteriskParam(List<? extends FormalParameter> formalParameters, Expression value, SymbolResolver resolver, Node parent) { TypeUsage type = value.calcType(); if (!type.isReference()) { return Either.left("An asterisk param should be an object"); } List<ActualParam> actualParams = new ArrayList<>(); TypeDefinition typeDefinition = type.asReferenceTypeUsage().getTypeDefinition(); Expression mapCreation = new Creation("turin.collections.MapBuilder", Collections.emptyList()); mapCreation.setParent(parent); for (FormalParameter formalParameter : formalParameters) { String getterName = getterName(formalParameter, resolver); InstanceMethodInvokation instanceMethodInvokation = new InstanceMethodInvokation(value, getterName, Collections.emptyList()); if (typeDefinition.hasMethodFor(getterName, Collections.emptyList(), false)) { if (formalParameter.hasDefaultValue()) { List<ActualParam> params = new ArrayList<>(); params.add(new ActualParam(new StringLiteral(formalParameter.getName()))); params.add(new ActualParam(instanceMethodInvokation)); mapCreation = new InstanceMethodInvokation(mapCreation, "put", params); } else { ActualParam actualParam = new ActualParam(instanceMethodInvokation); actualParam.setParent(parent); actualParams.add(actualParam); } } else { if (!formalParameter.hasDefaultValue()) { return Either.left("the given value has not a getter '" + getterName + "'"); } } } if (hasDefaultParams(formalParameters)) { mapCreation = new InstanceMethodInvokation(mapCreation, "build", ImmutableList.of()); mapCreation.setParent(parent); ActualParam mapForDefaultParams = new ActualParam(mapCreation); mapForDefaultParams.setParent(parent); actualParams.add(mapForDefaultParams); } return Either.right(actualParams); } }
ParamUtils { public static Either<String, List<ActualParam>> desugarizeAsteriskParam(List<? extends FormalParameter> formalParameters, Expression value, SymbolResolver resolver, Node parent) { TypeUsage type = value.calcType(); if (!type.isReference()) { return Either.left("An asterisk param should be an object"); } List<ActualParam> actualParams = new ArrayList<>(); TypeDefinition typeDefinition = type.asReferenceTypeUsage().getTypeDefinition(); Expression mapCreation = new Creation("turin.collections.MapBuilder", Collections.emptyList()); mapCreation.setParent(parent); for (FormalParameter formalParameter : formalParameters) { String getterName = getterName(formalParameter, resolver); InstanceMethodInvokation instanceMethodInvokation = new InstanceMethodInvokation(value, getterName, Collections.emptyList()); if (typeDefinition.hasMethodFor(getterName, Collections.emptyList(), false)) { if (formalParameter.hasDefaultValue()) { List<ActualParam> params = new ArrayList<>(); params.add(new ActualParam(new StringLiteral(formalParameter.getName()))); params.add(new ActualParam(instanceMethodInvokation)); mapCreation = new InstanceMethodInvokation(mapCreation, "put", params); } else { ActualParam actualParam = new ActualParam(instanceMethodInvokation); actualParam.setParent(parent); actualParams.add(actualParam); } } else { if (!formalParameter.hasDefaultValue()) { return Either.left("the given value has not a getter '" + getterName + "'"); } } } if (hasDefaultParams(formalParameters)) { mapCreation = new InstanceMethodInvokation(mapCreation, "build", ImmutableList.of()); mapCreation.setParent(parent); ActualParam mapForDefaultParams = new ActualParam(mapCreation); mapForDefaultParams.setParent(parent); actualParams.add(mapForDefaultParams); } return Either.right(actualParams); } private  ParamUtils(); }
ParamUtils { public static Either<String, List<ActualParam>> desugarizeAsteriskParam(List<? extends FormalParameter> formalParameters, Expression value, SymbolResolver resolver, Node parent) { TypeUsage type = value.calcType(); if (!type.isReference()) { return Either.left("An asterisk param should be an object"); } List<ActualParam> actualParams = new ArrayList<>(); TypeDefinition typeDefinition = type.asReferenceTypeUsage().getTypeDefinition(); Expression mapCreation = new Creation("turin.collections.MapBuilder", Collections.emptyList()); mapCreation.setParent(parent); for (FormalParameter formalParameter : formalParameters) { String getterName = getterName(formalParameter, resolver); InstanceMethodInvokation instanceMethodInvokation = new InstanceMethodInvokation(value, getterName, Collections.emptyList()); if (typeDefinition.hasMethodFor(getterName, Collections.emptyList(), false)) { if (formalParameter.hasDefaultValue()) { List<ActualParam> params = new ArrayList<>(); params.add(new ActualParam(new StringLiteral(formalParameter.getName()))); params.add(new ActualParam(instanceMethodInvokation)); mapCreation = new InstanceMethodInvokation(mapCreation, "put", params); } else { ActualParam actualParam = new ActualParam(instanceMethodInvokation); actualParam.setParent(parent); actualParams.add(actualParam); } } else { if (!formalParameter.hasDefaultValue()) { return Either.left("the given value has not a getter '" + getterName + "'"); } } } if (hasDefaultParams(formalParameters)) { mapCreation = new InstanceMethodInvokation(mapCreation, "build", ImmutableList.of()); mapCreation.setParent(parent); ActualParam mapForDefaultParams = new ActualParam(mapCreation); mapForDefaultParams.setParent(parent); actualParams.add(mapForDefaultParams); } return Either.right(actualParams); } private  ParamUtils(); static boolean verifyOrder(List<ActualParam> actualParams); static List<ActualParam> unnamedParams(List<ActualParam> actualParams); static List<ActualParam> namedParams(List<ActualParam> actualParams); static boolean hasDefaultParams(List<? extends FormalParameter> formalParameters); static Either<String, List<ActualParam>> desugarizeAsteriskParam(List<? extends FormalParameter> formalParameters, Expression value, SymbolResolver resolver, Node parent); static String getterName(FormalParameter formalParameter, SymbolResolver resolver); }
ParamUtils { public static Either<String, List<ActualParam>> desugarizeAsteriskParam(List<? extends FormalParameter> formalParameters, Expression value, SymbolResolver resolver, Node parent) { TypeUsage type = value.calcType(); if (!type.isReference()) { return Either.left("An asterisk param should be an object"); } List<ActualParam> actualParams = new ArrayList<>(); TypeDefinition typeDefinition = type.asReferenceTypeUsage().getTypeDefinition(); Expression mapCreation = new Creation("turin.collections.MapBuilder", Collections.emptyList()); mapCreation.setParent(parent); for (FormalParameter formalParameter : formalParameters) { String getterName = getterName(formalParameter, resolver); InstanceMethodInvokation instanceMethodInvokation = new InstanceMethodInvokation(value, getterName, Collections.emptyList()); if (typeDefinition.hasMethodFor(getterName, Collections.emptyList(), false)) { if (formalParameter.hasDefaultValue()) { List<ActualParam> params = new ArrayList<>(); params.add(new ActualParam(new StringLiteral(formalParameter.getName()))); params.add(new ActualParam(instanceMethodInvokation)); mapCreation = new InstanceMethodInvokation(mapCreation, "put", params); } else { ActualParam actualParam = new ActualParam(instanceMethodInvokation); actualParam.setParent(parent); actualParams.add(actualParam); } } else { if (!formalParameter.hasDefaultValue()) { return Either.left("the given value has not a getter '" + getterName + "'"); } } } if (hasDefaultParams(formalParameters)) { mapCreation = new InstanceMethodInvokation(mapCreation, "build", ImmutableList.of()); mapCreation.setParent(parent); ActualParam mapForDefaultParams = new ActualParam(mapCreation); mapForDefaultParams.setParent(parent); actualParams.add(mapForDefaultParams); } return Either.right(actualParams); } private  ParamUtils(); static boolean verifyOrder(List<ActualParam> actualParams); static List<ActualParam> unnamedParams(List<ActualParam> actualParams); static List<ActualParam> namedParams(List<ActualParam> actualParams); static boolean hasDefaultParams(List<? extends FormalParameter> formalParameters); static Either<String, List<ActualParam>> desugarizeAsteriskParam(List<? extends FormalParameter> formalParameters, Expression value, SymbolResolver resolver, Node parent); static String getterName(FormalParameter formalParameter, SymbolResolver resolver);  }