Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / derby-10.3.2.1 / java / engine / org / apache / derby / iapi / services / diag / DiagnosticUtil.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/iapi/services/diag/DiagnosticUtil.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/iapi/services/diag/DiagnosticUtil.java
new file mode 100644 (file)
index 0000000..a834e14
--- /dev/null
@@ -0,0 +1,159 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.iapi.services.diag.DiagnosticUtil\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to you under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.services.diag;\r
+\r
+/**\r
+\r
+The Diagnostic framework is meant to provide a way to include as much\r
+diagnostic capability within the distributed release of the Derby\r
+product without adversely affecting the runtime speed or foot print of\r
+a running configuration that needs not use this information.\r
+\r
+In order to decrease the class size of running objects diagnostic information\r
+should be put in "helper" classes.  So to provide diagnostic capabiility\r
+on the implementation of class Foo.java create a class D_Foo.java.  Class\r
+D_Foo must implement the Diagnosticable interface.  \r
+\r
+This class provide utility functions to get at the information provided by\r
+the D_* helper class:\r
+    findDiagnostic() - given and object "obj", get an instance of D_obj. \r
+    toDiagString()   - return the "best" diagnostic string available about\r
+                       a given object.\r
+\r
+**/\r
+\r
+public class DiagnosticUtil\r
+{\r
+    /* Constructors for This class: */\r
+    private DiagnosticUtil()\r
+    {\r
+    }\r
+\r
+    /* Private/Protected methods of This class: */\r
+\r
+    /**\r
+     * Given an object return instance of the diagnostic object for this class.\r
+     * <p>\r
+     * Given an object this routine will determine the classname of the object\r
+     * and then try to instantiate a new instance of the diagnostic object\r
+     * for this class by prepending on "D_" to the last element of theclassname.\r
+          If no matching class is found then the same lookup is made on the super-class\r
+          of the object, looking all the way up the hierachy until a diagnostic class\r
+          is found.\r
+        * <BR>\r
+          This routine will call "init(ref)" on the new instance and then return the new instance.\r
+     *\r
+        * @return A new instance of the diagnostic object for input object, or\r
+     *         null if one could not be found for some reason.\r
+     *\r
+     * @param ref   The object which to build the diagnostic object for.\r
+     **/\r
+    public static Diagnosticable findDiagnostic(Object ref)\r
+    {\r
+        Class refClass = ref.getClass();\r
+\r
+               for (;;) {\r
+                       try \r
+                       {\r
+                               String className = refClass.getName();\r
+                               int lastDot = className.lastIndexOf('.') + 1;\r
+                               String          diagClassName = \r
+                                       className.substring(0, lastDot) + \r
+                                       "D_" + className.substring(lastDot);\r
+\r
+                               Class diagClass;\r
+                               \r
+                               try {\r
+                                       diagClass = Class.forName(diagClassName);\r
+                                       //Added by Jeff Huang\r
+                                       //TODO: FIXIT\r
+                               } catch (ClassNotFoundException cnfe) {\r
+\r
+                                       // try the super-class of the object\r
+                                       refClass = refClass.getSuperclass();\r
+                                       if (refClass == null)\r
+                                               return null;\r
+\r
+                                       continue;\r
+                               }\r
+\r
+\r
+                               Diagnosticable diag_obj = (Diagnosticable) diagClass.newInstance();\r
+                               //Added by Jeff Huang\r
+                               //TODO: FIXIT\r
+                               \r
+                               diag_obj.init(ref);\r
+\r
+                               return diag_obj;\r
+                       }\r
+                       catch (Exception e)\r
+                       {\r
+                               return null;\r
+                       }\r
+               }\r
+       }\r
+\r
+    /**\r
+     * Return a diagnostic string associated with an object.\r
+     * <p>\r
+     * A utility interface to use if you just want to print a single string \r
+     * that represents the object in question.  In following order this routine\r
+     * will deliver the string to use:\r
+     * \r
+     *     1) find diagnostic help class, and use class.diag()\r
+     *     2) else just use class.toString()\r
+     *\r
+     * <p>\r
+     *\r
+        * @return The string describing the class input.\r
+     *\r
+     * @param obj The object to print out.\r
+     *\r
+     **/\r
+    public static String toDiagString(Object obj)\r
+    {\r
+        String ret_string = null;\r
+\r
+               if (obj == null) return "null";\r
+        \r
+        try \r
+        {\r
+            Diagnosticable diag = DiagnosticUtil.findDiagnostic(obj);\r
+            if (diag != null)\r
+                ret_string = diag.diag();\r
+        }\r
+        catch (Throwable t)\r
+        {\r
+            // do nothing, ret_string should still be null on error\r
+        }\r
+\r
+        if (ret_string == null)\r
+        {\r
+            ret_string = obj.toString();\r
+        }\r
+\r
+        return(ret_string);\r
+    }\r
+\r
+    /* Public Methods of This class: */\r
+    /* Public Methods of XXXX class: */\r
+}\r