001/*
002 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.logging;
024
025import java.lang.reflect.*;
026
027/**
028 * A java.lang.reflect proxy that hierarchically logs all method invocations along with their
029 * parameters and return values.
030 */
031public class LoggingProxy<T> implements InvocationHandler {
032
033    private T delegate;
034
035    public LoggingProxy(T delegate) {
036        this.delegate = delegate;
037    }
038
039    @Override
040    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
041        int argCount = args == null ? 0 : args.length;
042        if (method.getParameterTypes().length != argCount) {
043            throw new RuntimeException("wrong parameter count");
044        }
045        StringBuilder str = new StringBuilder();
046        str.append(method.getReturnType().getSimpleName() + " " + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "(");
047        for (int i = 0; i < argCount; i++) {
048            str.append(i == 0 ? "" : ", ");
049            str.append(Logger.pretty(args[i]));
050        }
051        str.append(")");
052        Logger.startScope(str.toString());
053        final Object result;
054        try {
055            if (args == null) {
056                result = method.invoke(delegate);
057            } else {
058                result = method.invoke(delegate, args);
059            }
060        } catch (InvocationTargetException e) {
061            Logger.endScope(" = Exception " + e.getMessage());
062            throw e.getCause();
063        }
064        Logger.endScope(" = " + Logger.pretty(result));
065        return result;
066    }
067
068    /**
069     * The object returned by this method will implement all interfaces that are implemented by
070     * delegate.
071     */
072    public static <T> T getProxy(Class<T> interf, T delegate) {
073        Class<?>[] interfaces = ProxyUtil.getAllInterfaces(delegate.getClass());
074        Object obj = Proxy.newProxyInstance(interf.getClassLoader(), interfaces, new LoggingProxy<>(delegate));
075        return interf.cast(obj);
076    }
077}