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

Tue, 03 Jun 2008 13:26:47 -0700

author
jjg
date
Tue, 03 Jun 2008 13:26:47 -0700
changeset 46
7708bd6d800d
child 54
eaf608c64fec
permissions
-rw-r--r--

4075303: Use javap to enquire aboput a specific inner class
4348375: Javap is not internationalized
4459541: "javap -l" shows line numbers as signed short; they should be unsigned
4501660: change diagnostic of -help as 'print this help message and exit'
4776241: unused source file in javap...
4870651: javap should recognize generics, varargs, enum
4876942: javap invoked without args does not print help screen
4880663: javap could output whitespace between class name and opening brace
4975569: javap doesn't print new flag bits
6271787: javap dumps LocalVariableTypeTable attribute in hex, needs to print a table
6305779: javap: support annotations
6439940: Clean up javap implementation
6469569: wrong check of searchpath in JavapEnvironment
6474890: javap does not open .zip files in -classpath
6587786: Javap throws error : "ERROR:Could not find <classname>" for JRE classes
6622215: javap ignores certain relevant access flags
6622216: javap names some attributes incorrectly
6622232: javap gets whitespace confused
6622260: javap prints negative bytes incorrectly in hex
Reviewed-by: ksrini

jjg@46 1 /*
jjg@46 2 * Copyright 2007 Sun Microsystems, Inc. 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
jjg@46 7 * published by the Free Software Foundation. Sun designates this
jjg@46 8 * particular file as subject to the "Classpath" exception as provided
jjg@46 9 * by Sun 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 *
jjg@46 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@46 22 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@46 23 * have any 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@46 34 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@46 35 * 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";
jjg@46 42 public static final String CharacterRangeTable = "CharacterRangeTable";
jjg@46 43 public static final String Code = "Code";
jjg@46 44 public static final String ConstantValue = "ConstantValue";
jjg@46 45 public static final String CompilationID = "CompilationID";
jjg@46 46 public static final String Deprecated = "Deprecated";
jjg@46 47 public static final String EnclosingMethod = "EnclosingMethod";
jjg@46 48 public static final String Exceptions = "Exceptions";
jjg@46 49 public static final String InnerClasses = "InnerClasses";
jjg@46 50 public static final String LineNumberTable = "LineNumberTable";
jjg@46 51 public static final String LocalVariableTable = "LocalVariableTable";
jjg@46 52 public static final String LocalVariableTypeTable = "LocalVariableTypeTable";
jjg@46 53 public static final String RuntimeVisibleAnnotations = "RuntimeVisibleAnnotations";
jjg@46 54 public static final String RuntimeInvisibleAnnotations = "RuntimeInvisibleAnnotations";
jjg@46 55 public static final String RuntimeVisibleParameterAnnotations = "RuntimeVisibleParameterAnnotations";
jjg@46 56 public static final String RuntimeInvisibleParameterAnnotations = "RuntimeInvisibleParameterAnnotations";
jjg@46 57 public static final String Signature = "Signature";
jjg@46 58 public static final String SourceDebugExtension = "SourceDebugExtension";
jjg@46 59 public static final String SourceFile = "SourceFile";
jjg@46 60 public static final String SourceID = "SourceID";
jjg@46 61 public static final String StackMap = "StackMap";
jjg@46 62 public static final String StackMapTable = "StackMapTable";
jjg@46 63 public static final String Synthetic = "Synthetic";
jjg@46 64
jjg@46 65 // JSR 277/294
jjg@46 66 public static final String Module = "Module";
jjg@46 67 public static final String ModuleExportTable = "ModuleExportTable";
jjg@46 68 public static final String ModuleMemberTable = "ModuleMemberTable";
jjg@46 69
jjg@46 70 public static class Factory {
jjg@46 71 public Factory() {
jjg@46 72 // defer init of standardAttributeClasses until after options set up
jjg@46 73 }
jjg@46 74
jjg@46 75 public void setCompat(boolean compat) {
jjg@46 76 this.compat = compat;
jjg@46 77 }
jjg@46 78
jjg@46 79 public void setJSR277(boolean jsr277) {
jjg@46 80 this.jsr277 = jsr277;
jjg@46 81 }
jjg@46 82
jjg@46 83 public Attribute createAttribute(ClassReader cr, int name_index, byte[] data)
jjg@46 84 throws IOException {
jjg@46 85 if (standardAttributes == null)
jjg@46 86 init();
jjg@46 87
jjg@46 88 ConstantPool cp = cr.getConstantPool();
jjg@46 89 try {
jjg@46 90 String name = cp.getUTF8Value(name_index);
jjg@46 91 Class<? extends Attribute> attrClass = standardAttributes.get(name);
jjg@46 92 if (attrClass != null) {
jjg@46 93 try {
jjg@46 94 Class<?>[] constrArgTypes = {ClassReader.class, int.class, int.class};
jjg@46 95 Constructor<? extends Attribute> constr = attrClass.getDeclaredConstructor(constrArgTypes);
jjg@46 96 return constr.newInstance(new Object[] { cr, name_index, data.length });
jjg@46 97 } catch (Throwable t) {
jjg@46 98 // fall through and use DefaultAttribute
jjg@46 99 // t.printStackTrace();
jjg@46 100 }
jjg@46 101 }
jjg@46 102 } catch (ConstantPoolException e) {
jjg@46 103 // fall through and use DefaultAttribute
jjg@46 104 }
jjg@46 105 return new DefaultAttribute(cr, name_index, data);
jjg@46 106 }
jjg@46 107
jjg@46 108 protected void init() {
jjg@46 109 standardAttributes = new HashMap<String,Class<? extends Attribute>>();
jjg@46 110 standardAttributes.put(AnnotationDefault, AnnotationDefault_attribute.class);
jjg@46 111 standardAttributes.put(CharacterRangeTable, CharacterRangeTable_attribute.class);
jjg@46 112 standardAttributes.put(Code, Code_attribute.class);
jjg@46 113 standardAttributes.put(ConstantValue, ConstantValue_attribute.class);
jjg@46 114 standardAttributes.put(Deprecated, Deprecated_attribute.class);
jjg@46 115 standardAttributes.put(EnclosingMethod, EnclosingMethod_attribute.class);
jjg@46 116 standardAttributes.put(Exceptions, Exceptions_attribute.class);
jjg@46 117 standardAttributes.put(InnerClasses, InnerClasses_attribute.class);
jjg@46 118 standardAttributes.put(LineNumberTable, LineNumberTable_attribute.class);
jjg@46 119 standardAttributes.put(LocalVariableTable, LocalVariableTable_attribute.class);
jjg@46 120 standardAttributes.put(LocalVariableTypeTable, LocalVariableTypeTable_attribute.class);
jjg@46 121
jjg@46 122 if (jsr277) {
jjg@46 123 standardAttributes.put(Module, Module_attribute.class);
jjg@46 124 standardAttributes.put(ModuleExportTable, ModuleExportTable_attribute.class);
jjg@46 125 standardAttributes.put(ModuleMemberTable, ModuleMemberTable_attribute.class);
jjg@46 126 }
jjg@46 127
jjg@46 128 if (!compat) { // old javap does not recognize recent attributes
jjg@46 129 standardAttributes.put(CompilationID, CompilationID_attribute.class);
jjg@46 130 standardAttributes.put(RuntimeInvisibleAnnotations, RuntimeInvisibleAnnotations_attribute.class);
jjg@46 131 standardAttributes.put(RuntimeInvisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations_attribute.class);
jjg@46 132 standardAttributes.put(RuntimeVisibleAnnotations, RuntimeVisibleAnnotations_attribute.class);
jjg@46 133 standardAttributes.put(RuntimeVisibleParameterAnnotations, RuntimeVisibleParameterAnnotations_attribute.class);
jjg@46 134 standardAttributes.put(Signature, Signature_attribute.class);
jjg@46 135 standardAttributes.put(SourceID, SourceID_attribute.class);
jjg@46 136 }
jjg@46 137
jjg@46 138 standardAttributes.put(SourceDebugExtension, SourceDebugExtension_attribute.class);
jjg@46 139 standardAttributes.put(SourceFile, SourceFile_attribute.class);
jjg@46 140 standardAttributes.put(StackMap, StackMap_attribute.class);
jjg@46 141 standardAttributes.put(StackMapTable, StackMapTable_attribute.class);
jjg@46 142 standardAttributes.put(Synthetic, Synthetic_attribute.class);
jjg@46 143 }
jjg@46 144
jjg@46 145 private Map<String,Class<? extends Attribute>> standardAttributes;
jjg@46 146 private boolean compat; // don't support recent attrs in compatibility mode
jjg@46 147 private boolean jsr277; // support new jsr277 attrs
jjg@46 148 }
jjg@46 149
jjg@46 150 public static Attribute read(ClassReader cr) throws IOException {
jjg@46 151 return cr.readAttribute();
jjg@46 152 }
jjg@46 153
jjg@46 154 protected Attribute(int name_index, int length) {
jjg@46 155 attribute_name_index = name_index;
jjg@46 156 attribute_length = length;
jjg@46 157 }
jjg@46 158
jjg@46 159 public String getName(ConstantPool constant_pool) throws ConstantPoolException {
jjg@46 160 return constant_pool.getUTF8Value(attribute_name_index);
jjg@46 161 }
jjg@46 162
jjg@46 163 public abstract <R,D> R accept(Attribute.Visitor<R,D> visitor, D data);
jjg@46 164
jjg@46 165 public final int attribute_name_index;
jjg@46 166 public final int attribute_length;
jjg@46 167
jjg@46 168
jjg@46 169 public interface Visitor<R,P> {
jjg@46 170 R visitDefault(DefaultAttribute attr, P p);
jjg@46 171 R visitAnnotationDefault(AnnotationDefault_attribute attr, P p);
jjg@46 172 R visitCharacterRangeTable(CharacterRangeTable_attribute attr, P p);
jjg@46 173 R visitCode(Code_attribute attr, P p);
jjg@46 174 R visitCompilationID(CompilationID_attribute attr, P p);
jjg@46 175 R visitConstantValue(ConstantValue_attribute attr, P p);
jjg@46 176 R visitDeprecated(Deprecated_attribute attr, P p);
jjg@46 177 R visitEnclosingMethod(EnclosingMethod_attribute attr, P p);
jjg@46 178 R visitExceptions(Exceptions_attribute attr, P p);
jjg@46 179 R visitInnerClasses(InnerClasses_attribute attr, P p);
jjg@46 180 R visitLineNumberTable(LineNumberTable_attribute attr, P p);
jjg@46 181 R visitLocalVariableTable(LocalVariableTable_attribute attr, P p);
jjg@46 182 R visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, P p);
jjg@46 183 R visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, P p);
jjg@46 184 R visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, P p);
jjg@46 185 R visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, P p);
jjg@46 186 R visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_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 R visitModule(Module_attribute attr, P p);
jjg@46 196 R visitModuleExportTable(ModuleExportTable_attribute attr, P p);
jjg@46 197 R visitModuleMemberTable(ModuleMemberTable_attribute attr, P p);
jjg@46 198 }
jjg@46 199 }

mercurial