summaryrefslogtreecommitdiff
path: root/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/modules/decompiler/IdeaNotNullHelper.java
blob: 0f5141a7aa52a6f0a605a3034af9d2a46b763872 (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
/*
 * 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.modules.decompiler;

import org.jetbrains.java.decompiler.code.CodeConstants;
import org.jetbrains.java.decompiler.modules.decompiler.exps.*;
import org.jetbrains.java.decompiler.modules.decompiler.stats.IfStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.SequenceStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
import org.jetbrains.java.decompiler.struct.StructMethod;
import org.jetbrains.java.decompiler.struct.attr.StructAnnotationAttribute;
import org.jetbrains.java.decompiler.struct.attr.StructAnnotationParameterAttribute;
import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute;
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.util.VBStyleCollection;

import java.util.List;

public class IdeaNotNullHelper {


  public static boolean removeHardcodedChecks(Statement root, StructMethod mt) {

    boolean checks_removed = false;

    // parameter @NotNull annotations
    while (findAndRemoveParameterCheck(root, mt)) { // iterate until nothing found. Each invocation removes one parameter check.
      checks_removed = true;
    }

    // method @NotNull annotation
    while (findAndRemoveReturnCheck(root, mt)) { // iterate until nothing found. Each invocation handles one method exit check.
      checks_removed = true;
    }

    return checks_removed;
  }

  private static boolean findAndRemoveParameterCheck(Statement stat, StructMethod mt) {

    Statement st = stat.getFirst();
    while (st.type == Statement.TYPE_SEQUENCE) {
      st = st.getFirst();
    }

    if (st.type == Statement.TYPE_IF) {

      IfStatement ifstat = (IfStatement)st;
      Statement ifbranch = ifstat.getIfstat();

      Exprent if_condition = ifstat.getHeadexprent().getCondition();

      boolean is_notnull_check = false;

      // TODO: FUNCTION_NE also possible if reversed order (in theory)
      if (ifbranch != null &&
          if_condition.type == Exprent.EXPRENT_FUNCTION &&
          ((FunctionExprent)if_condition).getFunctype() == FunctionExprent.FUNCTION_EQ &&
          ifbranch.type == Statement.TYPE_BASICBLOCK &&
          ifbranch.getExprents().size() == 1 &&
          ifbranch.getExprents().get(0).type == Exprent.EXPRENT_EXIT) {

        FunctionExprent func = (FunctionExprent)if_condition;
        Exprent first_param = func.getLstOperands().get(0);
        Exprent second_param = func.getLstOperands().get(1);

        if (second_param.type == Exprent.EXPRENT_CONST &&
            second_param.getExprType().type == CodeConstants.TYPE_NULL) { // TODO: reversed parameter order
          if (first_param.type == Exprent.EXPRENT_VAR) {
            VarExprent var = (VarExprent)first_param;

            boolean thisvar = !mt.hasModifier(CodeConstants.ACC_STATIC);

            MethodDescriptor md = MethodDescriptor.parseDescriptor(mt.getDescriptor());
            VBStyleCollection<StructGeneralAttribute, String> attributes = mt.getAttributes();

            // parameter annotations
            StructAnnotationParameterAttribute param_annotations = (StructAnnotationParameterAttribute)attributes
              .getWithKey(StructGeneralAttribute.ATTRIBUTE_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS);
            if (param_annotations != null) {

              List<List<AnnotationExprent>> param_annotations_lists = param_annotations.getParamAnnotations();
              int method_param_number = md.params.length;

              int index = thisvar ? 1 : 0;
              for (int i = 0; i < method_param_number; i++) {

                if (index == var.getIndex()) {
                  if (param_annotations_lists.size() >= method_param_number - i) {
                    int shift = method_param_number -
                                param_annotations_lists
                                  .size(); // NOTE: workaround for compiler bug, count annotations starting with the last parameter

                    List<AnnotationExprent> annotations = param_annotations_lists.get(i - shift);

                    for (AnnotationExprent ann : annotations) {
                      if (ann.getClassname().equals("org/jetbrains/annotations/NotNull")) {
                        is_notnull_check = true;
                      }
                    }
                  }

                  break;
                }

                index += md.params[i].stack_size;
              }
            }
          }
        }
      }

      if (!is_notnull_check) {
        return false;
      }

      removeParameterCheck(stat, mt);

      return true;
    }

    return false;
  }

  private static void removeParameterCheck(Statement stat, StructMethod mt) {

    Statement st = stat.getFirst();
    while (st.type == Statement.TYPE_SEQUENCE) {
      st = st.getFirst();
    }

    IfStatement ifstat = (IfStatement)st;

    if (ifstat.getElsestat() == null) { // if
      // TODO:
    }
    else { // if - else

      StatEdge ifedge = ifstat.getIfEdge();
      StatEdge elseedge = ifstat.getElseEdge();

      Statement ifbranch = ifstat.getIfstat();
      Statement elsebranch = ifstat.getElsestat();

      ifstat.getFirst().removeSuccessor(ifedge);
      ifstat.getFirst().removeSuccessor(elseedge);

      ifstat.getStats().removeWithKey(ifbranch.id);
      ifstat.getStats().removeWithKey(elsebranch.id);

      if (!ifbranch.getAllSuccessorEdges().isEmpty()) {
        ifbranch.removeSuccessor(ifbranch.getAllSuccessorEdges().get(0));
      }

      ifstat.getParent().replaceStatement(ifstat, elsebranch);
      ifstat.getParent().setAllParent();
    }
  }

  private static boolean findAndRemoveReturnCheck(Statement stat, StructMethod mt) {

    VBStyleCollection<StructGeneralAttribute, String> attributes = mt.getAttributes();

    boolean is_notnull_check = false;

    // method annotation, refers to the return value
    StructAnnotationAttribute attr =
      (StructAnnotationAttribute)attributes.getWithKey(StructGeneralAttribute.ATTRIBUTE_RUNTIME_INVISIBLE_ANNOTATIONS);
    if (attr != null) {
      List<AnnotationExprent> annotations = attr.getAnnotations();

      for (AnnotationExprent ann : annotations) {
        if (ann.getClassname().equals("org/jetbrains/annotations/NotNull")) {
          is_notnull_check = true;
        }
      }
    }

    if (is_notnull_check) {
      return removeReturnCheck(stat, mt);
    }

    return false;
  }


  private static boolean removeReturnCheck(Statement stat, StructMethod mt) {

    Statement parent = stat.getParent();

    if (parent != null && parent.type == Statement.TYPE_IF && stat.type == Statement.TYPE_BASICBLOCK && stat.getExprents().size() == 1) {
      Exprent exprent = stat.getExprents().get(0);
      if (exprent.type == Exprent.EXPRENT_EXIT) {
        ExitExprent exit_exprent = (ExitExprent)exprent;
        if (exit_exprent.getExittype() == ExitExprent.EXIT_RETURN) {
          Exprent exprent_value = exit_exprent.getValue();
          //if(exprent_value.type == Exprent.EXPRENT_VAR) {
          //	VarExprent var_value = (VarExprent)exprent_value;

          IfStatement ifparent = (IfStatement)parent;
          Exprent if_condition = ifparent.getHeadexprent().getCondition();

          if (ifparent.getElsestat() == stat && if_condition.type == Exprent.EXPRENT_FUNCTION &&
              ((FunctionExprent)if_condition).getFunctype() == FunctionExprent.FUNCTION_EQ) { // TODO: reversed order possible (in theory)

            FunctionExprent func = (FunctionExprent)if_condition;
            Exprent first_param = func.getLstOperands().get(0);
            Exprent second_param = func.getLstOperands().get(1);

            StatEdge ifedge = ifparent.getIfEdge();
            StatEdge elseedge = ifparent.getElseEdge();

            Statement ifbranch = ifparent.getIfstat();
            Statement elsebranch = ifparent.getElsestat();

            if (second_param.type == Exprent.EXPRENT_CONST &&
                second_param.getExprType().type == CodeConstants.TYPE_NULL) { // TODO: reversed parameter order
              //if(first_param.type == Exprent.EXPRENT_VAR && ((VarExprent)first_param).getIndex() == var_value.getIndex()) {
              if (first_param.equals(exprent_value)) {        // TODO: check for absence of side effects like method invocations etc.
                if (ifbranch.type == Statement.TYPE_BASICBLOCK &&
                    ifbranch.getExprents().size() == 1 &&
                    // TODO: special check for IllegalStateException
                    ifbranch.getExprents().get(0).type == Exprent.EXPRENT_EXIT) {

                  ifparent.getFirst().removeSuccessor(ifedge);
                  ifparent.getFirst().removeSuccessor(elseedge);

                  ifparent.getStats().removeWithKey(ifbranch.id);
                  ifparent.getStats().removeWithKey(elsebranch.id);

                  if (!ifbranch.getAllSuccessorEdges().isEmpty()) {
                    ifbranch.removeSuccessor(ifbranch.getAllSuccessorEdges().get(0));
                  }

                  if (!ifparent.getFirst().getExprents().isEmpty()) {
                    elsebranch.getExprents().addAll(0, ifparent.getFirst().getExprents());
                  }

                  ifparent.getParent().replaceStatement(ifparent, elsebranch);
                  ifparent.getParent().setAllParent();

                  return true;
                }
              }
            }
          }
          //}
        }
      }
    }
    else if (parent != null &&
             parent.type == Statement.TYPE_SEQUENCE &&
             stat.type == Statement.TYPE_BASICBLOCK &&
             stat.getExprents().size() == 1) {
      Exprent exprent = stat.getExprents().get(0);
      if (exprent.type == Exprent.EXPRENT_EXIT) {
        ExitExprent exit_exprent = (ExitExprent)exprent;
        if (exit_exprent.getExittype() == ExitExprent.EXIT_RETURN) {
          Exprent exprent_value = exit_exprent.getValue();

          SequenceStatement sequence = (SequenceStatement)parent;
          int sequence_stats_number = sequence.getStats().size();

          if (sequence_stats_number > 1 &&
              sequence.getStats().getLast() == stat &&
              sequence.getStats().get(sequence_stats_number - 2).type == Statement.TYPE_IF) {

            IfStatement ifstat = (IfStatement)sequence.getStats().get(sequence_stats_number - 2);
            Exprent if_condition = ifstat.getHeadexprent().getCondition();

            if (ifstat.iftype == IfStatement.IFTYPE_IF && if_condition.type == Exprent.EXPRENT_FUNCTION &&
                ((FunctionExprent)if_condition).getFunctype() == FunctionExprent.FUNCTION_EQ) { // TODO: reversed order possible (in theory)

              FunctionExprent func = (FunctionExprent)if_condition;
              Exprent first_param = func.getLstOperands().get(0);
              Exprent second_param = func.getLstOperands().get(1);

              Statement ifbranch = ifstat.getIfstat();

              if (second_param.type == Exprent.EXPRENT_CONST &&
                  second_param.getExprType().type == CodeConstants.TYPE_NULL) { // TODO: reversed parameter order
                if (first_param.equals(exprent_value)) {        // TODO: check for absence of side effects like method invocations etc.
                  if (ifbranch.type == Statement.TYPE_BASICBLOCK &&
                      ifbranch.getExprents().size() == 1 &&
                      // TODO: special check for IllegalStateException
                      ifbranch.getExprents().get(0).type == Exprent.EXPRENT_EXIT) {

                    ifstat.removeSuccessor(ifstat.getAllSuccessorEdges().get(0)); // remove 'else' edge

                    if (!ifstat.getFirst().getExprents().isEmpty()) {
                      stat.getExprents().addAll(0, ifstat.getFirst().getExprents());
                    }

                    for (StatEdge edge : ifstat.getAllPredecessorEdges()) {

                      ifstat.removePredecessor(edge);
                      edge.getSource().changeEdgeNode(Statement.DIRECTION_FORWARD, edge, stat);
                      stat.addPredecessor(edge);
                    }

                    sequence.getStats().removeWithKey(ifstat.id);
                    sequence.setFirst(sequence.getStats().get(0));

                    return true;
                  }
                }
              }
            }
          }
        }
      }
    }


    for (Statement st : stat.getStats()) {
      if (removeReturnCheck(st, mt)) {
        return true;
      }
    }

    return false;
  }
}