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?

4
  • I'm happy to realize that there are only 10 classes in his program. Commented Mar 24, 2010 at 21:35
  • Here's a Java Reflection tutorial: java.sun.com/docs/books/tutorial/reflect You may find it useful. Commented Mar 24, 2010 at 21:35
  • what do you mean by "generate that class" ? instantiate an object of that class ? Commented Mar 24, 2010 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 ) Commented Mar 24, 2010 at 21:58

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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
I agree it's open to interpretation
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.

Comments

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.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.