Search code examples
javadeprecatedsetterdefault-constructor

Preferred way of marking a Java constructor/method as not for client use?


I want to mark some default constructors and setters as not available/recommended for use. I need it to be somewhat similar to the annotation @Deprecated, but it shouldn't have the same meaning. I'm only adding that constructors and setters because some frameworks are making me do so (Hibernate, Spring, Jackson). Do you know if such construct exists?


Solution

  • The real answer here is to only expose interfaces and factories outside your API. This allows you to hide the methods that might be required to be non-private due to the framework.

    If you cannot do this, try to make the methods default scope (instead of protected).

    In addition, you might use annotations as an in-house marker. On my project, we have created annotations for this purpose: PresentForDependancyInjection and/or FrameworkEntryPoint. These are decoration-only annotations but get the point across.