1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 * Matthew Khouzam - Addition to have more descriptive errors
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.exceptions
;
16 import java
.lang
.reflect
.Field
;
18 import org
.antlr
.runtime
.MismatchedTokenException
;
19 import org
.antlr
.runtime
.RecognitionException
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
21 import org
.eclipse
.tracecompass
.ctf
.parser
.CTFLexer
;
24 * CTF Reader exception but dealing with Antlr-specific parsing problems.
26 * It is separated from the main {@link CTFException} - and is not part of the
27 * API - to isolate the Antlr-specific classes and avoid pushing that dependency
28 * to the users of this plugin.
30 * @author Matthew Khouzam
32 public class CtfAntlrException
extends CTFException
{
34 private static final long serialVersionUID
= -7078624493350073777L;
36 private final int fErrorLine
;
37 private final String fFile
;
38 private String fExpectingName
= ""; //$NON-NLS-1$
39 private int fExpectedValue
= -1;
40 private String fActualName
= ""; //$NON-NLS-1$
41 private int fActualValue
= -1;
43 private final int fCharPositionInLine
;
46 * Re-throw the exception but read its data
49 * the previous recognition exception (Antlr specific)
51 public CtfAntlrException(MismatchedTokenException e
) {
54 fCharPositionInLine
= e
.charPositionInLine
;
55 fFile
= "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
56 parseMismatchedException(e
);
60 * Re-throw the exception but read its data
63 * the previous recognition exception (Antlr specific)
65 public CtfAntlrException(RecognitionException e
) {
68 fCharPositionInLine
= e
.charPositionInLine
;
69 fFile
= "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
73 * Re-throw the exception but read its data
76 * the previous rewrite exception (Antlr specific)
78 public CtfAntlrException(Exception e
) {
81 fCharPositionInLine
= -1;
82 fFile
= "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
85 private void parseMismatchedException(MismatchedTokenException m
) {
86 // Iterate through the tokens that are hidden in the CTFLexer
87 // They are private static final int fields.
88 for (Field f
: CTFLexer
.class.getDeclaredFields()) {
89 f
.setAccessible(true);
94 final boolean isInt
= (f
.getType().isPrimitive());
96 value
= ((Integer
) f
.get(null)).intValue();
97 if (value
== m
.expecting
) {
98 this.fExpectingName
= name
;
99 this.fExpectedValue
= value
;
102 this.fActualName
= name
;
103 this.fActualValue
= value
;
106 } catch (NullPointerException e1
) {
107 // Pokemon, gotta catch em all!
108 // actually useful since f may not have a
110 } catch (IllegalArgumentException e1
) {
111 // Catch these exceptions (reflexion)
112 } catch (IllegalAccessException e1
) {
113 // Catch these exceptions (reflexion)
115 if (!this.fExpectingName
.isEmpty() && !this.fActualName
.isEmpty()) {
122 public String
getMessage() {
123 final String message
= super.getMessage();
124 if (fErrorLine
== -1) {
127 String expected
= "" + this.fExpectedValue
; //$NON-NLS-1$
128 String actual
= "" + this.fActualValue
; //$NON-NLS-1$
129 String newMessage
= message
.replaceAll(expected
, this.fExpectingName
);
130 newMessage
= newMessage
.replaceAll(actual
, this.fActualName
);
131 return newMessage
+ " at " + fFile
+ ":" + fErrorLine
+ ":" + fCharPositionInLine
; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$