1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package sun.tools.javap;

import java.util.*;
import java.io.*;

import static sun.tools.javap.RuntimeConstants.*;

/**
 * Strores method data informastion.
 *
 * @author  Sucheta Dambalkar (Adopted code from jdis)
 */
public class MethodData {
    
    ClassData cls;
    int access;
    int name_index;
    int descriptor_index;
    int attributes_count;
    byte[] code;
    Vector exception_table = new Vector(0);
    Vector lin_num_tb = new Vector(0);
    Vector loc_var_tb = new Vector(0);
    StackMapTableData[] stackMapTable;
    StackMapData[] stackMap;
    int[] exc_index_table=null; 
    Vector attrs=new Vector(0);
    Vector code_attrs=new Vector(0); 
    int max_stack,  max_locals;
    boolean isSynthetic=false;
    boolean isDeprecated=false;

    public MethodData(ClassData cls){
    this.cls=cls;
    }

    /**
     * Read method info.
     */
    public void read(DataInputStream in) throws IOException {
    access = in.readUnsignedShort(); 
    name_index=in.readUnsignedShort();
    descriptor_index =in.readUnsignedShort();
    int attributes_count = in.readUnsignedShort();
    for (int i = 0; i < attributes_count; i++) {
        int attr_name_index=in.readUnsignedShort();
        
    readAttr: {
        if (cls.getTag(attr_name_index)==CONSTANT_UTF8) {
            String  attr_name=cls.getString(attr_name_index);
            if ( attr_name.equals("Code")){
            readCode (in);
            AttrData attr=new AttrData(cls);
            attr.read(attr_name_index);
            attrs.addElement(attr);
            break readAttr;
            } else if ( attr_name.equals("Exceptions")){
            readExceptions(in);
            AttrData attr=new AttrData(cls);
            attr.read(attr_name_index);
            attrs.addElement(attr);
            break readAttr;
            } else if (attr_name.equals("Synthetic")){
            if (in.readInt()!=0) 
                throw new ClassFormatError("invalid Synthetic attr length");
            isSynthetic=true;
            AttrData attr=new AttrData(cls);
            attr.read(attr_name_index);
            attrs.addElement(attr);
            break readAttr;
            } else if (attr_name.equals("Deprecated")){ 
            if (in.readInt()!=0) 
                throw new ClassFormatError("invalid Synthetic attr length");
            isDeprecated = true;
            AttrData attr=new AttrData(cls);
            attr.read(attr_name_index);
            attrs.addElement(attr);
            break readAttr;
            }
        } 
        AttrData attr=new AttrData(cls);
        attr.read(attr_name_index, in);
        attrs.addElement(attr);
        }
    }
    }
    
    /**
     * Read code attribute info.
     */
    public void readCode(DataInputStream in) throws IOException {

    int attr_length = in.readInt(); 
    max_stack=in.readUnsignedShort();
    max_locals=in.readUnsignedShort();
    int codelen=in.readInt();

    code=new byte[codelen];
    int totalread = 0;
    while(totalread < codelen){
        totalread += in.read(code, totalread, codelen-totalread);
    }
    //  in.read(code, 0, codelen);
    int clen = 0;
    readExceptionTable(in);
    int code_attributes_count = in.readUnsignedShort();
    
    for (int k = 0 ; k < code_attributes_count ; k++) {
        int table_name_index=in.readUnsignedShort();
        int table_name_tag=cls.getTag(table_name_index);
        AttrData attr=new AttrData(cls);
        if (table_name_tag==CONSTANT_UTF8) {
        String table_name_tstr=cls.getString(table_name_index);
        if (table_name_tstr.equals("LineNumberTable")) {
            readLineNumTable(in);
            attr.read(table_name_index);
        } else if (table_name_tstr.equals("LocalVariableTable")) {
            readLocVarTable(in);
            attr.read(table_name_index);            
        } else if (table_name_tstr.equals("StackMapTable")) {
            readStackMapTable(in);
            attr.read(table_name_index);
        } else if (table_name_tstr.equals("StackMap")) {
            readStackMap(in);
            attr.read(table_name_index);
        } else {
                attr.read(table_name_index, in);
        }
        code_attrs.addElement(attr);
        continue;
        }
       
        attr.read(table_name_index, in);
        code_attrs.addElement(attr);
    }
    }
    
    /**
     * Read exception table info.
     */
    void readExceptionTable (DataInputStream in) throws IOException {
    int exception_table_len=in.readUnsignedShort();
    exception_table=new Vector(exception_table_len);
    for (int l = 0; l < exception_table_len; l++) {
        exception_table.addElement(new TrapData(in, l));
    }
    }

    /**
     * Read LineNumberTable attribute info.
     */
    void readLineNumTable (DataInputStream in) throws IOException {
    int attr_len = in.readInt(); // attr_length
    int lin_num_tb_len = in.readUnsignedShort();
    lin_num_tb=new Vector(lin_num_tb_len);
    for (int l = 0; l < lin_num_tb_len; l++) {
        lin_num_tb.addElement(new LineNumData(in));
    }
    }
    
    /**
     * Read LocalVariableTable attribute info.
     */
    void readLocVarTable (DataInputStream in) throws IOException {
    int attr_len=in.readInt(); // attr_length
    int loc_var_tb_len = in.readUnsignedShort();
    loc_var_tb = new Vector(loc_var_tb_len);
    for (int l = 0; l < loc_var_tb_len; l++) {
        loc_var_tb.addElement(new LocVarData(in));
    }
    }
    
    /**
     * Read Exception attribute info.
     */
    public void readExceptions(DataInputStream in) throws IOException {
    int attr_len=in.readInt(); // attr_length in prog
    int num_exceptions = in.readUnsignedShort();
    exc_index_table=new int[num_exceptions];
    for (int l = 0; l < num_exceptions; l++) {
        int exc=in.readShort();
        exc_index_table[l]=exc;
    }
    }
    
    /**
     * Read StackMapTable attribute info.
     */
    void readStackMapTable(DataInputStream in) throws IOException {
    int attr_len = in.readInt();  //attr_length
    int stack_map_tb_len = in.readUnsignedShort();
    stackMapTable = new StackMapTableData[stack_map_tb_len];
    for (int i=0; i<stack_map_tb_len; i++) {
        stackMapTable[i] = StackMapTableData.getInstance(in, this);
    }
    }
    
    /**
     * Read StackMap attribute info.
     */
    void readStackMap(DataInputStream in) throws IOException {
    int attr_len = in.readInt();  //attr_length
    int stack_map_len = in.readUnsignedShort();
    stackMap = new StackMapData[stack_map_len];
    for (int i = 0; i<stack_map_len; i++) {
        stackMap[i] = new StackMapData(in, this);
    }
    }
    
    /**
     * Return access of the method.
     */
    public String[] getAccess(){

    Vector v = new Vector();
    if ((access & ACC_PUBLIC)   !=0) v.addElement("public");
    if ((access & ACC_PRIVATE)   !=0) v.addElement("private");
    if ((access & ACC_PROTECTED)   !=0) v.addElement("protected");
    if ((access & ACC_STATIC)   !=0) v.addElement("static");
    if ((access & ACC_FINAL)    !=0) v.addElement("final");
    if ((access & ACC_SYNCHRONIZED) !=0) v.addElement("synchronized");
    if ((access & ACC_NATIVE) !=0) v.addElement("native");
    if ((access & ACC_ABSTRACT) !=0) v.addElement("abstract");
    if ((access & ACC_STRICT) !=0) v.addElement("strictfp");

    String[] accflags = new String[v.size()];
    v.copyInto(accflags);
    return accflags;
    }
    
    /**
     * Return name of the method.
     */
    public String getName(){
    return cls.getStringValue(name_index);
    }
    
    /**
     * Return internal siganature of the method.
     */
    public String getInternalSig(){
    return cls.getStringValue(descriptor_index);
    }
    
    /**
     * Return java return type signature of method.
     */
    public String getReturnType(){
    
    String rttype = (new TypeSignature(getInternalSig())).getReturnType();
    return rttype;
    }
    
    /**
     * Return java type parameter signature.
     */
    public String getParameters(){
    String ptype = (new TypeSignature(getInternalSig())).getParameters();
    
    return ptype;
    }
    
    /**
     * Return code attribute data of a method.
     */
    public byte[] getCode(){
    return code;
    }
    
    /**
     * Return LineNumberTable size.
     */
    public int getnumlines(){
    return lin_num_tb.size();
    }
    
    /**
     * Return LineNumberTable
     */
    public Vector getlin_num_tb(){
    return lin_num_tb;
    }
    
    /**
     * Return LocalVariableTable size.
     */
    public int getloc_var_tbsize(){
    return loc_var_tb.size();
    }
    
     
    /**
     * Return LocalVariableTable.
     */
    public Vector getloc_var_tb(){
    return loc_var_tb;
    }
    
    /**
     * Return StackMap.
     */
    public StackMapData[] getStackMap() {
    return stackMap;
    }
     
    /**
     * Return StackMapTable.
     */
    public StackMapTableData[] getStackMapTable() {
    return stackMapTable;
    }
    
    /**
     * Return number of arguments of that method.
     */
    public int getArgumentlength(){
    return new TypeSignature(getInternalSig()).getArgumentlength();
    }
    
    /**
     * Return true if method is static
     */
    public boolean isStatic(){
    if ((access & ACC_STATIC)   !=0) return true;
    return false;
    }
    
     
    /**
     * Return max depth of operand stack.
     */
    public int getMaxStack(){
    return  max_stack;
    }
    
     
    /**
     * Return number of local variables.
     */
    public int getMaxLocals(){
    return max_locals;
    }
    
     
    /**
     * Return exception index table in Exception attribute.
     */
    public int []get_exc_index_table(){
    return  exc_index_table;
    }
    
     
    /**
     * Return exception table in code attributre.
     */
    public Vector getexception_table(){
    return exception_table;
    }
    
     
    /**
     * Return method attributes.
     */
    public Vector getAttributes(){
    return attrs;
    }
    
     
    /**
     * Return code attributes.
     */
    public Vector getCodeAttributes(){
    return code_attrs;
    }
    
     
    /**
     * Return true if method id synthetic.
     */
    public boolean isSynthetic(){
    return isSynthetic;
    }
    
     
    /**
     * Return true if method is deprecated.
     */
    public boolean isDeprecated(){
    return isDeprecated;
    }
}    
			
			

Browsed Source: [clear]