summaryrefslogtreecommitdiffstats
path: root/src/org/jetbrains/java/decompiler/struct/StructMethod.java
blob: baaf6337ad4805ae19092f13874b6d60fe817dd5 (plain)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.java.decompiler.struct;

import org.jetbrains.java.decompiler.code.*;
import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute;
import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTableAttribute;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import org.jetbrains.java.decompiler.util.DataInputFullStream;
import org.jetbrains.java.decompiler.util.VBStyleCollection;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
    method_info {
    	u2 access_flags;
    	u2 name_index;
    	u2 descriptor_index;
    	u2 attributes_count;
    	attribute_info attributes[attributes_count];
    }
*/

public class StructMethod implements CodeConstants {

  // *****************************************************************************
  // public fields
  // *****************************************************************************

  public int name_index;

  public int descriptor_index;

  // *****************************************************************************
  // private fields
  // *****************************************************************************

  private static final int[] opr_iconst = new int[]{-1, 0, 1, 2, 3, 4, 5};

  private static final int[] opr_loadstore = new int[]{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3};

  private static final int[] opcs_load = new int[]{opc_iload, opc_lload, opc_fload, opc_dload, opc_aload};

  private static final int[] opcs_store = new int[]{opc_istore, opc_lstore, opc_fstore, opc_dstore, opc_astore};


  private int accessFlags;

  private VBStyleCollection<StructGeneralAttribute, String> attributes;

  private int localVariables;

  private int maxStack;

  private String name;

  private String descriptor;

  private InstructionSequence seq;

  private boolean containsCode = false;

  private boolean own;

  private StructClass classStruct;


  // lazy properties
  private boolean lazy;

  private boolean expanded;

  private byte[] code_content;

  private int code_length = 0;

  private int code_fulllength = 0;

  // *****************************************************************************
  // constructors
  // *****************************************************************************

  public StructMethod(DataInputFullStream in, boolean own, StructClass clstruct) throws IOException {
    this(in, true, own, clstruct);
  }

  public StructMethod(DataInputFullStream in, boolean lazy, boolean own, StructClass clstruct) throws IOException {

    this.own = own;
    this.lazy = lazy;
    this.expanded = !lazy;
    this.classStruct = clstruct;

    accessFlags = in.readUnsignedShort();
    name_index = in.readUnsignedShort();
    descriptor_index = in.readUnsignedShort();

    ConstantPool pool = clstruct.getPool();

    initStrings(pool, clstruct.this_class);

    VBStyleCollection<StructGeneralAttribute, String> lstAttribute = new VBStyleCollection<StructGeneralAttribute, String>();
    int len = in.readUnsignedShort();
    for (int i = 0; i < len; i++) {

      int attr_nameindex = in.readUnsignedShort();
      String attrname = pool.getPrimitiveConstant(attr_nameindex).getString();

      if (StructGeneralAttribute.ATTRIBUTE_CODE.equals(attrname)) {
        if (!this.own) {
          // skip code in foreign classes
          in.skip(8);
          in.skip(in.readInt());
          in.skip(8 * in.readUnsignedShort());
        }
        else {
          containsCode = true;

          in.skip(4);

          maxStack = in.readUnsignedShort();
          localVariables = in.readUnsignedShort();

          if (lazy) {
            code_length = in.readInt();

            in.skip(code_length);

            int exc_length = in.readUnsignedShort();
            code_fulllength = code_length + exc_length * 8 + 2;

            in.skip(exc_length * 8);
          }
          else {
            seq = parseBytecode(in, in.readInt(), pool);
          }
        }

        // code attributes
        int length = in.readUnsignedShort();
        for (int j = 0; j < length; j++) {
          int codeattr_nameindex = in.readUnsignedShort();
          String codeattrname = pool.getPrimitiveConstant(codeattr_nameindex).getString();

          readAttribute(in, pool, lstAttribute, codeattr_nameindex, codeattrname);
        }
      }
      else {
        readAttribute(in, pool, lstAttribute, attr_nameindex, attrname);
      }
    }

    attributes = lstAttribute;
  }


  // *****************************************************************************
  // public methods
  // *****************************************************************************

  public void writeToStream(DataOutputStream out) throws IOException {

    out.writeShort(accessFlags);
    out.writeShort(name_index);
    out.writeShort(descriptor_index);

    out.writeShort(attributes.size());

    for (StructGeneralAttribute attr : attributes) {
      if (StructGeneralAttribute.ATTRIBUTE_CODE.equals(attr.getName())) {
        out.writeShort(attr.getAttribute_name_index());

        if (lazy && !expanded) {
          out.writeInt(10 + code_content.length);
          out.writeShort(maxStack);
          out.writeShort(localVariables);
          out.writeInt(code_length);
          out.write(code_content);
        }
        else {
          ByteArrayOutputStream codeout = new ByteArrayOutputStream();
          seq.writeCodeToStream(new DataOutputStream(codeout));

          ByteArrayOutputStream excout = new ByteArrayOutputStream();
          seq.writeExceptionsToStream(new DataOutputStream(excout));

          out.writeInt(10 + codeout.size() + excout.size());

          out.writeShort(maxStack);
          out.writeShort(localVariables);
          out.writeInt(codeout.size());
          codeout.writeTo(out);
          excout.writeTo(out);
        }
        // no attributes
        out.writeShort(0);
      }
      else {
        attr.writeToStream(out);
      }
    }
  }

  private void readAttribute(DataInputFullStream in, ConstantPool pool, VBStyleCollection<StructGeneralAttribute, String> lstAttribute,
                             int attr_nameindex, String attrname) throws IOException {

    StructGeneralAttribute attribute = StructGeneralAttribute.getMatchingAttributeInstance(attr_nameindex, attrname);

    if (attribute != null) {
      attrname = attribute.getName();

      byte[] arr = new byte[in.readInt()];
      in.readFull(arr);
      attribute.setInfo(arr);

      attribute.initContent(pool);

      if (StructGeneralAttribute.ATTRIBUTE_LOCAL_VARIABLE_TABLE.equals(attrname) &&
          lstAttribute.containsKey(attrname)) {
        // merge all variable tables
        StructLocalVariableTableAttribute oldattr = (StructLocalVariableTableAttribute)lstAttribute.getWithKey(attrname);
        oldattr.addLocalVariableTable((StructLocalVariableTableAttribute)attribute);
      }
      else {
        lstAttribute.addWithKey(attribute, attribute.getName());
      }
    }
    else {
      in.skip(in.readInt());
    }
  }

  private void initStrings(ConstantPool pool, int class_index) {
    String[] values = pool.getClassElement(ConstantPool.METHOD, class_index, name_index, descriptor_index);
    name = values[0];
    descriptor = values[1];
  }

  public void expandData() throws IOException {
    if (containsCode && lazy && !expanded) {

      byte[] codearr = classStruct.getLoader().loadBytecode(this, code_fulllength);

      seq = parseBytecode(new DataInputFullStream(new ByteArrayInputStream(codearr)), code_length, classStruct.getPool());
      expanded = true;
    }
  }

  public void releaseResources() throws IOException {
    if (containsCode && lazy && expanded) {
      seq = null;
      expanded = false;
    }
  }

  // *****************************************************************************
  // private methods
  // *****************************************************************************

  private InstructionSequence parseBytecode(DataInputFullStream in, int length, ConstantPool pool) throws IOException {

    VBStyleCollection<Instruction, Integer> collinstr = new VBStyleCollection<Instruction, Integer>();

    int bytecode_version = classStruct.getBytecodeVersion();

    for (int i = 0; i < length; ) {

      int offset = i;

      int opcode = in.readUnsignedByte();
      int group = GROUP_GENERAL;

      boolean wide = (opcode == opc_wide);

      if (wide) {
        i++;
        opcode = in.readUnsignedByte();
      }

      List<Integer> operands = new ArrayList<Integer>();

      if (opcode >= opc_iconst_m1 && opcode <= opc_iconst_5) {
        operands.add(new Integer(opr_iconst[opcode - opc_iconst_m1]));
        opcode = opc_bipush;
      }
      else if (opcode >= opc_iload_0 && opcode <= opc_aload_3) {
        operands.add(new Integer(opr_loadstore[opcode - opc_iload_0]));
        opcode = opcs_load[(opcode - opc_iload_0) / 4];
      }
      else if (opcode >= opc_istore_0 && opcode <= opc_astore_3) {
        operands.add(new Integer(opr_loadstore[opcode - opc_istore_0]));
        opcode = opcs_store[(opcode - opc_istore_0) / 4];
      }
      else {
        switch (opcode) {
          case opc_bipush:
            operands.add(new Integer(in.readByte()));
            i++;
            break;
          case opc_ldc:
          case opc_newarray:
            operands.add(new Integer(in.readUnsignedByte()));
            i++;
            break;
          case opc_sipush:
          case opc_ifeq:
          case opc_ifne:
          case opc_iflt:
          case opc_ifge:
          case opc_ifgt:
          case opc_ifle:
          case opc_if_icmpeq:
          case opc_if_icmpne:
          case opc_if_icmplt:
          case opc_if_icmpge:
          case opc_if_icmpgt:
          case opc_if_icmple:
          case opc_if_acmpeq:
          case opc_if_acmpne:
          case opc_goto:
          case opc_jsr:
          case opc_ifnull:
          case opc_ifnonnull:
            if (opcode != opc_sipush) {
              group = GROUP_JUMP;
            }
            operands.add(new Integer(in.readShort()));
            i += 2;
            break;
          case opc_ldc_w:
          case opc_ldc2_w:
          case opc_getstatic:
          case opc_putstatic:
          case opc_getfield:
          case opc_putfield:
          case opc_invokevirtual:
          case opc_invokespecial:
          case opc_invokestatic:
          case opc_new:
          case opc_anewarray:
          case opc_checkcast:
          case opc_instanceof:
            operands.add(new Integer(in.readUnsignedShort()));
            i += 2;
            if (opcode >= opc_getstatic && opcode <= opc_putfield) {
              group = GROUP_FIELDACCESS;
            }
            else if (opcode >= opc_invokevirtual && opcode <= opc_invokestatic) {
              group = GROUP_INVOCATION;
            }
            break;
          case opc_invokedynamic:
            if (classStruct.isVersionGE_1_7()) { // instruction unused in Java 6 and before
              operands.add(new Integer(in.readUnsignedShort()));
              in.skip(2);
              group = GROUP_INVOCATION;
              i += 4;
            }
            break;
          case opc_iload:
          case opc_lload:
          case opc_fload:
          case opc_dload:
          case opc_aload:
          case opc_istore:
          case opc_lstore:
          case opc_fstore:
          case opc_dstore:
          case opc_astore:
          case opc_ret:
            if (wide) {
              operands.add(new Integer(in.readUnsignedShort()));
              i += 2;
            }
            else {
              operands.add(new Integer(in.readUnsignedByte()));
              i++;
            }
            if (opcode == opc_ret) {
              group = GROUP_RETURN;
            }
            break;
          case opc_iinc:
            if (wide) {
              operands.add(new Integer(in.readUnsignedShort()));
              operands.add(new Integer(in.readShort()));
              i += 4;
            }
            else {
              operands.add(new Integer(in.readUnsignedByte()));
              operands.add(new Integer(in.readByte()));
              i += 2;
            }
            break;
          case opc_goto_w:
          case opc_jsr_w:
            opcode = opcode == opc_jsr_w ? opc_jsr : opc_goto;
            operands.add(new Integer(in.readInt()));
            group = GROUP_JUMP;
            i += 4;
            break;
          case opc_invokeinterface:
            operands.add(new Integer(in.readUnsignedShort()));
            operands.add(new Integer(in.readUnsignedByte()));
            in.skip(1);
            group = GROUP_INVOCATION;
            i += 4;
            break;
          case opc_multianewarray:
            operands.add(new Integer(in.readUnsignedShort()));
            operands.add(new Integer(in.readUnsignedByte()));
            i += 3;
            break;
          case opc_tableswitch:
            in.skip((4 - (i + 1) % 4) % 4);
            i += ((4 - (i + 1) % 4) % 4); // padding
            operands.add(new Integer(in.readInt()));
            i += 4;
            int low = in.readInt();
            operands.add(new Integer(low));
            i += 4;
            int high = in.readInt();
            operands.add(new Integer(high));
            i += 4;

            for (int j = 0; j < high - low + 1; j++) {
              operands.add(new Integer(in.readInt()));
              i += 4;
            }
            group = GROUP_SWITCH;

            break;
          case opc_lookupswitch:
            in.skip((4 - (i + 1) % 4) % 4);
            i += ((4 - (i + 1) % 4) % 4); // padding
            operands.add(new Integer(in.readInt()));
            i += 4;
            int npairs = in.readInt();
            operands.add(new Integer(npairs));
            i += 4;

            for (int j = 0; j < npairs; j++) {
              operands.add(new Integer(in.readInt()));
              i += 4;
              operands.add(new Integer(in.readInt()));
              i += 4;
            }
            group = GROUP_SWITCH;
            break;
          case opc_ireturn:
          case opc_lreturn:
          case opc_freturn:
          case opc_dreturn:
          case opc_areturn:
          case opc_return:
          case opc_athrow:
            group = GROUP_RETURN;
        }
      }

      int[] ops = new int[operands.size()];
      for (int j = 0; j < operands.size(); j++) {
        ops[j] = ((Integer)operands.get(j)).intValue();
      }

      Instruction instr = ConstantsUtil.getInstructionInstance(opcode, wide, group, bytecode_version, ops);

      collinstr.addWithKey(instr, new Integer(offset));

      i++;
    }

    // initialize exception table
    List<ExceptionHandler> lstHandlers = new ArrayList<ExceptionHandler>();

    int exception_count = in.readUnsignedShort();
    for (int i = 0; i < exception_count; i++) {
      ExceptionHandler handler = new ExceptionHandler();
      handler.from = in.readUnsignedShort();
      handler.to = in.readUnsignedShort();
      handler.handler = in.readUnsignedShort();

      int excclass = in.readUnsignedShort();
      handler.class_index = excclass;
      if (excclass != 0) {
        handler.exceptionClass = pool.getPrimitiveConstant(excclass).getString();
      }

      lstHandlers.add(handler);
    }

    InstructionSequence seq = new FullInstructionSequence(collinstr, new ExceptionTable(lstHandlers));

    // initialize instructions
    int i = seq.length() - 1;
    seq.setPointer(i);

    while (i >= 0) {
      Instruction instr = seq.getInstr(i--);
      if (instr.group != GROUP_GENERAL) {
        instr.initInstruction(seq);
      }
      seq.addToPointer(-1);
    }

    return seq;
  }

  // *****************************************************************************
  // getter and setter methods
  // *****************************************************************************

  public InstructionSequence getInstructionSequence() {
    return seq;
  }

  public String getDescriptor() {
    return descriptor;
  }

  public String getName() {
    return name;
  }

  public int getAccessFlags() {
    return accessFlags;
  }

  public int getLocalVariables() {
    return localVariables;
  }

  public VBStyleCollection<StructGeneralAttribute, String> getAttributes() {
    return attributes;
  }

  public StructClass getClassStruct() {
    return classStruct;
  }

  public boolean containsCode() {
    return containsCode;
  }
}