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.Type;
033:
034: /**
035: * An {@link AnnotationVisitor} that collects the {@link Constant}s of the
036: * annotations it visits.
037: *
038: * @author Eric Bruneton
039: */
040: public class AnnotationConstantsCollector implements AnnotationVisitor {
041:
042: private AnnotationVisitor av;
043:
044: private ConstantPool cp;
045:
046: public AnnotationConstantsCollector(final AnnotationVisitor av,
047: final ConstantPool cp) {
048: this .av = av;
049: this .cp = cp;
050: }
051:
052: public void visit(final String name, final Object value) {
053: if (name != null) {
054: cp.newUTF8(name);
055: }
056: if (value instanceof Byte) {
057: cp.newInteger(((Byte) value).byteValue());
058: } else if (value instanceof Boolean) {
059: cp.newInteger(((Boolean) value).booleanValue() ? 1 : 0);
060: } else if (value instanceof Character) {
061: cp.newInteger(((Character) value).charValue());
062: } else if (value instanceof Short) {
063: cp.newInteger(((Short) value).shortValue());
064: } else if (value instanceof Type) {
065: cp.newUTF8(((Type) value).getDescriptor());
066: } else if (value instanceof byte[]) {
067: byte[] v = (byte[]) value;
068: for (int i = 0; i < v.length; i++) {
069: cp.newInteger(v[i]);
070: }
071: } else if (value instanceof boolean[]) {
072: boolean[] v = (boolean[]) value;
073: for (int i = 0; i < v.length; i++) {
074: cp.newInteger(v[i] ? 1 : 0);
075: }
076: } else if (value instanceof short[]) {
077: short[] v = (short[]) value;
078: for (int i = 0; i < v.length; i++) {
079: cp.newInteger(v[i]);
080: }
081: } else if (value instanceof char[]) {
082: char[] v = (char[]) value;
083: for (int i = 0; i < v.length; i++) {
084: cp.newInteger(v[i]);
085: }
086: } else if (value instanceof int[]) {
087: int[] v = (int[]) value;
088: for (int i = 0; i < v.length; i++) {
089: cp.newInteger(v[i]);
090: }
091: } else if (value instanceof long[]) {
092: long[] v = (long[]) value;
093: for (int i = 0; i < v.length; i++) {
094: cp.newLong(v[i]);
095: }
096: } else if (value instanceof float[]) {
097: float[] v = (float[]) value;
098: for (int i = 0; i < v.length; i++) {
099: cp.newFloat(v[i]);
100: }
101: } else if (value instanceof double[]) {
102: double[] v = (double[]) value;
103: for (int i = 0; i < v.length; i++) {
104: cp.newDouble(v[i]);
105: }
106: } else {
107: cp.newConst(value);
108: }
109: av.visit(name, value);
110: }
111:
112: public void visitEnum(final String name, final String desc,
113: final String value) {
114: if (name != null) {
115: cp.newUTF8(name);
116: }
117: cp.newUTF8(desc);
118: cp.newUTF8(value);
119: av.visitEnum(name, desc, value);
120: }
121:
122: public AnnotationVisitor visitAnnotation(final String name,
123: final String desc) {
124: if (name != null) {
125: cp.newUTF8(name);
126: }
127: cp.newUTF8(desc);
128: return new AnnotationConstantsCollector(av.visitAnnotation(
129: name, desc), cp);
130: }
131:
132: public AnnotationVisitor visitArray(final String name) {
133: if (name != null) {
134: cp.newUTF8(name);
135: }
136: return new AnnotationConstantsCollector(av.visitArray(name), cp);
137: }
138:
139: public void visitEnd() {
140: av.visitEnd();
141: }
142: }
|