# HG changeset patch
# User Ernst Reissner <rei3ner@arcor.de>
# Date 1471276054 -7200
#      Mon Aug 15 17:47:34 2016 +0200
# Node ID 51553d55681f02bea229a7c2366c68ba7bbc5720
# Parent  eaa3863b3e71fa69bd65de549f8b3ba775c39a9c
Redesign: extracted common code from
getConstructors(Class), , getMethods(Class) and getFields(Class)
into new methods appendModifiers and appendParamsThrwbs.

diff -r eaa3863b3e71 -r 51553d55681f scripts/java/org/octave/ClassHelper.java
--- a/scripts/java/org/octave/ClassHelper.java	Mon Aug 15 16:13:02 2016 +0200
+++ b/scripts/java/org/octave/ClassHelper.java	Mon Aug 15 17:47:34 2016 +0200
@@ -34,6 +34,8 @@
 import java.lang.reflect.Array;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Modifier;
+import java.lang.reflect.Member;
+import java.lang.reflect.Executable;
 
 //! Collection of methods being the core of the octave functions
 //! forming Octave's Java Interface.
@@ -237,6 +239,60 @@
     return file.toURI ().toURL ();
   }
 
+  //! Append to @c sb the modifiers of @c mem except @c public.
+  //!
+  //! @param mem
+  //!    a member with at least modifier @c public.
+  //! @param sb
+  //!    a string builder to append the relevant modifiers of @c mem
+  //!    as a string.
+  private static void appendModifiers(Member mem, StringBuffer sb)
+  {
+    int mod = mem.getModifiers () ^ Modifier.PUBLIC;
+    if (mod != 0)
+      {
+        sb.append (Modifier.toString(mod));
+        sb.append (' ');
+      }
+  }
+
+  //! Append to @c sb the list of parameters and of throwables of @c exe
+  //! in a form as usual in signatures.
+  //!
+  //! @param exe
+  //!    an executable, i.e. either a constructor or a method.
+  //! @param sb
+  //!    a string builder to append the relevant lists. 
+  private static void appendParamsThrwbs(Executable exe, StringBuffer sb)
+  {
+    // append parameter list
+    sb.append ('(');
+    Class theParameter[] = exe.getParameterTypes ();
+    if (theParameter.length > 0)
+      {
+        sb.append (theParameter[0].getCanonicalName ());
+        for (int j = 1; j < theParameter.length; j++)
+          {
+            sb.append (", ");
+            sb.append (theParameter[j].getCanonicalName ());
+          }
+      }
+    sb.append (')');
+
+    // append list of throwables if not empty
+    Class theExceptions[] = exe.getExceptionTypes ();
+    if (theExceptions.length > 0)
+      {
+        sb.append (" throws ");
+        sb.append (theExceptions[0].getCanonicalName ());
+        for (int j = 1; j < theExceptions.length; j++)
+          {
+            sb.append (", ");
+            sb.append (theExceptions[j].getCanonicalName ());
+          }
+      }
+  }
+
   // new -MH-
   //! Return a list of string representations of public methods
   //! of the class with the given class name @c classname
@@ -318,11 +374,9 @@
   private static String getMethods (Class klass, boolean isFull)
   {
     StringBuffer sb = new StringBuffer ();
-
     Method[] methods = klass.getMethods ();
     Method theMethod;
-    int mod;
-    for (int i = 0; i < methods.length; i++)
+     for (int i = 0; i < methods.length; i++)
       {
         theMethod = methods[i];
         if (i > 0)
@@ -332,15 +386,10 @@
 
         if (isFull)
           {
-            // the modifiers except 'public' 
-            mod = theMethod.getModifiers () ^ Modifier.PUBLIC;
-            if (mod != 0)
-              {
-                sb.append (Modifier.toString(mod));
-                sb.append (' ');
-              }
-
-            // the return type 
+            // append the modifiers except 'public'...
+            appendModifiers(theMethod, sb);
+  
+            // ...and the return type 
             sb.append (theMethod.getReturnType ().getCanonicalName ());
             sb.append (' ');
           }
@@ -350,31 +399,8 @@
 
         if (isFull)
           {
-            // parameter list
-            sb.append ('(');
-            Class theParameter[] = theMethod.getParameterTypes ();
-            for (int j = 0; j < theParameter.length; j++)
-              {
-                if (j > 0)
-                  {
-                    sb.append (", ");
-                  }
-                sb.append (theParameter[j].getCanonicalName ());
-              }
-            sb.append (')');
-
-            // exceptions
-            Class theExceptions[] = theMethod.getExceptionTypes ();
-            if (theExceptions.length > 0)
-              {
-                sb.append (" throws ");
-                sb.append (theExceptions[0].getCanonicalName ());
-                for (int j = 1; j < theExceptions.length; j++)
-                  {
-                    sb.append (", ");
-                    sb.append (theExceptions[j].getCanonicalName ());
-                  }
-              }
+            // append the list of parameters and throwables 
+            appendParamsThrwbs(theMethod, sb);
           }
       }
 
@@ -411,11 +437,9 @@
   private static String getFields (Class klass, boolean isFull)
   {
     StringBuffer sb = new StringBuffer ();
-
     Field[] fields = klass.getFields ();
     Field theField;
-    int mod;
-    for (int i = 0; i < theField.length; i++)
+    for (int i = 0; i < fields.length; i++)
       {
         theField = fields[i];
         if (i > 0)
@@ -425,20 +449,14 @@
 
         if (isFull)
           {
-            // the modifiers except 'public' 
-            mod = theField.getModifiers () ^ Modifier.PUBLIC;
-            if (mod != 0)
-              {
-                sb.append (Modifier.toString(mod));
-                sb.append (' ');
-              }
-
-            // the type 
+            // append the modifiers except 'public' and...
+             appendModifiers(theField, sb);
+            // ... its type
             sb.append (theField.getType ().getCanonicalName ());
             sb.append (' ');
             }
 
-        // the name 
+        // append the name 
         sb.append (theField.getName ());
       }
 
@@ -515,15 +533,11 @@
   //!    the parameter list and the throws-declaration.
   //!    For class <code>java.math.BigDecimal</code> this is 
   //!    <code>(String);(double)</code>.
-
-  // all are java.lang.reflect.Member
   private static String getConstructors (Class klass)
   {
     StringBuffer sb = new StringBuffer ();
-
     Constructor[] constructors = klass.getConstructors ();
     Constructor theConstructor;
-    int mod;
     for (int i = 0; i < constructors.length; i++)
       {
         theConstructor = constructors[i];
@@ -532,40 +546,10 @@
             sb.append (';');
           }
 
-        // the modifiers except 'public' 
-        mod = theConstructor.getModifiers () ^ Modifier.PUBLIC;
-        if (mod != 0)
-          {
-            sb.append (Modifier.toString(mod));
-            sb.append (' ');
-          }
-
-
-        // parameter list
-        sb.append ('(');
-        Class theParameter[] = theConstructor.getParameterTypes ();
-        for (int j = 0; j < theParameter.length; j++)
-          {
-            if (j > 0)
-              {
-                sb.append (", ");
-              }
-            sb.append (theParameter[j].getCanonicalName ());
-          }
-        sb.append (')');
-
-        // exceptions
-        Class theExceptions[] = theConstructor.getExceptionTypes ();
-        if (theExceptions.length > 0)
-          {
-            sb.append (" throws ");
-            sb.append (theExceptions[0].getCanonicalName ());
-            for (int j = 1; j < theExceptions.length; j++)
-              {
-                sb.append (", ");
-                sb.append (theExceptions[j].getCanonicalName ());
-              }
-          }
+        // append the modifiers except 'public' ...
+        appendModifiers(theConstructor, sb);
+        // ... and the list of parameters and throwables 
+        appendParamsThrwbs(theConstructor, sb);
       }
     return sb.toString();
   }
