decompiler: fixed line mapping in try-catch block
[idea/community.git] / plugins / java-decompiler / engine / src / org / jetbrains / java / decompiler / modules / decompiler / stats / CatchAllStatement.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.jetbrains.java.decompiler.modules.decompiler.stats;
17
18 import org.jetbrains.java.decompiler.code.CodeConstants;
19 import org.jetbrains.java.decompiler.main.DecompilerContext;
20 import org.jetbrains.java.decompiler.main.TextBuffer;
21 import org.jetbrains.java.decompiler.main.collectors.BytecodeMappingTracer;
22 import org.jetbrains.java.decompiler.main.collectors.CounterContainer;
23 import org.jetbrains.java.decompiler.modules.decompiler.DecHelper;
24 import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
25 import org.jetbrains.java.decompiler.modules.decompiler.StatEdge;
26 import org.jetbrains.java.decompiler.modules.decompiler.exps.VarExprent;
27 import org.jetbrains.java.decompiler.modules.decompiler.vars.VarProcessor;
28 import org.jetbrains.java.decompiler.struct.gen.VarType;
29
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashSet;
33 import java.util.List;
34
35 public class CatchAllStatement extends Statement {
36
37   private Statement handler;
38
39   private boolean isFinally;
40
41   private VarExprent monitor;
42
43   private List<VarExprent> vars = new ArrayList<VarExprent>();
44
45   // *****************************************************************************
46   // constructors
47   // *****************************************************************************
48
49   private CatchAllStatement() {
50     type = Statement.TYPE_CATCHALL;
51   }
52
53   private CatchAllStatement(Statement head, Statement handler) {
54
55     this();
56
57     first = head;
58     stats.addWithKey(head, head.id);
59
60     this.handler = handler;
61     stats.addWithKey(handler, handler.id);
62
63     List<StatEdge> lstSuccs = head.getSuccessorEdges(STATEDGE_DIRECT_ALL);
64     if (!lstSuccs.isEmpty()) {
65       StatEdge edge = lstSuccs.get(0);
66       if (edge.getType() == StatEdge.TYPE_REGULAR) {
67         post = edge.getDestination();
68       }
69     }
70
71     vars.add(new VarExprent(DecompilerContext.getCounterContainer().getCounterAndIncrement(CounterContainer.VAR_COUNTER),
72                             new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/Throwable"),
73                             (VarProcessor)DecompilerContext.getProperty(DecompilerContext.CURRENT_VAR_PROCESSOR)));
74   }
75
76
77   // *****************************************************************************
78   // public methods
79   // *****************************************************************************
80
81   public static Statement isHead(Statement head) {
82
83     if (head.getLastBasicType() != Statement.LASTBASICTYPE_GENERAL) {
84       return null;
85     }
86
87     HashSet<Statement> setHandlers = DecHelper.getUniquePredExceptions(head);
88
89     if (setHandlers.size() != 1) {
90       return null;
91     }
92
93     for (StatEdge edge : head.getSuccessorEdges(StatEdge.TYPE_EXCEPTION)) {
94       Statement exc = edge.getDestination();
95
96       if (edge.getExceptions() == null && setHandlers.contains(exc) && exc.getLastBasicType() == LASTBASICTYPE_GENERAL) {
97         List<StatEdge> lstSuccs = exc.getSuccessorEdges(STATEDGE_DIRECT_ALL);
98         if (lstSuccs.isEmpty() || lstSuccs.get(0).getType() != StatEdge.TYPE_REGULAR) {
99
100           if (head.isMonitorEnter() || exc.isMonitorEnter()) {
101             return null;
102           }
103
104           if (DecHelper.checkStatementExceptions(Arrays.asList(head, exc))) {
105             return new CatchAllStatement(head, exc);
106           }
107         }
108       }
109     }
110
111     return null;
112   }
113
114   public TextBuffer toJava(int indent, BytecodeMappingTracer tracer) {
115     String new_line_separator = DecompilerContext.getNewLineSeparator();
116
117     TextBuffer buf = new TextBuffer();
118
119     buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer));
120
121     boolean labeled = isLabeled();
122     if (labeled) {
123       buf.appendIndent(indent).append("label").append(this.id.toString()).append(":").appendLineSeparator();
124       tracer.incrementCurrentSourceLine();
125     }
126
127     List<StatEdge> lstSuccs = first.getSuccessorEdges(STATEDGE_DIRECT_ALL);
128     if (first.type == TYPE_TRYCATCH && first.varDefinitions.isEmpty() && isFinally &&
129         !labeled && !first.isLabeled() && (lstSuccs.isEmpty() || !lstSuccs.get(0).explicit)) {
130       TextBuffer content = ExprProcessor.jmpWrapper(first, indent, true, tracer);
131       content.setLength(content.length() - new_line_separator.length());
132       tracer.incrementCurrentSourceLine(-1);
133       buf.append(content);
134     }
135     else {
136       buf.appendIndent(indent).append("try {").appendLineSeparator();
137       tracer.incrementCurrentSourceLine();
138       buf.append(ExprProcessor.jmpWrapper(first, indent + 1, true, tracer));
139       buf.appendIndent(indent).append("}");
140     }
141
142     buf.append(isFinally ? " finally" :
143                " catch (" + vars.get(0).toJava(indent, tracer) + ")").append(" {").appendLineSeparator();
144     tracer.incrementCurrentSourceLine();
145
146     if (monitor != null) {
147       buf.appendIndent(indent+1).append("if(").append(monitor.toJava(indent, tracer)).append(") {").appendLineSeparator();
148       tracer.incrementCurrentSourceLine();
149     }
150
151     buf.append(ExprProcessor.jmpWrapper(handler, indent + 1 + (monitor != null ? 1 : 0), true, tracer));
152
153     if (monitor != null) {
154       buf.appendIndent(indent + 1).append("}").appendLineSeparator();
155       tracer.incrementCurrentSourceLine();
156     }
157
158     buf.appendIndent(indent).append("}").appendLineSeparator();
159     tracer.incrementCurrentSourceLine();
160
161     return buf;
162   }
163
164   public void replaceStatement(Statement oldstat, Statement newstat) {
165
166     if (handler == oldstat) {
167       handler = newstat;
168     }
169
170     super.replaceStatement(oldstat, newstat);
171   }
172
173   public Statement getSimpleCopy() {
174
175     CatchAllStatement cas = new CatchAllStatement();
176
177     cas.isFinally = this.isFinally;
178
179     if (this.monitor != null) {
180       cas.monitor = new VarExprent(DecompilerContext.getCounterContainer().getCounterAndIncrement(CounterContainer.VAR_COUNTER),
181                                    VarType.VARTYPE_INT,
182                                    (VarProcessor)DecompilerContext.getProperty(DecompilerContext.CURRENT_VAR_PROCESSOR));
183     }
184
185     if (!this.vars.isEmpty()) {
186       // FIXME: WTF??? vars?!
187       vars.add(new VarExprent(DecompilerContext.getCounterContainer().getCounterAndIncrement(CounterContainer.VAR_COUNTER),
188                               new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/Throwable"),
189                               (VarProcessor)DecompilerContext.getProperty(DecompilerContext.CURRENT_VAR_PROCESSOR)));
190     }
191
192     return cas;
193   }
194
195   public void initSimpleCopy() {
196     first = stats.get(0);
197     handler = stats.get(1);
198   }
199
200   // *****************************************************************************
201   // getter and setter methods
202   // *****************************************************************************
203
204   public Statement getHandler() {
205     return handler;
206   }
207
208
209   public void setHandler(Statement handler) {
210     this.handler = handler;
211   }
212
213
214   public boolean isFinally() {
215     return isFinally;
216   }
217
218
219   public void setFinally(boolean isFinally) {
220     this.isFinally = isFinally;
221   }
222
223
224   public VarExprent getMonitor() {
225     return monitor;
226   }
227
228
229   public void setMonitor(VarExprent monitor) {
230     this.monitor = monitor;
231   }
232
233   public List<VarExprent> getVars() {
234     return vars;
235   }
236 }