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

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 1473
31780dd06ec7
child 1874
891c5ecb8306
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

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

mercurial