Mercurial > hg > graal-jvmci-8
comparison graal/Runtime/src/com/sun/hotspot/c1x/logging/LoggingProxy.java @ 2297:099e697d8934
Renaming c1x4hotspotsrc => graal and HotSpotVM => Runtime
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Fri, 22 Apr 2011 15:08:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2296:34354e2e40a3 | 2297:099e697d8934 |
---|---|
1 /* | |
2 * Copyright (c) 2010 Sun Microsystems, Inc. All rights reserved. | |
3 * | |
4 * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product | |
5 * that is described in this document. In particular, and without limitation, these intellectual property | |
6 * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or | |
7 * more additional patents or pending patent applications in the U.S. and in other countries. | |
8 * | |
9 * U.S. Government Rights - Commercial software. Government users are subject to the Sun | |
10 * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its | |
11 * supplements. | |
12 * | |
13 * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or | |
14 * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks | |
15 * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the | |
16 * U.S. and other countries. | |
17 * | |
18 * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open | |
19 * Company, Ltd. | |
20 */ | |
21 package com.sun.hotspot.c1x.logging; | |
22 | |
23 import java.lang.reflect.*; | |
24 | |
25 import com.sun.hotspot.c1x.server.*; | |
26 | |
27 /** | |
28 * A java.lang.reflect proxy that hierarchically logs all method invocations along with their parameters and return values. | |
29 * | |
30 * @author Lukas Stadler | |
31 */ | |
32 public class LoggingProxy<T> implements InvocationHandler { | |
33 | |
34 private T delegate; | |
35 | |
36 public LoggingProxy(T delegate) { | |
37 this.delegate = delegate; | |
38 } | |
39 | |
40 @Override | |
41 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | |
42 int argCount = args == null ? 0 : args.length; | |
43 if (method.getParameterTypes().length != argCount) { | |
44 throw new RuntimeException("wrong parameter count"); | |
45 } | |
46 StringBuilder str = new StringBuilder(); | |
47 str.append(method.getReturnType().getSimpleName() + " " + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "("); | |
48 for (int i = 0; i < argCount; i++) { | |
49 str.append(i == 0 ? "" : ", "); | |
50 str.append(Logger.pretty(args[i])); | |
51 } | |
52 str.append(")"); | |
53 Logger.startScope(str.toString()); | |
54 final Object result; | |
55 try { | |
56 if (args == null) { | |
57 result = method.invoke(delegate); | |
58 } else { | |
59 result = method.invoke(delegate, args); | |
60 } | |
61 } catch (InvocationTargetException e) { | |
62 Logger.endScope(" = Exception " + e.getMessage()); | |
63 throw e.getCause(); | |
64 } | |
65 Logger.endScope(" = " + Logger.pretty(result)); | |
66 return result; | |
67 } | |
68 | |
69 /** | |
70 * The object returned by this method will implement all interfaces that are implemented by delegate. | |
71 */ | |
72 public static <T> T getProxy(Class<T> interf, T delegate) { | |
73 Class<?>[] interfaces = ReplacingStreams.getAllInterfaces(delegate.getClass()); | |
74 Object obj = Proxy.newProxyInstance(interf.getClassLoader(), interfaces, new LoggingProxy<T>(delegate)); | |
75 return interf.cast(obj); | |
76 } | |
77 } |