View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.util;
21  
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import java.lang.reflect.Method;
27  import java.util.Arrays;
28  
29  
30  /**
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public final class MethodUtils
34  {
35      /** The logger. */
36      private static final Logger LOG = LoggerFactory.getLogger( MethodUtils.class );
37  
38  
39      /**
40       * Private constructor.
41       */
42      private MethodUtils()
43      {
44      }
45  
46  
47      /**
48       * A replacement for {@link java.lang.Class#getMethod} with extended capability.
49       * 
50       * <p>
51       * This method returns parameter-list assignment-compatible method as well as
52       * exact-signature matching method.
53       * 
54       * @param clazz The class which will be queried for the method.
55       * @param candidateMethodName Name of the method been looked for.
56       * @param candidateParameterTypes Types of the parameters in the signature of the method being loooked for.
57       * @return The Method found.
58       * @throws NoSuchMethodException when the method cannot be found
59       */
60      public static Method getAssignmentCompatibleMethod( Class<?> clazz,
61          String candidateMethodName,
62          Class<?>[] candidateParameterTypes
63          ) throws NoSuchMethodException
64      {
65          if ( LOG.isDebugEnabled() )
66          {
67              StringBuilder buf = new StringBuilder();
68              buf.append( "call to getAssignmentCompatibleMethod(): \n\tclazz = " );
69              buf.append( clazz.getName() );
70              buf.append( "\n\tcandidateMethodName = " );
71              buf.append( candidateMethodName );
72              buf.append( "\n\tcandidateParameterTypes = " );
73  
74              for ( Class<?> argClass : candidateParameterTypes )
75              {
76                  buf.append( "\n\t\t" );
77                  buf.append( argClass.getName() );
78              }
79  
80              LOG.debug( buf.toString() );
81          }
82  
83          try
84          {
85              // Look for exactly the same signature.
86              Method exactMethod = clazz.getMethod( candidateMethodName, candidateParameterTypes );
87  
88              if ( exactMethod != null )
89              {
90                  return exactMethod;
91              }
92          }
93          catch ( Exception e )
94          {
95              LOG.info( "Could not find accessible exact match for candidateMethod {}", candidateMethodName, e );
96          }
97  
98          /**
99           * Look for the assignment-compatible signature.
100          */
101 
102         // Get all methods of the class.
103         Method[] methods = clazz.getMethods();
104 
105         // For each method of the class...
106         for ( int mx = 0; mx < methods.length; mx++ )
107         {
108             // If the method name does not match...
109             if ( !candidateMethodName.equals( methods[mx].getName() ) )
110             {
111                 // ... Go on with the next method.
112                 continue;
113             }
114 
115             // ... Get parameter types list.
116             Class<?>[] parameterTypes = methods[mx].getParameterTypes();
117 
118             // If parameter types list length mismatch...
119             if ( parameterTypes.length != candidateParameterTypes.length )
120             {
121                 // ... Go on with the next method.
122                 continue;
123             }
124             // If parameter types list length is OK...
125             // ... For each parameter of the method...
126             for ( int px = 0; px < parameterTypes.length; px++ )
127             {
128                 // ... If the parameter is not assignment-compatible with the candidate parameter type...
129                 if ( !parameterTypes[px].isAssignableFrom( candidateParameterTypes[px] ) )
130                 {
131                     // ... Go on with the next method.
132                     break;
133                 }
134             }
135 
136             // Return the only one possible and found method.
137             return methods[mx];
138         }
139 
140         throw new NoSuchMethodException( clazz.getName() + "." + candidateMethodName
141             + "(" + Arrays.toString( candidateParameterTypes ) + ")" );
142     }
143 }