We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Dynamic class naming
Pages: 1 2 
Dynamic class naming (Read 6146 times)
Re: Dynamic class naming
Reply #15 - Jan 10th, 2010, 12:09am
 
yes tx. just threw all that in because was confusing Smiley

how would i point to a directory:

String path = sketchPath;
className = sketchPath+"/some_directory/" + _p;

???
Re: Dynamic class naming
Reply #16 - Jan 10th, 2010, 2:01am
 
See savePath and friends.
Re: Dynamic class naming
Reply #17 - Jan 10th, 2010, 6:40am
 
sorry, maybe i'm being dumb. what does the "$" mean in:
className = this.getClass().getName() + "$c" + _p;

when i try:
String spath = savePath;

i get error: "cannot find anything named savePath";

when i try className = sketchPath+"/c" + _p;
or className = sketchPath+"$c" + _p;
doesn't work.

basically i want to read a folder or subfolders.
so can get the list of directories, but not point to the actual class file:

File[] files = listFiles(path);
 for (int i = 0; i < files.length; i++) {
     f = files[i];  
   if ( f.isDirectory()) {  
   println("Name: " + f.getName());
   println("-----------------------");
  }
 }


but at the moment can;t work out how to even point there manually.

Re: Dynamic class naming
Reply #18 - Jan 10th, 2010, 7:30am
 
I suggest to follow my link and read the description...

The $ here is a separator between a main class and the name of its inner classes.
Re: Dynamic class naming
Reply #19 - Jan 12th, 2010, 5:23am
 
for the life of me i can't figure this out.
works fine for images, etc. but can't figure out loading the class

any other guiders appreciated.

regards
Re: Dynamic class naming
Reply #20 - Jan 12th, 2010, 6:10am
 
Can you provide a simple sample code illustrating your use case, along with the file organization, so I can figure out what goes wrong?
Preferably close of your final goal, so we don't try stuff to be thrown away later.
Re: Dynamic class naming
Reply #21 - Feb 11th, 2010, 12:05pm
 
sorry for the very late reply - been sucked into "real work"  :-/.

this is a bit beyond my programming abilities. i've left the code messy, because i don't really understand what's happening in the code.

here is the basic class.
it works fine if the external classes are all in the same directory as the main .pde holder.

but as soon as i move them into their own directories - i can't seem to target them. i've tried all the combinations at the top of the class, classname = ....


Code:

public void loadInClass(String _p){
println("class: " + _p);
className = this.getClass().getName() + "$" + _p;
className = this.getClass().getName() +path+ "/$" + _p;
className = path + "/" + _p + "/$" + _p;
className = path + "/" + _p + "/$" + _p;
println("class: " + className);

try
{ cl = Class.forName(className); }
catch (ClassNotFoundException e) {
// println("Unknown class: " + className);
}
currentClass = null;
if (cl != null) {
try {
// Alas, this doesn't work for inner classes!
// Will work if you put classes in separate .java files
currentClass = (gCore) cl.newInstance();
}
catch (InstantiationException e){
//println("Cannot create an instance of " + className);
}
catch (IllegalAccessException e) {
println("Cannot access " + className + ": " + e.getMessage());
}
// Internet to the rescue!
try
{
// Get the constructor(s)
java.lang.reflect.Constructor[] ctors = cl.getDeclaredConstructors();
// Create an instance with the parent object as parameter (needed for inner classes)
currentClass = (gCore) ctors[0].newInstance(new Object[] { this });

}
catch (InstantiationException e) {
//println("Cannot create an instance of " + className);
}
catch (IllegalAccessException e) {
//println("Cannot access " + className + ": " + e.getMessage());
}
catch (Exception e) // Lot of stuff can go wrong...
{
e.printStackTrace();
}
}
}




here the entire project:

www.radarboy.com/george/dynamic_class_loading.zip


any pointers much appreciated.
Re: Dynamic class naming
Reply #22 - Feb 13th, 2010, 1:24am
 
cl appears to be null in all three tries..What happend...It's an interesting topic,so I spent more than one hour debugging the project with my limited knowledge in Java. CryCould you bail me out?Thank you.
Re: Dynamic class naming
Reply #23 - Feb 13th, 2010, 2:51am
 
When the PDE loads your sketch, the problem is obvious: you see none of c1, c2, c3 in the tabs. Processing just ignore there are classes in sub-folders, so it can't compile them. Since these are not compiled, you cannot load them.

Not sure how to solve that. Either you bring the classes back to top level, or you make these as .java classes and try to compile them separately with javac...
Re: Dynamic class naming
Reply #24 - Feb 13th, 2010, 3:34am
 
Thank you very much,Philo!I'll try again:)
Re: Dynamic class naming
Reply #25 - Feb 13th, 2010, 4:42am
 
yea i noticed that. even if you move the classes back into the top level directory you need to close the sketch/processing and restart for them to be seen.

so you saying there is no way to leave them as .pde files and put them in subdirectories without making them .java files? do you know what the IDE does on sketch startup? if the IDE is written in java then surely there a way to refresh this?

cheers
Pages: 1 2