reflection - Overriding or modifying a Java class constructor at runtime -


i have class being instantiated part of sequence (a method goes through random list of classes , creates 3 instances of each)

i writing 1 of classes in list (note: classes exist in same package)

i unable modify source files of of other classes , need either stop instantiation of classes following own or override constructor empty method.

i have far tried using reflection, , have been looking @ classloader cant figure out how this

i can gain reference constructor reflection far have been unable interfere it

edit -> thought had (dynamically re-write , recompile target classes), code compiles im getting exception stops new class compiling

private void rewrite(string name) {//throws ioexception {     try{     file sourcefile = new file("/temp/" + "name" + ".java");     system.out.println("file defined");     filewriter writer = new filewriter(sourcefile);     system.out.println("filewriter defined");      writer.write("public class "+name+" extends creature {/n"             + "private static int instancecount = 0;/n" + "public "+name+"() {/n"             + " instancecount++;/n" + "}/n" + "public void doturn() {}/n"             + "public final void creaturedestroyed() {/n"             + " instancecount--;/n" + "}/n"             + "public final int getinstancecount() {/n"             + " return instancecount;/n" + "}/n" + "}/n");     writer.close();     system.out.println("written");      javacompiler compiler = toolprovider.getsystemjavacompiler();     standardjavafilemanager filemanager = compiler.getstandardfilemanager(null,null,null);      filemanager.setlocation(standardlocation.class_output,             arrays.aslist(new file("/temp")));     // compile file     compiler.gettask(             null,             filemanager,             null,             null,             null,             filemanager.getjavafileobjectsfromfiles(arrays                     .aslist(sourcefile))).call();     system.out.println("compiled");     filemanager.close();     }catch(exception e){         system.out.println("fuckery");     } } 

edit-> following code providing based on comments below

try {         getw = (world.class.getdeclaredmethod("getworld"));         getw.setaccessible(true);         theworld = getw.invoke(world.class);         getk = (creature.class.getdeclaredmethod("die"));         getk.setaccessible(true);     } catch (nosuchmethodexception exc) {         system.out.println("method not found.");     } catch (exception e) {         system.out.println("well shit");     }      try {         f = world.class.getdeclaredfield("locations");         f.setaccessible(true);         locations = f.get(theworld);         loc = (arraylist<creature>) locations;     } catch (exception e) {         system.out.println("well poop");     }     if (loc.size() > 0 && !allmine) {         (int = 0; < loc.size(); i++) {             if (loc.get(i).getclass() != colonial.class                     && loc.get(i).getclass() != plant.class) {                  try {                     getk.invoke(loc.get(i));                 } catch (exception e) {                  }                  loc.set(i, new openland());             }         }     } 

you can use sun.misc.unsafe#allocateinstance(myclass.class) construct instances without calling constructor. see http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/ more info.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -