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: */
030: package org.objectweb.asm.util;
031:
032: import org.objectweb.asm.AnnotationVisitor;
033: import org.objectweb.asm.Attribute;
034:
035: /**
036: * An abstract trace visitor.
037: *
038: * @author Eric Bruneton
039: */
040: @SuppressWarnings("unchecked")
041: public abstract class TraceAbstractVisitor extends AbstractVisitor {
042:
043: /**
044: * Constant used in {@link #appendDescriptor appendDescriptor} for internal
045: * type names in bytecode notation.
046: */
047: public final static int INTERNAL_NAME = 0;
048:
049: /**
050: * Constant used in {@link #appendDescriptor appendDescriptor} for field
051: * descriptors, formatted in bytecode notation
052: */
053: public final static int FIELD_DESCRIPTOR = 1;
054:
055: /**
056: * Constant used in {@link #appendDescriptor appendDescriptor} for field
057: * signatures, formatted in bytecode notation
058: */
059: public final static int FIELD_SIGNATURE = 2;
060:
061: /**
062: * Constant used in {@link #appendDescriptor appendDescriptor} for method
063: * descriptors, formatted in bytecode notation
064: */
065: public final static int METHOD_DESCRIPTOR = 3;
066:
067: /**
068: * Constant used in {@link #appendDescriptor appendDescriptor} for method
069: * signatures, formatted in bytecode notation
070: */
071: public final static int METHOD_SIGNATURE = 4;
072:
073: /**
074: * Constant used in {@link #appendDescriptor appendDescriptor} for class
075: * signatures, formatted in bytecode notation
076: */
077: public final static int CLASS_SIGNATURE = 5;
078:
079: /**
080: * Constant used in {@link #appendDescriptor appendDescriptor} for field or
081: * method return value signatures, formatted in default Java notation
082: * (non-bytecode)
083: */
084: public final static int TYPE_DECLARATION = 6;
085:
086: /**
087: * Constant used in {@link #appendDescriptor appendDescriptor} for class
088: * signatures, formatted in default Java notation (non-bytecode)
089: */
090: public final static int CLASS_DECLARATION = 7;
091:
092: /**
093: * Constant used in {@link #appendDescriptor appendDescriptor} for method
094: * parameter signatures, formatted in default Java notation (non-bytecode)
095: */
096: public final static int PARAMETERS_DECLARATION = 8;
097:
098: /**
099: * Tab for class members.
100: */
101: protected String tab = " ";
102:
103: /**
104: * Prints a disassembled view of the given annotation.
105: *
106: * @param desc the class descriptor of the annotation class.
107: * @param visible <tt>true</tt> if the annotation is visible at runtime.
108: * @return a visitor to visit the annotation values.
109: */
110: public AnnotationVisitor visitAnnotation(final String desc,
111: final boolean visible) {
112: buf.setLength(0);
113: buf.append(tab).append('@');
114: appendDescriptor(FIELD_DESCRIPTOR, desc);
115: buf.append('(');
116: text.add(buf.toString());
117: TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
118: text.add(tav.getText());
119: text.add(visible ? ")\n" : ") // invisible\n");
120: return tav;
121: }
122:
123: /**
124: * Prints a disassembled view of the given attribute.
125: *
126: * @param attr an attribute.
127: */
128: public void visitAttribute(final Attribute attr) {
129: buf.setLength(0);
130: buf.append(tab).append("ATTRIBUTE ");
131: appendDescriptor(-1, attr.type);
132:
133: if (attr instanceof Traceable) {
134: ((Traceable) attr).trace(buf, null);
135: } else {
136: buf.append(" : unknown\n");
137: }
138:
139: text.add(buf.toString());
140: }
141:
142: /**
143: * Does nothing.
144: */
145: public void visitEnd() {
146: // does nothing
147: }
148:
149: // ------------------------------------------------------------------------
150: // Utility methods
151: // ------------------------------------------------------------------------
152:
153: protected TraceAnnotationVisitor createTraceAnnotationVisitor() {
154: return new TraceAnnotationVisitor();
155: }
156:
157: /**
158: * Appends an internal name, a type descriptor or a type signature to
159: * {@link #buf buf}.
160: *
161: * @param type indicates if desc is an internal name, a field descriptor, a
162: * method descriptor, a class signature, ...
163: * @param desc an internal name, type descriptor, or type signature. May be
164: * <tt>null</tt>.
165: */
166: protected void appendDescriptor(final int type, final String desc) {
167: if (type == CLASS_SIGNATURE || type == FIELD_SIGNATURE
168: || type == METHOD_SIGNATURE) {
169: if (desc != null) {
170: buf.append("// signature ").append(desc).append('\n');
171: }
172: } else {
173: buf.append(desc);
174: }
175: }
176:
177: }
|