java - Get value of unknown class field -


i have bunch of unknown classes stored in list/array. how value of fields these classes?

what should paste in f.get() ?

for(class<?> cl : classlist){         for(field f : cl.getfields()){             try {                 f.setaccessible(true);                 writeln(f.get( <what paste here> ));             } catch (exception e) {                 e.printstacktrace();             }         }     } 

as far reflection concerned, prefer rely on frameworks more readable api standard java reflection api. i've used two:

the first 1 mirror. getting fields values (either static or instance) looks this:

getting value of static field:

class clazz; object value = new mirror().on(clazz).get().field("fieldname"); 

getting value of instance field:

object target; object value = new mirror().on(target).get().field("fieldname"); 

you can pass java.lang.reflect.field instead of fieldname string.

getting value of static field:

field afield; class clazz; object value = new mirror().on(clazz).get().field(afield); 

fest-reflect nice library (fest fluent assertions nice, btw).

import static org.fest.reflect.core.reflection.*;  // retrieves value of field "name" string name = field("name").oftype(string.class).in(person).get();  // retrieves value of field "powers" list<string> powers = field("powers").oftype(new typeref<list<string>>() {})                                      .in(jedi).get();  // retrieves value of static field "count" in person.class int count = field("count").oftype(int.class).in(person.class).get();   // retrieves value of static field "commonpowers" in jedi.class list<string> commmonpowers = field("commonpowers")                                     .oftype(new typeref<list<string>>() {})                                     .in(jedi.class).get(); 

all examples taken respective library's documentation.

some quick , rough examples based on code you've provided: achieve want (if understand correctly) mirror:

for(class<?> cl : classlist){    mirror mirror = new mirror().on(cl);    for(field f: mirror.reflectall().fields()) {       if(java.lang.reflect.modifier.isstatic(f.getmodifiers()) {          writeln(mirror.get().field(f));       }    } } 

i've assumed want read static fields. object list:

for(object obj : objectlist){    for(field f: new mirror().on(obj.getclass()).reflectall().fields()) {        writeln(new mirror().on(obj).get().field(f));    } } 

the same fest-reflect:

for(class<?> cl : classlist){     for(field f : cl.getfields()){       if(java.lang.reflect.modifier.isstatic(f.getmodifiers()) {          writeln(field(f.getname()).oftype(f.gettype()).in(cl).get());       }     } } 

and:

for(object obj : objectlist){    for(field f: obj.getclass().getfields()) {       if(!java.lang.reflect.modifier.isstatic(f.getmodifiers()) { //not 100% sure if required          writeln(field(f.getname()).oftype(f.gettype()).in(obj).get());       }    } } 

note can't read instance filed values without actual class instance (object) read them from.

i hope helps. examples use refactoring, of course.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -