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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
How about something like this?
Art Metzer
Ranch Hand
Joined: Oct 31, 2000
Posts: 241
posted
0
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
posted
0
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
posted
0
Art, try this: javac *.java or this: javac Class*.java -- Susan
Robert Scheffer
Greenhorn
Joined: Jan 18, 2001
Posts: 2
posted
0
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
posted
0
The only problem is that this gives you 50 instances of 1 class. The request was to make 1 instance of 50 different classes.