[libvirt] [libvirt-java] [PATCH 15/65] Start refactoring of error handling

Claudio Bley cbley at av-test.de
Thu Feb 13 15:22:23 UTC 2014


Almost every class contains a processError() method with an identical
definition, just forwarding the call to ErrorHandler.processError(Libvirt).

This function is always called after a libvirt function call (as per its
javadoc comment).

But, actually, there's no use in always calling processError when there was
no error signalled by the libvirt function having been called. This is just
a waste of CPU cycles.

Furthermore, it's more than ugly that the error handling is littered all
over the place in every class.

This patch lays ground for generalizing the error handling in a common
place and removing those functions from the individual classes.

Basically, this copies the processError(int) and processError<T>(T)
methods from the Connect class to the ErrorHandler class as static
methods.

It deprecates the processError(Libvirt) method, which will be removed
eventually in a later patch.

Signed-off-by: Claudio Bley <cbley at av-test.de>
---
 src/main/java/org/libvirt/ErrorHandler.java |   47 ++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/libvirt/ErrorHandler.java b/src/main/java/org/libvirt/ErrorHandler.java
index 434c85d..8dcac8f 100644
--- a/src/main/java/org/libvirt/ErrorHandler.java
+++ b/src/main/java/org/libvirt/ErrorHandler.java
@@ -3,6 +3,11 @@ package org.libvirt;
 import org.libvirt.jna.Libvirt;
 import org.libvirt.jna.virError;
 
+import static org.libvirt.Library.libvirt;
+
+import com.sun.jna.Pointer;
+import com.sun.jna.PointerType;
+
 /**
  * Utility class which processes the last error from the libvirt library. It
  * turns errors into Libvirt Exceptions.
@@ -18,7 +23,12 @@ public class ErrorHandler {
      *            the active connection
      * @throws LibvirtException
      */
-    public static void processError(Libvirt libvirt) throws LibvirtException {
+    @Deprecated
+    static void processError(Libvirt libvirt) throws LibvirtException {
+        processError();
+    }
+
+    private static final void processError() throws LibvirtException {
         virError vError = libvirt.virGetLastError();
         if (vError != null) {
             Error error = new Error(vError);
@@ -30,4 +40,39 @@ public class ErrorHandler {
             }
         }
     }
+
+    /**
+     * Calls {@link #processError()} when the given libvirt return code
+     * indicates an error.
+     *
+     * @param  ret libvirt return code, indicating error if -1.
+     * @return {@code ret}
+     * @throws LibvirtException
+     */
+    static final int processError(int ret) throws LibvirtException {
+        if (ret == -1) processError();
+        return ret;
+    }
+
+    /**
+     * Calls {@link #processError()} if {@code arg} is null.
+     *
+     * @param  arg  An arbitrary object returned by libvirt.
+     * @return {@code arg}
+     * @throws LibvirtException
+     */
+    static final <T extends PointerType> T processError(T arg) throws LibvirtException {
+        if (arg == null) processError();
+        return arg;
+    }
+
+    static final Pointer processError(Pointer arg) throws LibvirtException {
+        if (arg == null) processError();
+        return arg;
+    }
+
+    static final String processError(String str) throws LibvirtException {
+        if (str == null) processError();
+        return str;
+    }
 }
-- 
1.7.9.5




More information about the libvir-list mailing list