in src/org/jetbrains/java/decompiler/modules/decompiler/IdeaNotNullHelper.java [208:346]
private static boolean removeReturnCheck(Statement stat, StructMethod mt) {
Statement parent = stat.getParent();
if (parent != null && parent.type == StatementType.IF && stat.type == StatementType.BASIC_BLOCK && 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().getType() == 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 == StatementType.BASIC_BLOCK &&
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 == StatementType.SEQUENCE &&
stat.type == StatementType.BASIC_BLOCK &&
stat.getExprents() !=null &&
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 == StatementType.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().getType() == 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 == StatementType.BASIC_BLOCK &&
ifbranch.getExprents().size() == 1 &&
// TODO: special check for IllegalStateException
(ifbranch.getExprents().get(0).type == Exprent.EXPRENT_EXIT ||
(ifbranch.getExprents().get(0) instanceof InvocationExprent invocationExprent &&
invocationExprent.getName() != null &&
invocationExprent.getName().startsWith(REPORT_NOT_NULL)))) {
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(EdgeDirection.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;
}