KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > retrace > StackTrace


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.retrace;
22
23 import java.io.*;
24 import java.util.*;
25
26 import proguard.obfuscate.MappingProcessor;
27
28
29 /**
30  * This class represents an obfuscated stack trace. It can read, de-obfuscate,
31  * and then write its contents.
32  *
33  * @author Eric Lafortune
34  */

35 class StackTrace implements MappingProcessor
36 {
37     // The stack trace settings.
38
private boolean verbose;
39
40     // The stack trace items.
41
private List stackTraceItems = new ArrayList();
42
43
44     /**
45      * Creates a new StackTrace.
46      * @param verbose specifies whether the de-obfuscated stack trace should
47      * be verbose.
48      */

49     public StackTrace(boolean verbose)
50     {
51         this.verbose = verbose;
52     }
53
54
55     /**
56      * Reads the stack trace file.
57      */

58     public void read(File stackTraceFile) throws IOException
59     {
60         LineNumberReader lineNumberReader = null;
61
62         try
63         {
64             Reader reader = stackTraceFile == null ?
65                 (Reader)new InputStreamReader(System.in) :
66                 (Reader)new BufferedReader(new FileReader(stackTraceFile));
67
68             lineNumberReader = new LineNumberReader(reader);
69
70             // Read the line in the stack trace.
71
while (true)
72             {
73                 String JavaDoc line = lineNumberReader.readLine();
74                 if (line == null)
75                 {
76                     break;
77                 }
78
79                 line = line.trim();
80                 if (line.length() == 0)
81                 {
82                     continue;
83                 }
84
85                 // Put the line in a stack trace item.
86
StackTraceItem item = new StackTraceItem(verbose);
87
88                 item.parse(line);
89
90                 stackTraceItems.add(item);
91             }
92         }
93         catch (IOException ex)
94         {
95             throw new IOException("Can't read stack trace (" + ex.getMessage() + ")");
96         }
97         finally
98         {
99             if (stackTraceFile != null &&
100                 lineNumberReader != null)
101             {
102                 try
103                 {
104                     lineNumberReader.close();
105                 }
106                 catch (IOException ex)
107                 {
108                 }
109             }
110         }
111     }
112
113
114     /**
115      * Prints out the de-obfuscated stack trace.
116      */

117     public void print()
118     {
119         // Delegate to each of the stack trace items.
120
for (int index = 0; index < stackTraceItems.size(); index++)
121         {
122             StackTraceItem item = (StackTraceItem)stackTraceItems.get(index);
123
124             item.print();
125         }
126     }
127
128
129     // Implementations for MappingProcessor.
130

131     public boolean processClassMapping(String JavaDoc className,
132                                        String JavaDoc newClassName)
133     {
134         // Delegate to each of the stack trace items.
135
boolean present = false;
136         for (int index = 0; index < stackTraceItems.size(); index++)
137         {
138             StackTraceItem item = (StackTraceItem)stackTraceItems.get(index);
139
140             present |= item.processClassMapping(className,
141                                                 newClassName);
142         }
143
144         return present;
145     }
146
147
148     public void processFieldMapping(String JavaDoc className,
149                                     String JavaDoc fieldType,
150                                     String JavaDoc fieldName,
151                                     String JavaDoc newFieldName)
152     {
153         // A stack trace never contains any fields.
154
}
155
156
157     public void processMethodMapping(String JavaDoc className,
158                                      int firstLineNumber,
159                                      int lastLineNumber,
160                                      String JavaDoc methodReturnType,
161                                      String JavaDoc methodNameAndArguments,
162                                      String JavaDoc newMethodName)
163     {
164         // Delegate to each of the stack trace items.
165
for (int index = 0; index < stackTraceItems.size(); index++)
166         {
167             StackTraceItem item = (StackTraceItem)stackTraceItems.get(index);
168
169             item.processMethodMapping(className,
170                                       firstLineNumber,
171                                       lastLineNumber,
172                                       methodReturnType,
173                                       methodNameAndArguments,
174                                       newMethodName);
175         }
176     }
177 }
178
Popular Tags