Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
11252342 | 2 | * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Alexandre Montplaisir - Initial API and implementation | |
a94410d9 | 10 | * Contributors: Matthew Khouzam - Addition to have more descriptive errors |
866e5b51 FC |
11 | *******************************************************************************/ |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.trace; | |
14 | ||
a94410d9 MK |
15 | import java.lang.reflect.Field; |
16 | ||
17 | import org.antlr.runtime.MismatchedTokenException; | |
18 | import org.antlr.runtime.RecognitionException; | |
19 | import org.eclipse.linuxtools.ctf.parser.CTFLexer; | |
20 | ||
866e5b51 FC |
21 | /** |
22 | * General exception that is thrown when there is a problem somewhere with the | |
23 | * CTF trace reader. | |
a94410d9 | 24 | * |
d37aaa7f FC |
25 | * @version 1.0 |
26 | * @author Alexandre Montplaisir | |
866e5b51 FC |
27 | */ |
28 | public class CTFReaderException extends Exception { | |
29 | ||
30 | private static final long serialVersionUID = 2065258365219777672L; | |
a94410d9 MK |
31 | private int fErrorLine = -1; |
32 | private String fFile = ""; //$NON-NLS-1$ | |
33 | private String fExpectingName = ""; //$NON-NLS-1$ | |
34 | private int fExpectedValue = -1; | |
35 | private String fActualName = ""; //$NON-NLS-1$ | |
36 | private int fActualValue = -1; | |
866e5b51 FC |
37 | |
38 | /** | |
39 | * Default constructor with no message. | |
40 | */ | |
41 | public CTFReaderException() { | |
42 | super(); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Constructor with an attached message. | |
47 | * | |
48 | * @param message | |
be6df2d8 | 49 | * The message attached to this exception |
866e5b51 FC |
50 | */ |
51 | public CTFReaderException(String message) { | |
52 | super(message); | |
53 | } | |
54 | ||
55 | /** | |
56 | * Re-throw an exception into this type. | |
57 | * | |
58 | * @param e | |
be6df2d8 | 59 | * The previous Exception we caught |
866e5b51 FC |
60 | */ |
61 | public CTFReaderException(Exception e) { | |
62 | super(e); | |
63 | } | |
a94410d9 MK |
64 | |
65 | /** | |
66 | * Re-throw the exception but read its data | |
67 | * | |
68 | * @param e | |
69 | * the previous recognition exception (Antlr specific) | |
70 | * @since 2.0 | |
71 | */ | |
72 | public CTFReaderException(RecognitionException e) { | |
73 | super(e); | |
74 | this.fErrorLine = e.line; | |
75 | this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata | |
76 | } | |
77 | ||
78 | /** | |
79 | * Re-throw the exception but read its data | |
80 | * | |
81 | * @param e | |
82 | * the previous recognition exception (Antlr specific) | |
83 | * @since 2.0 | |
84 | */ | |
85 | public CTFReaderException(MismatchedTokenException e){ | |
86 | super(e); | |
87 | this.fErrorLine = e.line; | |
88 | this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata | |
89 | parseMismatchedException(e); | |
90 | } | |
91 | ||
92 | private void parseMismatchedException(MismatchedTokenException m) { | |
93 | // Iterate through the tokens that are hidden in the CTFLexer | |
94 | // They are private static final int fields. | |
95 | for (Field f : CTFLexer.class.getDeclaredFields()) { | |
96 | f.setAccessible(true); | |
97 | String name; | |
98 | int value; | |
99 | try { | |
100 | name = f.getName(); | |
101 | final boolean isInt = (f.getType().isPrimitive()); | |
102 | if (isInt) { | |
103 | value = ((Integer) f.get(null)).intValue(); | |
104 | if (value == m.expecting) { | |
105 | this.fExpectingName = name; | |
106 | this.fExpectedValue = value; | |
107 | } | |
108 | if (value == m.c) { | |
109 | this.fActualName = name; | |
110 | this.fActualValue = value; | |
111 | } | |
112 | } | |
113 | } catch (NullPointerException e1) { | |
114 | // Pokemon, gotta catch em all! | |
115 | // actually useful since f may not have a | |
116 | // value | |
117 | } catch (IllegalArgumentException e1) { | |
118 | // Catch these exceptions (reflexion) | |
119 | } catch (IllegalAccessException e1) { | |
120 | // Catch these exceptions (reflexion) | |
121 | } | |
122 | if (!this.fExpectingName.isEmpty() && !this.fActualName.isEmpty()) { | |
123 | return; | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
128 | @Override | |
129 | public String getMessage() { | |
130 | final String message = super.getMessage(); | |
131 | if (fErrorLine == -1) { | |
132 | return message; | |
133 | } | |
134 | String expected = "" + this.fExpectedValue; //$NON-NLS-1$ | |
135 | String actual = "" + this.fActualValue; //$NON-NLS-1$ | |
136 | String newMessage = message.replaceAll(expected, this.fExpectingName); | |
137 | newMessage = newMessage.replaceAll(actual, this.fActualName); | |
138 | return newMessage + " at " + fFile + ":" + fErrorLine; //$NON-NLS-1$ //$NON-NLS-2$ | |
139 | } | |
866e5b51 | 140 | } |