Initializing and passing child class types as parameters to function interfaces in Java

Viewed 28

I want to learn if there's a way to create a method to generalize the way we handle incoming requests by using function variables and reflectivity of classes.

To make things more clear let's say I have the following class and it's children:

public class ParentClassRequest implements Serializable {
    private String name;
}

public class ChildClassOneRequest extends ParentClassRequest implements Serializable {
    private Integer money;
}

public class ChildClassTwoRequest extends ParentClassRequest implements Serializable {
    private Integer candy;
}

public class ParentClassResponse implements Serializable {
    private String name;
}

public class ChildClassOneResponse extends ParentClassResponse implements Serializable {
    private Integer money;
}

public class ChildClassTwoResponse extends ParentClassResponse implements Serializable {
    private Integer candy;
}

And I have the following adapter classes and methods:


@Service
public class PersonSender {

    public ChildClassOneResponse childClassOneSender(final ChildClassOneRequest childClassOne) {
        
        
        final ChildClassOneResponse childClassOneResponse = new ChildClassOneResponse();
        childClassOneResponse.setMoney = childClassOne.getMoney;

        return childClassOneResponse
    }

    public ChildClassTwoResponse childClassTwoSender(final ChildClassTwo childClassTwoRequ) {
        
        
        final ChildClassTwoResponse childClassTwoResponse = new ChildClassTwoResponse();
        childClassTwoResponse.setCandy = childClassTwo.getCandy;

        return childClassTwoResponse
    }
 
}

I want to create a service class in a way to generalize the parameters such that:

public class TransportationService {
    
    // I have a session, I get the request using this
    @Autowired
    private Session session;

    public ParentClassRequest transport(
            Class<? extends ParentClassRequest> childClassRequestClass,
            Class<? extends ParentClassResponse> childClassResponseClass,
            Function<? extends ParentClassRequest, ? extends ParentClassResponse> adapterMethod) {

        //I need to declare the variable with the given type here and initialize it with the function given

        final childClassRequestClass request = session.get(childClassRequestClass);
        request.setName = "Test";

        final childClassResponseClass response = Function.apply(request);

        return response;
    }
}

So I can just call this function and pass the classes and the method as:

TransportationService.transport(ChildClassOneRequest.class, ChildClassOneResponse.class, PersonSender::childClassOneSender)

TransportationService.transport(ChildClassTwoRequest.class, ChildClassTwoResponse.class, PersonSender::childClassTwoSender)

However, there are two problems. First, I think I cannot pass functions with generic types. I get Incompatible types: ParentClassRequest is not convertible to ChildClassOneRequest. Secondly, I cannot declare a new variable of the class type given as a parameter. I want to declare it so I can change some fields and send it to the adapter using the passed-in method. I think I am not able to achieve these. Any help or pointers would be beneficial. If this is not possible, I can try a pattern to solve this issue, but I hope to achieve everything using these.

0 Answers
Related