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 org.ejb3unit.asm.optimizer;
030:
031: import org.ejb3unit.asm.AnnotationVisitor;
032: import org.ejb3unit.asm.Attribute;
033: import org.ejb3unit.asm.ClassAdapter;
034: import org.ejb3unit.asm.ClassVisitor;
035: import org.ejb3unit.asm.FieldVisitor;
036: import org.ejb3unit.asm.MethodVisitor;
037: import org.ejb3unit.asm.Opcodes;
038:
039: /**
040: * A {@link ClassVisitor} that collects the {@link Constant}s of the classes it
041: * visits.
042: *
043: * @author Eric Bruneton
044: */
045: public class ClassConstantsCollector extends ClassAdapter {
046:
047: private ConstantPool cp;
048:
049: public ClassConstantsCollector(final ClassVisitor cv,
050: final ConstantPool cp) {
051: super (cv);
052: this .cp = cp;
053: }
054:
055: public void visit(final int version, final int access,
056: final String name, final String signature,
057: final String super Name, final String[] interfaces) {
058: if ((access & Opcodes.ACC_DEPRECATED) != 0) {
059: cp.newUTF8("Deprecated");
060: }
061: if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
062: cp.newUTF8("Synthetic");
063: }
064: cp.newClass(name);
065: if (signature != null) {
066: cp.newUTF8("Signature");
067: cp.newUTF8(signature);
068: }
069: if (super Name != null) {
070: cp.newClass(super Name);
071: }
072: if (interfaces != null) {
073: for (int i = 0; i < interfaces.length; ++i) {
074: cp.newClass(interfaces[i]);
075: }
076: }
077: cv.visit(version, access, name, signature, super Name,
078: interfaces);
079: }
080:
081: public void visitSource(final String source, final String debug) {
082: if (source != null) {
083: cp.newUTF8("SourceFile");
084: cp.newUTF8(source);
085: }
086: if (debug != null) {
087: cp.newUTF8("SourceDebugExtension");
088: }
089: cv.visitSource(source, debug);
090: }
091:
092: public void visitOuterClass(final String owner, final String name,
093: final String desc) {
094: cp.newUTF8("EnclosingMethod");
095: cp.newClass(owner);
096: if (name != null && desc != null) {
097: cp.newNameType(name, desc);
098: }
099: cv.visitOuterClass(owner, name, desc);
100: }
101:
102: public AnnotationVisitor visitAnnotation(final String desc,
103: final boolean visible) {
104: cp.newUTF8(desc);
105: if (visible) {
106: cp.newUTF8("RuntimeVisibleAnnotations");
107: } else {
108: cp.newUTF8("RuntimeInvisibleAnnotations");
109: }
110: return new AnnotationConstantsCollector(cv.visitAnnotation(
111: desc, visible), cp);
112: }
113:
114: public void visitAttribute(final Attribute attr) {
115: // can do nothing
116: cv.visitAttribute(attr);
117: }
118:
119: public void visitInnerClass(final String name,
120: final String outerName, final String innerName,
121: final int access) {
122: cp.newUTF8("InnerClasses");
123: if (name != null) {
124: cp.newClass(name);
125: }
126: if (outerName != null) {
127: cp.newClass(outerName);
128: }
129: if (innerName != null) {
130: cp.newUTF8(innerName);
131: }
132: cv.visitInnerClass(name, outerName, innerName, access);
133: }
134:
135: public FieldVisitor visitField(final int access, final String name,
136: final String desc, final String signature,
137: final Object value) {
138: if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
139: cp.newUTF8("Synthetic");
140: }
141: if ((access & Opcodes.ACC_DEPRECATED) != 0) {
142: cp.newUTF8("Deprecated");
143: }
144: cp.newUTF8(name);
145: cp.newUTF8(desc);
146: if (signature != null) {
147: cp.newUTF8("Signature");
148: cp.newUTF8(signature);
149: }
150: if (value != null) {
151: cp.newConst(value);
152: }
153: return new FieldConstantsCollector(cv.visitField(access, name,
154: desc, signature, value), cp);
155: }
156:
157: public MethodVisitor visitMethod(final int access,
158: final String name, final String desc,
159: final String signature, final String[] exceptions) {
160: if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
161: cp.newUTF8("Synthetic");
162: }
163: if ((access & Opcodes.ACC_DEPRECATED) != 0) {
164: cp.newUTF8("Deprecated");
165: }
166: cp.newUTF8(name);
167: cp.newUTF8(desc);
168: if (signature != null) {
169: cp.newUTF8("Signature");
170: cp.newUTF8(signature);
171: }
172: if (exceptions != null) {
173: cp.newUTF8("Exceptions");
174: for (int i = 0; i < exceptions.length; ++i) {
175: cp.newClass(exceptions[i]);
176: }
177: }
178: return new MethodConstantsCollector(cv.visitMethod(access,
179: name, desc, signature, exceptions), cp);
180: }
181:
182: public void visitEnd() {
183: cv.visitEnd();
184: }
185: }
|