public class Service{
String serviceName;
//setter and getter
}
public class Version{
int VersionID;
//setter and getter
}
public void test(Object list){
//it shd print the obtained list
}
List< Service> list1; //Service is a Bean
List< Version> list2; //Version is a Bean
test(list1);
test(list2);
Now the test method shd print the obtained list - (i.e) If the list is of type Service ,then serviceName should be printed using its getter. If the list type is Version versionID should be printed.
Is it possible to achieve this without using Interface or abstract class?
@danLeon has the simplest idea so far (adding toString
to the classes), assuming you have access to Service
and Version
.
I'm not sure why you were thinking of reflection, but the only thing I can think of is that you want something that will work with any object that has a single attribute String
getter, and then you would do something like this (crazy IMO, but it uses reflection):
Class clazz = list.get(0).getClass();
Method[] methods = clazz.getDeclaredMethods();
Method onlyStringGetter = null;
for (Method method: methods) {
String mName = method.getName();
if (mName.matches("get\w+") {
if (method.getReturnType().equals(String.class) {
if (onlyStringGetter != null) thrown new RuntimeException("More than one String getter available");
onlyStringGetter = method;
}
}
}
if (onlyStringGetter == null) throw new RuntimeException("No String getter found for class: " + clazz.getName());
List<String> strings = new ArrayList<String>();
for (Object singleStringAttribObj: list) {
// some exception handling needed for below
String result = (String)onlyStringGetter.invoke(singleStringAttribObj);
strings.add(result);
}
System.out.println(strings);
I haven't compiled or tried, but that is approximately right. Definitely some additional exception handling is required
if(Object instanceof List) {
List list = (List)Object ;
for(int index=0; index < list.length();index++) {
Object obj = list.get(index);
if(obj instanceof Service) {
//cast to service and print value or use reflection
Service service= (Service)obj ;
System.out.println(service.geServiceName());
} else if(obj instanceof Version) {
// cast to Version and print versionID someting
Version version = (Version)obj ;
System.out.println(version.getVersionId());
}
}
}
static public class Service {
String serviceName;
@Override
public String toString() {
return serviceName;
}
}
static public class Version {
String VersionID;
@Override
public String toString() {
return VersionID;
}
}
static public void test(List<?> list) {
for (Object object : list) {
System.out.println(object.toString());
}
}
public void test(Object list) {
if(list instanceof List)
test((List)list);
}
public void test(List<?> list) {
if (!list.isEmpty()) {
Object o = list.get(0);
if (o instanceof Version) {
@SuppressWarnings("unchecked")
List<Version> lVersion = (List<Version>) list;
for (Version v : lVersion) {
System.out.println(v.getVersionID());
}
} else if (o instanceof Service) {
@SuppressWarnings("unchecked")
List<Service> lService = (List<Service>) list;
for (Service s : lService) {
System.out.println(s.getServiceName());
}
}
}
}