title: How to prevent public methods from being called from specific classes
body: <p>I have an existing class into which I want to add a method. But I want the method to be called only from a specific method from a specific class. Is there any way that I can prevent that call from other classes/methods?</p>
  
  <p>For example, I have an existing class A</p>
  
  <pre><code>public final class A
  {
      //other stuff available for all classes/methods
  
      //I want to add a method that does its job only if called from a specific method of a class, for example:
  
      public void method()
      {
          //proceed if called from Class B.anotherMethod() else throw Exception
      }
  }
  </code></pre>
  
  <p>One way of doing this is getting the <code>StackTrace</code> inside the <code>method()</code> and then confirming the parent method?</p>
  
  <p>What I am looking for is a solution that is more clean and advisable solution like a pattern or something.</p>