Passing parameters with reflection

F

frank

I'm trying to create a class instance dynamically using reflection. I
can do it if I want to use the default constructor (i.e. no parameters),
and I also found an example how to have it call the constructor with
parameters if the parameters are a privative JAVA type

import java.lang.reflect.*;

public class constructor2 {
public constructor2()
{
}

public constructor2(int a, int b)
{
System.out.println(
"a = " + a + " b = " + b);
}

public static void main(String args[])
{
try {
Class cls = Class.forName("constructor2");
Class partypes[] = new Class[2];
partypes[0] = Integer.TYPE; //i.e. would be my own class
partypes[1] = Integer.TYPE;
Constructor ct
= cls.getConstructor(partypes);
Object arglist[] = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = ct.newInstance(arglist);
}
catch (Throwable e) {
System.err.println(e);
}
}
}


But how do I pass/call a construtor where the patameters is not Integer?

In fact it's one of my own classes?

Thanks,

Frank
 
T

Thomas Fritsch

frank said:
I'm trying to create a class instance dynamically using reflection. I can
do it if I want to use the default constructor (i.e. no parameters), and I
also found an example how to have it call the constructor with parameters
if the parameters are a privative JAVA type

import java.lang.reflect.*;

public class constructor2 {
public constructor2()
{
}

public constructor2(int a, int b)
{
System.out.println(
"a = " + a + " b = " + b);
}

public static void main(String args[])
{
try {
Class cls = Class.forName("constructor2");
Class partypes[] = new Class[2];
partypes[0] = Integer.TYPE; //i.e. would be my own class
partypes[1] = Integer.TYPE;
Constructor ct
= cls.getConstructor(partypes);
Object arglist[] = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = ct.newInstance(arglist);
}
catch (Throwable e) {
System.err.println(e);
}
}
}


But how do I pass/call a construtor where the patameters is not Integer?
For a constructor like
public constructor2(String s, Object o)
{
}
you would do the following:
Class partypes[] = new Class[2];
partypes[0] = String.class; // the so-called class-literal is the
trick
partypes[1] = Object.class;
Constructor ct
= cls.getConstructor(partypes);
Object arglist[] = new Object[2];
arglist[0] = "wrzlbrmpft";
arglist[1] = anyObject;
Object retobj = ct.newInstance(arglist);
 
F

frank

Thank you that worked perfectly!!!!

Thomas said:
frank said:
I'm trying to create a class instance dynamically using reflection. I can
do it if I want to use the default constructor (i.e. no parameters), and I
also found an example how to have it call the constructor with parameters
if the parameters are a privative JAVA type

import java.lang.reflect.*;

public class constructor2 {
public constructor2()
{
}

public constructor2(int a, int b)
{
System.out.println(
"a = " + a + " b = " + b);
}

public static void main(String args[])
{
try {
Class cls = Class.forName("constructor2");
Class partypes[] = new Class[2];
partypes[0] = Integer.TYPE; //i.e. would be my own class
partypes[1] = Integer.TYPE;
Constructor ct
= cls.getConstructor(partypes);
Object arglist[] = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = ct.newInstance(arglist);
}
catch (Throwable e) {
System.err.println(e);
}
}
}


But how do I pass/call a construtor where the patameters is not Integer?

For a constructor like
public constructor2(String s, Object o)
{
}
you would do the following:
Class partypes[] = new Class[2];
partypes[0] = String.class; // the so-called class-literal is the
trick
partypes[1] = Object.class;
Constructor ct
= cls.getConstructor(partypes);
Object arglist[] = new Object[2];
arglist[0] = "wrzlbrmpft";
arglist[1] = anyObject;
Object retobj = ct.newInstance(arglist);

In fact it's one of my own classes?

Thanks,

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top