Sun, 16 Dec 2012 11:09:36 +0100
8005098: Provide isSynthesized() information on Attribute.Compound
Reviewed-by: jjg
1 /*
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.classfile;
28 import java.io.IOException;
29 import java.lang.reflect.Constructor;
30 import java.util.HashMap;
31 import java.util.Map;
33 /**
34 * <p><b>This is NOT part of any supported API.
35 * If 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 */
40 public abstract class Attribute {
41 public static final String AnnotationDefault = "AnnotationDefault";
42 public static final String BootstrapMethods = "BootstrapMethods";
43 public static final String CharacterRangeTable = "CharacterRangeTable";
44 public static final String Code = "Code";
45 public static final String ConstantValue = "ConstantValue";
46 public static final String CompilationID = "CompilationID";
47 public static final String Deprecated = "Deprecated";
48 public static final String EnclosingMethod = "EnclosingMethod";
49 public static final String Exceptions = "Exceptions";
50 public static final String InnerClasses = "InnerClasses";
51 public static final String LineNumberTable = "LineNumberTable";
52 public static final String LocalVariableTable = "LocalVariableTable";
53 public static final String LocalVariableTypeTable = "LocalVariableTypeTable";
54 public static final String RuntimeVisibleAnnotations = "RuntimeVisibleAnnotations";
55 public static final String RuntimeInvisibleAnnotations = "RuntimeInvisibleAnnotations";
56 public static final String RuntimeVisibleParameterAnnotations = "RuntimeVisibleParameterAnnotations";
57 public static final String RuntimeInvisibleParameterAnnotations = "RuntimeInvisibleParameterAnnotations";
58 public static final String Signature = "Signature";
59 public static final String SourceDebugExtension = "SourceDebugExtension";
60 public static final String SourceFile = "SourceFile";
61 public static final String SourceID = "SourceID";
62 public static final String StackMap = "StackMap";
63 public static final String StackMapTable = "StackMapTable";
64 public static final String Synthetic = "Synthetic";
66 public static class Factory {
67 public Factory() {
68 // defer init of standardAttributeClasses until after options set up
69 }
71 public void setCompat(boolean compat) {
72 this.compat = compat;
73 }
75 public Attribute createAttribute(ClassReader cr, int name_index, byte[] data)
76 throws IOException {
77 if (standardAttributes == null)
78 init();
80 ConstantPool cp = cr.getConstantPool();
81 try {
82 String name = cp.getUTF8Value(name_index);
83 Class<? extends Attribute> attrClass = standardAttributes.get(name);
84 if (attrClass != null) {
85 try {
86 Class<?>[] constrArgTypes = {ClassReader.class, int.class, int.class};
87 Constructor<? extends Attribute> constr = attrClass.getDeclaredConstructor(constrArgTypes);
88 return constr.newInstance(new Object[] { cr, name_index, data.length });
89 } catch (Throwable t) {
90 // fall through and use DefaultAttribute
91 // t.printStackTrace();
92 }
93 }
94 } catch (ConstantPoolException e) {
95 // fall through and use DefaultAttribute
96 }
97 return new DefaultAttribute(cr, name_index, data);
98 }
100 protected void init() {
101 standardAttributes = new HashMap<String,Class<? extends Attribute>>();
102 standardAttributes.put(AnnotationDefault, AnnotationDefault_attribute.class);
103 standardAttributes.put(BootstrapMethods, BootstrapMethods_attribute.class);
104 standardAttributes.put(CharacterRangeTable, CharacterRangeTable_attribute.class);
105 standardAttributes.put(Code, Code_attribute.class);
106 standardAttributes.put(ConstantValue, ConstantValue_attribute.class);
107 standardAttributes.put(Deprecated, Deprecated_attribute.class);
108 standardAttributes.put(EnclosingMethod, EnclosingMethod_attribute.class);
109 standardAttributes.put(Exceptions, Exceptions_attribute.class);
110 standardAttributes.put(InnerClasses, InnerClasses_attribute.class);
111 standardAttributes.put(LineNumberTable, LineNumberTable_attribute.class);
112 standardAttributes.put(LocalVariableTable, LocalVariableTable_attribute.class);
113 standardAttributes.put(LocalVariableTypeTable, LocalVariableTypeTable_attribute.class);
115 if (!compat) { // old javap does not recognize recent attributes
116 standardAttributes.put(CompilationID, CompilationID_attribute.class);
117 standardAttributes.put(RuntimeInvisibleAnnotations, RuntimeInvisibleAnnotations_attribute.class);
118 standardAttributes.put(RuntimeInvisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations_attribute.class);
119 standardAttributes.put(RuntimeVisibleAnnotations, RuntimeVisibleAnnotations_attribute.class);
120 standardAttributes.put(RuntimeVisibleParameterAnnotations, RuntimeVisibleParameterAnnotations_attribute.class);
121 standardAttributes.put(Signature, Signature_attribute.class);
122 standardAttributes.put(SourceID, SourceID_attribute.class);
123 }
125 standardAttributes.put(SourceDebugExtension, SourceDebugExtension_attribute.class);
126 standardAttributes.put(SourceFile, SourceFile_attribute.class);
127 standardAttributes.put(StackMap, StackMap_attribute.class);
128 standardAttributes.put(StackMapTable, StackMapTable_attribute.class);
129 standardAttributes.put(Synthetic, Synthetic_attribute.class);
130 }
132 private Map<String,Class<? extends Attribute>> standardAttributes;
133 private boolean compat; // don't support recent attrs in compatibility mode
134 }
136 public static Attribute read(ClassReader cr) throws IOException {
137 return cr.readAttribute();
138 }
140 protected Attribute(int name_index, int length) {
141 attribute_name_index = name_index;
142 attribute_length = length;
143 }
145 public String getName(ConstantPool constant_pool) throws ConstantPoolException {
146 return constant_pool.getUTF8Value(attribute_name_index);
147 }
149 public abstract <R,D> R accept(Attribute.Visitor<R,D> visitor, D data);
151 public int byteLength() {
152 return 6 + attribute_length;
153 }
155 public final int attribute_name_index;
156 public final int attribute_length;
159 public interface Visitor<R,P> {
160 R visitBootstrapMethods(BootstrapMethods_attribute attr, P p);
161 R visitDefault(DefaultAttribute attr, P p);
162 R visitAnnotationDefault(AnnotationDefault_attribute attr, P p);
163 R visitCharacterRangeTable(CharacterRangeTable_attribute attr, P p);
164 R visitCode(Code_attribute attr, P p);
165 R visitCompilationID(CompilationID_attribute attr, P p);
166 R visitConstantValue(ConstantValue_attribute attr, P p);
167 R visitDeprecated(Deprecated_attribute attr, P p);
168 R visitEnclosingMethod(EnclosingMethod_attribute attr, P p);
169 R visitExceptions(Exceptions_attribute attr, P p);
170 R visitInnerClasses(InnerClasses_attribute attr, P p);
171 R visitLineNumberTable(LineNumberTable_attribute attr, P p);
172 R visitLocalVariableTable(LocalVariableTable_attribute attr, P p);
173 R visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, P p);
174 R visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, P p);
175 R visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, P p);
176 R visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, P p);
177 R visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, P p);
178 R visitSignature(Signature_attribute attr, P p);
179 R visitSourceDebugExtension(SourceDebugExtension_attribute attr, P p);
180 R visitSourceFile(SourceFile_attribute attr, P p);
181 R visitSourceID(SourceID_attribute attr, P p);
182 R visitStackMap(StackMap_attribute attr, P p);
183 R visitStackMapTable(StackMapTable_attribute attr, P p);
184 R visitSynthetic(Synthetic_attribute attr, P p);
185 }
186 }