Here is a custom executor that executes an application by searching for its main in the loaded classes loaded by a custom loader. There is a problem when I try to execute a program using my executor.
The source code of the program being executed can be located on the following link
http://agile.csc.ncsu.edu/SEMaterials/realestate/code/RealEstate.zip
package executorOfLoaderClasses;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import customClassLoader.ClassLoaderOfExtClass;
/**
* #author Sanyam
*
* sanyamgoyal007#gmail.com
*/
public class ClassExecutor{
private ClassLoaderOfExtClass classLoader;
private byte[][] ArrayOfClasses;
private String[] ArrayOfBinaryNames;
#SuppressWarnings("rawtypes")
private ArrayList<Class> loadedClasses;
private ArrayList<String> loadedClasesNames;
private Object[] parameters;
#SuppressWarnings("rawtypes")
public ClassExecutor() {
classLoader = new ClassLoaderOfExtClass();
new ArrayList<Class>();
loadedClasses = new ArrayList<Class>();
loadedClasesNames = new ArrayList<String>();
}
#SuppressWarnings("unchecked")
public void execute(File[] file, String[] binaryPaths) {
Object[] actuals = { new String[] { "" } };
Method m = null;
try {
Field classesx=ClassLoaderOfExtClass.class.getDeclaredField("classes");
classesx.setAccessible(true);
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
}
/*for (int i = 0; i < file.length; i++) {
for (int j = 0; j < file.length; j++) {
try {
#SuppressWarnings("rawtypes")
Class c = classLoader.loadClassCustom(file[i], binaryPaths[i]);
//Fied classex=classLoader.getResource("classes");
}catch(Exception e){
}
}
}
Class<?>[]classesxx= getLoadedClasses(classLoader);
System.out.println("Loaded classes have size "+ classesxx.length);*/
for (int i = 0; i < file.length; i++) {
try {
#SuppressWarnings("rawtypes")
Class c = classLoader.loadClassCustom(file[i], binaryPaths[i]);
try {
if (c.getMethod("main", new Class[] { String[].class }) != null) {
m = c.getMethod("main", new Class[] { String[].class });
} else {
System.out.println("This class does not contain main");
continue;
}
} catch (NoSuchMethodException e) {
// System.out.println("Main not found!!!");
// System.out.println("M here");
// e.printStackTrace(); // not printing stack trace
} catch (SecurityException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
System.out.println("No such class definition exist!!");
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
try {
m.invoke(null, actuals);
// CallStack.print();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#SuppressWarnings({ })
public void execute(ArrayList<byte[]> stuffedFiles,
ArrayList<String> binaryPaths) {
convertToArray(stuffedFiles, binaryPaths);
loadAllClasses(ArrayOfClasses, ArrayOfBinaryNames);
Thread myThread = new MyThread();
myThread.start();
/*Object[] actuals = { new String[] { "" } };
Method m = null;
* Method[] m1= new Method[10]; for (Class c : loadedClasses) {
* m1=c.getMethods(); } for(Method m2: m1){
* System.out.println(m2.getName()); }
System.out.println(loadedClasses.size());
for (Class c : loadedClasses) {
* System.out.println(c.toString());
* System.out.println(c.getConstructors());
// for (int i = 1; i < file.size(); i++) {
* for(Method meth : c.getMethods()){ meth.setAccessible(true);
*
* }
try {
if (c.getMethod("main", new Class[] { String[].class }) != null) {
m = c.getMethod("main", new Class[] { String[].class });
break;
} else {
// System.out.println("This class does not contain main");
continue;
}
} catch (NoSuchMethodException e) {
System.out.println("Program does not contain main");
} catch (SecurityException e) {
e.printStackTrace();
}
}
try {
if(parameters==null){
m.invoke(null, actuals);
}
else{
try {
System.out.println("It Fails Here");
m.invoke(null, parameters);
} catch (Exception e) {
System.out.println("Illegal arguments");
}
}
// CallStack.print();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
// remove till here
/*TraceParser pr = new TraceParser();
pr.traceCollector();
pr.traceStruct();
ArrayList<SingleTraceStructure> parsedExpressions = pr
.getTracedObjects();
AllStackTraceValidator validator = new AllStackTraceValidator(
parsedExpressions);
finalObjects = validator.getTraceObjects();
for(SingleTraceStructure ob : finalObjects){
validatedTraceObjects.add(ob);
}
TraceObjectsMinimizer tracerObj = new TraceObjectsMinimizer();
tracerObj.sortObjects(finalObjects);*/
/*
* for(SingleTraceStructure obj : finalObjects){
* System.out.println(obj.getCalledBy());
* System.out.println(obj.getClassName()+":"+obj.getMethodName()+":"+
* obj.getCallSequenceNumbr()+obj.getCalledBy()); }
*/
}
private void convertToArray(ArrayList<byte[]> classes,
ArrayList<String> binaryPaths) {
/* = new byte[classes.size()][]; */
ArrayOfClasses = new byte[classes.size()][];
ArrayOfBinaryNames = new String[binaryPaths.size()];
int i = 0;
for (byte[] tempClass : classes) {
ArrayOfClasses[i] = tempClass;
i++;
}
int j = 0;
for (String name : binaryPaths) {
ArrayOfBinaryNames[j] = name;
j++;
}
}
#SuppressWarnings("rawtypes")
public void loadAllClasses(byte[][] classes, String[] names) {
for (int i = 0; i < classes.length; i++) {
System.out.println("Round ----->" + i);
Class c = null;
for (int j = 0; j < classes.length; j++) {
if (classes[j] != null) {
try {
c = classLoader.loadClassCustom(classes[j], names[j]);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("unsucessful");
e.printStackTrace();
}
if (c != null) {
System.out.println("loading successfull");
loadedClasses.add(c);
loadedClasesNames.add(names[j]);
classes[j] = null;
names[j] = null;
} else {
// move on
}
} else {
// do nothing
}
}
}
}
#SuppressWarnings("rawtypes")
public ArrayList<Class> getLoadedClasses() {
return loadedClasses;
}
public void parametersToMain(ArrayList<String> strs){
if(strs!=null){
String[] obj = new String[strs.size()];
int i=0;
for(String str : strs){
obj[i]= (String)str;
i++;
}
parameters=obj;
}
else{
parameters=null;
}
}
// return loaded classes of a loader
public static Class<?>[] getLoadedClasses(final ClassLoader loader){
final Class<?>[] classes = getLoadedClasses(loader);
return classes;
}
public class MyThread extends Thread{
Object[] actuals = { new String[] { "" }};
public void run(){
Method m = null;
/*
* Method[] m1= new Method[10]; for (Class c : loadedClasses) {
* m1=c.getMethods(); } for(Method m2: m1){
* System.out.println(m2.getName()); }
*/
/* System.out.println(loadedClasses.size()); */
for (Class<?> c : loadedClasses) {
/*
* System.out.println(c.toString());
* System.out.println(c.getConstructors());
*/
// for (int i = 1; i < file.size(); i++) {
/*
* for(Method meth : c.getMethods()){ meth.setAccessible(true);
*
* }
*/
try {
if (c.getMethod("main", new Class[] { String[].class }) != null) {
m = c.getMethod("main", new Class[] { String[].class });
break;
} else {
// System.out.println("This class does not contain main");
continue;
}
} catch (NoSuchMethodException e) {
System.out.println("Program does not contain main");
} catch (SecurityException e) {
e.printStackTrace();
}
}
try {
if(parameters==null){
//System.out.println("Invoker of" + actuals[1]);
m.invoke(null, actuals);
}
else{
try {
System.out.println("It Fails Here");
m.invoke(null, parameters);
} catch (Exception e) {
System.out.println("Illegal arguments");
}
}
// CallStack.print();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
The problem with my executor is while executing the realestate program genetraes the following error
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at executorOfLoaderClasses.ClassExecutor$MyThread.run(ClassExecutor.java:365)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at edu.ncsu.realestate.gui.Main.main(Main.java:39)
... 5 more
I realized that while invoking the main I am passing a parameter to the main which is an object of string "" however realestate program does not take any arguments by default ,so for the time being I changed the argument passed to the method to null just to check whether it works fine now.
and again an error was generated as shown below
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at executorOfLoaderClasses.ClassExecutor$MyThread.run(ClassExecutor.java:366)
Why is this happening the realestate program as mentioned in the http link does not take any arguments by default.
Is this way of execution buggy to other possible input programs ??
The realestate program I mentioned is working absolutely fine when run in eclipse as a Java application
The actual bug in the code from the question
The Main method you invoke expects either zero or two arguments, but you pass one.
public static void main(String[] args) {
GameMaster master = GameMaster.instance();
MainWindow window = new MainWindow();
GameBoard gameBoard = null;
if(args.length > 0) {
if(args[0].equals("test")) {
master.setTestMode(true);
}
try {
Class c = Class.forName(args[1]); // <-- this is l. 39
gameBoard = (GameBoard)c.newInstance();
}
You should write
Object[] actuals = { new String[] { }};
or
Object[] actuals = { new String[0] };
in order to pass not a single empty argument, but no argument at all.
General information about argument types
If main is the startup method of a Java application, it always has signature
public static void main(String[] args)
For this reason, from the java perspective it will always be a method expecting a single argument, which is an array of strings. So on the command line, you use a variable number of arguments, but within the Java application, it is always a single array. Some developers might declare the arguments as String... args, which means that you might pass a variable number of strings in the Java application, but that is only syntactic sugar which the compiler will translate into an array creation. For reflection, you will always have to pass the array yourself, as there is no automatic array creation for variadic methods there. So always pass a single argument of type String[] via reflection.
Method.invoke takes an Object... for the arguments, so you can either pass an Object[] containing all the Java arguments, or you can pass each argument separately and have the compiler construct an Object[] from those. So it is up to you whether you pass the String[] array directly to invoke or wrap it in an Object[] as you did with your actuals.
Related
Is there a way to do the following? Check if a class exists (in the same package) and if it does exist, check if a particular method exists, and if so, calling it?
Say that I have class X. In some method of class X, I want to do the following:
if (class Y exists) { //Maybe use Class.forName("Y")?
if ( Y has method a(String, String) ) {
call Y.a("hello", "world");
}
}
Is such a thing possible? And is doing such a thing reasonable? Thanks.
Is such a thing possible? And is doing such a thing reasonable?
Thanks.
Of course it is possible.
If you develop a program or a library that has to discover dynamically some classes, it is a very reasonable thing.
If it is not the case, it could not be.
If your need makes sense, you should ask you an additional question : should you invoke a static or instance method ?
Here is a sample example with both solutions :
ReflectionClass that contains the logic using reflection :
import java.lang.reflect.Method;
public class ReflectionCalls {
public static void main(String[] args) {
new ReflectionCalls();
}
public ReflectionCalls() {
callMethod(true);
callMethod(false);
}
private void callMethod(boolean isInstanceMethod) {
String className = "DiscoveredClass";
String staticMethodName = "methodStatic";
String instanceMethodName = "methodInstance";
Class<?>[] formalParameters = { int.class, String.class };
Object[] effectiveParameters = new Object[] { 5, "hello" };
String packageName = getClass().getPackage().getName();
try {
Class<?> clazz = Class.forName(packageName + "." + className);
if (!isInstanceMethod) {
Method method = clazz.getMethod(staticMethodName, formalParameters);
method.invoke(null, effectiveParameters);
}
else {
Method method = clazz.getMethod(instanceMethodName, formalParameters);
Object newInstance = clazz.newInstance();
method.invoke(newInstance, effectiveParameters);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DiscoveredClass (the class we manipulate in the example)
package reflectionexp;
public class DiscoveredClass {
public static void methodStatic(int x, String string) {
System.out.println("static method with " + x + " and " + string);
}
public void methodInstance(int x, String string) {
System.out.println("instance method with " + x + " and " + string);
}
}
Output :
instance method with 5 and hello
static method with 5 and hello
Yes, this can be done. I've created a Test class in the same Package as the current class.
import java.lang.reflect.Method;
public class Sample {
public static void main(String[] args) {
Class<?> clazz = null;
try {
clazz = Class.forName("Test");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (clazz == null) {
System.out.println("class not found. Go eat some waffles and correct the name");
return;
}
Method m = null;
try {
m = clazz.getMethod("foo", null);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (m == null) {
System.out.println("method not found. Go eat some waffles and correct the name");
return;
}
Test t;
try {
t = (Test) clazz.newInstance();
m.invoke(t, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Test {
static {
System.out.println("test...");
}
public void foo() {
System.out.println("foo");
}
}
O/P :
test...
foo
You can use Class.forName:
try {
Class yourClass = Class.forName( "classname" );
Object o = yourClass.newInstance();
} catch( ClassNotFoundException e ) {
//Throw error or whatever
}
To check if a method exists you could use the NoSuchMethodError e in a try/catch
You can do this using reflection, however it isnt really practical unless you are trying to access classes that potentially will not be present at runtime or if you are trying to access private or hidden fields. Example below.
public static void reflectionDemo(){
//Here we attempt to get the common URI class
//If it is found, we attempt to get the create method
//We then invoke the create method and print the class name of the result.
try {
Class<?> uriClass = Class.forName("java.net.URI");
//getMethod(String name, Class<?>... args);
java.lang.reflect.Method create = uriClass.getMethod("create", String.class);
//The first parameter is null because this is a static method.
//invoke(Object target, Object... args);
System.out.println(create.invoke(null, "some/uri").getClass());
//Will print class java.net.URI
} catch (ClassNotFoundException e) {
// If class doesnt exist
e.printStackTrace();
} catch (NoSuchMethodException e) {
// If method doesnt exist
e.printStackTrace();
} catch (SecurityException e) {
// See Javadoc
e.printStackTrace();
} catch (IllegalAccessException e) {
// From invoke
e.printStackTrace();
} catch (IllegalArgumentException e) {
// From invoke
e.printStackTrace();
} catch (java.lang.reflect.InvocationTargetException e) {
// From invoke
e.printStackTrace();
}
}
To find whether a class exists, you can use the forName() method on Class.
To find whether a method exists, you can use the getMethod() method on Class.
Documentation here:
https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName(java.lang.String)
https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethod(java.lang.String,%20java.lang.Class...)
For your class problem, you'd want to use code like:
try {
Class.forName("Y");
}
catch (ClassNotFoundException e) {
}
For your method problem, you'd want to use code like:
try {
Class.getMethod(a);
}
catch (NoSuchMethodException e) {
}
You can check if the Class exists with Class.forName("classname");
See this SO question: Check if class exists somewhere in package
If a method exists can be catched with NoSuchMethodError in your try/catch.
See this SO question: Check if method exists at Runtime in Java
try {
Object object = Class.forName("Y").newInstance();
object.a(String, String);
} catch( ClassNotFoundException | NoSuchMethodError ex) {
//do Something else
}
I have been programming java a long time and for some reason I am unable to figure out why this gives me an argument type mismatch. I created a very simple JUnit test anyone can run. Just copy and paste the code below. Any help would be much appreciated. Thanks!
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
public class TestObjectArrayConstructor {
#Test
public void testLabelValueObjectArrayConstructor(){
Constructor constructor = null;
try {
constructor = LabelValue.class.getConstructor(
new Class[]{Object[].class});
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object[] array = new Object[]{"Doll"};
Object labelValue = null;
try {
labelValue = constructor.newInstance(array);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("don");
}
}
public class LabelValue {
private String label;
private String value;
public LabelValue(){
}
public LabelValue(Object[] array)
{
if(array != null && array.length > 0)
{
this.label = (String)array[0];
this.value = (String)array[0];
}
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
try this (not tested) :
constructor.newInstance(new Object[]{array});
because newInstance expects an array of Object (the different args), and your case is ambiguous, as your param is an array of Object.
When you want to create a newInstance from a constructorthen you need to pass your actual parameters, in your case a single argument array as new Object[]:
replace
labelValue = constructor.newInstance(array);
with
labelValue = constructor.newInstance(new Object[]{array});
Hi I am trying to implement my own classifier in a java .Here is what I have gotten so far:
import weka.core.*;
public class RandomProbability extends Classifier {
Instances data;
public RandomProbability ()
{
/*DataSource d = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
data=((Object) d).getSourceData();*/
DataSource source = null;
try {
source = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Instances instances = source.getDataSet();
//instances.setClassIndex(instances.numAttributes() - 1);
// Print header and instances.
System.out.println("\nDataset:\n");
System.out.println(instances);
Now the problem is I cant get it to classify the data in the dataset (as good or bad).
I need help in trying to access a single instance in this code.
You could use instanceOf operator as below:
int countA = 0, countB=0;
double pred;
for (int i=0;i<57;i++) {
pred = classifyInstance(instances.instance(i));
System.out.println("===== Classified instance =====");
System.out.println("Class predicted:" + instances.classAttribute().value((int) pred));
if (instances.classAttribute().value((int) pred).toString().equals("bad")) {
countB++;
} else {
countA++;
}
}
package dm;
//import javax.activation.DataSource;
import weka.core.converters.ConverterUtils.DataSource;
import weka.classifiers.*;
import weka.core.*;
import org.jfree.data.*;
import weka.core.*;
public class RandomProbability extends Classifier {
Instances data;
public RandomProbability ()
{
/*DataSource d = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
data=((Object) d).getSourceData();*/
DataSource source = null;
try {
source = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Instances instances = source.getDataSet();
instances.setClassIndex(instances.numAttributes() - 1);
// Print header and instances.
System.out.println("\nDataset:\n");
System.out.println(instances);
int total=instances.numInstances();
System.out.println("total"+total);
Attribute attr= instances.attribute(16);
System.out.println("attr"+attr);
// checking class
int countA = 0, countB=0;
double pred;
for (int i=0;i<57;i++)
{
pred=0;
pred = classifyInstance(instances.instance(i));
System.out.println("===== Classified instance =====");
System.out.println("Class predicted:" + instances.classAttribute().value((int) pred));
if (instances.classAttribute().value((int) pred).toString().equals("bad"))
{
countB++;
}
else {
countA++;
}
}
System.out.println("good instances"+countA);
System.out.println("bad instances"+ countB);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void buildClassifier (Instances data)
{
}
public double classifyInstance (Instance inst)
{
return 0;
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I invoke a java method when given the method name as a string?
**I have looked at this : Call a function from a string array (Java or Groovy) and it didn't work for me, perhaps it is my scenario*
I have a String array with some values, and I want to be able to call a method which is one of those values, of course this can change, and I could potentially have 100's of values, so it's not feasible to use a if/else if construct, or a switch statement.
Is there any way I can call the method, as I would like, as displayed in the code below?
private String[] = {"Hit","Slap","Blop"};
private String et = "Slap";
public void Action(){
for(int i = 0; i < arr.length;i++){
if(et.equals(arr[i])){
//Call method of that name ( Slap(); )
}
}
}
public void Run(){
///
}
public void Slap(){
///
}
public void Blop(){
///
}
EDIT: My attempt to integrate reflection:
for(int i = 0; i < arr.length;i++){
if(et.equals(arr[i])){
//Call method of that name
try {
method = this.getClass().getMethod(arr[i]);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
return (String) method.invoke(this);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "FAIL";
You will have to use reflection. Something like this:
getClass().getMethod(arr[i]).invoke(this);
You can use the ReflectionAPI to achieve this. Just get the method with this name from your desired class (if available ) and invoke it with your args, in this case null.
BUT it's a bad design and you should rethink your application flow!
here is an example:
public class HelloWorld {
public static void main(String[] args) {
HelloWorld hello = new HelloWorld();
String[] calls = { "def", "abc", "ghi" };
try {
for (String call : calls) {
Method method = HelloWorld.class.getMethod(call, null);
method.invoke(hello, null);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public void abc() {
System.out.println("abc");
}
public void def() {
System.out.println("def");
}
public void ghi() {
System.out.println("ghi");
}
}
If I have 2 classes, "A" and "B", how can I create a generic factory so I will only need to pass the class name as a string to receive an instance?
Example:
public static void factory(String name) {
// An example of an implmentation I would need, this obviously doesn't work
return new name.CreateClass();
}
Thanks!
Joel
Class c= Class.forName(className);
return c.getDeclaredConstructor().newInstance();//assuming you aren't worried about constructor .
javadoc
For invoking constructor with argument
public static Object createObject(Constructor constructor,
Object[] arguments) {
System.out.println("Constructor: " + constructor.toString());
Object object = null;
try {
object = constructor.newInstance(arguments);
System.out.println("Object: " + object.toString());
return object;
} catch (InstantiationException e) {
//handle it
} catch (IllegalAccessException e) {
//handle it
} catch (IllegalArgumentException e) {
//handle it
} catch (InvocationTargetException e) {
//handle it
}
return object;
}
}
have a look
You may take a look at Reflection:
import java.awt.Rectangle;
public class SampleNoArg {
public static void main(String[] args) {
Rectangle r = (Rectangle) createObject("java.awt.Rectangle");
System.out.println(r.toString());
}
static Object createObject(String className) {
Object object = null;
try {
Class classDefinition = Class.forName(className);
object = classDefinition.newInstance();
} catch (InstantiationException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
System.out.println(e);
}
return object;
}
}