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