aspose file tools
The moose likes Java in General and the fly likes Easy way to generate fifty classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Easy way to generate fifty classes?" Watch "Easy way to generate fifty classes?" New topic
Author

Easy way to generate fifty classes?

Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hello, all.
As part of some testing I would like to do, I would like to generate, oh, say fifty classes. (That's _classes_, not objects.) How might I go about this? Would it require my using the java.lang.reflect package?
Any help would be greatly appreciated.
Thank you,
Art
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Do they have to be functional in any way? Do they need to be public or can they all sit in the same file?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Cindy.
No functionality required, they can all be of the form "class ClassName { }". And I have no problem with them all belonging to the default package: whether these classes are public or not doesn't matter.
Thanks again for your help,
Art
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
It does matter if you want to avoid compiling 50 java files. It would be a piece of cake to write one class MyClass01 in wordpad. Copy and paste it 49 times and increment the 01. Then with one compile you get 50 classes IF they are not declared public.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Cindy.
I'm sorry; when I said it doesn't matter if the classes are public or not, I meant it didn't matter to me and my little test whether the classes were public: I see by your reply that it indeed does matter to the solution.
Thank you very much for your feedback, Cindy. Before I give it a try just yet: Is there an easier way that uses introspection and/or java.lang.Class? The reason I ask is because, in case fifty classes passes my test, I'd like to (easily) be able to increase the number of classes created and test that quantity, and so forth.
Again, many thanks.
Art
Susan Hoover
Ranch Hand

Joined: Jan 04, 2001
Posts: 64
How about something like this?
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Thanks, Susan, works like a charm?
(P.S. After this, I swear I'll leave you alone...but is there now a shorthand way to compile all those source files?)
Merci beaucoup,
Art
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Susan,
Cute little program. Can you write an auto-generator for the things you're working on?
-Peter
Susan Hoover
Ranch Hand

Joined: Jan 04, 2001
Posts: 64
Art, try this:
javac *.java
or this:
javac Class*.java
--
Susan
Robert Scheffer
Greenhorn

Joined: Jan 18, 2001
Posts: 2
Another way to do this would be perhaps to make n deep copies of any class you have around. I'm not sure what you are trying to do but if the classes need to have some functionality to them you could use this Cloner class. I found it in the Java Developers Journal Volume 5 Issue 11 page 78
package clone;
import java.io.*;
public class Cloner {
/**
* Cloner constructor comment.
*/
public Cloner() {
}
/**
* Description: Returns a deep copy of Object o.
* @return java.lang.Object
* @param o java.lang.Object
* @exception java.lang.Exception The exception description.
*/
public static Object clone(Object o) throws java.lang.Exception {
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(b);
out.writeObject(o);
out.close();
ByteArrayInputStream bi = new ByteArrayInputStream(b.toByteArray());
ObjectInputStream in = new ObjectInputStream(bi);
Object no = in.readObject();
return no;
}
}
You use it like this:
Foo foo = new Foo();
Foo bar = (Foo)Cloner.cloner(foo);
The only draw back is that the class that you are making a deep copy of must implement Serializable to have this work.
You would probably want to do this:
Vector v = new Vector();
for( int i=0; i<=numberOfClasses; i++ )
v.insertElementAt( (Foo)Cloner.cloner(foo), v.size() );
Giving you a vector full of deep copied Foo classes.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The only problem is that this gives you 50 instances of 1 class. The request was to make 1 instance of 50 different classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Easy way to generate fifty classes?
 
Similar Threads
Using Eclipse 3.1.0 IDE, JBOSS 4.0.0 server & ANT as build tool
hibernate property
dynamic applet
oop-2 naturallanguagemultiply
Top down WS development in JDK1.4