0
package reflection;

import java.io.*;

import java.lang.reflect.*;

class class0
{
  public void writeout0()
  {
    System.out.println("class0");
  }
}

class class1
{
  public void writeout1()
  { 
    System.out.println("class1");
  }
}

class class2
{
  public void writeout2()
  {
    System.out.println("class2");
  }
}

 class class3

{

  public void run()

  {

    try

    {    

        BufferedReader reader= new BufferedReader(new InputStreamReader

(System.in)); 

          String line=reader.readLine(); 

          Class cls=Class.forName(line);
                 //define method here



    }

    catch(Exception ee)

    {

   System.out.println("here "+ee);

    }


  }
  public void writeout3()
  {
      System.out.println("class3");
  }
}

class class4
{
  public void writeout4()
  {
    System.out.println("class4");
  }
}

class class5
{
  public void writeout5()
  {
    System.out.println("class5");
  }
}

class class6
{
  public void writeout6()
  {
    System.out.println("class6");
  }
}

class class7
{
  public void writeout7()
  {
    System.out.println("class7");
  }
}

class class8
{
  public void writeout8()
  {
    System.out.println("class8");
  }
}

class class9
{
  public void writeout9()
  {
    System.out.println("class9");
  }
}



class testclass {

    public static void main(String[] args) {

       System.out.println("Write class name : ");

        class3 example=new class3();

        example.run();
    }

}

Question is ; third class will read the name of the class as String from console. Upon reading the name of the class, it will automatically and dynamically generate that class and call its writeout method.If that class is not read from input, it will not be initialized.

but I can't continue any more ; I need to more something for 3.class, what can I do?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jacob
  • 1
  • 1
  • 1
  • I'm happy to realize that there are only 10 classes in his program. – Roman Mar 24 '10 at 21:35
  • Here's a Java Reflection tutorial: http://java.sun.com/docs/books/tutorial/reflect/ You may find it useful. – BalusC Mar 24 '10 at 21:35
  • what do you mean by "generate that class" ? instantiate an object of that class ? – LB40 Mar 24 '10 at 21:36
  • @Jacob: you shouldn't put the `run` method on class 3, it is ok to have that same piece of code in the `main` method of the testclass. ( BTW in Java class names start with uppercase by convention ) – OscarRyz Mar 24 '10 at 21:58

5 Answers5

2

Up to this point you have a Class object. You need to instantiate the class to have an object to call a method on. Once you have the instance you need, you can find the method you want with a call to getMethod(String name, Class... parameterTypes) on the Class object. It's helpful if all of the classes have the same method name for the "writeout()" method, otherwise you have to figure out what the method is named inside of the class. Here is the code you're missing to make the call to "writeout()":

  Method m = cls.getMethod("writeout", null);
  Object writerInstance = cls.newInstance();

  m.invoke(writerInstance, null);

With the class and method naming scheme you have in your example, you could figure out the method name by parsing the class name and extracting the number. It's much easier if all the classes share the method name, though.

Rob Heiser
  • 2,792
  • 1
  • 21
  • 28
1

You need to load the class dynamically via the classloader. A trivial way to do this is to use:

Class.forName(yourClassName);

to get the class object, and then newInstance() to create a new instance of that class. That particular method will only work for classes with a default constructor. See the Class javadoc for more options re. calling constructors.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • I think he wants to check the methods first using getMethods and then checking that writeout is in, and instantiates the object after that..but i'm not to understand the question – LB40 Mar 24 '10 at 21:45
  • I agree it's open to interpretation – Brian Agnew Mar 24 '10 at 22:05
0

Simple example for your reference:

Class<?> cls_obj = Class.forName("MyClass.java");
Method method = cls_obj.getMethod("show");
Object obj = cls_obj.newInstance();
method.invoke(obj);
Srinivasan.S
  • 3,065
  • 1
  • 24
  • 15
0

For generating Java class files dynamically, there is a library, called Byte Code Engeneering Library (BCEL), which will wrap generated byte code instructions around a class file and load it into the runtime.

You will have to learn something about the Java Virtual Machine, though.

I suggest creating an interface, which contains the "writeOut" method. The generated class needs to implement this interface. When you load the class, you don't need (much) reflection for calling the method. The only reflection you'll need is creating an instance of the generated class.

Pindatjuh
  • 10,550
  • 1
  • 41
  • 68
0

Continue with something like:

//define method here

Method writeout = null;
for( Method m : cls.getDeclaredMethods() ) {
   if( m.getName().startsWith("writeout")) {
       writeout = m;
       break;
   }
}
Object o = cls.newInstance();
writeout.invoke( o );

Basically, once you've found the class, you have to find the method you want to invoke and create an instance of that class to finally invoke that method on that new instance.

To understand more ( and better ) all this concepts you should read the java tutorial on the subject: The Reflection API

BTW you are not generating a class here, but rather instantiating it dynamically.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569