
org.apache.ws.jaxme.util.ClassLoader Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2003, 2004 The Apache Software Foundation
*
* 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.
*/
package org.apache.ws.jaxme.util;
/** Helper class for working with class loaders.
*
* @author Jochen Wiedmann
* @version $Id: ClassLoader.java 231893 2004-07-27 13:19:25 +0200 (Tue, 27 Jul 2004) jochen $
*/
public class ClassLoader {
/** Loads a class with the given name. First attempts to use
* the context class loader, then its own class loader.
*
* @param pName The fully qualified name of the class being loaded.
* @throws ClassNotFoundException Loading the class failed.
* @return The class with the name pName
.
*/
public static Class getClass(String pName) throws ClassNotFoundException {
// First try: The default ClassLoader
try {
return Class.forName(pName);
} catch (ClassNotFoundException e) {
// Second try: The threads context ClassLoader
try {
java.lang.ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
throw new ClassNotFoundException(pName);
}
return cl.loadClass(pName);
} catch (ClassNotFoundException f) {
throw e;
}
}
}
/** Loads a class with the given name using getClass(String)
.
* If an instance of the returned class cannot be assigned to the
* class or interface pAssignableTo
, throws an
* IllegalArgumentException.
*
* @throws ClassNotFoundException The class with the name pName
* could not be loaded.
* @throws IllegalArgumentException Instances of the class with the name
* pName
are not assignable to the interface or class
* pAssignableTo
.
*/
public static Class getClass(String pName, Class pAssignableTo)
throws ClassNotFoundException {
Class result = getClass(pName);
if (pAssignableTo != null && !pAssignableTo.isAssignableFrom(result)) {
throw new IllegalArgumentException("The class " + result.getName() +
" is not implementing or extending " +
pAssignableTo.getName());
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy