comparison graal/com.oracle.jvmci.hotspot/src/com/oracle/jvmci/hotspot/logging/LoggingProxy.java @ 21551:5324104ac4f3

moved com.oracle.graal.hotspot.jvmci classes to com.oracle.jvmci.hotspot module (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Tue, 26 May 2015 17:13:37 +0200
parents
children
comparison
equal deleted inserted replaced
21550:f48a6cea31eb 21551:5324104ac4f3
1 /*
2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.jvmci.hotspot.logging;
24
25 import java.lang.reflect.*;
26
27 /**
28 * A java.lang.reflect proxy that hierarchically logs all method invocations along with their
29 * parameters and return values.
30 */
31 public class LoggingProxy<T> implements InvocationHandler {
32
33 private T delegate;
34
35 public LoggingProxy(T delegate) {
36 this.delegate = delegate;
37 }
38
39 @Override
40 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
41 int argCount = args == null ? 0 : args.length;
42 if (method.getParameterTypes().length != argCount) {
43 throw new RuntimeException("wrong parameter count");
44 }
45 StringBuilder str = new StringBuilder();
46 str.append(method.getReturnType().getSimpleName() + " " + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "(");
47 for (int i = 0; i < argCount; i++) {
48 str.append(i == 0 ? "" : ", ");
49 str.append(Logger.pretty(args[i]));
50 }
51 str.append(")");
52 Logger.startScope(str.toString());
53 final Object result;
54 try {
55 if (args == null) {
56 result = method.invoke(delegate);
57 } else {
58 result = method.invoke(delegate, args);
59 }
60 } catch (InvocationTargetException e) {
61 Logger.endScope(" = Exception " + e.getMessage());
62 throw e.getCause();
63 }
64 Logger.endScope(" = " + Logger.pretty(result));
65 return result;
66 }
67
68 /**
69 * The object returned by this method will implement all interfaces that are implemented by
70 * delegate.
71 */
72 public static <T> T getProxy(Class<T> interf, T delegate) {
73 Class<?>[] interfaces = ProxyUtil.getAllInterfaces(delegate.getClass());
74 Object obj = Proxy.newProxyInstance(interf.getClassLoader(), interfaces, new LoggingProxy<>(delegate));
75 return interf.cast(obj);
76 }
77 }