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






Get a compatible constructor for the given value type

   

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;
  }
}

   
    
    
  








Related examples in the same category

1.Determining from Where a Class Was Loaded
2.Loading a Class That Is Not on the Classpath
3.Dynamically Reloading a Modified Class
4.Get the path from where a class is loaded
5.Using the forName() method
6.Unqualified names
7.Return the context classloader.
8.Jar Class Loader
9.Zip Class Loader
10.Load class from Thread's ContextClassLoader, classloader of derived class or the system ClassLoader
11.Various Class Loader utilities
12.Load Class
13.Load classes
14.Class Finder
15.Class For Name
16.Context ClassLoader
17.Analyze ClassLoader hierarchy for any given object or class loader
18.A class loader which loads classes using a searchlist of other classloaders
19.Equivalently to invoking Thread.currentThread().getContextClassLoader().loadClass(className); but it supports primitive types and array classes of object types or primitive types.
20.A class to simplify access to resources through the classloader
21.Force the given class to be loaded fully.
22.does Class Exist
23.Java interpreter replacement
24.Utility to essentially do Class forName and allow configurable Classloaders.
25.Attempts to list all the classes in the specified package as determined by the context class loader...
26.Resolve class and load class