rt/javap/src/main/java/org/apidesign/javap/MethodData.java
changeset 772 d382dacfd73f
parent 397 2adac52f955e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/javap/src/main/java/org/apidesign/javap/MethodData.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,394 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package org.apidesign.javap;
    1.30 +
    1.31 +
    1.32 +import java.io.DataInputStream;
    1.33 +import java.io.IOException;
    1.34 +import static org.apidesign.javap.RuntimeConstants.*;
    1.35 +
    1.36 +/**
    1.37 + * Strores method data informastion.
    1.38 + *
    1.39 + * @author  Sucheta Dambalkar (Adopted code from jdis)
    1.40 + */
    1.41 +public class MethodData {
    1.42 +
    1.43 +    ClassData cls;
    1.44 +    int access;
    1.45 +    int name_index;
    1.46 +    int descriptor_index;
    1.47 +    int attributes_count;
    1.48 +    byte[] code;
    1.49 +    Vector exception_table = new Vector(0);
    1.50 +    Vector lin_num_tb = new Vector(0);
    1.51 +    Vector loc_var_tb = new Vector(0);
    1.52 +    StackMapTableData[] stackMapTable;
    1.53 +    StackMapData[] stackMap;
    1.54 +    int[] exc_index_table=null;
    1.55 +    Vector attrs=new Vector(0);
    1.56 +    Vector code_attrs=new Vector(0);
    1.57 +    int max_stack,  max_locals;
    1.58 +    boolean isSynthetic=false;
    1.59 +    boolean isDeprecated=false;
    1.60 +
    1.61 +    public MethodData(ClassData cls){
    1.62 +        this.cls=cls;
    1.63 +    }
    1.64 +
    1.65 +    /**
    1.66 +     * Read method info.
    1.67 +     */
    1.68 +    public void read(DataInputStream in) throws IOException {
    1.69 +        access = in.readUnsignedShort();
    1.70 +        name_index=in.readUnsignedShort();
    1.71 +        descriptor_index =in.readUnsignedShort();
    1.72 +        int attributes_count = in.readUnsignedShort();
    1.73 +        for (int i = 0; i < attributes_count; i++) {
    1.74 +            int attr_name_index=in.readUnsignedShort();
    1.75 +
    1.76 +        readAttr: {
    1.77 +                if (cls.getTag(attr_name_index)==CONSTANT_UTF8) {
    1.78 +                    String  attr_name=cls.getString(attr_name_index);
    1.79 +                    if ( attr_name.equals("Code")){
    1.80 +                        readCode (in);
    1.81 +                        AttrData attr=new AttrData(cls);
    1.82 +                        attr.read(attr_name_index);
    1.83 +                        attrs.addElement(attr);
    1.84 +                        break readAttr;
    1.85 +                    } else if ( attr_name.equals("Exceptions")){
    1.86 +                        readExceptions(in);
    1.87 +                        AttrData attr=new AttrData(cls);
    1.88 +                        attr.read(attr_name_index);
    1.89 +                        attrs.addElement(attr);
    1.90 +                        break readAttr;
    1.91 +                    } else if (attr_name.equals("Synthetic")){
    1.92 +                        if (in.readInt()!=0)
    1.93 +                            throw new ClassFormatError("invalid Synthetic attr length");
    1.94 +                        isSynthetic=true;
    1.95 +                        AttrData attr=new AttrData(cls);
    1.96 +                        attr.read(attr_name_index);
    1.97 +                        attrs.addElement(attr);
    1.98 +                        break readAttr;
    1.99 +                    } else if (attr_name.equals("Deprecated")){
   1.100 +                        if (in.readInt()!=0)
   1.101 +                            throw new ClassFormatError("invalid Synthetic attr length");
   1.102 +                        isDeprecated = true;
   1.103 +                        AttrData attr=new AttrData(cls);
   1.104 +                        attr.read(attr_name_index);
   1.105 +                        attrs.addElement(attr);
   1.106 +                        break readAttr;
   1.107 +                    }
   1.108 +                }
   1.109 +                AttrData attr=new AttrData(cls);
   1.110 +                attr.read(attr_name_index, in);
   1.111 +                attrs.addElement(attr);
   1.112 +            }
   1.113 +        }
   1.114 +    }
   1.115 +
   1.116 +    /**
   1.117 +     * Read code attribute info.
   1.118 +     */
   1.119 +    public void readCode(DataInputStream in) throws IOException {
   1.120 +
   1.121 +        int attr_length = in.readInt();
   1.122 +        max_stack=in.readUnsignedShort();
   1.123 +        max_locals=in.readUnsignedShort();
   1.124 +        int codelen=in.readInt();
   1.125 +
   1.126 +        code=new byte[codelen];
   1.127 +        int totalread = 0;
   1.128 +        while(totalread < codelen){
   1.129 +            totalread += in.read(code, totalread, codelen-totalread);
   1.130 +        }
   1.131 +        //      in.read(code, 0, codelen);
   1.132 +        int clen = 0;
   1.133 +        readExceptionTable(in);
   1.134 +        int code_attributes_count = in.readUnsignedShort();
   1.135 +
   1.136 +        for (int k = 0 ; k < code_attributes_count ; k++) {
   1.137 +            int table_name_index=in.readUnsignedShort();
   1.138 +            int table_name_tag=cls.getTag(table_name_index);
   1.139 +            AttrData attr=new AttrData(cls);
   1.140 +            if (table_name_tag==CONSTANT_UTF8) {
   1.141 +                String table_name_tstr=cls.getString(table_name_index);
   1.142 +                if (table_name_tstr.equals("LineNumberTable")) {
   1.143 +                    readLineNumTable(in);
   1.144 +                    attr.read(table_name_index);
   1.145 +                } else if (table_name_tstr.equals("LocalVariableTable")) {
   1.146 +                    readLocVarTable(in);
   1.147 +                    attr.read(table_name_index);
   1.148 +                } else if (table_name_tstr.equals("StackMapTable")) {
   1.149 +                    readStackMapTable(in);
   1.150 +                    attr.read(table_name_index);
   1.151 +                } else if (table_name_tstr.equals("StackMap")) {
   1.152 +                    readStackMap(in);
   1.153 +                    attr.read(table_name_index);
   1.154 +                } else {
   1.155 +                    attr.read(table_name_index, in);
   1.156 +                }
   1.157 +                code_attrs.addElement(attr);
   1.158 +                continue;
   1.159 +            }
   1.160 +
   1.161 +            attr.read(table_name_index, in);
   1.162 +            code_attrs.addElement(attr);
   1.163 +        }
   1.164 +    }
   1.165 +
   1.166 +    /**
   1.167 +     * Read exception table info.
   1.168 +     */
   1.169 +    void readExceptionTable (DataInputStream in) throws IOException {
   1.170 +        int exception_table_len=in.readUnsignedShort();
   1.171 +        exception_table=new Vector(exception_table_len);
   1.172 +        for (int l = 0; l < exception_table_len; l++) {
   1.173 +            exception_table.addElement(new TrapData(in, l));
   1.174 +        }
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     * Read LineNumberTable attribute info.
   1.179 +     */
   1.180 +    void readLineNumTable (DataInputStream in) throws IOException {
   1.181 +        int attr_len = in.readInt(); // attr_length
   1.182 +        int lin_num_tb_len = in.readUnsignedShort();
   1.183 +        lin_num_tb=new Vector(lin_num_tb_len);
   1.184 +        for (int l = 0; l < lin_num_tb_len; l++) {
   1.185 +            lin_num_tb.addElement(new LineNumData(in));
   1.186 +        }
   1.187 +    }
   1.188 +
   1.189 +    /**
   1.190 +     * Read LocalVariableTable attribute info.
   1.191 +     */
   1.192 +    void readLocVarTable (DataInputStream in) throws IOException {
   1.193 +        int attr_len=in.readInt(); // attr_length
   1.194 +        int loc_var_tb_len = in.readUnsignedShort();
   1.195 +        loc_var_tb = new Vector(loc_var_tb_len);
   1.196 +        for (int l = 0; l < loc_var_tb_len; l++) {
   1.197 +            loc_var_tb.addElement(new LocVarData(in));
   1.198 +        }
   1.199 +    }
   1.200 +
   1.201 +    /**
   1.202 +     * Read Exception attribute info.
   1.203 +     */
   1.204 +    public void readExceptions(DataInputStream in) throws IOException {
   1.205 +        int attr_len=in.readInt(); // attr_length in prog
   1.206 +        int num_exceptions = in.readUnsignedShort();
   1.207 +        exc_index_table=new int[num_exceptions];
   1.208 +        for (int l = 0; l < num_exceptions; l++) {
   1.209 +            int exc=in.readShort();
   1.210 +            exc_index_table[l]=exc;
   1.211 +        }
   1.212 +    }
   1.213 +
   1.214 +    /**
   1.215 +     * Read StackMapTable attribute info.
   1.216 +     */
   1.217 +    void readStackMapTable(DataInputStream in) throws IOException {
   1.218 +        int attr_len = in.readInt();  //attr_length
   1.219 +        int stack_map_tb_len = in.readUnsignedShort();
   1.220 +        stackMapTable = new StackMapTableData[stack_map_tb_len];
   1.221 +        for (int i=0; i<stack_map_tb_len; i++) {
   1.222 +            stackMapTable[i] = StackMapTableData.getInstance(in, this);
   1.223 +        }
   1.224 +    }
   1.225 +
   1.226 +    /**
   1.227 +     * Read StackMap attribute info.
   1.228 +     */
   1.229 +    void readStackMap(DataInputStream in) throws IOException {
   1.230 +        int attr_len = in.readInt();  //attr_length
   1.231 +        int stack_map_len = in.readUnsignedShort();
   1.232 +        stackMap = new StackMapData[stack_map_len];
   1.233 +        for (int i = 0; i<stack_map_len; i++) {
   1.234 +            stackMap[i] = new StackMapData(in, this);
   1.235 +        }
   1.236 +    }
   1.237 +
   1.238 +    /**
   1.239 +     * Return access of the method.
   1.240 +     */
   1.241 +    public int getAccess(){
   1.242 +        return access;
   1.243 +    }
   1.244 +
   1.245 +    /**
   1.246 +     * Return name of the method.
   1.247 +     */
   1.248 +    public String getName(){
   1.249 +        return cls.getStringValue(name_index);
   1.250 +    }
   1.251 +
   1.252 +    /**
   1.253 +     * Return internal siganature of the method.
   1.254 +     */
   1.255 +    public String getInternalSig(){
   1.256 +        return cls.getStringValue(descriptor_index);
   1.257 +    }
   1.258 +
   1.259 +    /**
   1.260 +     * Return code attribute data of a method.
   1.261 +     */
   1.262 +    public byte[] getCode(){
   1.263 +        return code;
   1.264 +    }
   1.265 +
   1.266 +    /**
   1.267 +     * Return LineNumberTable size.
   1.268 +     */
   1.269 +    public int getnumlines(){
   1.270 +        return lin_num_tb.size();
   1.271 +    }
   1.272 +
   1.273 +    /**
   1.274 +     * Return LineNumberTable
   1.275 +     */
   1.276 +    public Vector getlin_num_tb(){
   1.277 +        return lin_num_tb;
   1.278 +    }
   1.279 +
   1.280 +    /**
   1.281 +     * Return LocalVariableTable size.
   1.282 +     */
   1.283 +    public int getloc_var_tbsize(){
   1.284 +        return loc_var_tb.size();
   1.285 +    }
   1.286 +
   1.287 +
   1.288 +    /**
   1.289 +     * Return LocalVariableTable.
   1.290 +     */
   1.291 +    public Vector getloc_var_tb(){
   1.292 +        return loc_var_tb;
   1.293 +    }
   1.294 +
   1.295 +    /**
   1.296 +     * Return StackMap.
   1.297 +     */
   1.298 +    public StackMapData[] getStackMap() {
   1.299 +        return stackMap;
   1.300 +    }
   1.301 +
   1.302 +    /**
   1.303 +     * Return StackMapTable.
   1.304 +     */
   1.305 +    public StackMapTableData[] getStackMapTable() {
   1.306 +        return stackMapTable;
   1.307 +    }
   1.308 +
   1.309 +    public StackMapIterator createStackMapIterator() {
   1.310 +        return new StackMapIterator(this);
   1.311 +    }
   1.312 +
   1.313 +    /**
   1.314 +     * Return true if method is static
   1.315 +     */
   1.316 +    public boolean isStatic(){
   1.317 +        if ((access & ACC_STATIC)   !=0) return true;
   1.318 +        return false;
   1.319 +    }
   1.320 +
   1.321 +
   1.322 +    /**
   1.323 +     * Return max depth of operand stack.
   1.324 +     */
   1.325 +    public int getMaxStack(){
   1.326 +        return  max_stack;
   1.327 +    }
   1.328 +
   1.329 +
   1.330 +    /**
   1.331 +     * Return number of local variables.
   1.332 +     */
   1.333 +    public int getMaxLocals(){
   1.334 +        return max_locals;
   1.335 +    }
   1.336 +
   1.337 +
   1.338 +    /**
   1.339 +     * Return exception index table in Exception attribute.
   1.340 +     */
   1.341 +    public int []get_exc_index_table(){
   1.342 +        return  exc_index_table;
   1.343 +    }
   1.344 +
   1.345 +
   1.346 +    /**
   1.347 +     * Return exception table in code attributre.
   1.348 +     */
   1.349 +    public TrapDataIterator getTrapDataIterator(){
   1.350 +        return new TrapDataIterator(exception_table);
   1.351 +    }
   1.352 +    
   1.353 +
   1.354 +    /**
   1.355 +     * Return method attributes.
   1.356 +     */
   1.357 +    public Vector getAttributes(){
   1.358 +        return attrs;
   1.359 +    }
   1.360 +
   1.361 +
   1.362 +    /**
   1.363 +     * Return code attributes.
   1.364 +     */
   1.365 +    public Vector getCodeAttributes(){
   1.366 +        return code_attrs;
   1.367 +    }
   1.368 +
   1.369 +
   1.370 +    /**
   1.371 +     * Return true if method id synthetic.
   1.372 +     */
   1.373 +    public boolean isSynthetic(){
   1.374 +        return isSynthetic;
   1.375 +    }
   1.376 +
   1.377 +
   1.378 +    /**
   1.379 +     * Return true if method is deprecated.
   1.380 +     */
   1.381 +    public boolean isDeprecated(){
   1.382 +        return isDeprecated;
   1.383 +    }
   1.384 +
   1.385 +    public byte[] findAnnotationData(boolean classRetention) {
   1.386 +        String n = classRetention ?
   1.387 +            "RuntimeInvisibleAnnotations" : // NOI18N
   1.388 +            "RuntimeVisibleAnnotations"; // NOI18N
   1.389 +        AttrData[] arr = new AttrData[attrs.size()];
   1.390 +        attrs.copyInto(arr);
   1.391 +        return ClassData.findAttr(n, arr);
   1.392 +    }
   1.393 +
   1.394 +    public boolean isConstructor() {
   1.395 +        return "<init>".equals(getName());
   1.396 +    }
   1.397 +}