[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

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



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.
---
During refactoring, in order to disambiguate between the processError
method taking a Libvirt object as a parameter and the generic
processError<T> method, the processError method is specialized for any
type extending from PointerType (such as DomainPointer, ConnectionPointer
et cetera), for Pointer and for String. This will be removed later.

 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.8.5.2.msysgit.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]