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