Results 1 to 4 of 4
  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Rep Power

    Exclamation 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:

    The person who manipulates these functions out of calls their class

    In my Main I have to create an instance of this persons 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 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?

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    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 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.

    Java Code:
    public interface TheyMustComply {
      public boolean didItWork(String whyNot);
    }  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"));
    I didn't show any details of their class, because that's irrelevant to your problem.

  3. #3
    Moncleared is offline Member
    Join Date
    Jan 2009
    Rep Power


    Do they have to pre-compile their class or is there a way I can compile their .java from within Main?

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    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

  1. Super class and Subclass in same source file
    By makbar24 in forum New To Java
    Replies: 17
    Last Post: 09-10-2008, 02:24 PM
  2. Yet another Wrapper Class confused guy.
    By JAdeline in forum New To Java
    Replies: 2
    Last Post: 08-15-2008, 05:04 AM
  3. Wrapper Class
    By haiforhussain in forum New To Java
    Replies: 6
    Last Post: 06-24-2008, 09:08 AM
  4. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:31 PM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 09:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts