private static Class<?> determineRealClass(HashMap<Class<?>, ObjectEstimator> byType, Deque<Object> stack, Field field, Class<?> fieldClass, Object fieldObj) { if (fieldObj == null) return fieldClass; Class<?> realFieldClass = fieldObj.getClass(); if (!fieldClass.equals(realFieldClass)) { addToProcessing(byType, stack, null, fieldClass); return realFieldClass; } return fieldClass; }
private static void addArrayEstimator(HashMap<Class<?>, ObjectEstimator> byType, Deque<Object> stack, Field field, Object fieldObj) { if (fieldObj == null) return; int arrayLen = Array.getLength(fieldObj); LOG.trace("Empty array {}", field); for (int i = 0; i < arrayLen; ++i) { Object element = Array.get(fieldObj, i); if (element != null) { addToProcessing(byType, stack, element, element.getClass()); } } Class<?> elementClass = fieldObj.getClass().getComponentType(); addToProcessing(byType, stack, null, elementClass); }
private static void addCollectionEstimator(HashMap<Class<?>, ObjectEstimator> byType, Deque<Object> stack, Field field, Class<?> fieldClass, Object fieldObj) { Collection<?> fieldCol = null; if (fieldObj != null) { fieldCol = (Collection<?>) fieldObj; if (fieldCol.size() == 0) { fieldCol = null; LOG.trace("Empty collection {}", field); } } if (fieldCol != null) { for (Object element : fieldCol) { if (element != null) { addToProcessing(byType, stack, element, element.getClass()); } } } if (field != null) { Class<?> collectionArg = getCollectionArg(field); if (collectionArg != null) { addToProcessing(byType, stack, null, collectionArg); } // TODO: there was code here to create guess-estimate for collection wrt how usage changes // when removing elements. However it's too error-prone for anything involving // pre-allocated capacity, so it was discarded. // We will estimate collection as an object (only if it's a field). addToProcessing(byType, stack, fieldObj, fieldClass); } }
Object k = element.getKey(), v = element.getValue(); if (k != null) { addToProcessing(byType, stack, k, k.getClass()); addToProcessing(byType, stack, v, v.getClass()); if (mapArgs != null) { for (Class<?> mapArg : mapArgs) { addToProcessing(byType, stack, null, mapArg); addToProcessing(byType, stack, fieldObj, fieldClass);
private static Deque<Object> createWorkStack(Object rootObj, HashMap<Class<?>, ObjectEstimator> byType) { Deque<Object> stack = new ArrayDeque<Object>(32); Class<?> rootClass = rootObj.getClass(); if (Class.class.equals(rootClass)) { rootClass = (Class<?>) rootObj; rootObj = null; } else { // If root object is an array, map or collection, add estimators as for fields if (rootClass.isArray() && !rootClass.getComponentType().isPrimitive()) { addArrayEstimator(byType, stack, null, rootObj); } else if (Collection.class.isAssignableFrom(rootClass)) { addCollectionEstimator(byType, stack, null, rootClass, rootObj); } else if (Map.class.isAssignableFrom(rootClass)) { addMapEstimator(byType, stack, null, rootClass, rootObj); } } addToProcessing(byType, stack, rootObj, rootClass); return stack; }
} else { estimator.addField(FieldType.OTHER, field); addToProcessing(byType, stack, fieldObj, fieldClass);
private static Class<?> determineRealClass(HashMap<Class<?>, ObjectEstimator> byType, Deque<Object> stack, Field field, Class<?> fieldClass, Object fieldObj) { if (fieldObj == null) return fieldClass; Class<?> realFieldClass = fieldObj.getClass(); if (!fieldClass.equals(realFieldClass)) { addToProcessing(byType, stack, null, fieldClass); return realFieldClass; } return fieldClass; }
private static void addArrayEstimator(HashMap<Class<?>, ObjectEstimator> byType, Deque<Object> stack, Field field, Object fieldObj) { if (fieldObj == null) return; int arrayLen = Array.getLength(fieldObj); LOG.trace("Empty array {}", field); for (int i = 0; i < arrayLen; ++i) { Object element = Array.get(fieldObj, i); if (element != null) { addToProcessing(byType, stack, element, element.getClass()); } } Class<?> elementClass = fieldObj.getClass().getComponentType(); addToProcessing(byType, stack, null, elementClass); }
private static void addCollectionEstimator(HashMap<Class<?>, ObjectEstimator> byType, Deque<Object> stack, Field field, Class<?> fieldClass, Object fieldObj) { Collection<?> fieldCol = null; if (fieldObj != null) { fieldCol = (Collection<?>) fieldObj; if (fieldCol.size() == 0) { fieldCol = null; LOG.trace("Empty collection {}", field); } } if (fieldCol != null) { for (Object element : fieldCol) { if (element != null) { addToProcessing(byType, stack, element, element.getClass()); } } } if (field != null) { Class<?> collectionArg = getCollectionArg(field); if (collectionArg != null) { addToProcessing(byType, stack, null, collectionArg); } // TODO: there was code here to create guess-estimate for collection wrt how usage changes // when removing elements. However it's too error-prone for anything involving // pre-allocated capacity, so it was discarded. // We will estimate collection as an object (only if it's a field). addToProcessing(byType, stack, fieldObj, fieldClass); } }
Object k = element.getKey(), v = element.getValue(); if (k != null) { addToProcessing(byType, stack, k, k.getClass()); addToProcessing(byType, stack, v, v.getClass()); if (mapArgs != null) { for (Class<?> mapArg : mapArgs) { addToProcessing(byType, stack, null, mapArg); addToProcessing(byType, stack, fieldObj, fieldClass);
private static Deque<Object> createWorkStack(Object rootObj, HashMap<Class<?>, ObjectEstimator> byType) { Deque<Object> stack = new ArrayDeque<Object>(32); Class<?> rootClass = rootObj.getClass(); if (Class.class.equals(rootClass)) { rootClass = (Class<?>) rootObj; rootObj = null; } else { // If root object is an array, map or collection, add estimators as for fields if (rootClass.isArray() && !rootClass.getComponentType().isPrimitive()) { addArrayEstimator(byType, stack, null, rootObj); } else if (Collection.class.isAssignableFrom(rootClass)) { addCollectionEstimator(byType, stack, null, rootClass, rootObj); } else if (Map.class.isAssignableFrom(rootClass)) { addMapEstimator(byType, stack, null, rootClass, rootObj); } } addToProcessing(byType, stack, rootObj, rootClass); return stack; }
} else { estimator.addField(FieldType.OTHER, field); addToProcessing(byType, stack, fieldObj, fieldClass);