1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.jocl;
  17. import java.lang.reflect.Constructor;
  18. import java.lang.reflect.InvocationTargetException;
  19. /**
  20. * Miscellaneous {@link Constructor} related utility functions.
  21. *
  22. * @author Rodney Waldhoff
  23. * @version $Revision: 1.6 $ $Date: 2004/02/28 12:18:18 $
  24. */
  25. public class ConstructorUtil {
  26. /**
  27. * Returns a {@link Constructor} for the given method signature, or <tt>null</tt>
  28. * if no such <tt>Constructor</tt> can be found.
  29. *
  30. * @param type the (non-<tt>null</tt>) type of {@link Object} the returned {@link Constructor} should create
  31. * @param argTypes a non-<tt>null</tt> array of types describing the parameters to the {@link Constructor}.
  32. * @return a {@link Constructor} for the given method signature, or <tt>null</tt>
  33. * if no such <tt>Constructor</tt> can be found.
  34. * @see #invokeConstructor
  35. */
  36. public static Constructor getConstructor(Class type, Class[] argTypes) {
  37. if(null == type || null == argTypes) {
  38. throw new NullPointerException();
  39. }
  40. Constructor ctor = null;
  41. try {
  42. ctor = type.getConstructor(argTypes);
  43. } catch(Exception e) {
  44. ctor = null;
  45. }
  46. if(null == ctor) {
  47. // no directly declared matching constructor,
  48. // look for something that will work
  49. // XXX this should really be more careful to
  50. // adhere to the jls mechanism for late binding
  51. Constructor[] ctors = type.getConstructors();
  52. for(int i=0;i<ctors.length;i++) {
  53. Class[] paramtypes = ctors[i].getParameterTypes();
  54. if(paramtypes.length == argTypes.length) {
  55. boolean canuse = true;
  56. for(int j=0;j<paramtypes.length;j++) {
  57. if(paramtypes[j].isAssignableFrom(argTypes[j])) {
  58. continue;
  59. } else {
  60. canuse = false;
  61. break;
  62. }
  63. }
  64. if(canuse == true) {
  65. ctor = ctors[i];
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. return ctor;
  72. }
  73. /**
  74. * Creates a new instance of the specified <tt><i>type</i></tt>
  75. * using a {@link Constructor} described by the given parameter types
  76. * and values.
  77. *
  78. * @param type the type of {@link Object} to be created
  79. * @param argTypes a non-<tt>null</tt> array of types describing the parameters to the {@link Constructor}.
  80. * @param argValues a non-<tt>null</tt> array containing the values of the parameters to the {@link Constructor}.
  81. * @return a new instance of the specified <tt><i>type</i></tt>
  82. * using a {@link Constructor} described by the given parameter types
  83. * and values.
  84. * @exception InstantiationException
  85. * @exception IllegalAccessException
  86. * @exception InvocationTargetException
  87. */
  88. public static Object invokeConstructor(Class type, Class[] argTypes, Object[] argValues) throws InstantiationException, IllegalAccessException, InvocationTargetException {
  89. return ConstructorUtil.getConstructor(type,argTypes).newInstance(argValues);
  90. }
  91. }