| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 Ericsson |
| 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: |
| 10 | * Alexandre Montplaisir - Initial API and implementation |
| 11 | * Matthew Khouzam - Addition to have more descriptive errors |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions; |
| 15 | |
| 16 | import java.lang.reflect.Field; |
| 17 | |
| 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; |
| 22 | |
| 23 | /** |
| 24 | * CTF Reader exception but dealing with Antlr-specific parsing problems. |
| 25 | * |
| 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. |
| 29 | * |
| 30 | * @author Matthew Khouzam |
| 31 | */ |
| 32 | public class CtfAntlrException extends CTFReaderException { |
| 33 | |
| 34 | private static final long serialVersionUID = -7078624493350073777L; |
| 35 | |
| 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; |
| 42 | |
| 43 | /** |
| 44 | * Re-throw the exception but read its data |
| 45 | * |
| 46 | * @param e |
| 47 | * the previous recognition exception (Antlr specific) |
| 48 | */ |
| 49 | public CtfAntlrException(RecognitionException e) { |
| 50 | super(e); |
| 51 | this.fErrorLine = e.line; |
| 52 | this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Re-throw the exception but read its data |
| 57 | * |
| 58 | * @param e |
| 59 | * the previous recognition exception (Antlr specific) |
| 60 | */ |
| 61 | public CtfAntlrException(MismatchedTokenException e) { |
| 62 | super(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); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Re-throw the exception but read its data |
| 70 | * |
| 71 | * @param e |
| 72 | * the previous rewrite exception (Antlr specific) |
| 73 | */ |
| 74 | public CtfAntlrException(Exception e) { |
| 75 | super(e); |
| 76 | this.fErrorLine = -1; |
| 77 | this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata |
| 78 | } |
| 79 | |
| 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); |
| 85 | String name; |
| 86 | int value; |
| 87 | try { |
| 88 | name = f.getName(); |
| 89 | final boolean isInt = (f.getType().isPrimitive()); |
| 90 | if (isInt) { |
| 91 | value = ((Integer) f.get(null)).intValue(); |
| 92 | if (value == m.expecting) { |
| 93 | this.fExpectingName = name; |
| 94 | this.fExpectedValue = value; |
| 95 | } |
| 96 | if (value == m.c) { |
| 97 | this.fActualName = name; |
| 98 | this.fActualValue = value; |
| 99 | } |
| 100 | } |
| 101 | } catch (NullPointerException e1) { |
| 102 | // Pokemon, gotta catch em all! |
| 103 | // actually useful since f may not have a |
| 104 | // value |
| 105 | } catch (IllegalArgumentException e1) { |
| 106 | // Catch these exceptions (reflexion) |
| 107 | } catch (IllegalAccessException e1) { |
| 108 | // Catch these exceptions (reflexion) |
| 109 | } |
| 110 | if (!this.fExpectingName.isEmpty() && !this.fActualName.isEmpty()) { |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public String getMessage() { |
| 118 | final String message = super.getMessage(); |
| 119 | if (fErrorLine == -1) { |
| 120 | return message; |
| 121 | } |
| 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$ |
| 127 | } |
| 128 | |
| 129 | } |