Added the ability to recursively print objects in an extraction context.
*/
package com.blackducksoftware.integration.hub.detect.bomtool.search.report;
+import java.io.File;
import java.lang.reflect.Field;
+import java.util.HashSet;
+import java.util.Set;
+import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@@ -45,7 +49,7 @@ public class ExtractionReporter {
logger.info("Identifier: " + Integer.toString(context.hashCode()));
logger.info("Extractor: " + strategy.getExtractorClass().getSimpleName());
logger.info("Context: " + strategy.getExtractionContextClass().getSimpleName());
- printObject(context);
+ printObject(null, context);
logger.info(ReportConstants.SEPERATOR);
}
@@ -61,22 +65,60 @@ public class ExtractionReporter {
logger.info(ReportConstants.SEPERATOR);
}
- private void printObject(final Object guy) {
+ private void printObject(final String prefix, final Object guy) {
for (final Field field : guy.getClass().getFields()) {
final String name = field.getName();
String value = "unknown";
+ Object obj = null;
try {
- final Object obj = field.get(guy);
+ obj = field.get(guy);
+ } catch (final Exception e) {
+
+ }
if (obj == null) {
value = "null";
} else {
value = obj.toString();
}
- } catch (final Exception e) {
+ if (StringUtils.isBlank(prefix)) {
+ logger.info(name + " : " + value);
+ }else {
+ logger.info(prefix + "." + name + " : " + value);
+ }
+ if (obj != null) {
+ if (shouldRecursivelyPrintType(obj.getClass())) {
+ String nestedPrefix = name;
+ if (StringUtils.isNotBlank(prefix)) {
+ nestedPrefix = prefix + "." + nestedPrefix;
+ }
+ printObject(nestedPrefix, obj);
+ }
+ }
+ }
}
- logger.info(name + " : " + value);
+
+ public static boolean shouldRecursivelyPrintType(final Class<?> clazz)
+ {
+ return !NON_NESTED_TYPES.contains(clazz);
}
+ private static final Set<Class<?>> NON_NESTED_TYPES = getNonNestedTypes();
+
+ private static Set<Class<?>> getNonNestedTypes()
+ {
+ final Set<Class<?>> ret = new HashSet<>();
+ ret.add(File.class);
+ ret.add(String.class);
+ ret.add(Boolean.class);
+ ret.add(Character.class);
+ ret.add(Byte.class);
+ ret.add(Short.class);
+ ret.add(Integer.class);
+ ret.add(Long.class);
+ ret.add(Float.class);
+ ret.add(Double.class);
+ ret.add(Void.class);
+ return ret;
}
}
