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 a开发者_JAVA技巧s 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?
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.
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.
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.
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.
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);
精彩评论