Reputation: 663
I would like to make this method generic for all class types:
public static void printListSample(List<Class<?>> dateExtend) {
System.out.println("dateExtend size: "+dateExtend.size());
if(dateExtend.size() > 0){
for(Class<?> mp : dateExtend){
System.out.print("\n print entry - ");
if(mp.getDate() != null){
String dateDb = new SimpleDateFormat("dd.MM.yyyy.").format(mp.getDate());
System.out.print("value: "+dateDb);
}
}
}
System.out.println();
}
Notice Class< ? > - now like this it doesen't work; but I think I am on the right track.
I would like to dinamically cast Class passed in the parameter and then dinamically cast it for for-loop (so I don't have to do if (instance of SomeClass.. )
How to do this? I would need example code, really don't know how to write this.
Edit: getDate is method from my custom object I passed as param. So Java would need to figure out dynamically what class type was object that was passed.
I would like to write generic method for printing List (or ArrayList) of any passed type:
<MyClass> or <String> or <SomeOtherClass>,
so I wouldn't have to worry about types and casting would be done automatically.
Edit (2): I would like to determine class type INSIDE method (I presume I can grab it from:
<T> or <?>
with class info I am sending) ?
Upvotes: 0
Views: 88
Reputation: 39
Have you tried using
public static <T> void printListSample(List<T> dateExtend){
...
if...
for(T mp : dateExtend){
...
}
}
you will likely have the issue that the getDate() function is undefined for type T. There are a couple solutions to this. 1) you could cast mp (not usually considered a good meathod) 2) you could extend T in your input to only classes with a getDate function 3) you could use a try catch
//option 2
public static <T extends MyClass> void printListSample(List<T> dateExtend){
}
"myClass" will need to be a parent class so that no matter what type T is in your list, it has inhered the function getDate();
Upvotes: 0
Reputation: 140494
No Class
instance has a getDate()
method on it.
I suspect you need something like List<? extends SomeClassWithAGetDateMethod>
as the parameter:
public static void printListSample(List<? extends SomeClassWithAGetDateMethod> dateExtend) {
System.out.println("dateExtend size: "+dateExtend.size());
if(dateExtend.size() > 0){
for(SomeClassWithAGetDateMethod mp : dateExtend){
if (mp.getDate() != null) {
...
}
}
}
}
Incidentally, you don't need to check if a List
is empty before iterating it: just iterate it, and if it has no elements, nothing in the loop is run.
if (list.size() > 0) { for (ElementType e : list) { ... } }
is equivalent to:
for (ElementType e : list) { ... }
(and also, !list.isEmpty()
can be more efficient (and is no less efficient) than checking if the size is greater than zero).
Upvotes: 4
Reputation: 4218
Because you are trying to invoke mp.getDate()
I imagine that all the classes you want to accept in your list have this method;
If it's the case you can have a look at Generic Methods and Bounded Type Parameters
In order to have something like this :
public interface WithDate {
public Date getDate();
}
public static void printListSample(List<? extends WithDate> dateExtend) {
System.out.println("dateExtend size: "+dateExtend.size());
if(dateExtend.size() > 0){
for(WithDate mp : dateExtend){
System.out.print("\n print entry - ");
if(mp.getDate() != null){
String dateDb = new SimpleDateFormat("dd.MM.yyyy.").format(mp.getDate());
System.out.print("value: "+dateDb);
}
}
}
System.out.println();
}
Upvotes: 1