summaryrefslogtreecommitdiff
path: root/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/struct/consts/LinkConstant.java
blob: 71bf146155585ec1e9393ec2f067ec5ea63ef69e (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
/*
 * 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.consts;

import java.io.DataOutputStream;
import java.io.IOException;

/*
 *   NameAndType, FieldRef, MethodRef, InterfaceMethodref
 *   InvokeDynamic, MethodHandle
 */

public class LinkConstant extends PooledConstant {

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

  public int index1, index2;

  public String classname;

  public String elementname;

  public String descriptor;

  public int paramCount = 0;

  public boolean isVoid = false;

  public boolean returnCategory2 = false;


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

  public LinkConstant(int type, String classname, String elementname, String descriptor) {
    this.type = type;
    this.classname = classname;
    this.elementname = elementname;
    this.descriptor = descriptor;

    initConstant();
  }

  public LinkConstant(int type, int index1, int index2) {
    this.type = type;
    this.index1 = index1;
    this.index2 = index2;
  }


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

  public void resolveConstant(ConstantPool pool) {

    if (type == CONSTANT_NameAndType) {
      elementname = pool.getPrimitiveConstant(index1).getString();
      descriptor = pool.getPrimitiveConstant(index2).getString();
    }
    else if (type == CONSTANT_MethodHandle) {
      LinkConstant ref_info = pool.getLinkConstant(index2);

      classname = ref_info.classname;
      elementname = ref_info.elementname;
      descriptor = ref_info.descriptor;
    }
    else {
      if (type != CONSTANT_InvokeDynamic) {
        classname = pool.getPrimitiveConstant(index1).getString();
      }

      LinkConstant nametype = pool.getLinkConstant(index2);
      elementname = nametype.elementname;
      descriptor = nametype.descriptor;
    }

    initConstant();
  }

  public void writeToStream(DataOutputStream out) throws IOException {
    out.writeByte(type);
    if (type == CONSTANT_MethodHandle) {
      out.writeByte(index1);
    }
    else {
      out.writeShort(index1);
    }
    out.writeShort(index2);
  }


  public boolean equals(Object o) {
    if (o == this) return true;
    if (o == null || !(o instanceof LinkConstant)) return false;

    LinkConstant cn = (LinkConstant)o;
    return this.type == cn.type &&
           this.elementname.equals(cn.elementname) &&
           this.descriptor.equals(cn.descriptor) &&
           (this.type != CONSTANT_NameAndType || this.classname.equals(cn.classname));
  }

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

  private void initConstant() {

    if (type == CONSTANT_Methodref ||
        type == CONSTANT_InterfaceMethodref ||
        type == CONSTANT_InvokeDynamic ||
        type == CONSTANT_MethodHandle) {
      resolveDescriptor(descriptor);
    }
    else if (type == CONSTANT_Fieldref) {
      returnCategory2 = ("D".equals(descriptor) || "J".equals(descriptor));
    }
  }

  private void resolveDescriptor(String descr) {

    String[] arr = descr.split("[()]");
    String par = arr[1];

    int index = 0, counter = 0;
    int len = par.length();

    while (index < len) {

      char c = par.charAt(index);
      if (c == 'L') {
        index = par.indexOf(";", index);
      }
      else if (c == '[') {
        index++;
        continue;
      }

      counter++;
      index++;
    }

    paramCount = counter;
    isVoid = "V".equals(arr[2]);
    returnCategory2 = ("D".equals(arr[2]) || "J".equals(arr[2]));
  }
}