summaryrefslogtreecommitdiffstats
path: root/src/org/jetbrains/java/decompiler/struct/StructClass.java
blob: 78e941d5b93386a50bf3a73b3de9541442027165 (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
/*
 * 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.CodeConstants;
import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import org.jetbrains.java.decompiler.struct.consts.PrimitiveConstant;
import org.jetbrains.java.decompiler.struct.lazy.LazyLoader;
import org.jetbrains.java.decompiler.util.DataInputFullStream;
import org.jetbrains.java.decompiler.util.InterpreterUtil;
import org.jetbrains.java.decompiler.util.VBStyleCollection;

import java.io.*;

/*
    ClassFile {
    	u4 magic;
    	u2 minor_version;
    	u2 major_version;
    	u2 constant_pool_count;
    	cp_info constant_pool[constant_pool_count-1];
    	u2 access_flags;
    	u2 this_class;
    	u2 super_class;
    	u2 interfaces_count;
    	u2 interfaces[interfaces_count];
    	u2 fields_count;
    	field_info fields[fields_count];
    	u2 methods_count;
    	method_info methods[methods_count];
    	u2 attributes_count;
    	attribute_info attributes[attributes_count];
    }
*/

public class StructClass {

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

  public int minor_version;

  public int major_version;

  public int access_flags;

  public int this_class;

  public int super_class;

  public PrimitiveConstant thisClass;

  public PrimitiveConstant superClass;

  public String qualifiedName;


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

  private ConstantPool pool;

  private int[] interfaces;

  private String[] interfaceNames;

  private VBStyleCollection<StructField, String> fields = new VBStyleCollection<StructField, String>();

  private VBStyleCollection<StructMethod, String> methods = new VBStyleCollection<StructMethod, String>();

  private VBStyleCollection<StructGeneralAttribute, String> attributes = new VBStyleCollection<StructGeneralAttribute, String>();

  private boolean own = true;

  private LazyLoader loader;

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

  public StructClass(String filename, boolean own, LazyLoader loader) throws IOException {
    this(new FileInputStream(filename), own, loader);
  }

  public StructClass(InputStream inStream, boolean own, LazyLoader loader) throws IOException {
    this(new DataInputFullStream(inStream), own, loader);
  }

  public StructClass(DataInputFullStream inStream, boolean own, LazyLoader loader) throws IOException {
    this.own = own;
    this.loader = loader;

    initStruct(inStream);
  }

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

  public boolean hasField(String name, String descriptor) {
    return getField(name, descriptor) != null;
  }

  public StructField getField(String name, String descriptor) {
    return fields.getWithKey(InterpreterUtil.makeUniqueKey(name, descriptor));
  }

  public StructMethod getMethod(String key) {
    return methods.getWithKey(key);
  }

  public StructMethod getMethod(String name, String descriptor) {
    return methods.getWithKey(InterpreterUtil.makeUniqueKey(name, descriptor));
  }

  public void writeToFile(File file) throws IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
    writeToOutputStream(out);
    out.close();
  }

  public void writeToOutputStream(DataOutputStream out) throws IOException {

    out.writeInt(0xCAFEBABE);
    out.writeShort(minor_version);
    out.writeShort(major_version);

    getPool().writeToOutputStream(out);

    out.writeShort(access_flags);
    out.writeShort(this_class);
    out.writeShort(super_class);

    out.writeShort(interfaces.length);
    for (int i = 0; i < interfaces.length; i++) {
      out.writeShort(interfaces[i]);
    }

    out.writeShort(fields.size());
    for (int i = 0; i < fields.size(); i++) {
      fields.get(i).writeToStream(out);
    }

    out.writeShort(methods.size());
    for (int i = 0; i < methods.size(); i++) {
      methods.get(i).writeToStream(out);
    }

    out.writeShort(attributes.size());
    for (StructGeneralAttribute attr : attributes) {
      attr.writeToStream(out);
    }
  }

  public String getInterface(int i) {
    return interfaceNames[i];
  }

  public void releaseResources() {
    if (loader != null) {
      pool = null;
    }
  }

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

  private void initStruct(DataInputFullStream in) throws IOException {

    in.skip(4);

    this.minor_version = in.readUnsignedShort();
    this.major_version = in.readUnsignedShort();

    pool = new ConstantPool(in);

    this.access_flags = in.readUnsignedShort();

    this_class = in.readUnsignedShort();
    thisClass = pool.getPrimitiveConstant(this_class);
    qualifiedName = thisClass.getString();

    super_class = in.readUnsignedShort();
    superClass = pool.getPrimitiveConstant(super_class);

    // interfaces
    int length = in.readUnsignedShort();
    int[] arrInterfaces = new int[length];
    String[] arrInterfaceNames = new String[length];

    for (int i = 0; i < length; i++) {
      arrInterfaces[i] = in.readUnsignedShort();
      arrInterfaceNames[i] = pool.getPrimitiveConstant(arrInterfaces[i]).getString();
    }
    this.interfaces = arrInterfaces;
    this.interfaceNames = arrInterfaceNames;

    // fields
    VBStyleCollection<StructField, String> lstFields = new VBStyleCollection<StructField, String>();
    length = in.readUnsignedShort();
    for (int i = 0; i < length; i++) {
      StructField field = new StructField();
      field.access_flags = in.readUnsignedShort();
      field.name_index = in.readUnsignedShort();
      field.descriptor_index = in.readUnsignedShort();

      field.initStrings(pool, this_class);

      field.setAttributes(readAttributes(in));

      lstFields.addWithKey(field, InterpreterUtil.makeUniqueKey(field.getName(), field.getDescriptor()));
    }
    this.fields = lstFields;

    // methods
    length = in.readUnsignedShort();
    for (int i = 0; i < length; i++) {
      StructMethod meth = new StructMethod(in, own, this);

      //if(qualifiedName.endsWith("JUnitStatusLine") && !meth.getName().equals("onProcessStarted") && !meth.getName().startsWith("access")) {
      //if(!meth.getName().equals("run")) {
      //	continue;
      //}

      methods.addWithKey(meth, InterpreterUtil.makeUniqueKey(meth.getName(), meth.getDescriptor()));
    }

    // attributes
    this.attributes = readAttributes(in);


    // release memory
    if (loader != null) {
      pool = null;
    }
  }

  private VBStyleCollection<StructGeneralAttribute, String> readAttributes(DataInputFullStream in) throws IOException {

    VBStyleCollection<StructGeneralAttribute, String> lstAttribute = new VBStyleCollection<StructGeneralAttribute, String>();

    int length = in.readUnsignedShort();
    for (int i = 0; i < length; i++) {
      int attr_nameindex = in.readUnsignedShort();
      String attrname = pool.getPrimitiveConstant(attr_nameindex).getString();

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

      if (attr != null) {
        byte[] arr = new byte[in.readInt()];
        in.readFull(arr);
        attr.setInfo(arr);

        attr.initContent(pool);
        lstAttribute.addWithKey(attr, attr.getName());
      }
      else {
        in.skip(in.readInt());
      }
    }

    return lstAttribute;
  }


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

  public ConstantPool getPool() {

    if (pool == null && loader != null) {
      pool = loader.loadPool(qualifiedName);
    }

    return pool;
  }

  public int[] getInterfaces() {
    return interfaces;
  }

  public String[] getInterfaceNames() {
    return interfaceNames;
  }

  public VBStyleCollection<StructMethod, String> getMethods() {
    return methods;
  }

  public VBStyleCollection<StructField, String> getFields() {
    return fields;
  }

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

  public boolean isOwn() {
    return own;
  }

  public LazyLoader getLoader() {
    return loader;
  }

  public boolean isVersionGE_1_5() {
    return (major_version > 48 || (major_version == 48 && minor_version > 0)); // FIXME: check second condition
  }

  public boolean isVersionGE_1_7() {
    return (major_version >= 51);
  }

  public int getBytecodeVersion() {
    switch (major_version) {
      case 52:
        return CodeConstants.BYTECODE_JAVA_8;
      case 51:
        return CodeConstants.BYTECODE_JAVA_7;
      case 50:
        return CodeConstants.BYTECODE_JAVA_6;
      case 49:
        return CodeConstants.BYTECODE_JAVA_5;
    }

    return CodeConstants.BYTECODE_JAVA_LE_4;
  }
}