Java Polymorphism and Downgrading

I play with the Java reflection APIs and I write methods that validate a given object and describe its fields, methods, etc.

I use the getFields() method to iterate over various attributes and display the contents of an object:

 public static void display(Integer i) { System.out.println("An integer: " + i); } // Basically a method for each primitive type wrapper public static void display(Object o) { for (Field f : c.getFields()) { System.out.println("A " + o.getClass() + " which is composed of:"); display(f.get(o)); } } 

(Other primitive types and arrays are omitted for simplicity).

The eventough Field get method returns Object , I thought that the correct methods would be called for primitives of primitive type (integers, strings, etc.), but actually only display(Object o) called (no implicit lowering is performed).

Currently, the only solution I have found is to brutally suppress objects, if possible:

 public static void display(Object o) { if (o instanceof Integer) { display((Integer) o); return; } else if (o instanceof String { ... } // And so on for (Field f : c.getFields()) { System.out.println("A " + o.getClass() + " which is composed of:"); display(f.get(o)); } } 

This, however, seems ugly, and I was wondering if there is a more elegant way to make sure the correct method is called. Any ideas?

Thanks, good people from StackOverflow!

+6
source share
3 answers

What you want is the so-called "double submit", which Java does not support.

You can simulate it through reflection: check all of your display methods and their argument types and dynamically call one of them based on the type of runtime of the object.

This is overly smart for this problem. Your if-else chain is great.

+1
source
Good question. I was also interested last night;)

So the answer is that there are two different types of polymorphism: compilation and run-time polymorphism.

When you use the supertype method and the overridden sybtypes methods, this is polymorphism at runtime.

When you use super types and subtypes as parameters for different overloaded methods, this is a compile-time polymorphism.

Therefore, the compiler, in your case, must know at compile time which method (/ overload) to execute, it will not make this decision at runtime.

If you had control over the field class, then the solution would be to use generics so that you define

 class Field<T> 

using method

 T get( Object o ) 

Then the compiler will be able to find out at compile time which method to use to display, as he would know, at compile time, what type field.get returns.

But according to your comments, you are using the java.lang.reflect.Field JDK class and therefore you have no control over it to make it shared. In this case, yes, the only solution is a mapping method that handles all the various possible types for field.get. But really, the String.valueOf method could help a lot here, since it provides a string representation of any type in java, it is already doing the overload job you're looking for.

The source is here .

Regards, StΓ©phane

+3
source

Java does not support "multiple polymorphism." You might want to take a look at the visitor template. This is an almost educational example of a visitor.

0
source

Source: https://habr.com/ru/post/890427/


All Articles