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
Stefan Ram - 13 Dec 2007 07:20 GMT
>I need to programmatically create a class
You can use the compiler API to programmatically create a
class, if you are using Sun's Java implementation and the JDK
is available. For an example that creates the class
»ExpressionWrapper«, see
http://www.purl.org/stefan_ram/pub/evaluating-expressions-with-java
Stefan Ram - 13 Dec 2007 07:28 GMT
>>I need to programmatically create a class
>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.
Andrew Thompson - 13 Dec 2007 08:57 GMT
>Hi,
>
>It's hard to explain why I need this, ..
I don't care why *you* need this.
Try explaining it to me as if I were an end user. In terms of..
"This software offers YYY' where YYY is something that I,
as an end user might, GAF* about.
* 'Give A ..Flying care'

Signature
Andrew Thompson
http://www.physci.org/
rossum - 13 Dec 2007 10:20 GMT
>Hi,
>
[quoted text clipped - 16 lines]
>
>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
Mark Space - 13 Dec 2007 18:55 GMT
>> It's hard to explain why I need this, but I need to programmatically
>> create a class that has certain fields in it.
> 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.)
Lew - 14 Dec 2007 02:09 GMT
> (Software engineering hint: always think exit strategy.)
Business hint: same.
Conflict hint: same.
Relationship hint: same.
Life hint: same.

Signature
Lew
Arne Vajhøj - 13 Dec 2007 21:14 GMT
> It's hard to explain why I need this, but I need to programmatically
> create a class that has certain fields in it.
[quoted text clipped - 10 lines]
> 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