java - Reflection and constructors with interface argument -
i'm trying modify private field inside class, has constructor taking interface argument. having trouble instantiating such class (it throws java.lang.illegalargumentexception: wrong number of arguments). code stripped important details follows:
here reflection code inject different boolean value (unique field true default want false there):
private void modifysitepatterns() { try { thread thread = thread.currentthread(); classloader classloader = thread.getcontextclassloader(); class<?> classtomodify = class.forname( "dr.evolution.alignment.sitepatterns", true, classloader); constructor<?>[] constructors = classtomodify .getdeclaredconstructors(); field[] fields = classtomodify.getdeclaredfields(); object classobj = constructors[0].newinstance(new object[] {}); //this throws exception (int = 0; < fields.length; i++) { if (fields[i].getname() == "unique") { system.out.println(i); fields[i].setaccessible(true); fields[i].set(classobj, false); } } } catch (exception e) { e.printstacktrace(); } }// end: modifysitepatterns()
here class i'm trying modify:
public class sitepatterns implements sitelist, dr.util.xhtmlable { //omitted private boolean unique = true; public sitepatterns(alignment alignment) {// constructor 0 this(alignment, null, 0, 0, 1); } }
and argument giving me trouble:
public interface alignment extends sequencelist, sitelist { //omitted public abstract class abstract implements alignment { } //omitted }
how should proceed passing fake argument instance of constructor?
you have no concrete implementation have shown us. not know how expect without concrete implementation of alignment.
//anonymous implementation object classobj = constructors[0].newinstance(new alignment() { //alignment implementation... }); //or concrete implementation object classobj = constructors[0].newinstance(new alignmentimpl());
Comments
Post a Comment