1 /*
   2  * Copyright (c) 2007, 2018, 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  */
  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 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  */
  39 
  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 MethodParameters         = "MethodParameters";
  55     public static final String Module                   = "Module";
  56     public static final String ModuleHashes             = "ModuleHashes";
  57     public static final String ModuleMainClass          = "ModuleMainClass";
  58     public static final String ModulePackages           = "ModulePackages";
  59     public static final String ModuleResolution         = "ModuleResolution";
  60     public static final String ModuleTarget             = "ModuleTarget";
  61     public static final String NestHost                 = "NestHost";
  62     public static final String NestMembers              = "NestMembers";
  63     public static final String Record                   = "Record";
  64     public static final String RuntimeVisibleAnnotations = "RuntimeVisibleAnnotations";
  65     public static final String RuntimeInvisibleAnnotations = "RuntimeInvisibleAnnotations";
  66     public static final String RuntimeVisibleParameterAnnotations = "RuntimeVisibleParameterAnnotations";
  67     public static final String RuntimeInvisibleParameterAnnotations = "RuntimeInvisibleParameterAnnotations";
  68     public static final String RuntimeVisibleTypeAnnotations = "RuntimeVisibleTypeAnnotations";
  69     public static final String RuntimeInvisibleTypeAnnotations = "RuntimeInvisibleTypeAnnotations";
  70     public static final String Signature                = "Signature";
  71     public static final String SourceDebugExtension     = "SourceDebugExtension";
  72     public static final String SourceFile               = "SourceFile";
  73     public static final String SourceID                 = "SourceID";
  74     public static final String StackMap                 = "StackMap";
  75     public static final String StackMapTable            = "StackMapTable";
  76     public static final String Synthetic                = "Synthetic";
  77 
  78     public static class Factory {
  79         public Factory() {
  80             // defer init of standardAttributeClasses until after options set up
  81         }
  82 
  83         public Attribute createAttribute(ClassReader cr, int name_index, byte[] data)
  84                 throws IOException {
  85             if (standardAttributes == null) {
  86                 init();
  87             }
  88 
  89             ConstantPool cp = cr.getConstantPool();
  90             String reasonForDefaultAttr;
  91             try {
  92                 String name = cp.getUTF8Value(name_index);
  93                 Class<? extends Attribute> attrClass = standardAttributes.get(name);
  94                 if (attrClass != null) {
  95                     try {
  96                         Class<?>[] constrArgTypes = {ClassReader.class, int.class, int.class};
  97                         Constructor<? extends Attribute> constr = attrClass.getDeclaredConstructor(constrArgTypes);
  98                         return constr.newInstance(cr, name_index, data.length);
  99                     } catch (Throwable t) {
 100                         reasonForDefaultAttr = t.toString();
 101                         // fall through and use DefaultAttribute
 102                         // t.printStackTrace();
 103                     }
 104                 } else {
 105                     reasonForDefaultAttr = "unknown attribute";
 106                 }
 107             } catch (ConstantPoolException e) {
 108                 reasonForDefaultAttr = e.toString();
 109                 // fall through and use DefaultAttribute
 110             }
 111             return new DefaultAttribute(cr, name_index, data, reasonForDefaultAttr);
 112         }
 113 
 114         protected void init() {
 115             standardAttributes = new HashMap<>();
 116             standardAttributes.put(AnnotationDefault, AnnotationDefault_attribute.class);
 117             standardAttributes.put(BootstrapMethods,  BootstrapMethods_attribute.class);
 118             standardAttributes.put(CharacterRangeTable, CharacterRangeTable_attribute.class);
 119             standardAttributes.put(Code,              Code_attribute.class);
 120             standardAttributes.put(CompilationID,     CompilationID_attribute.class);
 121             standardAttributes.put(ConstantValue,     ConstantValue_attribute.class);
 122             standardAttributes.put(Deprecated,        Deprecated_attribute.class);
 123             standardAttributes.put(EnclosingMethod,   EnclosingMethod_attribute.class);
 124             standardAttributes.put(Exceptions,        Exceptions_attribute.class);
 125             standardAttributes.put(InnerClasses,      InnerClasses_attribute.class);
 126             standardAttributes.put(LineNumberTable,   LineNumberTable_attribute.class);
 127             standardAttributes.put(LocalVariableTable, LocalVariableTable_attribute.class);
 128             standardAttributes.put(LocalVariableTypeTable, LocalVariableTypeTable_attribute.class);
 129             standardAttributes.put(MethodParameters,  MethodParameters_attribute.class);
 130             standardAttributes.put(Module,            Module_attribute.class);
 131             standardAttributes.put(ModuleHashes,      ModuleHashes_attribute.class);
 132             standardAttributes.put(ModuleMainClass,   ModuleMainClass_attribute.class);
 133             standardAttributes.put(ModulePackages,    ModulePackages_attribute.class);
 134             standardAttributes.put(ModuleResolution,  ModuleResolution_attribute.class);
 135             standardAttributes.put(ModuleTarget,      ModuleTarget_attribute.class);
 136             standardAttributes.put(NestHost, NestHost_attribute.class);
 137             standardAttributes.put(NestMembers, NestMembers_attribute.class);
 138             standardAttributes.put(Record, Record_attribute.class);
 139             standardAttributes.put(RuntimeInvisibleAnnotations, RuntimeInvisibleAnnotations_attribute.class);
 140             standardAttributes.put(RuntimeInvisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations_attribute.class);
 141             standardAttributes.put(RuntimeVisibleAnnotations, RuntimeVisibleAnnotations_attribute.class);
 142             standardAttributes.put(RuntimeVisibleParameterAnnotations, RuntimeVisibleParameterAnnotations_attribute.class);
 143             standardAttributes.put(RuntimeVisibleTypeAnnotations, RuntimeVisibleTypeAnnotations_attribute.class);
 144             standardAttributes.put(RuntimeInvisibleTypeAnnotations, RuntimeInvisibleTypeAnnotations_attribute.class);
 145             standardAttributes.put(Signature,         Signature_attribute.class);
 146             standardAttributes.put(SourceDebugExtension, SourceDebugExtension_attribute.class);
 147             standardAttributes.put(SourceFile,        SourceFile_attribute.class);
 148             standardAttributes.put(SourceID,          SourceID_attribute.class);
 149             standardAttributes.put(StackMap,          StackMap_attribute.class);
 150             standardAttributes.put(StackMapTable,     StackMapTable_attribute.class);
 151             standardAttributes.put(Synthetic,         Synthetic_attribute.class);
 152         }
 153 
 154         private Map<String,Class<? extends Attribute>> standardAttributes;
 155     }
 156 
 157     public static Attribute read(ClassReader cr) throws IOException {
 158         return cr.readAttribute();
 159     }
 160 
 161     protected Attribute(int name_index, int length) {
 162         attribute_name_index = name_index;
 163         attribute_length = length;
 164     }
 165 
 166     public String getName(ConstantPool constant_pool) throws ConstantPoolException {
 167         return constant_pool.getUTF8Value(attribute_name_index);
 168     }
 169 
 170     public abstract <R,D> R accept(Attribute.Visitor<R,D> visitor, D data);
 171 
 172     public int byteLength() {
 173         return 6 + attribute_length;
 174     }
 175 
 176     public final int attribute_name_index;
 177     public final int attribute_length;
 178 
 179 
 180     public interface Visitor<R,P> {
 181         R visitBootstrapMethods(BootstrapMethods_attribute attr, P p);
 182         R visitDefault(DefaultAttribute attr, P p);
 183         R visitAnnotationDefault(AnnotationDefault_attribute attr, P p);
 184         R visitCharacterRangeTable(CharacterRangeTable_attribute attr, P p);
 185         R visitCode(Code_attribute attr, P p);
 186         R visitCompilationID(CompilationID_attribute attr, P p);
 187         R visitConstantValue(ConstantValue_attribute attr, P p);
 188         R visitDeprecated(Deprecated_attribute attr, P p);
 189         R visitEnclosingMethod(EnclosingMethod_attribute attr, P p);
 190         R visitExceptions(Exceptions_attribute attr, P p);
 191         R visitInnerClasses(InnerClasses_attribute attr, P p);
 192         R visitLineNumberTable(LineNumberTable_attribute attr, P p);
 193         R visitLocalVariableTable(LocalVariableTable_attribute attr, P p);
 194         R visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, P p);
 195         R visitMethodParameters(MethodParameters_attribute attr, P p);
 196         R visitModule(Module_attribute attr, P p);
 197         R visitModuleHashes(ModuleHashes_attribute attr, P p);
 198         R visitModuleMainClass(ModuleMainClass_attribute attr, P p);
 199         R visitModulePackages(ModulePackages_attribute attr, P p);
 200         R visitModuleResolution(ModuleResolution_attribute attr, P p);
 201         R visitModuleTarget(ModuleTarget_attribute attr, P p);
 202         R visitNestHost(NestHost_attribute attr, P p);
 203         R visitNestMembers(NestMembers_attribute attr, P p);
 204         R visitRecord(Record_attribute attr, P p);
 205         R visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, P p);
 206         R visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, P p);
 207         R visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, P p);
 208         R visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, P p);
 209         R visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, P p);
 210         R visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, P p);
 211         R visitSignature(Signature_attribute attr, P p);
 212         R visitSourceDebugExtension(SourceDebugExtension_attribute attr, P p);
 213         R visitSourceFile(SourceFile_attribute attr, P p);
 214         R visitSourceID(SourceID_attribute attr, P p);
 215         R visitStackMap(StackMap_attribute attr, P p);
 216         R visitStackMapTable(StackMapTable_attribute attr, P p);
 217         R visitSynthetic(Synthetic_attribute attr, P p);
 218     }
 219 }