I am trying to get values from a class in another class which will be created dynamically and also methods too. Check these examples i've gone through .
InterfaceA.java
public interface InterfaceA{
public ArrayList<?> getValues();
}
ClassA.java / ClassB.java(consider another same class have value="World")
public Class A implements InterfaceA{
String value = "Hello";
public ArrayList<?> getValues(){
ArrayList<String> values = new ArrayList<String>();
values.add(this.value);
return values ;
}
}
ClassC.java
public Class C{
public void getValues(){
Object modelObject;
Method getValues;
modelObject = resolveClass("A"); // arg = classPath
getValues= modelObject.getClass().getMethod("getValues");
getValues.invoke(modelObject);
ArrayList<?> classValues;
// How to access Class A values from here
// I want to do These Lines
// classValue = get value from A/B.getValues() dynamically
}
private Object resolveClass(String className) throws
ClassNotFoundException, NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
Class<?> loadClass = Class.forName(className);
Constructor<?> constructor = loadClass.getConstructor(String.class);
Object object = constructor.newInstance(new Object[] {});
return object;
}
}
How to access that method returned values as ArrayList<> mentioned in comments?
Aucun commentaire:
Enregistrer un commentaire