Class WrapperGenerator

java.lang.Object
org.glassfish.pfl.basic.logex.WrapperGenerator

public class WrapperGenerator extends Object
Given an annotated interface, return a Proxy that implements that interface. Interface must be annotated with @ExceptionWrapper( String idPrefix, String loggerName ). id prefix defaults to empty, loggerName defaults to the package name of the annotated class.

Also, note that this returned wrapper will always implement the MessageInfo interface, which provides a way to capture all of the messages and IDs used in the interface. This is used to generate resource bundles. In order for this to work, it is required that the interface declare a field

public static final [class name] self = ExceptionWrapper.makeWrapper( ... ) ;

This is necessary because the extension mechanism allows the construction of message IDs that cannot be predicted based on the annotations alone.

The behavior of the implementation of each method on the interface is determined in part by its return type as follows:

  • void. Such a method can only log a message. Must have @Log, @Message is optional.
  • String. Such a method may log a message, and also returns the message. Both @Log and @Message are optional.
  • A subclass of Exception. Such a method may log a message, and also returns an exception containing the message. Both @Log and @Message are optional.
Each method may be annotated as follows:
  • @Message( String value ). This defines the message to be placed in a resource bundle (generated at build time by a separate tool). The key to the resource bundle is <loggerName>.<methodName>. The message is prepended with the idPrefix and the id from the @Log annotation (if @Log is present, otherwise nothing is prepended to the message). If this annotation is not present, a default message is created from the method name and the arguments.
  • @Log( LogLevel level, int id ). The presence of this annotation indicates that a log record must be generated, and logger IF the appropriate logger is enabled at the given level (note that LogLevel is an enum used for the annotation, each member of which returns the java.util.logging.Level from a getLevel() method).
In addition, the @Chain annotation may be used on a method parameter (whose type must be a subclass of Throwable) of a method that returns an exception to indicate that the parameter should be the cause of the returned exception. All other method parameters are used as arguments in formatting the message.
  • Field Details

  • Constructor Details

    • WrapperGenerator

      private WrapperGenerator()
  • Method Details

    • getStandardLogId

      public static String getStandardLogId(Method method)
      Expose the standard log ID for the method. This is simply the annotated value in the @Log annotation: it is not processed in any way.
      Parameters:
      method - The method for which the ID is requested.
      Returns:
      The ID (as a string), or null if no @Log annotation is present.
    • makeStandardException

      static Throwable makeStandardException(String msg, Method method)
    • getStandardLoggerName

      static String getStandardLoggerName(Class<?> cls)
    • findAnnotatedParameter

      static int findAnnotatedParameter(Annotation[][] pannos, Class<? extends Annotation> cls)
    • getWithSkip

      static Object[] getWithSkip(Object[] args, int skip)
    • getMsgKey

      static String getMsgKey(String logger, Method method)
    • getMessageMap

      static Map<String,String> getMessageMap(Class<?> cls, WrapperGenerator.Extension extension)
    • getMessage

      static String getMessage(Method method, String prefix, WrapperGenerator.Extension extension)
    • getMessageOrKey

      static String getMessageOrKey(Logger logger, Method method, WrapperGenerator.Extension extension)
    • handleMessageOnly

      static String handleMessageOnly(Method method, WrapperGenerator.Extension extension, Logger logger, Object[] messageParams)
    • classifyReturnType

      static WrapperGenerator.ReturnType classifyReturnType(Method method)
    • makeLogRecord

      static LogRecord makeLogRecord(Level level, String key, Object[] args, Logger logger)
    • trimStackTrace

      static void trimStackTrace(Throwable exc, LogRecord lrec)
    • isMajorLevel

      static boolean isMajorLevel(Level level)
    • needStackTrace

      static boolean needStackTrace(Level level, Method method)
    • handleFullLogging

      static Object handleFullLogging(Log log, Method method, WrapperGenerator.ReturnType rtype, Logger logger, String idPrefix, Object[] messageParams, Throwable cause, WrapperGenerator.Extension extension)
    • makeWrapper

      public static <T> T makeWrapper(Class<T> cls)
      Given an interface annotated with @ExceptionWrapper, return a proxy implementing the interface.
      Type Parameters:
      T - The annotated interface type.
      Parameters:
      cls - The class of the annotated interface.
      Returns:
      An instance of the interface.
    • makeWrapper

      public static <T> T makeWrapper(Class<T> cls, WrapperGenerator.Extension extension)
      Given an interface annotated with @ExceptionWrapper, return a proxy implementing the interface.
      Type Parameters:
      T - The annotated interface type.
      Parameters:
      cls - The class of the annotated interface.
      extension - The extension instance used to override the default behavior.
      Returns:
      An instance of the interface.