001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
016 */
017package org.nuxeo.ecm.platform.sessioninspector.util;
018
019import java.lang.reflect.Array;
020import java.lang.reflect.Field;
021import java.lang.reflect.Modifier;
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.IdentityHashMap;
025import java.util.Map;
026
027import org.nuxeo.ecm.platform.sessioninspector.jsf.model.ObjectStatistics;
028import org.nuxeo.runtime.javaagent.AgentLoader;
029
030/**
031 * Build the graph of reference of a given object.
032 *
033 * @since 5.9.2
034 */
035public class ObjectVisitor {
036
037    protected final Map<Object, Object> visited = new IdentityHashMap<Object, Object>();
038
039    public Collection<ObjectStatistics> getObjectStatisticsList() {
040        Map<String, ObjectStatistics> map = new HashMap<String, ObjectStatistics>();
041        for (Object o : visited.keySet()) {
042            final String type = o.getClass().getCanonicalName();
043            ObjectStatistics os = map.get(type);
044            if (os == null) {
045                os = new ObjectStatistics(type, 1, AgentLoader.INSTANCE.getSizer().sizeOf(o));
046                map.put(type, os);
047            } else {
048                os.setNbInstance(os.getNbInstance() + 1);
049                os.setCumulatedSize(os.getCumulatedSize() + AgentLoader.INSTANCE.getSizer().sizeOf(o));
050            }
051        }
052        return map.values();
053    }
054
055    public Map<Object, Object> getVisited() {
056        return visited;
057    }
058
059    public ObjectVisitor() {
060        super();
061    }
062
063    public void visit(Object each) {
064        if (each == null) {
065            return;
066        }
067        if (visited.containsKey(each)) {
068            return;
069        }
070        visited.put(each, each);
071        Class<?> eachType = each.getClass();
072        if (eachType.isArray()) {
073            if (eachType.getComponentType().isPrimitive()) {
074                return;
075            }
076            for (int i = 0; i < Array.getLength(each); i++) {
077                visit(Array.get(each, i));
078            }
079        } else {
080            visit(each, eachType);
081        }
082    }
083
084    protected void visit(Object each, Class<?> eachType) {
085        if (eachType.equals(Object.class)) {
086            return;
087        }
088        for (Field eachField : eachType.getDeclaredFields()) {
089            visit(each, eachField);
090        }
091        visit(each, eachType.getSuperclass());
092    }
093
094    protected void visit(Object each, Field eachField) {
095        if ((eachField.getModifiers() & Modifier.STATIC) != 0) {
096            return;
097        }
098        if (eachField.getType().isPrimitive()) {
099            return;
100        }
101        boolean oldAccessible = eachField.isAccessible();
102        eachField.setAccessible(true);
103        try {
104            visit(eachField.get(each));
105        } catch (Exception e) {
106            throw new RuntimeException("Exception trying to access field " + eachField, e);
107        } finally {
108            eachField.setAccessible(oldAccessible);
109        }
110    }
111}