src/share/classes/com/sun/tools/classfile/Attribute.java

changeset 46
7708bd6d800d
child 54
eaf608c64fec
equal deleted inserted replaced
42:f7e64b33d5a4 46:7708bd6d800d
1 /*
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package com.sun.tools.classfile;
27
28 import java.io.IOException;
29 import java.lang.reflect.Constructor;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 /**
34 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
35 * you write code that depends on this, you do so at your own risk.
36 * This code and its internal interfaces are subject to change or
37 * deletion without notice.</b>
38 */
39
40 public abstract class Attribute {
41 public static final String AnnotationDefault = "AnnotationDefault";
42 public static final String CharacterRangeTable = "CharacterRangeTable";
43 public static final String Code = "Code";
44 public static final String ConstantValue = "ConstantValue";
45 public static final String CompilationID = "CompilationID";
46 public static final String Deprecated = "Deprecated";
47 public static final String EnclosingMethod = "EnclosingMethod";
48 public static final String Exceptions = "Exceptions";
49 public static final String InnerClasses = "InnerClasses";
50 public static final String LineNumberTable = "LineNumberTable";
51 public static final String LocalVariableTable = "LocalVariableTable";
52 public static final String LocalVariableTypeTable = "LocalVariableTypeTable";
53 public static final String RuntimeVisibleAnnotations = "RuntimeVisibleAnnotations";
54 public static final String RuntimeInvisibleAnnotations = "RuntimeInvisibleAnnotations";
55 public static final String RuntimeVisibleParameterAnnotations = "RuntimeVisibleParameterAnnotations";
56 public static final String RuntimeInvisibleParameterAnnotations = "RuntimeInvisibleParameterAnnotations";
57 public static final String Signature = "Signature";
58 public static final String SourceDebugExtension = "SourceDebugExtension";
59 public static final String SourceFile = "SourceFile";
60 public static final String SourceID = "SourceID";
61 public static final String StackMap = "StackMap";
62 public static final String StackMapTable = "StackMapTable";
63 public static final String Synthetic = "Synthetic";
64
65 // JSR 277/294
66 public static final String Module = "Module";
67 public static final String ModuleExportTable = "ModuleExportTable";
68 public static final String ModuleMemberTable = "ModuleMemberTable";
69
70 public static class Factory {
71 public Factory() {
72 // defer init of standardAttributeClasses until after options set up
73 }
74
75 public void setCompat(boolean compat) {
76 this.compat = compat;
77 }
78
79 public void setJSR277(boolean jsr277) {
80 this.jsr277 = jsr277;
81 }
82
83 public Attribute createAttribute(ClassReader cr, int name_index, byte[] data)
84 throws IOException {
85 if (standardAttributes == null)
86 init();
87
88 ConstantPool cp = cr.getConstantPool();
89 try {
90 String name = cp.getUTF8Value(name_index);
91 Class<? extends Attribute> attrClass = standardAttributes.get(name);
92 if (attrClass != null) {
93 try {
94 Class<?>[] constrArgTypes = {ClassReader.class, int.class, int.class};
95 Constructor<? extends Attribute> constr = attrClass.getDeclaredConstructor(constrArgTypes);
96 return constr.newInstance(new Object[] { cr, name_index, data.length });
97 } catch (Throwable t) {
98 // fall through and use DefaultAttribute
99 // t.printStackTrace();
100 }
101 }
102 } catch (ConstantPoolException e) {
103 // fall through and use DefaultAttribute
104 }
105 return new DefaultAttribute(cr, name_index, data);
106 }
107
108 protected void init() {
109 standardAttributes = new HashMap<String,Class<? extends Attribute>>();
110 standardAttributes.put(AnnotationDefault, AnnotationDefault_attribute.class);
111 standardAttributes.put(CharacterRangeTable, CharacterRangeTable_attribute.class);
112 standardAttributes.put(Code, Code_attribute.class);
113 standardAttributes.put(ConstantValue, ConstantValue_attribute.class);
114 standardAttributes.put(Deprecated, Deprecated_attribute.class);
115 standardAttributes.put(EnclosingMethod, EnclosingMethod_attribute.class);
116 standardAttributes.put(Exceptions, Exceptions_attribute.class);
117 standardAttributes.put(InnerClasses, InnerClasses_attribute.class);
118 standardAttributes.put(LineNumberTable, LineNumberTable_attribute.class);
119 standardAttributes.put(LocalVariableTable, LocalVariableTable_attribute.class);
120 standardAttributes.put(LocalVariableTypeTable, LocalVariableTypeTable_attribute.class);
121
122 if (jsr277) {
123 standardAttributes.put(Module, Module_attribute.class);
124 standardAttributes.put(ModuleExportTable, ModuleExportTable_attribute.class);
125 standardAttributes.put(ModuleMemberTable, ModuleMemberTable_attribute.class);
126 }
127
128 if (!compat) { // old javap does not recognize recent attributes
129 standardAttributes.put(CompilationID, CompilationID_attribute.class);
130 standardAttributes.put(RuntimeInvisibleAnnotations, RuntimeInvisibleAnnotations_attribute.class);
131 standardAttributes.put(RuntimeInvisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations_attribute.class);
132 standardAttributes.put(RuntimeVisibleAnnotations, RuntimeVisibleAnnotations_attribute.class);
133 standardAttributes.put(RuntimeVisibleParameterAnnotations, RuntimeVisibleParameterAnnotations_attribute.class);
134 standardAttributes.put(Signature, Signature_attribute.class);
135 standardAttributes.put(SourceID, SourceID_attribute.class);
136 }
137
138 standardAttributes.put(SourceDebugExtension, SourceDebugExtension_attribute.class);
139 standardAttributes.put(SourceFile, SourceFile_attribute.class);
140 standardAttributes.put(StackMap, StackMap_attribute.class);
141 standardAttributes.put(StackMapTable, StackMapTable_attribute.class);
142 standardAttributes.put(Synthetic, Synthetic_attribute.class);
143 }
144
145 private Map<String,Class<? extends Attribute>> standardAttributes;
146 private boolean compat; // don't support recent attrs in compatibility mode
147 private boolean jsr277; // support new jsr277 attrs
148 }
149
150 public static Attribute read(ClassReader cr) throws IOException {
151 return cr.readAttribute();
152 }
153
154 protected Attribute(int name_index, int length) {
155 attribute_name_index = name_index;
156 attribute_length = length;
157 }
158
159 public String getName(ConstantPool constant_pool) throws ConstantPoolException {
160 return constant_pool.getUTF8Value(attribute_name_index);
161 }
162
163 public abstract <R,D> R accept(Attribute.Visitor<R,D> visitor, D data);
164
165 public final int attribute_name_index;
166 public final int attribute_length;
167
168
169 public interface Visitor<R,P> {
170 R visitDefault(DefaultAttribute attr, P p);
171 R visitAnnotationDefault(AnnotationDefault_attribute attr, P p);
172 R visitCharacterRangeTable(CharacterRangeTable_attribute attr, P p);
173 R visitCode(Code_attribute attr, P p);
174 R visitCompilationID(CompilationID_attribute attr, P p);
175 R visitConstantValue(ConstantValue_attribute attr, P p);
176 R visitDeprecated(Deprecated_attribute attr, P p);
177 R visitEnclosingMethod(EnclosingMethod_attribute attr, P p);
178 R visitExceptions(Exceptions_attribute attr, P p);
179 R visitInnerClasses(InnerClasses_attribute attr, P p);
180 R visitLineNumberTable(LineNumberTable_attribute attr, P p);
181 R visitLocalVariableTable(LocalVariableTable_attribute attr, P p);
182 R visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, P p);
183 R visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, P p);
184 R visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, P p);
185 R visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, P p);
186 R visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, P p);
187 R visitSignature(Signature_attribute attr, P p);
188 R visitSourceDebugExtension(SourceDebugExtension_attribute attr, P p);
189 R visitSourceFile(SourceFile_attribute attr, P p);
190 R visitSourceID(SourceID_attribute attr, P p);
191 R visitStackMap(StackMap_attribute attr, P p);
192 R visitStackMapTable(StackMapTable_attribute attr, P p);
193 R visitSynthetic(Synthetic_attribute attr, P p);
194
195 R visitModule(Module_attribute attr, P p);
196 R visitModuleExportTable(ModuleExportTable_attribute attr, P p);
197 R visitModuleMemberTable(ModuleMemberTable_attribute attr, P p);
198 }
199 }

mercurial