From ee16e93675a9feb45aa61da1be39106dc7c77b29 Mon Sep 17 00:00:00 2001 From: superpenguin612 <74030080+superpenguin612@users.noreply.github.com> Date: Wed, 20 Dec 2023 20:15:46 -0500 Subject: [PATCH] Do not require name for @Log, instead infer based on var name --- .../houndlog/LogAnnotationHandler.java | 24 +++++++++++-------- .../houndutil/houndlog/interfaces/Log.java | 2 +- .../houndlog/interfaces/SendableLog.java | 2 +- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/techhounds/houndutil/houndlog/LogAnnotationHandler.java b/src/main/java/com/techhounds/houndutil/houndlog/LogAnnotationHandler.java index 23ab1db..1b087a6 100644 --- a/src/main/java/com/techhounds/houndutil/houndlog/LogAnnotationHandler.java +++ b/src/main/java/com/techhounds/houndutil/houndlog/LogAnnotationHandler.java @@ -71,6 +71,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis for (Method method : loggedObject.getClass().getDeclaredMethods()) { Log subLogAnnotation = method.getAnnotation(Log.class); if (subLogAnnotation != null) { + String varName = subLogAnnotation.name() != "" ? subLogAnnotation.name() : method.getName(); method.setAccessible(true); Supplier valueSupplier = () -> { @@ -82,7 +83,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis } }; - Optional optLogger = getLoggerForValue(valueSupplier, subLogAnnotation); + Optional optLogger = getLoggerForValue(valueSupplier, subLogAnnotation, varName); if (optLogger.isPresent()) { loggers.add(optLogger.get()); } @@ -93,6 +94,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis field.setAccessible(true); Log subLogAnnotation = field.getAnnotation(Log.class); if (subLogAnnotation != null) { + String varName = subLogAnnotation.name().equals("") ? field.getName() : subLogAnnotation.name(); LoggedObject loggedObjectAnnotation = field.getType().getAnnotation(LoggedObject.class); if (loggedObjectAnnotation != null) { try { @@ -102,7 +104,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis updatedSubkeys.add(name); updatedSubkeys.addAll(Arrays.asList(logGroups)); - handleLoggedObject(field.get(loggedObject), subLogAnnotation.name(), updatedSubkeys); + handleLoggedObject(field.get(loggedObject), varName, updatedSubkeys); } catch (Exception e) { e.printStackTrace(); } @@ -128,7 +130,7 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis } }; - TunableDouble logger = new TunableDouble(getName(subLogAnnotation), + TunableDouble logger = new TunableDouble(getName(subLogAnnotation, varName), (double) valueSupplier.get(), consumer); loggers.add(logger); } else if (value.getClass() == Boolean.class) { @@ -140,13 +142,13 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis } }; - TunableBoolean logger = new TunableBoolean(getName(subLogAnnotation), + TunableBoolean logger = new TunableBoolean(getName(subLogAnnotation, varName), (boolean) valueSupplier.get(), consumer); loggers.add(logger); } } else { - Optional optLogger = getLoggerForValue(valueSupplier, subLogAnnotation); + Optional optLogger = getLoggerForValue(valueSupplier, subLogAnnotation, varName); if (optLogger.isPresent()) { loggers.add(optLogger.get()); } @@ -158,7 +160,8 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis if (subSendableLogAnnotation != null) { ArrayList nameComponents = new ArrayList(); nameComponents.addAll(Arrays.asList(subSendableLogAnnotation.groups())); - nameComponents.add(subSendableLogAnnotation.name()); + String varName = subSendableLogAnnotation.name().equals("") ? field.getName() : subLogAnnotation.name(); + nameComponents.add(varName); String formattedName = String.join("/", nameComponents); try { @@ -176,10 +179,10 @@ public static void handleLoggedObject(Object loggedObject, String name, ArrayLis new LogGroup(loggers.toArray(new Logger[loggers.size()]))); } - public static String getName(Log logAnnotation) { + public static String getName(Log logAnnotation, String varName) { ArrayList nameComponents = new ArrayList(); nameComponents.addAll(Arrays.asList(logAnnotation.groups())); - nameComponents.add(logAnnotation.name()); + nameComponents.add(varName); return String.join("/", nameComponents); } @@ -189,10 +192,11 @@ public static String getName(Log logAnnotation) { * @param object the object to get the value from * @return */ - public static Optional getLoggerForValue(Supplier valueSupplier, Log logAnnotation) { + public static Optional getLoggerForValue(Supplier valueSupplier, Log logAnnotation, + String varName) { try { Object value = valueSupplier.get(); - String name = getName(logAnnotation); + String name = getName(logAnnotation, varName); if (valueSupplier.get() instanceof Supplier) { Map, Supplier> supplierClassToLoggerMap = Map.ofEntries( diff --git a/src/main/java/com/techhounds/houndutil/houndlog/interfaces/Log.java b/src/main/java/com/techhounds/houndutil/houndlog/interfaces/Log.java index 68cfc09..1f77281 100644 --- a/src/main/java/com/techhounds/houndutil/houndlog/interfaces/Log.java +++ b/src/main/java/com/techhounds/houndutil/houndlog/interfaces/Log.java @@ -9,7 +9,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.METHOD }) public @interface Log { - public String name(); + public String name() default ""; public String[] groups() default {}; diff --git a/src/main/java/com/techhounds/houndutil/houndlog/interfaces/SendableLog.java b/src/main/java/com/techhounds/houndutil/houndlog/interfaces/SendableLog.java index d29d72d..e8fde06 100644 --- a/src/main/java/com/techhounds/houndutil/houndlog/interfaces/SendableLog.java +++ b/src/main/java/com/techhounds/houndutil/houndlog/interfaces/SendableLog.java @@ -8,7 +8,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface SendableLog { - public String name(); + public String name() default ""; public String[] groups() default {};