Results 1 to 4 of 4
Thread: Custom Class, Wrapper/Subclass?
- 02-07-2009, 03:27 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Custom Class, Wrapper/Subclass?
Ok so I'm going to try to explain my problem again and see if I can get some more direction on what I should do.
I have a class that has multiple public functions that a user is allowed to use. That user makes his own java class file that manipulates these functions at his discretion.
My question is, I know my class name that holds all of those functions is: playerLink.java
The person who manipulates these functions out of playerLink.java calls their class maniuplate.java
In my Main I have to create an instance of this persons manipulate.java before I can use it.
How can I wrap their class file, no matter the name of it, into another class I've predefined and compile?
So if I knew the person was going to name their class Manipulate.java I could easily just change my code to be:
Manipulate player1 = new Manipulate();
and then compile, but I don't know what the name of their class/file will be.
Any suggestions?
- 02-07-2009, 07:28 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Well, it sounds like you need to use reflection. What you need to do is define an interface that all writers of a manipulate type class must follow. You will have no insight into their class other than through this interface. Then, at run time, you are somehow informed of their class name (maybe through a System.property or command line argument or whatever).
You then use Class.forName("{the name of their class here}) to get an instance of their class, and then the "newInstance" method will construct it. Then you cast it to the pre-agreed interface, and you're good to go.
I didn't show any details of their class, because that's irrelevant to your problem.Java Code:public interface TheyMustComply { public boolean didItWork(String whyNot); } ...in your code, let's say they pass in their class name on the command line... ...main(String[] args) throws {all the various exceptions} { Class theirClass = Class.forName(args[0]); TheyMustComply hopeTheyDidorElse = (TheyMustComply) theirClass.newInstance(); System.out.println(hopeTheyDidOrElse.didItWork("hope so")); }
- 02-09-2009, 04:29 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Do they have to pre-compile their class or is there a way I can compile their .java from within Main?
- 02-09-2009, 11:08 PM #4
First of all, why do you have to invoke their class, when their class is using yours? They could put the main() in their class, and the problem is solved.
Are you planning to allow them to upload their source and run their applications on a central server? If so, you are wide open to malicious code...
Assuming you really want to do this, Toadaly is steering you in the right direction with an interface called Manipulate. If you require the user to implement that interface in their class, then you can deal with their class as an instance of Manipulate, regardless of what they call their class.
Warning: You need to place some restrictions on their class name. The first thing I suggest is that they must use a package name you specify. That will help on several levels. Second, their class name must be unique from all the other classes placed in that package.
I would get the source and then shell out to javac to compile it. Then you can use Class.forName(), as Toadaly said, to get an instance.
As part of your interface, you will have to specify a method, like run(), that starts their logic running.
Again, this sounds very creative, but you are going to have to guard against bad code, even if the problems are inadvertent.
Similar Threads
-
Super class and Subclass in same source file
By makbar24 in forum New To JavaReplies: 17Last Post: 09-10-2008, 01:24 PM -
Yet another Wrapper Class confused guy.
By JAdeline in forum New To JavaReplies: 2Last Post: 08-15-2008, 04:04 AM -
Wrapper Class
By haiforhussain in forum New To JavaReplies: 6Last Post: 06-24-2008, 08:08 AM -
subclass vs inner class
By bugger in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:31 PM -
which class is superclass and subclass?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks