All Downloads are FREE. Search and download functionalities are using the official Maven repository.

delombok.io.github.qsy7.logging.util.ContextualDataUtil Maven / Gradle / Ivy

The newest version!
// Generated by delombok at Fri Apr 08 14:43:06 EDT 2022
package io.github.qsy7.logging.util;

import io.github.qsy7.logging.FieldContextualLoggable;
import io.github.qsy7.logging.annotation.ContextualLoggableField;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ContextualDataUtil {
  public static String getContextualData(final Object target) {
    final StringBuilder buffer = new StringBuilder();
    getContextual(target).entrySet().forEach(entry -> buffer.append(entry.getKey()).append("->").append(ArgumentUtil.getArgument(false, entry.getValue())));
    return buffer.toString();
  }

  public static Map getContextual(final Object target) {
    final Map contextualData = new HashMap<>();
    getContextual(contextualData, target, target.getClass());
    return contextualData;
  }

  private static void getContextual(final Map contextual, final Object object, final Class targetClass) {
    for (final Field field : targetClass.getDeclaredFields()) {
      if (field.isAnnotationPresent(ContextualLoggableField.class)) {
        try {
          final boolean wasAccessible = field.canAccess(object);
          field.setAccessible(true);
          contextual.put(targetClass.getName() + "." + field.getName(), field.get(object));
          if (!wasAccessible) {
            field.setAccessible(wasAccessible);
          }
        } catch (Exception e) {
        }
      }
      // pass
    }
    if (targetClass.getSuperclass() != null && targetClass.getSuperclass().isAssignableFrom(FieldContextualLoggable.class)) {
      getContextual(contextual, object, targetClass.getSuperclass());
    }
  }

  @java.lang.SuppressWarnings("all")
  private ContextualDataUtil() {
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy