class - Java - Using reflection to attach data to object if its type matches a string -


so here's question java experts.

i have definitions keep in text files, let's they're properties files. have wide variety of different objects of implement interface, we'll call cardable. guarantees methods attach these definitions , retrieve them display within application.

public interface cardable {    public void attach(card c);    public card retrieve(); } 

now, when build static list(s) of these objects, types vary, see if type matches set of classes declared in definition so

thing.bag.leatherbag= ... (definitions) 

if creating class (definition condensed show idea)

public class leatherbag extends bag {    ... }  public class bag extends thing {    ... }  public class thing implements cardable {    ... } 

the process go through list during pre-loading phase , assign specific card given class. defined arbitrarily as, instance, more classes specified (thing.bag.leatherbag more specific bag.leatherbag rule.)

once break class names out of string, how go using instanceof check if object (once verify implements cardable) instance of of specified classes ? instanceof seems pertain types, not strings containing type/class names.

if understand correctly, have class represented string name, , want determine if implements set of interfaces represented string names , if extends set of classes represented string names. can class class.

void isvalid(string classname) {     class clazz = class.forname(classname);     class[] interfaces = clazz.getinterfaces();     arraylist<class> superclasses = new arraylist<>();     class temp = clazz.getsuperclass();     superclasses.add(temp);     while(temp != object.class) {         temp = temp.getsuperclass();         superclasses.add(temp);     } } 

this should leave interfaces holding of class's interfaces, , `superclasses' holding of class's superclasses. can call class#getname on these class objects , compare them string representations of interfaces / classes see if class in question meets criteria.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -