Skip to content

Commit

Permalink
fix rootcontainer runtime instance checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Oblarg committed May 24, 2019
1 parent bfd7db4 commit 0dd774b
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions lib/src/main/java/io/github/oblarg/oblog/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private static void configureLogging(LogType logType,
break;
}

// Set up method to be called on Loggables
Consumer<Loggable> log = (toLog) -> logLoggable(logType,
separate,
toLog,
Expand All @@ -150,41 +151,58 @@ private static void configureLogging(LogType logType,
null,
new HashSet<>(Collections.singletonList(toLog)));

// Check all fields of the rootcontainer
for (Field field : rootContainer.getClass().getDeclaredFields()) {
if (isLoggableClassOrArrayOrCollection(field, rootContainer) && isIncluded(field, logType)) {
field.setAccessible(true);
// Handle arrays elementwise...
if (field.getType().isArray()) {
Loggable[] toLogs;
List<Loggable> toLogs = new ArrayList<>();
try {
toLogs = (Loggable[]) field.get(rootContainer);
// Skip if primitive array
if (!Object.class.isAssignableFrom(field.get(rootContainer).getClass().getComponentType())) {
continue;
}
// Include all elements whose runtime class is Loggable
for (Object obj : (Object[]) field.get(rootContainer)) {
if (obj instanceof Loggable) {
toLogs.add((Loggable) obj);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
toLogs = new Loggable[0];
}
// Proceed on all valid elements
for (Loggable toLog : toLogs) {
{
log.accept(toLog);
}
log.accept(toLog);
}
// Handle collections similarly
} else if (Collection.class.isAssignableFrom(field.getType())) {
Collection<Loggable> toLogs;
List<Loggable> toLogs = new ArrayList<>();
try {
toLogs = (Collection) field.get(rootContainer);
// Include all elements whose runtime class is Loggable
for (Object obj : (Collection) field.get(rootContainer)) {
if (obj instanceof Loggable) {
toLogs.add((Loggable) obj);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
toLogs = new HashSet<>();
}
// Proceed on all valid elements
for (Loggable toLog : toLogs) {
log.accept(toLog);
}
} else {
// If not array or collection, object itself is loggable
Loggable toLog;
try {
toLog = (Loggable) field.get(rootContainer);
} catch (IllegalAccessException e) {
e.printStackTrace();
toLog = null;
}
// Proceed on field
log.accept(toLog);
}
}
Expand Down

0 comments on commit 0dd774b

Please sign in to comment.