Java Reflection/Generic -
given following 3 classes how can use reflection call initialize function parent class(es) , subclass:
public class test { public static void main(string[] args) { extendedelement ee = new extendedelement(); initialize(ee); } public static void initialize(element element) { system.out.println(element.getclass()); initialize(element.getclass()); } public static void initialize(class clazz) { system.out.println(clazz.getclass()); } } public class element { protected string value; public string getvalue() { return value; } public void setvalue(string value) { this.value = value; } } public class extendedelement extends element { protected string extendedvalue; public void setextendedvalue(string extendedvalue) { this.extendedvalue = extendedvalue; } public string getextendedvalue() { return extendedvalue; } }
i'm not quite sure on how paramertize initialize function in test class, clazz parameter raw type.
what need call initialize class hierarchy if pass initialize of subclass of element.
something following:
public void initialize(class clazz) { if (element.class.isinstance(clazz.getclass().getsuperclass()) { initialize(clazz.getclass().getsuperclass()); } //work call initialize function }
edit 1:
can't parameterize above pseudo function differently retain type of object , call function need to?
what i'm trying avoid having have same method overridden each of classes , allow inheritance selenium 2 page objects. need able introspect superclass(es) of self , initialize each of webelement fields prior running tests on these fields.
these being injected spring, , further complicate things allowing tests written using spring expression language. lazy loading beans, , using initializingbean interface attempt initialize webelements prior usage avoid npes.
i had wrap webelements custom object inject location strategies using spring (we reuse lot of pieces, have different ids / class names dependent upon used in application; done prior me getting here , not changed @ time despite arguments consistency). example have date widget has different granularities, need month, month , year, etc... it'd nice if use abstract class , break these commonalities down least common denominator , extend there. need able following in base class:
public abstract class pageobject implements initializingbean { ... public void afterpropertiesset() { //pass in concrete impl working - allows me initialize initializewebelements(this.getclass()); } ... public void initializewebelements(class clazz) { //this not grab inherited fields, need initialized (field field : clazz.getdeclaredfields()) { if (widgetelement.class == field.gettype()) { method getwidgetelement = clazz.getdeclaredmethod("get" + stringutils.capitalize(field.getname()), new class [] {}); widgetelement element = (widgetelement) getwidgetelement.invoke(this, new object [] {}); element.initelement(); } }
you can't call method @ specific level. thing have access super
keyword inside class itself.
to make work, want call super.initialize()
within each subclass, call via reflection.
this not c++, can call specific method @ specific level of inheritance hierarchy.
Comments
Post a Comment