vendredi 17 novembre 2017

how to create a variable that can store object of unrelated classes

I'm using reflection to call methods of different classes which are unrelated in terms of functionality but related in terms of operation. Hence I have to pass the object of those classes to invoke method, but how do i store the object in the same variable.

            Method target;
            if (handlerType.equals("database")){
                target = DatabaseRequestHandler.class.getMethod(method, Args.class);
            } else if (handlerType.equals("document")){
                target = DocumentRequestHandler.class.getMethod(method, Args.class);
            } else if (handlerType.equals("dictionary")){
                target = DictionaryRequestHandler.class.getMethod(method, Args.class);
            } else {
                throw new IllegalArgumentException("Handler not implemented for this call");
            }
            //Method target = RequestHandler.class.getMethod(method, Args.class);
            if (target.getReturnType().equals(Void.TYPE)) {
                target.invoke(requestHandler, args);
            } else {
                Object result = target.invoke(requestHandler, args);
                body = ValueSerializer.serialize(result, memory);
            }

How should I declare requestHandler variable so that I can store the object of DatabaseHandler, DocumentHandler and DictionaryHandler. All these classes are not related to each other, so there is not point of using the inheritance with them. I'm from python background so really not sure how do I achieve this.

Thanks for the help





Aucun commentaire:

Enregistrer un commentaire