ctf: Put Antlr-specific exceptions in a separate class
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Thu, 25 Apr 2013 21:54:02 +0000 (17:54 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Mon, 29 Apr 2013 18:30:18 +0000 (14:30 -0400)
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>

org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFReaderException.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Metadata.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/exceptions/CtfAntlrException.java [new file with mode: 0644]

index f43d81b4c4d0940d52fe0516decd89ad81ce8c9c..430c5b9f20aca3e2c19baeb3da8a14d171e91af9 100644 (file)
@@ -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$
-    }
 }
index 5daf5da74c106481ee89800e1b98d30f83d02686..57847803a7a56b77de1d83008de8c10f44dbde19 100644 (file)
@@ -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/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/exceptions/CtfAntlrException.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/exceptions/CtfAntlrException.java
new file mode 100644 (file)
index 0000000..e23d253
--- /dev/null
@@ -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$
+    }
+
+}
This page took 0.035131 seconds and 5 git commands to generate.