Class ThisClass

java.lang.Object
space.arim.omnibus.util.ThisClass

public final class ThisClass extends Object
Simple utility to get one's own literal class, including in a static context.

The most common use case is where one requires a statically constructed Logger:
private static final Logger logger = LoggerFactory.getLogger(LoggingClass.class);

where LoggingClass is the class in which the logger is placed. This approach is prone to particular error due to the class literal passed to LoggerFactory.getLogger. If improperly copied and pasted, the wrong logger might be retrieved.

The solution involves using this utility class to determine the class:
private static final Logger logger = LoggerFactory.getLogger(ThisClass.get());

The improved code uses ThisClass.get() instead of the class literal, and thus may be copied and pasted at will with no fear of error.

Moreover, ThisClass.get() is more readable than the alternative MethodHandles.lookup().lookupClass().
Author:
A248
  • Method Details

    • get

      public static Class<?> get()
      Gets the literal class which called this method.
      Returns:
      the literal class in which this method is called