1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Alexandre Montplaisir - Initial API and implementation
10 * Contributors: Matthew Khouzam - Addition to have more descriptive errors
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
15 import java
.lang
.reflect
.Field
;
17 import org
.antlr
.runtime
.MismatchedTokenException
;
18 import org
.antlr
.runtime
.RecognitionException
;
19 import org
.eclipse
.linuxtools
.ctf
.parser
.CTFLexer
;
22 * General exception that is thrown when there is a problem somewhere with the
26 * @author Alexandre Montplaisir
28 public class CTFReaderException
extends Exception
{
30 private static final long serialVersionUID
= 2065258365219777672L;
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;
39 * Default constructor with no message.
41 public CTFReaderException() {
46 * Constructor with an attached message.
49 * The message attached to this exception
51 public CTFReaderException(String message
) {
56 * Re-throw an exception into this type.
59 * The previous Exception we caught
61 public CTFReaderException(Exception e
) {
66 * Re-throw the exception but read its data
69 * the previous recognition exception (Antlr specific)
72 public CTFReaderException(RecognitionException e
) {
74 this.fErrorLine
= e
.line
;
75 this.fFile
= "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
79 * Re-throw the exception but read its data
82 * the previous recognition exception (Antlr specific)
85 public CTFReaderException(MismatchedTokenException 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
);
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);
101 final boolean isInt
= (f
.getType().isPrimitive());
103 value
= ((Integer
) f
.get(null)).intValue();
104 if (value
== m
.expecting
) {
105 this.fExpectingName
= name
;
106 this.fExpectedValue
= value
;
109 this.fActualName
= name
;
110 this.fActualValue
= value
;
113 } catch (NullPointerException e1
) {
114 // Pokemon, gotta catch em all!
115 // actually useful since f may not have a
117 } catch (IllegalArgumentException e1
) {
118 // Catch these exceptions (reflexion)
119 } catch (IllegalAccessException e1
) {
120 // Catch these exceptions (reflexion)
122 if (!this.fExpectingName
.isEmpty() && !this.fActualName
.isEmpty()) {
129 public String
getMessage() {
130 final String message
= super.getMessage();
131 if (fErrorLine
== -1) {
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$