text: Q:
  
  Java prevent calling private or protected methods outside of class
  
  Let's supppose that I have created a Java library, called Foo and I have a class inside that library called Bar. Let's suppose further that in the Bar class I have a private method, called fooBar.
  public class Bar {
      //...
      private Object fooBar() {
          //Do something
      }
      //...
  }
  
  One can run this method without any difficulties with a code written in a class, like this:
  public static Object runMethod(Object object, String methodName) {
      Method method = object.getClass().getDeclaredMethod(methodName);
      method.setAccessible(true);
      return method.invoke(object);
  }
  
  However, let us suppose that we intend to discourage this habit for fooBar. How can we do something like that? Should we get the stack trace from somewhere and check where it was called? Or should we do something else?
  
  A:
  
  You need a security manager...
  https://docs.oracle.com/javase/tutorial/essential/environment/security.html
  
  A security manager is an object that defines a security policy for an
    application. This policy specifies actions that are unsafe or
    sensitive. Any actions not allowed by the security policy cause a
    SecurityException to be thrown. An application can also query its
    security manager to discover which actions are allowed.
  
  It supports disallowing setAccessible() to make private and protected methods invocable via reflection.
  
  A:
  
  I think if you want to be extreme, you could get the stack trace like this:
  public class Bar {
      //...
      private Object fooBar() {
          try {
              throw new CheckIfCalledFromMethodException(); 
          } catch(CheckIfCalledFromMethodException e) {
              //here you have access to stack trace in your exception
          }
  
          //Do something
      }
      //...
  }
  
  I put together a simple scenario in which it checks if the second instance that calls the class is the same object or something else.
  public class StackTraceTest {
      private void execute() {
          try {
              throw new Exception();
          } catch (Exception e) {
              /*for(int i = 0; i < e.getStackTrace().length; i++) {
                  StackTraceElement stackTraceElement = e.getStackTrace()[i];
                  System.out.println(stackTraceElement.getFileName());
              }
              System.out.println("");
              e.printStackTrace();*/
              if(!e.getStackTrace()[1].getFileName().equals(StackTraceTest.class.getSimpleName() + ".java")) {
                  throw new IllegalAccessError("Illegal Access.");
              }
          }
      }
  
      public void executeExternal() {
          this.execute();
      }
  }
  
  And
  public class AccsessorTest {
      public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
          StackTraceTest stackTraceTest = new StackTraceTest();
          stackTraceTest.executeExternal();
          System.out.println("Accessed from within other method in class.");
          System.out.println("");
          Class<?> clazz = StackTraceTest.class;
          Method method = clazz.getDeclaredMethod("execute");
          method.setAccessible(true);
          System.out.println("Accessing through reflection...");
          method.invoke(stackTraceTest);
      }
  }
  
  Then I get
  Accessed from within other method in class.
  
  Reflection test start.
  Accessing through reflection...
  Exception in thread "main" java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at stacktracetest.another.AccsessorTest.main(AccsessorTest.java:22)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  Caused by: java.lang.IllegalAccessError: Illegal Access.
      at stacktracetest.StackTraceTest.execute(StackTraceTest.java:18)
      ... 10 more
  
  So I guess it is possible to check with stack trace element magic, but you should check if this actually works, this is extremely rough and I've just put it together a second ago.
  
  