Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 26334 invoked from network); 16 Jun 2006 23:32:50 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 16 Jun 2006 23:32:50 -0000 Received: (qmail 10403 invoked by uid 500); 16 Jun 2006 23:32:50 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 10356 invoked by uid 500); 16 Jun 2006 23:32:50 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 10345 invoked by uid 99); 16 Jun 2006 23:32:49 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 16 Jun 2006 16:32:49 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 16 Jun 2006 16:32:49 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id CC1311A9842; Fri, 16 Jun 2006 16:32:28 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@directory.apache.org From: ersiner@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060616233228.CC1311A9842@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 Apache Directory Project + * @version $Rev:$ + */ +public class DirectoryClassUtils +{ + + /** + * A replacement {@link java.lang.Class.getMethod} with extended functionality. + * + *

+ * 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(); + + } + +}