Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/classfile/Attribute.java
38899 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.classfile;2627import java.io.IOException;28import java.lang.reflect.Constructor;29import java.util.HashMap;30import java.util.Map;3132/**33* <p><b>This is NOT part of any supported API.34* If you write code that depends on this, you do so at your own risk.35* This code and its internal interfaces are subject to change or36* deletion without notice.</b>37*/3839public abstract class Attribute {40public static final String AnnotationDefault = "AnnotationDefault";41public static final String BootstrapMethods = "BootstrapMethods";42public static final String CharacterRangeTable = "CharacterRangeTable";43public static final String Code = "Code";44public static final String ConstantValue = "ConstantValue";45public static final String CompilationID = "CompilationID";46public static final String Deprecated = "Deprecated";47public static final String EnclosingMethod = "EnclosingMethod";48public static final String Exceptions = "Exceptions";49public static final String InnerClasses = "InnerClasses";50public static final String LineNumberTable = "LineNumberTable";51public static final String LocalVariableTable = "LocalVariableTable";52public static final String LocalVariableTypeTable = "LocalVariableTypeTable";53public static final String MethodParameters = "MethodParameters";54public static final String RuntimeVisibleAnnotations = "RuntimeVisibleAnnotations";55public static final String RuntimeInvisibleAnnotations = "RuntimeInvisibleAnnotations";56public static final String RuntimeVisibleParameterAnnotations = "RuntimeVisibleParameterAnnotations";57public static final String RuntimeInvisibleParameterAnnotations = "RuntimeInvisibleParameterAnnotations";58public static final String RuntimeVisibleTypeAnnotations = "RuntimeVisibleTypeAnnotations";59public static final String RuntimeInvisibleTypeAnnotations = "RuntimeInvisibleTypeAnnotations";60public static final String Signature = "Signature";61public static final String SourceDebugExtension = "SourceDebugExtension";62public static final String SourceFile = "SourceFile";63public static final String SourceID = "SourceID";64public static final String StackMap = "StackMap";65public static final String StackMapTable = "StackMapTable";66public static final String Synthetic = "Synthetic";6768public static class Factory {69public Factory() {70// defer init of standardAttributeClasses until after options set up71}7273public Attribute createAttribute(ClassReader cr, int name_index, byte[] data)74throws IOException {75if (standardAttributes == null) {76init();77}7879ConstantPool cp = cr.getConstantPool();80String reasonForDefaultAttr;81try {82String name = cp.getUTF8Value(name_index);83Class<? extends Attribute> attrClass = standardAttributes.get(name);84if (attrClass != null) {85try {86Class<?>[] constrArgTypes = {ClassReader.class, int.class, int.class};87Constructor<? extends Attribute> constr = attrClass.getDeclaredConstructor(constrArgTypes);88return constr.newInstance(new Object[] { cr, name_index, data.length });89} catch (Throwable t) {90reasonForDefaultAttr = t.toString();91// fall through and use DefaultAttribute92// t.printStackTrace();93}94} else {95reasonForDefaultAttr = "unknown attribute";96}97} catch (ConstantPoolException e) {98reasonForDefaultAttr = e.toString();99// fall through and use DefaultAttribute100}101return new DefaultAttribute(cr, name_index, data, reasonForDefaultAttr);102}103104protected void init() {105standardAttributes = new HashMap<String,Class<? extends Attribute>>();106standardAttributes.put(AnnotationDefault, AnnotationDefault_attribute.class);107standardAttributes.put(BootstrapMethods, BootstrapMethods_attribute.class);108standardAttributes.put(CharacterRangeTable, CharacterRangeTable_attribute.class);109standardAttributes.put(Code, Code_attribute.class);110standardAttributes.put(CompilationID, CompilationID_attribute.class);111standardAttributes.put(ConstantValue, ConstantValue_attribute.class);112standardAttributes.put(Deprecated, Deprecated_attribute.class);113standardAttributes.put(EnclosingMethod, EnclosingMethod_attribute.class);114standardAttributes.put(Exceptions, Exceptions_attribute.class);115standardAttributes.put(InnerClasses, InnerClasses_attribute.class);116standardAttributes.put(LineNumberTable, LineNumberTable_attribute.class);117standardAttributes.put(LocalVariableTable, LocalVariableTable_attribute.class);118standardAttributes.put(LocalVariableTypeTable, LocalVariableTypeTable_attribute.class);119standardAttributes.put(MethodParameters, MethodParameters_attribute.class);120standardAttributes.put(RuntimeInvisibleAnnotations, RuntimeInvisibleAnnotations_attribute.class);121standardAttributes.put(RuntimeInvisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations_attribute.class);122standardAttributes.put(RuntimeVisibleAnnotations, RuntimeVisibleAnnotations_attribute.class);123standardAttributes.put(RuntimeVisibleParameterAnnotations, RuntimeVisibleParameterAnnotations_attribute.class);124standardAttributes.put(RuntimeVisibleTypeAnnotations, RuntimeVisibleTypeAnnotations_attribute.class);125standardAttributes.put(RuntimeInvisibleTypeAnnotations, RuntimeInvisibleTypeAnnotations_attribute.class);126standardAttributes.put(Signature, Signature_attribute.class);127standardAttributes.put(SourceDebugExtension, SourceDebugExtension_attribute.class);128standardAttributes.put(SourceFile, SourceFile_attribute.class);129standardAttributes.put(SourceID, SourceID_attribute.class);130standardAttributes.put(StackMap, StackMap_attribute.class);131standardAttributes.put(StackMapTable, StackMapTable_attribute.class);132standardAttributes.put(Synthetic, Synthetic_attribute.class);133}134135private Map<String,Class<? extends Attribute>> standardAttributes;136}137138public static Attribute read(ClassReader cr) throws IOException {139return cr.readAttribute();140}141142protected Attribute(int name_index, int length) {143attribute_name_index = name_index;144attribute_length = length;145}146147public String getName(ConstantPool constant_pool) throws ConstantPoolException {148return constant_pool.getUTF8Value(attribute_name_index);149}150151public abstract <R,D> R accept(Attribute.Visitor<R,D> visitor, D data);152153public int byteLength() {154return 6 + attribute_length;155}156157public final int attribute_name_index;158public final int attribute_length;159160161public interface Visitor<R,P> {162R visitBootstrapMethods(BootstrapMethods_attribute attr, P p);163R visitDefault(DefaultAttribute attr, P p);164R visitAnnotationDefault(AnnotationDefault_attribute attr, P p);165R visitCharacterRangeTable(CharacterRangeTable_attribute attr, P p);166R visitCode(Code_attribute attr, P p);167R visitCompilationID(CompilationID_attribute attr, P p);168R visitConstantValue(ConstantValue_attribute attr, P p);169R visitDeprecated(Deprecated_attribute attr, P p);170R visitEnclosingMethod(EnclosingMethod_attribute attr, P p);171R visitExceptions(Exceptions_attribute attr, P p);172R visitInnerClasses(InnerClasses_attribute attr, P p);173R visitLineNumberTable(LineNumberTable_attribute attr, P p);174R visitLocalVariableTable(LocalVariableTable_attribute attr, P p);175R visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, P p);176R visitMethodParameters(MethodParameters_attribute attr, P p);177R visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, P p);178R visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, P p);179R visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, P p);180R visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, P p);181R visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, P p);182R visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, P p);183R visitSignature(Signature_attribute attr, P p);184R visitSourceDebugExtension(SourceDebugExtension_attribute attr, P p);185R visitSourceFile(SourceFile_attribute attr, P p);186R visitSourceID(SourceID_attribute attr, P p);187R visitStackMap(StackMap_attribute attr, P p);188R visitStackMapTable(StackMapTable_attribute attr, P p);189R visitSynthetic(Synthetic_attribute attr, P p);190}191}192193194