Results 1 to 7 of 7
  1. #1
    TrolleY is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default How to disable using one method in another?

    Hello

    Like in subject - I want to disable using several methods in one (even if it is going to be overriden). Is it possible? How to do that? It will be the best if I will get compile error when I will try to call it. Maybe I should use annotations? Is it possible to do sth like this?

    Java Code:
    @LimitedMethod
    void someLimitedMethod()
    {
    }
    
    void anotherMethod()
    {
         someLimitedMethod(); // it's fine
    }
    
    @UsesLimitations
    void usingLimitationsMethod()
    {
         someLimitedMethod(); // it gives error
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: How to disable using one method in another?

    Why do you want to do this?

    What if you have something like this:

    Java Code:
    @LimitedMethod
    void someLimitedMethod()
    {
    }
     
    void anotherMethod()
    {
         maybeCallMethod(); // it's fine
    }
     
    @UsesLimitations
    void usingLimitationsMethod()
    {
         maybeCallMethod(); // it gives error
    }
    
    void maybeCallMethod(){
       if(someBoolean){
          someLimitedMethod();
       }
    }
    How would you verify that at compile time?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: How to disable using one method in another?

    Is this supposed to occur at run time or compile time? At compile time you can control which classes are used during the build. At runtime, if a method is allowed to be overridden then I don't believe you can keep it from being called. You can certainly use flags and such in the super class method to affect how it behaves. From a user interface (GUI) you can use a technique of disabling buttons to limit what the user may do in certain situations.

    And for the record, you can't get a compile-time error when you call the method because you need to compile the code before you call it.

    I suggest you provide more information on what your real intention/goal is so other alternatives, if they exist, could be provided.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: How to disable using one method in another?

    Well you could achieve something like that through subclassing and cleverly using modifiers (public, protected, private). But that will require learning the finer points about (abstract) classes, interfaces and inheritance
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    TrolleY is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to disable using one method in another?

    I was curious if some more abstract mechanizm to achieve my goal can be used. I have written some tool called MultiThreadWorker - kind of thread manager which uses specified in constructor amount of threads to execute tasks. It also provides workFinishedListener. I've added by mistake listener which tries to pause work. But method pause waits for all threads for join so there were deadlock. I wanted to prevent to execute public methods from my class by listeners. Or maybe I should just execute listeners in separate thread which will handle all "events"?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: How to disable using one method in another?

    There is already a thread specifically designed to handle events. It's called the EDT, or event dispatching thread. I don't really understand your problem, but it might be worth looking into.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    TrolleY is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to disable using one method in another?

    Quote Originally Posted by KevinWorkman View Post
    Why do you want to do this?

    What if you have something like this:

    Java Code:
    @LimitedMethod
    void someLimitedMethod()
    {
    }
     
    void anotherMethod()
    {
         maybeCallMethod(); // it's fine
    }
     
    @UsesLimitations
    void usingLimitationsMethod()
    {
         maybeCallMethod(); // it gives error
    }
    
    void maybeCallMethod(){
       if(someBoolean){
          someLimitedMethod();
       }
    }
    How would you verify that at compile time?
    It should also give compile error but as I told above - I was just curious if is it possible to write some abstract mechanism which will give errors at compile time.

    Quote Originally Posted by KevinWorkman
    There is already a thread specifically designed to handle events. It's called the EDT, or event dispatching thread. I don't really understand your problem, but it might be worth looking into.
    Ok, I will check EDT out.

Similar Threads

  1. Disable Attach API in Sun JVM
    By monthana in forum Advanced Java
    Replies: 7
    Last Post: 05-27-2012, 06:31 PM
  2. BackButton Disable
    By R S Reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-11-2011, 07:47 AM
  3. Disable xml tag on run time
    By venkatragh in forum XML
    Replies: 0
    Last Post: 04-30-2010, 07:56 AM
  4. How to disable the whole fieldset
    By amma2009 in forum Advanced Java
    Replies: 0
    Last Post: 01-29-2010, 03:47 PM
  5. How to disable the form
    By lukky in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-05-2008, 02:00 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
  •