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