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 & HelpPrograms › ClassLoader
Page Index Toggle Pages: 1
ClassLoader (Read 1408 times)
ClassLoader
Oct 10th, 2005, 2:28pm
 
Hey board,

Since this is my first post, I have to say, that Processings seems to be a great tool for graphics and visualization...
But as always I have a little problem at the moment, and do not really know where I should post it, so I post it here:

I want to instantiate a class which's name is determined by

String s;

[...]

println(s);
--> "Grain"

Object o = new "Class of type s";

---------snap--------

I already know how to instantiate a ClassLoader (think, it should work with this tool), but I cannot do this in processing:

-----snip----
ClassLoader cLoader = new ClassLoader();
--> The constructor "ClassLoader();" in type "java.lang.ClassLoader" has protected access and is not accessible here.
-----snap----

So, how else should I manage this?

btw. I don't wan to write an applet...


thanx in advance
Till
Re: ClassLoader
Reply #1 - Oct 10th, 2005, 4:53pm
 
you need to read more about how ClassLoader works. you can't create one like that because the constructor has "protected" access. generally you probably need to make your own subclass, or use:
ClassLoader sys = ClassLoader.getSystemClassLoader();
Re: ClassLoader
Reply #2 - Oct 11th, 2005, 3:44pm
 
Hi fry, all,

thanx for moving my post to the right thread, and answering that fast.
After using yout hint, I still  get a new (for me very strange) problem.

-- snip --
class Test {
  Test() {
   }
}
void setup(){
 [...]
 Test t = new Test();
 // print the Class name:
 println(t.getClass().toString());

 ClassLoader sys = ClassLoader.getSystemClassLoader();  
  try {    
     sys.loadClass("Test").newInstance();
   } catch (Exception e) {
     println("Problem...: "+e);
   }
}
-------snap--------
When I do this in Processing, I get a strange classname (something like
 "Temporary_2127_8958$Grain"
) which is not the classname I was suspecting...
And of course it does not work properly...

I think, it is something which relates to the processing environment, right?
How to prevent this?
I definitely need to instantiate a class setermined by its used classname...
("Test" in this case)

Any hints anyone?

Thanks in advance
Till
Re: ClassLoader
Reply #3 - Oct 14th, 2005, 10:37am
 
no one knows about this? thats a pitty, really...
i think it is because of the use of jikes here... is there a jikes-specific ClassLoader anywhere accessible?

__
till
Re: ClassLoader
Reply #4 - Oct 14th, 2005, 3:38pm
 
the temporary name comes from how things are compiled inside processing, because you're not specifying a class name, it needs to give you a temporary one. and in fact, behind the scenes it's using its own classloader to handle this, so if you want to see how that works, you can dig in the processing source (dev.processing.org/source).

what is it you're trying to do with the class loader? is it not something that could be handled for Class.forName() or something like that? using a ClassLoader is very low level and almost always unnecessary.
Re: ClassLoader
Reply #5 - Oct 21st, 2005, 1:56pm
 
I want to dynamically instatiate classes from out of a String containing the class name... (send via OSC and received with oscP5)...

Supposed I have the following classes defined in Processing's edit window:

class Grain {
 Grain () {
 }
}

class GBox extends Grain {
 GBox () {
 }
}

----snap----

I want to do something like

String classname = "GBox"
Grain act = null;

// this works (as expected)

if (classname.equals("GBox")) {
     act = new GBox();
}

// this not

try {
    Class grainClass = Class.forName(classname);
     act = (Grain) grainClass.newInstance();
   } catch (Exception e) {
     println(e);
     return;
   }

// prints "java.lang.ClassNotfoundException: GBox"

So: how to find the correct name?

Thanx for any help
Till
Re: ClassLoader
Reply #6 - Oct 21st, 2005, 4:09pm
 
I've no idea if this'll work, but you could try
Code:

String theClass=this.getClass().getName() + NameOfClassYouWant

to get the correct class.

Edit: Actually, you may need to add a "$" between the two.

Here's some code that prints the "real" classnames:

Code:
class foo
{
foo()
{
println(this.getClass().getName());
}
}

foo f;

void setup()
{
size(200,200,P3D);
println(this.getClass().getName());
f=new foo();
}


Prints:
Code:
Temporary_4895_9691
Temporary_4895_9691$foo
Re: ClassLoader
Reply #7 - Oct 24th, 2005, 5:47pm
 
thanx john, fry for your help...

--------snip------------
class foo {
 foo() {
 }

}

void setup() {

 size(200,200);


 String fooName = "foo";

 try {
   foo theFoo = (foo) Class.forName(this.getClass().getName() + "$" + fooName).newInstance();
 } catch (Exception e) {
   println(e);
 }
}
---------snap------------

unfortunaltely this does not create the desired Objects, but prints a
--- snip ---
java.lang.InstantiationException: Temporary_9757_7229$foo
--- snap ---

(btw: how to insert code properly into this forum?)

regards
Till
Re: ClassLoader
Reply #8 - Nov 4th, 2005, 2:57pm
 
Hey all,

I'm still interested in a solution of the above-described problem...
So if someone could point me into the right direction, or just tells me, how to manage this, give me a hint, please...


have a nice day

Till
Re: ClassLoader
Reply #9 - Nov 4th, 2005, 10:14pm
 
my guess is that the InstantiationException comes from trying to create an inner class, which probably has weird dynamics to it. especially because you haven't set the constructor or the class itself to be "public".

to get around the "temporary" thing, explicitly name the sketch. ie. if your sketch is named "poo" do this:

public class poo extends PApplet {

however, once you've done that, you're on your own as far as making sure that the code is proper java, and that the name is in sync with the sketch name.

for the other classes, you're best off creating them as .java files. so create a new tab, name it "something.java" and it'll be just straight java code (no preprocessor anymore), and not an inner class. in doing so, you'll need to pass the PApplet object to the other class (in its constructor, for instance), in order to make things like drawing work properly. keep in mind this is all fairly messy/advanced stuff...
Re: ClassLoader
Reply #10 - Nov 11th, 2005, 5:13pm
 
Hey fry, all,

thanx, I'll look at it, soon Wink

have a nice day
Till
Page Index Toggle Pages: 1