Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/DynamicInvoker.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap;
39
40 import java.lang.reflect.Method;
41 import java.util.Hashtable;
42
43 /**
44 * Utility class to dynamically find methods of a class and to invoke
45 * them
46 */
47 class DynamicInvoker {
48 static Object invokeMethod( Object obj,
49 String packageName,
50 String methodName,
51 Object[] args,
52 String[] argNames )
53 throws LDAPException {
54 try {
55 Method m = getMethod( packageName,
56 methodName,
57 argNames );
58 if ( m != null ) {
59 return m.invoke( obj, args );
60 } else {
61 System.err.println( "No such method: " +
62 packageName + "." + methodName );
63 }
64 } catch ( Exception e ) {
65 e.printStackTrace();
66 throw new LDAPException( "Invoking " + methodName + ": " +
67 e.toString(),
68 LDAPException.PARAM_ERROR );
69 }
70
71 return null;
72 }
73
74 static Method getMethod( String packageName,
75 String methodName,
76 String[] args ) throws LDAPException {
77 try {
78 Method method = null;
79 String suffix = "";
80 if ( args != null ) {
81 for ( int i = 0; i < args.length; i++ ) {
82 suffix = suffix + args[i].getClass().getName();
83 }
84 }
85 String key = packageName + "." + methodName + "." + suffix;
86 if ((method = m_methodLookup.get(key)) != null) {
87 return method;
88 }
89
90 Class<?> c = Class.forName( packageName );
91 Method[] m = c.getMethods();
92 for ( int i = 0; i < m.length; i++ ) {
93 Class<?>[] params = m[i].getParameterTypes();
94 if (m[i].getName().equals(methodName) &&
95 signatureCorrect(params, args)) {
96 m_methodLookup.put( key, m[i] );
97 return m[i];
98 }
99 }
100 throw new LDAPException( "Method " +
101 methodName +
102 " not found in " +
103 packageName );
104 } catch ( ClassNotFoundException e ) {
105 throw new LDAPException( "Class " + packageName + " not found" );
106 }
107 }
108
109 static private boolean signatureCorrect( Class<?> params[],
110 String args[] ) {
111 if ( args == null ) {
112 return true;
113 }
114 if ( params.length != args.length ) {
115 System.err.println( params.length + " method parameters vs " +
116 args.length + " expected" );
117 return false;
118 }
119 for ( int i = 0; i < params.length; i++ ) {
120 if ( !params[i].getName().equals(args[i]) ) {
121 System.err.println( params[i].getName() +
122 " method parameter vs " +
123 args[i] + " expected" );
124 return false;
125 }
126 }
127 return true;
128 }
129
130 private static Hashtable<String, Method> m_methodLookup = new Hashtable<>();
131 }