root/trunk/src/java/org/jcoderz/commons/tracing/AsmUtil.java

Revision 1628, 6.5 kB (checked in by amandel, 2 years ago)

Add work in progress tracing components.
- Extended TracingProxy? that allows different Traces to be used
- TracingInjector? that allows aspect like injection of the tracing aspect.

Please note that this is still work in progress.

  • Property svn:mime-type set to text/plain
Line 
1/*
2 * $Id: ArraysUtil.java 1011 2008-06-16 17:57:36Z amandel $
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.tracing;
34
35
36import org.objectweb.asm.Opcodes;
37import org.objectweb.asm.tree.ClassNode;
38import org.objectweb.asm.tree.InsnList;
39import org.objectweb.asm.tree.InsnNode;
40import org.objectweb.asm.tree.IntInsnNode;
41import org.objectweb.asm.tree.LocalVariableNode;
42import org.objectweb.asm.tree.MethodNode;
43
44
45/**
46 * Utility classes around the ASM package. See http://asm.objectweb.org/
47 *
48 * @author Andreas Mandel
49 */
50public final class AsmUtil
51{
52    /** Maximum value that can be set using the Opcodes.ICONST_0. */
53    public static final int MAX_ICONST = 5;
54
55    /** Maximum value that can be set using the Opcodes.BIPUSH. */
56    public static final int MAX_BIPUSH = 127;
57
58    /** Maximum value that can be set using the Opcodes.SIPUSH. */
59    public static final int MAX_SIPUSH = 32767;
60
61    /** No instances. */
62    private AsmUtil ()
63    {
64        // No instances.
65    }
66
67    /**
68     * Add a command to the stack that loads the given integer constant.
69     * The method takes care to use the most efficient opcode.
70     *
71     * @param cmd the command list where to add the command.
72     * @param i the value to be loaded.
73     */
74    public static void loadConstant (InsnList cmd, int i)
75    {
76        if (i <= MAX_ICONST)
77        {
78            cmd.add(new InsnNode(Opcodes.ICONST_0 + i));
79        }
80        else if (i <= MAX_BIPUSH)
81        {
82            cmd.add(new IntInsnNode(Opcodes.BIPUSH, i));
83        }
84        else if (i <= MAX_SIPUSH)
85        {
86            cmd.add(new IntInsnNode(Opcodes.SIPUSH, i));
87        }
88        else
89        {
90            cmd.add(new IntInsnNode(Opcodes.LDC, i));
91        }
92    }
93
94    /**
95     * To string helper to get nice comments about the MethodNode.
96     *
97     * @param mn the method node.
98     * @return a human readable string representation describing the
99     *         method node
100     */
101    public static String toString (MethodNode mn)
102    {
103        return toString(mn.access) + " " + mn.name + " " + mn.desc;
104    }
105
106    /**
107     * Human readable name of the class.
108     *
109     * @param classNode the asm class node.
110     * @return Human readable name of the class.
111     */
112    public static String toString (ClassNode classNode)
113    {
114        return (toString(classNode.access)
115            + ((classNode.access & Opcodes.ACC_INTERFACE) == 0
116                ? " class "
117                : " interface ") + classNode.name).trim();
118    }
119
120    /**
121     * Human readable name of the method.
122     *
123     * @param cn the asm class node.
124     * @param mn the asm method node.
125     * @return Human readable name of the class and method.
126     */
127    public static String toString (ClassNode cn, MethodNode mn)
128    {
129        return toString(mn.access) + " " + cn.name + "#" + mn.name + mn.desc;
130    }
131
132    /**
133     * Human readable name of the local variable.
134     *
135     * @param node the asm local variable node.
136     * @return Human readable name of the local variable.
137     */
138    public static String toString (LocalVariableNode node)
139    {
140        return node.name + ": " + node.desc + "@" + node.index + "["
141            + node.start + ":" + node.end + "]";
142    }
143
144    /**
145     * Returns a string representation containing the access keywords
146     * implied by the given int. See {@link Opcodes} ACC* for the
147     * values.
148     *
149     * @param access the int value holding the access bits. See
150     *        {@link Opcodes} ACC* for the values.
151     * @return a string representation containing the access keywords
152     *         implied by the given int.
153     */
154    public static String toString (int access)
155    {
156        final StringBuffer sb = new StringBuffer();
157        if ((access & Opcodes.ACC_PUBLIC) != 0)
158        {
159            sb.append("public ");
160        }
161        if ((access & Opcodes.ACC_PROTECTED) != 0)
162        {
163            sb.append("protected ");
164        }
165        if ((access & Opcodes.ACC_PRIVATE) != 0)
166        {
167            sb.append("private ");
168        }
169        if ((access & Opcodes.ACC_STATIC) != 0)
170        {
171            sb.append("static ");
172        }
173        if ((access & Opcodes.ACC_FINAL) != 0)
174        {
175            sb.append("final ");
176        }
177        if ((access & Opcodes.ACC_SYNCHRONIZED) != 0)
178        {
179            sb.append("synchronized ");
180        }
181        if ((access & Opcodes.ACC_VOLATILE) != 0)
182        {
183            sb.append("volatile ");
184        }
185        if ((access & Opcodes.ACC_TRANSIENT) != 0)
186        {
187            sb.append("transient ");
188        }
189        if ((access & Opcodes.ACC_NATIVE) != 0)
190        {
191            sb.append("native ");
192        }
193        if ((access & Opcodes.ACC_ABSTRACT) != 0)
194        {
195            sb.append("abstract ");
196        }
197        if (sb.length() > 0)
198        {
199            sb.setLength(sb.length() - 1);
200        }
201        return sb.toString();
202    }
203}
Note: See TracBrowser for help on using the browser.