1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/classfile/Attribute.java Tue Jun 03 13:26:47 2008 -0700 1.3 @@ -0,0 +1,199 @@ 1.4 +/* 1.5 + * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.classfile; 1.30 + 1.31 +import java.io.IOException; 1.32 +import java.lang.reflect.Constructor; 1.33 +import java.util.HashMap; 1.34 +import java.util.Map; 1.35 + 1.36 +/** 1.37 + * <p><b>This is NOT part of any API supported by Sun Microsystems. If 1.38 + * you write code that depends on this, you do so at your own risk. 1.39 + * This code and its internal interfaces are subject to change or 1.40 + * deletion without notice.</b> 1.41 + */ 1.42 + 1.43 +public abstract class Attribute { 1.44 + public static final String AnnotationDefault = "AnnotationDefault"; 1.45 + public static final String CharacterRangeTable = "CharacterRangeTable"; 1.46 + public static final String Code = "Code"; 1.47 + public static final String ConstantValue = "ConstantValue"; 1.48 + public static final String CompilationID = "CompilationID"; 1.49 + public static final String Deprecated = "Deprecated"; 1.50 + public static final String EnclosingMethod = "EnclosingMethod"; 1.51 + public static final String Exceptions = "Exceptions"; 1.52 + public static final String InnerClasses = "InnerClasses"; 1.53 + public static final String LineNumberTable = "LineNumberTable"; 1.54 + public static final String LocalVariableTable = "LocalVariableTable"; 1.55 + public static final String LocalVariableTypeTable = "LocalVariableTypeTable"; 1.56 + public static final String RuntimeVisibleAnnotations = "RuntimeVisibleAnnotations"; 1.57 + public static final String RuntimeInvisibleAnnotations = "RuntimeInvisibleAnnotations"; 1.58 + public static final String RuntimeVisibleParameterAnnotations = "RuntimeVisibleParameterAnnotations"; 1.59 + public static final String RuntimeInvisibleParameterAnnotations = "RuntimeInvisibleParameterAnnotations"; 1.60 + public static final String Signature = "Signature"; 1.61 + public static final String SourceDebugExtension = "SourceDebugExtension"; 1.62 + public static final String SourceFile = "SourceFile"; 1.63 + public static final String SourceID = "SourceID"; 1.64 + public static final String StackMap = "StackMap"; 1.65 + public static final String StackMapTable = "StackMapTable"; 1.66 + public static final String Synthetic = "Synthetic"; 1.67 + 1.68 + // JSR 277/294 1.69 + public static final String Module = "Module"; 1.70 + public static final String ModuleExportTable = "ModuleExportTable"; 1.71 + public static final String ModuleMemberTable = "ModuleMemberTable"; 1.72 + 1.73 + public static class Factory { 1.74 + public Factory() { 1.75 + // defer init of standardAttributeClasses until after options set up 1.76 + } 1.77 + 1.78 + public void setCompat(boolean compat) { 1.79 + this.compat = compat; 1.80 + } 1.81 + 1.82 + public void setJSR277(boolean jsr277) { 1.83 + this.jsr277 = jsr277; 1.84 + } 1.85 + 1.86 + public Attribute createAttribute(ClassReader cr, int name_index, byte[] data) 1.87 + throws IOException { 1.88 + if (standardAttributes == null) 1.89 + init(); 1.90 + 1.91 + ConstantPool cp = cr.getConstantPool(); 1.92 + try { 1.93 + String name = cp.getUTF8Value(name_index); 1.94 + Class<? extends Attribute> attrClass = standardAttributes.get(name); 1.95 + if (attrClass != null) { 1.96 + try { 1.97 + Class<?>[] constrArgTypes = {ClassReader.class, int.class, int.class}; 1.98 + Constructor<? extends Attribute> constr = attrClass.getDeclaredConstructor(constrArgTypes); 1.99 + return constr.newInstance(new Object[] { cr, name_index, data.length }); 1.100 + } catch (Throwable t) { 1.101 + // fall through and use DefaultAttribute 1.102 + // t.printStackTrace(); 1.103 + } 1.104 + } 1.105 + } catch (ConstantPoolException e) { 1.106 + // fall through and use DefaultAttribute 1.107 + } 1.108 + return new DefaultAttribute(cr, name_index, data); 1.109 + } 1.110 + 1.111 + protected void init() { 1.112 + standardAttributes = new HashMap<String,Class<? extends Attribute>>(); 1.113 + standardAttributes.put(AnnotationDefault, AnnotationDefault_attribute.class); 1.114 + standardAttributes.put(CharacterRangeTable, CharacterRangeTable_attribute.class); 1.115 + standardAttributes.put(Code, Code_attribute.class); 1.116 + standardAttributes.put(ConstantValue, ConstantValue_attribute.class); 1.117 + standardAttributes.put(Deprecated, Deprecated_attribute.class); 1.118 + standardAttributes.put(EnclosingMethod, EnclosingMethod_attribute.class); 1.119 + standardAttributes.put(Exceptions, Exceptions_attribute.class); 1.120 + standardAttributes.put(InnerClasses, InnerClasses_attribute.class); 1.121 + standardAttributes.put(LineNumberTable, LineNumberTable_attribute.class); 1.122 + standardAttributes.put(LocalVariableTable, LocalVariableTable_attribute.class); 1.123 + standardAttributes.put(LocalVariableTypeTable, LocalVariableTypeTable_attribute.class); 1.124 + 1.125 + if (jsr277) { 1.126 + standardAttributes.put(Module, Module_attribute.class); 1.127 + standardAttributes.put(ModuleExportTable, ModuleExportTable_attribute.class); 1.128 + standardAttributes.put(ModuleMemberTable, ModuleMemberTable_attribute.class); 1.129 + } 1.130 + 1.131 + if (!compat) { // old javap does not recognize recent attributes 1.132 + standardAttributes.put(CompilationID, CompilationID_attribute.class); 1.133 + standardAttributes.put(RuntimeInvisibleAnnotations, RuntimeInvisibleAnnotations_attribute.class); 1.134 + standardAttributes.put(RuntimeInvisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations_attribute.class); 1.135 + standardAttributes.put(RuntimeVisibleAnnotations, RuntimeVisibleAnnotations_attribute.class); 1.136 + standardAttributes.put(RuntimeVisibleParameterAnnotations, RuntimeVisibleParameterAnnotations_attribute.class); 1.137 + standardAttributes.put(Signature, Signature_attribute.class); 1.138 + standardAttributes.put(SourceID, SourceID_attribute.class); 1.139 + } 1.140 + 1.141 + standardAttributes.put(SourceDebugExtension, SourceDebugExtension_attribute.class); 1.142 + standardAttributes.put(SourceFile, SourceFile_attribute.class); 1.143 + standardAttributes.put(StackMap, StackMap_attribute.class); 1.144 + standardAttributes.put(StackMapTable, StackMapTable_attribute.class); 1.145 + standardAttributes.put(Synthetic, Synthetic_attribute.class); 1.146 + } 1.147 + 1.148 + private Map<String,Class<? extends Attribute>> standardAttributes; 1.149 + private boolean compat; // don't support recent attrs in compatibility mode 1.150 + private boolean jsr277; // support new jsr277 attrs 1.151 + } 1.152 + 1.153 + public static Attribute read(ClassReader cr) throws IOException { 1.154 + return cr.readAttribute(); 1.155 + } 1.156 + 1.157 + protected Attribute(int name_index, int length) { 1.158 + attribute_name_index = name_index; 1.159 + attribute_length = length; 1.160 + } 1.161 + 1.162 + public String getName(ConstantPool constant_pool) throws ConstantPoolException { 1.163 + return constant_pool.getUTF8Value(attribute_name_index); 1.164 + } 1.165 + 1.166 + public abstract <R,D> R accept(Attribute.Visitor<R,D> visitor, D data); 1.167 + 1.168 + public final int attribute_name_index; 1.169 + public final int attribute_length; 1.170 + 1.171 + 1.172 + public interface Visitor<R,P> { 1.173 + R visitDefault(DefaultAttribute attr, P p); 1.174 + R visitAnnotationDefault(AnnotationDefault_attribute attr, P p); 1.175 + R visitCharacterRangeTable(CharacterRangeTable_attribute attr, P p); 1.176 + R visitCode(Code_attribute attr, P p); 1.177 + R visitCompilationID(CompilationID_attribute attr, P p); 1.178 + R visitConstantValue(ConstantValue_attribute attr, P p); 1.179 + R visitDeprecated(Deprecated_attribute attr, P p); 1.180 + R visitEnclosingMethod(EnclosingMethod_attribute attr, P p); 1.181 + R visitExceptions(Exceptions_attribute attr, P p); 1.182 + R visitInnerClasses(InnerClasses_attribute attr, P p); 1.183 + R visitLineNumberTable(LineNumberTable_attribute attr, P p); 1.184 + R visitLocalVariableTable(LocalVariableTable_attribute attr, P p); 1.185 + R visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, P p); 1.186 + R visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, P p); 1.187 + R visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, P p); 1.188 + R visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, P p); 1.189 + R visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, P p); 1.190 + R visitSignature(Signature_attribute attr, P p); 1.191 + R visitSourceDebugExtension(SourceDebugExtension_attribute attr, P p); 1.192 + R visitSourceFile(SourceFile_attribute attr, P p); 1.193 + R visitSourceID(SourceID_attribute attr, P p); 1.194 + R visitStackMap(StackMap_attribute attr, P p); 1.195 + R visitStackMapTable(StackMapTable_attribute attr, P p); 1.196 + R visitSynthetic(Synthetic_attribute attr, P p); 1.197 + 1.198 + R visitModule(Module_attribute attr, P p); 1.199 + R visitModuleExportTable(ModuleExportTable_attribute attr, P p); 1.200 + R visitModuleMemberTable(ModuleMemberTable_attribute attr, P p); 1.201 + } 1.202 +}