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
.linuxtools
.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
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
21 import org
.eclipse
.linuxtools
.ctf
.parser
.CTFLexer
;
24 * CTF Reader exception but dealing with Antlr-specific parsing problems.
26 * It is separated from the main {@link CTFReaderException} - and is not part of
27 * the API - to isolate the Antlr-specific classes and avoid pushing that
28 * dependency to the users of this plugin.
30 * @author Matthew Khouzam
32 public class CtfAntlrException
extends CTFReaderException
{
34 private static final long serialVersionUID
= -7078624493350073777L;
36 private int fErrorLine
= -1;
37 private String fFile
= ""; //$NON-NLS-1$
38 private String fExpectingName
= ""; //$NON-NLS-1$
39 private int fExpectedValue
= -1;
40 private String fActualName
= ""; //$NON-NLS-1$
41 private int fActualValue
= -1;
44 * Re-throw the exception but read its data
47 * the previous recognition exception (Antlr specific)
49 public CtfAntlrException(RecognitionException e
) {
51 this.fErrorLine
= e
.line
;
52 this.fFile
= "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
56 * Re-throw the exception but read its data
59 * the previous recognition exception (Antlr specific)
61 public CtfAntlrException(MismatchedTokenException e
) {
63 this.fErrorLine
= e
.line
;
64 this.fFile
= "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
65 parseMismatchedException(e
);
69 * Re-throw the exception but read its data
72 * the previous rewrite exception (Antlr specific)
74 public CtfAntlrException(Exception e
) {
77 this.fFile
= "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
80 private void parseMismatchedException(MismatchedTokenException m
) {
81 // Iterate through the tokens that are hidden in the CTFLexer
82 // They are private static final int fields.
83 for (Field f
: CTFLexer
.class.getDeclaredFields()) {
84 f
.setAccessible(true);
89 final boolean isInt
= (f
.getType().isPrimitive());
91 value
= ((Integer
) f
.get(null)).intValue();
92 if (value
== m
.expecting
) {
93 this.fExpectingName
= name
;
94 this.fExpectedValue
= value
;
97 this.fActualName
= name
;
98 this.fActualValue
= value
;
101 } catch (NullPointerException e1
) {
102 // Pokemon, gotta catch em all!
103 // actually useful since f may not have a
105 } catch (IllegalArgumentException e1
) {
106 // Catch these exceptions (reflexion)
107 } catch (IllegalAccessException e1
) {
108 // Catch these exceptions (reflexion)
110 if (!this.fExpectingName
.isEmpty() && !this.fActualName
.isEmpty()) {
117 public String
getMessage() {
118 final String message
= super.getMessage();
119 if (fErrorLine
== -1) {
122 String expected
= "" + this.fExpectedValue
; //$NON-NLS-1$
123 String actual
= "" + this.fActualValue
; //$NON-NLS-1$
124 String newMessage
= message
.replaceAll(expected
, this.fExpectingName
);
125 newMessage
= newMessage
.replaceAll(actual
, this.fActualName
);
126 return newMessage
+ " at " + fFile
+ ":" + fErrorLine
; //$NON-NLS-1$ //$NON-NLS-2$