Get a compatible constructor for the given value type : Constructor « Reflection « Java Tutorial






import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

public class Main {

  /////////////////////////////////////////////////////////////////////////
  //                           Coercion Methods                          //
  /////////////////////////////////////////////////////////////////////////

  /**
   * Get a compatible constructor for the given value type
   *
   * @param type       Class to look for constructor in
   * @param valueType  Argument type for constructor
   * @return           Constructor or null
   */
  public static Constructor getCompatibleConstructor(final Class type,
                                                     final Class valueType)
  {
     // first try and find a constructor with the exact argument type
     try {
        return type.getConstructor(new Class[] { valueType });
     }
     catch (Exception ignore) {
        // if the above failed, then try and find a constructor with
        // an compatible argument type

        // get an array of compatible types
        Class[] types = type.getClasses();

        for (int i=0; i<types.length; i++) {
           try {
              return type.getConstructor(new Class[] { types[i] });
           }
           catch (Exception ignore2) {}
        }
     }

     // if we get this far, then we can't find a compatible constructor
     return null;
  }
}








7.3.Constructor
7.3.1.Get all constructors or by parameters
7.3.2.Demonstrates use of Constructor objects
7.3.3.A program that displays a class synopsis for the named class
7.3.4.Create new instance from Constructor
7.3.5.Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED
7.3.6.Load class with Class.forName
7.3.7.Call Private constructor
7.3.8.Get constructor by parameter type
7.3.9.Invoke a constructor which throws Exception
7.3.10.Passing a parameter to the constructor and calling a method dynamically
7.3.11.Getting a Constructor of a Class Object: By obtaining a list of all Constructors object
7.3.12.Getting a Constructor of a Class Object: By obtaining a particular Constructor object.
7.3.13.Get a compatible constructor for the given value type
7.3.14.Has Declared Constructor