directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ersi...@apache.org
Subject svn commit: r414959 - /directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DirectoryClassUtils.java
Date Fri, 16 Jun 2006 23:32:28 GMT
Author: ersiner
Date: Fri Jun 16 16:32:28 2006
New Revision: 414959

URL: http://svn.apache.org/viewvc?rev=414959&view=rev
Log:
Added new Class Utils class.
Will merge other Class Utils classes to this one later.

Added:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DirectoryClassUtils.java

Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DirectoryClassUtils.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DirectoryClassUtils.java?rev=414959&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DirectoryClassUtils.java
(added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DirectoryClassUtils.java
Fri Jun 16 16:32:28 2006
@@ -0,0 +1,94 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   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 org.apache.directory.shared.ldap.util;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev:$
+ */
+public class DirectoryClassUtils
+{
+    
+    /**
+     * A replacement {@link java.lang.Class.getMethod} with extended functionality.
+     * 
+     * <p>
+     * This method returns parameter-list assignment-compatible method as well as
+     * exact-signature matching method.
+     * 
+     * @param clazz
+     * @param candidateMethodName
+     * @param candidateParameterTypes
+     * @return
+     * @throws NoSuchMethodException
+     */
+    public static Method getAssignmentCompatibleMethod( Class clazz,
+                                                        String candidateMethodName,
+                                                        Class[] candidateParameterTypes
+                                                      ) throws NoSuchMethodException
+    {
+        try
+        {
+            // Look for exactly the same signature.
+            clazz.getMethod( candidateMethodName, candidateParameterTypes );
+        }
+        catch ( SecurityException e ) { }
+        catch ( NoSuchMethodException e ) { }
+        
+        /**
+         * Look for the assignment-compatible signature.
+         */
+        
+        // Get all methods of the clazz.
+        Method[] methods = clazz.getMethods();
+        
+        // For each method of the clazz...
+        for ( int mx = 0; mx < methods.length; mx++ )
+        {
+            // ... Get parameter types list.
+            Class[] parameterTypes = methods[ mx ].getParameterTypes();
+            
+            // If parameter types list length mismatch...
+            if ( parameterTypes.length != candidateParameterTypes.length )
+            {
+                // ... Go on with the next method.
+                continue;
+            }
+            // If parameter types list length is OK...
+            // ... For each parameter of the method...
+            for ( int px = 0; px < parameterTypes.length; px++ )
+            {
+                // ... If the parameter is not assignment-compatible with the candidate parameter
type...
+                if ( ! parameterTypes[ px ].isAssignableFrom( candidateParameterTypes[ px
] ) )
+                {
+                    // ... Go on with the next method.
+                    break;
+                }
+            }
+            
+            // Return the only one possible and found method.
+            return methods[ mx ];
+        }
+        
+        throw new NoSuchMethodException();
+        
+    }
+
+}



Mime
View raw message