diff options
author | Alexandre Montplaisir | 2013-04-25 21:54:02 +0000 |
---|---|---|
committer | Alexandre Montplaisir | 2013-04-29 18:30:18 +0000 |
commit | 73a5b9de297d04db42bf5950269f28e69e34b612 (patch) | |
tree | 9816ba95853a43c3f6313b7047cde20fddc2f9c3 | |
parent | c2135413b1bc8a12e47cf22dcd4b745629bdfce2 (diff) | |
download | org.eclipse.linuxtools-73a5b9de297d04db42bf5950269f28e69e34b612.tar.gz org.eclipse.linuxtools-73a5b9de297d04db42bf5950269f28e69e34b612.tar.xz org.eclipse.linuxtools-73a5b9de297d04db42bf5950269f28e69e34b612.zip |
ctf: Put Antlr-specific exceptions in a separate class
Since CTFReaderException is in the public API, any external
plugin using it would have had to import the antlr-runtime
packages.
By isolating the Antlr-specific stuff in a separate, non-API
class, external plugins can happily continue to use
CTFReaderException without fear of leaking dependencies.
Change-Id: I27cccd60dd0ad1ca51a91c10b067a19b07c24f2e
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/12224
Tested-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
IP-Clean: Matthew Khouzam <matthew.khouzam@ericsson.com>
3 files changed, 124 insertions, 92 deletions
diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFReaderException.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFReaderException.java index f43d81b4c4..430c5b9f20 100644 --- a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFReaderException.java +++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFReaderException.java @@ -1,23 +1,17 @@ /******************************************************************************* - * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others + * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal * * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: Alexandre Montplaisir - Initial API and implementation - * Contributors: Matthew Khouzam - Addition to have more descriptive errors + * Contributors: + * Alexandre Montplaisir - Initial API and implementation *******************************************************************************/ package org.eclipse.linuxtools.ctf.core.trace; -import java.lang.reflect.Field; - -import org.antlr.runtime.MismatchedTokenException; -import org.antlr.runtime.RecognitionException; -import org.eclipse.linuxtools.ctf.parser.CTFLexer; - /** * General exception that is thrown when there is a problem somewhere with the * CTF trace reader. @@ -28,12 +22,6 @@ import org.eclipse.linuxtools.ctf.parser.CTFLexer; public class CTFReaderException extends Exception { private static final long serialVersionUID = 2065258365219777672L; - private int fErrorLine = -1; - private String fFile = ""; //$NON-NLS-1$ - private String fExpectingName = ""; //$NON-NLS-1$ - private int fExpectedValue = -1; - private String fActualName = ""; //$NON-NLS-1$ - private int fActualValue = -1; /** * Default constructor with no message. @@ -62,79 +50,4 @@ public class CTFReaderException extends Exception { super(e); } - /** - * Re-throw the exception but read its data - * - * @param e - * the previous recognition exception (Antlr specific) - * @since 2.0 - */ - public CTFReaderException(RecognitionException e) { - super(e); - this.fErrorLine = e.line; - this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata - } - - /** - * Re-throw the exception but read its data - * - * @param e - * the previous recognition exception (Antlr specific) - * @since 2.0 - */ - public CTFReaderException(MismatchedTokenException e){ - super(e); - this.fErrorLine = e.line; - this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata - parseMismatchedException(e); - } - - private void parseMismatchedException(MismatchedTokenException m) { - // Iterate through the tokens that are hidden in the CTFLexer - // They are private static final int fields. - for (Field f : CTFLexer.class.getDeclaredFields()) { - f.setAccessible(true); - String name; - int value; - try { - name = f.getName(); - final boolean isInt = (f.getType().isPrimitive()); - if (isInt) { - value = ((Integer) f.get(null)).intValue(); - if (value == m.expecting) { - this.fExpectingName = name; - this.fExpectedValue = value; - } - if (value == m.c) { - this.fActualName = name; - this.fActualValue = value; - } - } - } catch (NullPointerException e1) { - // Pokemon, gotta catch em all! - // actually useful since f may not have a - // value - } catch (IllegalArgumentException e1) { - // Catch these exceptions (reflexion) - } catch (IllegalAccessException e1) { - // Catch these exceptions (reflexion) - } - if (!this.fExpectingName.isEmpty() && !this.fActualName.isEmpty()) { - return; - } - } - } - - @Override - public String getMessage() { - final String message = super.getMessage(); - if (fErrorLine == -1) { - return message; - } - String expected = "" + this.fExpectedValue; //$NON-NLS-1$ - String actual = "" + this.fActualValue; //$NON-NLS-1$ - String newMessage = message.replaceAll(expected, this.fExpectingName); - newMessage = newMessage.replaceAll(actual, this.fActualName); - return newMessage + " at " + fFile + ":" + fErrorLine; //$NON-NLS-1$ //$NON-NLS-2$ - } } diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Metadata.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Metadata.java index 5daf5da74c..57847803a7 100644 --- a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Metadata.java +++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Metadata.java @@ -34,6 +34,7 @@ import org.eclipse.linuxtools.ctf.parser.CTFLexer; import org.eclipse.linuxtools.ctf.parser.CTFParser; import org.eclipse.linuxtools.ctf.parser.CTFParser.parse_return; import org.eclipse.linuxtools.internal.ctf.core.event.metadata.IOStructGen; +import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.CtfAntlrException; import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException; /** @@ -176,9 +177,9 @@ public class Metadata { } catch (ParseException e) { tempException = new CTFReaderException(e); } catch (MismatchedTokenException e) { - tempException = new CTFReaderException(e); + tempException = new CtfAntlrException(e); } catch (RecognitionException e) { - tempException = new CTFReaderException(e); + tempException = new CtfAntlrException(e); } /* Ghetto resource management. Java 7 will deliver us from this... */ diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/exceptions/CtfAntlrException.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/exceptions/CtfAntlrException.java new file mode 100644 index 0000000000..e23d2539d0 --- /dev/null +++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/exceptions/CtfAntlrException.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2013 Ericsson + * + * All rights reserved. This program and the accompanying materials are made + * available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexandre Montplaisir - Initial API and implementation + * Matthew Khouzam - Addition to have more descriptive errors + *******************************************************************************/ + +package org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions; + +import java.lang.reflect.Field; + +import org.antlr.runtime.MismatchedTokenException; +import org.antlr.runtime.RecognitionException; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.linuxtools.ctf.parser.CTFLexer; + +/** + * CTF Reader exception but dealing with Antlr-specific parsing problems. + * + * It is separated from the main {@link CTFReaderException} - and is not part of + * the API - to isolate the Antlr-specific classes and avoid pushing that + * dependency to the users of this plugin. + * + * @author Matthew Khouzam + */ +public class CtfAntlrException extends CTFReaderException { + + private static final long serialVersionUID = -7078624493350073777L; + + private int fErrorLine = -1; + private String fFile = ""; //$NON-NLS-1$ + private String fExpectingName = ""; //$NON-NLS-1$ + private int fExpectedValue = -1; + private String fActualName = ""; //$NON-NLS-1$ + private int fActualValue = -1; + + + /** + * Re-throw the exception but read its data + * + * @param e + * the previous recognition exception (Antlr specific) + */ + public CtfAntlrException(RecognitionException e) { + super(e); + this.fErrorLine = e.line; + this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata + } + + /** + * Re-throw the exception but read its data + * + * @param e + * the previous recognition exception (Antlr specific) + */ + public CtfAntlrException(MismatchedTokenException e){ + super(e); + this.fErrorLine = e.line; + this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata + parseMismatchedException(e); + } + + private void parseMismatchedException(MismatchedTokenException m) { + // Iterate through the tokens that are hidden in the CTFLexer + // They are private static final int fields. + for (Field f : CTFLexer.class.getDeclaredFields()) { + f.setAccessible(true); + String name; + int value; + try { + name = f.getName(); + final boolean isInt = (f.getType().isPrimitive()); + if (isInt) { + value = ((Integer) f.get(null)).intValue(); + if (value == m.expecting) { + this.fExpectingName = name; + this.fExpectedValue = value; + } + if (value == m.c) { + this.fActualName = name; + this.fActualValue = value; + } + } + } catch (NullPointerException e1) { + // Pokemon, gotta catch em all! + // actually useful since f may not have a + // value + } catch (IllegalArgumentException e1) { + // Catch these exceptions (reflexion) + } catch (IllegalAccessException e1) { + // Catch these exceptions (reflexion) + } + if (!this.fExpectingName.isEmpty() && !this.fActualName.isEmpty()) { + return; + } + } + } + + @Override + public String getMessage() { + final String message = super.getMessage(); + if (fErrorLine == -1) { + return message; + } + String expected = "" + this.fExpectedValue; //$NON-NLS-1$ + String actual = "" + this.fActualValue; //$NON-NLS-1$ + String newMessage = message.replaceAll(expected, this.fExpectingName); + newMessage = newMessage.replaceAll(actual, this.fActualName); + return newMessage + " at " + fFile + ":" + fErrorLine; //$NON-NLS-1$ //$NON-NLS-2$ + } + +} |