001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package com.tc.asm.util;
030:
031: import com.tc.asm.AnnotationVisitor;
032: import com.tc.asm.Attribute;
033:
034: /**
035: * An abstract trace visitor.
036: *
037: * @author Eric Bruneton
038: */
039: public abstract class TraceAbstractVisitor extends AbstractVisitor {
040:
041: /**
042: * Constant used in {@link #appendDescriptor appendDescriptor} for internal
043: * type names in bytecode notation.
044: */
045: public final static int INTERNAL_NAME = 0;
046:
047: /**
048: * Constant used in {@link #appendDescriptor appendDescriptor} for field
049: * descriptors, formatted in bytecode notation
050: */
051: public final static int FIELD_DESCRIPTOR = 1;
052:
053: /**
054: * Constant used in {@link #appendDescriptor appendDescriptor} for field
055: * signatures, formatted in bytecode notation
056: */
057: public final static int FIELD_SIGNATURE = 2;
058:
059: /**
060: * Constant used in {@link #appendDescriptor appendDescriptor} for method
061: * descriptors, formatted in bytecode notation
062: */
063: public final static int METHOD_DESCRIPTOR = 3;
064:
065: /**
066: * Constant used in {@link #appendDescriptor appendDescriptor} for method
067: * signatures, formatted in bytecode notation
068: */
069: public final static int METHOD_SIGNATURE = 4;
070:
071: /**
072: * Constant used in {@link #appendDescriptor appendDescriptor} for class
073: * signatures, formatted in bytecode notation
074: */
075: public final static int CLASS_SIGNATURE = 5;
076:
077: /**
078: * Constant used in {@link #appendDescriptor appendDescriptor} for field or
079: * method return value signatures, formatted in default Java notation
080: * (non-bytecode)
081: */
082: public final static int TYPE_DECLARATION = 6;
083:
084: /**
085: * Constant used in {@link #appendDescriptor appendDescriptor} for class
086: * signatures, formatted in default Java notation (non-bytecode)
087: */
088: public final static int CLASS_DECLARATION = 7;
089:
090: /**
091: * Constant used in {@link #appendDescriptor appendDescriptor} for method
092: * parameter signatures, formatted in default Java notation (non-bytecode)
093: */
094: public final static int PARAMETERS_DECLARATION = 8;
095:
096: /**
097: * Tab for class members.
098: */
099: protected String tab = " ";
100:
101: /**
102: * Prints a disassembled view of the given annotation.
103: *
104: * @param desc the class descriptor of the annotation class.
105: * @param visible <tt>true</tt> if the annotation is visible at runtime.
106: * @return a visitor to visit the annotation values.
107: */
108: public AnnotationVisitor visitAnnotation(final String desc,
109: final boolean visible) {
110: buf.setLength(0);
111: buf.append(tab).append('@');
112: appendDescriptor(FIELD_DESCRIPTOR, desc);
113: buf.append('(');
114: text.add(buf.toString());
115: TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
116: text.add(tav.getText());
117: text.add(visible ? ")\n" : ") // invisible\n");
118: return tav;
119: }
120:
121: /**
122: * Prints a disassembled view of the given attribute.
123: *
124: * @param attr an attribute.
125: */
126: public void visitAttribute(final Attribute attr) {
127: buf.setLength(0);
128: buf.append(tab).append("ATTRIBUTE ");
129: appendDescriptor(-1, attr.type);
130:
131: if (attr instanceof Traceable) {
132: ((Traceable) attr).trace(buf, null);
133: } else {
134: buf.append(" : unknown\n");
135: }
136:
137: text.add(buf.toString());
138: }
139:
140: /**
141: * Does nothing.
142: */
143: public void visitEnd() {
144: // does nothing
145: }
146:
147: // ------------------------------------------------------------------------
148: // Utility methods
149: // ------------------------------------------------------------------------
150:
151: protected TraceAnnotationVisitor createTraceAnnotationVisitor() {
152: return new TraceAnnotationVisitor();
153: }
154:
155: /**
156: * Appends an internal name, a type descriptor or a type signature to
157: * {@link #buf buf}.
158: *
159: * @param type indicates if desc is an internal name, a field descriptor, a
160: * method descriptor, a class signature, ...
161: * @param desc an internal name, type descriptor, or type signature. May be
162: * <tt>null</tt>.
163: */
164: protected void appendDescriptor(final int type, final String desc) {
165: if (type == CLASS_SIGNATURE || type == FIELD_SIGNATURE
166: || type == METHOD_SIGNATURE) {
167: if (desc != null) {
168: buf.append("// signature ").append(desc).append('\n');
169: }
170: } else {
171: buf.append(desc);
172: }
173: }
174:
175: }
|