From 76a052da52013877659c024c2fd7b720d9eb944b Mon Sep 17 00:00:00 2001
From: ribeaudc <ribeaudc>
Date: Thu, 14 Jun 2007 08:21:20 +0000
Subject: [PATCH] add: - getCurrentMethod() with its corresponding JUnit test.

SVN: 499
---
 .../cisd/common/utilities/ClassUtils.java     | 33 ++++++++++++++++
 .../cisd/common/utilities/ClassUtilsTest.java | 39 +++++++++++++++++++
 2 files changed, 72 insertions(+)
 create mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java

diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java
index 28af9cb2d00..6c222ec345f 100644
--- a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java
+++ b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java
@@ -17,10 +17,12 @@
 package ch.systemsx.cisd.common.utilities;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.List;
 
 import ch.systemsx.cisd.common.annotation.Mandatory;
+import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
 
 /**
  * Operations on classes using reflection.
@@ -61,4 +63,35 @@ public final class ClassUtils
         return list;
     }
 
+    /**
+     * Returns the currently called <code>Method</code>.
+     * <p>
+     * Returns <code>null</code> if none could be found.
+     * </p>
+     */
+    // TODO 2007.06.14 Christian Ribeaud: 'method.getName()' is not specific enough. You have to used kind of
+    // or part of 'Method.toGenericString()'.
+    public final static Method getCurrentMethod()
+    {
+        StackTraceElement[] elements = new Throwable().getStackTrace();
+        // Index 0 is *this* method
+        StackTraceElement element = elements[1];
+        String methodName = element.getMethodName();
+        try
+        {
+            Method[] methods = Class.forName(element.getClassName()).getMethods();
+            for (Method method : methods)
+            {
+                if (method.getName().equals(methodName))
+                {
+                    return method;
+                }
+            }
+            // SecurityException, ClassNotFoundException 
+        } catch (Exception ex)
+        {
+            throw CheckedExceptionTunnel.wrapIfNecessary(ex);
+        }
+        return null;
+    }
 }
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java
new file mode 100644
index 00000000000..a00ed2a2852
--- /dev/null
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2007 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.common.utilities;
+
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+/**
+ * Test cases for the {@link ClassUtils} class.
+ * 
+ * @author Christian Ribeaud
+ */
+public class ClassUtilsTest
+{
+
+    /**
+     * Test method for {@link ch.systemsx.cisd.common.utilities.ClassUtils#getCurrentMethod()}.
+     */
+    @Test
+    public final void testGetCurrentMethod()
+    {
+        assertEquals("testGetCurrentMethod", ClassUtils.getCurrentMethod().getName());
+    }
+
+}
-- 
GitLab