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

Sat, 29 Dec 2012 17:33:17 -0800

author
jjg
date
Sat, 29 Dec 2012 17:33:17 -0800
changeset 1473
31780dd06ec7
parent 826
5cf6c432ef2f
child 1521
71f35e4b93a5
permissions
-rw-r--r--

8004727: Add compiler support for parameter reflection
Reviewed-by: jjg
Contributed-by: eric.mccorkle@oracle.com

jjg@46 1 /*
jjg@1473 2 * Copyright (c) 2007, 2012, 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@46 59 public static final String Signature = "Signature";
jjg@46 60 public static final String SourceDebugExtension = "SourceDebugExtension";
jjg@46 61 public static final String SourceFile = "SourceFile";
jjg@46 62 public static final String SourceID = "SourceID";
jjg@46 63 public static final String StackMap = "StackMap";
jjg@46 64 public static final String StackMapTable = "StackMapTable";
jjg@46 65 public static final String Synthetic = "Synthetic";
jjg@46 66
jjg@46 67 public static class Factory {
jjg@46 68 public Factory() {
jjg@46 69 // defer init of standardAttributeClasses until after options set up
jjg@46 70 }
jjg@46 71
jjg@46 72 public void setCompat(boolean compat) {
jjg@46 73 this.compat = compat;
jjg@46 74 }
jjg@46 75
jjg@46 76 public Attribute createAttribute(ClassReader cr, int name_index, byte[] data)
jjg@46 77 throws IOException {
jjg@46 78 if (standardAttributes == null)
jjg@46 79 init();
jjg@46 80
jjg@46 81 ConstantPool cp = cr.getConstantPool();
jjg@46 82 try {
jjg@46 83 String name = cp.getUTF8Value(name_index);
jjg@46 84 Class<? extends Attribute> attrClass = standardAttributes.get(name);
jjg@46 85 if (attrClass != null) {
jjg@46 86 try {
jjg@46 87 Class<?>[] constrArgTypes = {ClassReader.class, int.class, int.class};
jjg@46 88 Constructor<? extends Attribute> constr = attrClass.getDeclaredConstructor(constrArgTypes);
jjg@46 89 return constr.newInstance(new Object[] { cr, name_index, data.length });
jjg@46 90 } catch (Throwable t) {
jjg@46 91 // fall through and use DefaultAttribute
jjg@46 92 // t.printStackTrace();
jjg@46 93 }
jjg@46 94 }
jjg@46 95 } catch (ConstantPoolException e) {
jjg@46 96 // fall through and use DefaultAttribute
jjg@46 97 }
jjg@46 98 return new DefaultAttribute(cr, name_index, data);
jjg@46 99 }
jjg@46 100
jjg@46 101 protected void init() {
jjg@46 102 standardAttributes = new HashMap<String,Class<? extends Attribute>>();
jjg@46 103 standardAttributes.put(AnnotationDefault, AnnotationDefault_attribute.class);
ksrini@826 104 standardAttributes.put(BootstrapMethods, BootstrapMethods_attribute.class);
jjg@46 105 standardAttributes.put(CharacterRangeTable, CharacterRangeTable_attribute.class);
jjg@46 106 standardAttributes.put(Code, Code_attribute.class);
jjg@46 107 standardAttributes.put(ConstantValue, ConstantValue_attribute.class);
jjg@46 108 standardAttributes.put(Deprecated, Deprecated_attribute.class);
jjg@46 109 standardAttributes.put(EnclosingMethod, EnclosingMethod_attribute.class);
jjg@46 110 standardAttributes.put(Exceptions, Exceptions_attribute.class);
jjg@46 111 standardAttributes.put(InnerClasses, InnerClasses_attribute.class);
jjg@46 112 standardAttributes.put(LineNumberTable, LineNumberTable_attribute.class);
jjg@46 113 standardAttributes.put(LocalVariableTable, LocalVariableTable_attribute.class);
jjg@46 114 standardAttributes.put(LocalVariableTypeTable, LocalVariableTypeTable_attribute.class);
jjg@46 115
jjg@46 116 if (!compat) { // old javap does not recognize recent attributes
jjg@1473 117 standardAttributes.put(MethodParameters, MethodParameters_attribute.class);
jjg@46 118 standardAttributes.put(CompilationID, CompilationID_attribute.class);
jjg@46 119 standardAttributes.put(RuntimeInvisibleAnnotations, RuntimeInvisibleAnnotations_attribute.class);
jjg@46 120 standardAttributes.put(RuntimeInvisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations_attribute.class);
jjg@46 121 standardAttributes.put(RuntimeVisibleAnnotations, RuntimeVisibleAnnotations_attribute.class);
jjg@46 122 standardAttributes.put(RuntimeVisibleParameterAnnotations, RuntimeVisibleParameterAnnotations_attribute.class);
jjg@46 123 standardAttributes.put(Signature, Signature_attribute.class);
jjg@46 124 standardAttributes.put(SourceID, SourceID_attribute.class);
jjg@46 125 }
jjg@46 126
jjg@46 127 standardAttributes.put(SourceDebugExtension, SourceDebugExtension_attribute.class);
jjg@46 128 standardAttributes.put(SourceFile, SourceFile_attribute.class);
jjg@46 129 standardAttributes.put(StackMap, StackMap_attribute.class);
jjg@46 130 standardAttributes.put(StackMapTable, StackMapTable_attribute.class);
jjg@46 131 standardAttributes.put(Synthetic, Synthetic_attribute.class);
jjg@46 132 }
jjg@46 133
jjg@46 134 private Map<String,Class<? extends Attribute>> standardAttributes;
jjg@46 135 private boolean compat; // don't support recent attrs in compatibility mode
jjg@46 136 }
jjg@46 137
jjg@46 138 public static Attribute read(ClassReader cr) throws IOException {
jjg@46 139 return cr.readAttribute();
jjg@46 140 }
jjg@46 141
jjg@46 142 protected Attribute(int name_index, int length) {
jjg@46 143 attribute_name_index = name_index;
jjg@46 144 attribute_length = length;
jjg@46 145 }
jjg@46 146
jjg@46 147 public String getName(ConstantPool constant_pool) throws ConstantPoolException {
jjg@46 148 return constant_pool.getUTF8Value(attribute_name_index);
jjg@46 149 }
jjg@46 150
jjg@46 151 public abstract <R,D> R accept(Attribute.Visitor<R,D> visitor, D data);
jjg@46 152
jjg@345 153 public int byteLength() {
jjg@345 154 return 6 + attribute_length;
jjg@345 155 }
jjg@345 156
jjg@46 157 public final int attribute_name_index;
jjg@46 158 public final int attribute_length;
jjg@46 159
jjg@46 160
jjg@46 161 public interface Visitor<R,P> {
ksrini@826 162 R visitBootstrapMethods(BootstrapMethods_attribute attr, P p);
jjg@46 163 R visitDefault(DefaultAttribute attr, P p);
jjg@46 164 R visitAnnotationDefault(AnnotationDefault_attribute attr, P p);
jjg@46 165 R visitCharacterRangeTable(CharacterRangeTable_attribute attr, P p);
jjg@46 166 R visitCode(Code_attribute attr, P p);
jjg@46 167 R visitCompilationID(CompilationID_attribute attr, P p);
jjg@46 168 R visitConstantValue(ConstantValue_attribute attr, P p);
jjg@46 169 R visitDeprecated(Deprecated_attribute attr, P p);
jjg@46 170 R visitEnclosingMethod(EnclosingMethod_attribute attr, P p);
jjg@46 171 R visitExceptions(Exceptions_attribute attr, P p);
jjg@46 172 R visitInnerClasses(InnerClasses_attribute attr, P p);
jjg@46 173 R visitLineNumberTable(LineNumberTable_attribute attr, P p);
jjg@46 174 R visitLocalVariableTable(LocalVariableTable_attribute attr, P p);
jjg@46 175 R visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, P p);
jjg@1473 176 R visitMethodParameters(MethodParameters_attribute attr, P p);
jjg@46 177 R visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, P p);
jjg@46 178 R visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, P p);
jjg@46 179 R visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, P p);
jjg@46 180 R visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, P p);
jjg@46 181 R visitSignature(Signature_attribute attr, P p);
jjg@46 182 R visitSourceDebugExtension(SourceDebugExtension_attribute attr, P p);
jjg@46 183 R visitSourceFile(SourceFile_attribute attr, P p);
jjg@46 184 R visitSourceID(SourceID_attribute attr, P p);
jjg@46 185 R visitStackMap(StackMap_attribute attr, P p);
jjg@46 186 R visitStackMapTable(StackMapTable_attribute attr, P p);
jjg@46 187 R visitSynthetic(Synthetic_attribute attr, P p);
jjg@46 188 }
jjg@46 189 }

mercurial