How to instantiate class from with class.forName()

I try to create new class from a class name but I fail. My first state it's find a good class, I manage this part but after I find no possibility to instantiate from the class name. I need to do that because all my methods is used everywhere in my code and I need to find information from those in a single place. I hope my purpose is clear...

When I pass the name find with method Field by method forName() the console return Unhandled exception type ClassNotFoundException

import java.util.Iterator;
import java.lang.reflect.*; 

Target class_a = new Target() ;
Target class_b = new Target() ;
Truc class_c = new Truc() ;

void setup() {
  class_a.is(true);
  class_b.is(false);

  Field [] f = this.getClass().getDeclaredFields();
  println("field num:",f.length);
  for(int i = 0 ; i < f.length ; i++) {
    if(f[i].getType().getName().contains("Target")) {
     println("BRAVO it's a good classes");
     println("class:",f[i].getClass());
     println("name:",f[i].getName());

     // I imagine pass the name here to instantiate the class but that's don't work
     //Class<?> class_type = Class.forName(f[i].getName());
     Class<?> class_type = Class.forName("class_a");
     //Class<?> class_type = Class.forName(f[i].getClass());
     Target t = (Target) class_type.newInstance();


     // here I try to call method of the selected class
     println(t.get_is());
   }
 }
}


class Target{
  boolean is;
  Target() {}
  void is(boolean is) {
   this.is = is;
  }  
  boolean get_is() {
    return is;
  }
}


class Truc{
  Truc() {}
}

Answers

  • Please note that there’s a new forum

    Better post there

  • again a new forum :) Ok I post on it. Thx

  • Thx @GoToLoop I improve my code, but I cannot use the Field.getName()to point on the declared Classes created before. I find to information to do that.

    import java.lang.reflect.*; 
    
    Inner in_1;
    Inner in_2;
    
    void setup() {
      in_1 = new Inner();
      in_2 = new Inner();
      in_1.is(true);
      in_1.is(false);
      /*
      println(this.getClass().getDeclaredConstructors());
      println(this.getClass().getDeclaredClasses());
      println(this.getClass().getDeclaredMethods());
      println(this.getClass().getDeclaredFields());
      */
    
      Class<?> appCls = getClass(), innerCls = null;
      try {
        innerCls = Class.forName(appCls.getName() + "$Inner");
      }
      catch (final ClassNotFoundException ex) {
        System.err.println(ex);
      }
      Field [] f = this.getClass().getDeclaredFields();
      println(f);
      for(int i = 0 ; i < f.length ; i++) {
        println(f[i].getType());
        println(innerCls);
        if(f[i].getType() == innerCls) {
          println("LA CLASSE, now the mission is to catch info from declared Classes in_1 and in_2");
          println(f[i].getName());
          // println(f[i].get(innerCls));
        }
    
      }
    }
    
    
    
    
    class Inner {
      boolean is; 
      void is(boolean is) {
        this.is = is;
      }
    }
    
  • edited June 13 Answer ✓
    /**
     * getBoolFromField() (v1.1)
     * GoToLoop (2018/Jun/13)
     *
     * Forum.Processing.org/two/discussion/28053/
     * how-to-instantiate-class-from-with-class-forname#Item_6
     */
    
    Inner in;
    
    void setup() {
      in = new Inner(true);
      //final boolean bool = in.is;
      final boolean bool = readBoolFromField(in, "is");
      println(bool);
      exit();
    }
    
    static final boolean readBoolFromField(final Object o, final String f) {
      try {
        return o.getClass().getDeclaredField(f).getBoolean(o);
      }
      catch (final ReflectiveOperationException cause) {
        throw new RuntimeException(cause);
      }
    }
    
    class Inner {
      boolean is;
    
      Inner() {
      }
    
      Inner(final boolean bool) {
        is(bool);
      }
    
      Inner is(final boolean bool) {
        is = bool;
        return this;
      }
    
      @ Override String toString() {
        return "" + is;
      }
    }
    
  • Whaouuu, very intresting way. I try tomorrow to use the Field.getName()to pass at method readBoolFromField(in, "is") so I must transform String to Object ?

  • I think continued on the new forum

Sign In or Register to comment.