Load Class : Class Loader « Reflection « Java Tutorial






// $Id: ReflectionHelper.java 16271 2009-04-07 20:20:12Z hardy.ferentschik $
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,  
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


import java.beans.Introspector;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * Some reflection utility methods.
 *
 * @author Hardy Ferentschik
 */
public class ReflectionHelper {

  public static Class<?> loadClass(String name, Class<?> caller) throws ClassNotFoundException {
    try {
      //try context classloader, if fails try caller classloader
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      if ( loader != null ) {
        return loader.loadClass( name );
      }
    }
    catch ( ClassNotFoundException e ) {
      //trying caller classloader
      if ( caller == null ) {
        throw e;
      }
    }
    return Class.forName( name, true, caller.getClassLoader() );
  }
}








7.7.Class Loader
7.7.1.URL class loader
7.7.2.extends URLClassLoader
7.7.3.Load classes
7.7.4.how to use reflection to print the names and values of all nonstatic fields of an object
7.7.5.Runs a jar application from any url
7.7.6.BufferedReader reflection
7.7.7.Get the class By way of an object
7.7.8.Get the class By way of a string
7.7.9.Get the class By way of .class
7.7.10.Catch InvocationTargetException
7.7.11.Determining from Where a Class Was Loaded
7.7.12.Dynamically Reloading a Modified Class
7.7.13.Creating an Object Using a Constructor Object
7.7.14.Create an object from a string
7.7.15.Using the forName() method
7.7.16.Context ClassLoader
7.7.17.A tree structure that maps inheritance hierarchies of classes
7.7.18.Analyze ClassLoader hierarchy for any given object or class loader
7.7.19.Instantiate unknown class at runtime and call the object's methods
7.7.20.Load Class