How to create a class with given fields?

A

Aaron Fude

Hi,

It's hard to explain why I need this, but I need to programmatically
create a class that has certain fields in it.

For examle, is it possible to write a function

class MyClass {

public static Class createClass(String field1, String field2) {

}
}

that creates a class with two int fields whose names are contained in
variables field1 and field2.

Many thanks in advance!!!

Aaron
 
S

Stefan Ram

You can use the compiler API

Without it, you can still write a class file, even with only
the JRE.

To do this, you need to learn the class file format. Then you
can write them using java.io and load them with a class loader.

If all classes only need to consist of some String fields, you
can also create some such class files with the compiler and
then inspect their format to learn the format for this
special simple case.
 
R

rossum

Hi,

It's hard to explain why I need this, but I need to programmatically
create a class that has certain fields in it.

For examle, is it possible to write a function

class MyClass {

public static Class createClass(String field1, String field2) {

}
}

that creates a class with two int fields whose names are contained in
variables field1 and field2.

Many thanks in advance!!!

Aaron
If all your fields are to be Integers than you could build the class
as a wrapper round a HashMap<String, Integer>. The constructor could
take an array (or List) of Strings as a parameter so you could put as
many entries in the HashMap as you needed. The getter and setter
would each take a String parameter to indicate which entry in the
HashMap (i.e. which field) to get or set.

rossum
 
M

Mark Space

rossum said:
On Wed, 12 Dec 2007 21:49:24 -0800 (PST), Aaron Fude
If all your fields are to be Integers than you could build the class
as a wrapper round a HashMap<String, Integer>. The constructor could
take an array (or List) of Strings as a parameter so you could put as
many entries in the HashMap as you needed. The getter and setter
would each take a String parameter to indicate which entry in the
HashMap (i.e. which field) to get or set.


Right. And you could put anything in there, not just Integers, if you
use Objects instead of Integers. This I think is similar to what the
ResultSet object does when it returns rows from a database. Largely,
you are responsible for sorting out the type of the objects.

If you really need to build a class like the compiler does, you are in
for a lot of work, and the result could be hard to maintain and modify.
If you can get by with a list of Objects in an array or a Collection,
you'll be much happier. And it'll be much easier to fob the result off
on some new guy. (Software engineering hint: always think exit strategy.)
 
A

Arne Vajhøj

Aaron said:
It's hard to explain why I need this, but I need to programmatically
create a class that has certain fields in it.

For examle, is it possible to write a function

class MyClass {

public static Class createClass(String field1, String field2) {

}
}

that creates a class with two int fields whose names are contained in
variables field1 and field2.

Several tools to do so exist:
BCEL
javassist
ASM

Try and see which one suits you.

Arne
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top