/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Creates new instances of classes.
*
* <p>
* This code handles the case where the class is not public or the constructor is
* not public.
*
* @since 2.0
* @author Kohsuke Kawaguchi
*/
public final class ClassFactory {
/**
* Cache from a class to its default constructor.
*
* To avoid synchronization among threads, we use {@link ThreadLocal}.
*/
private static final ThreadLocal<Map<Class, WeakReference<Constructor>>> tls = new ThreadLocal<Map<Class,WeakReference<Constructor>>>() {
}
};
public static void cleanCache() {
try {
} catch (Exception e) {
logger.log(Level.WARNING, "Unable to clean Thread Local cache of classes used in Unmarshaller: {0}", e.getLocalizedMessage());
}
}
}
/**
* Creates a new instance of the class but throw exceptions without catching it.
*/
public static <T> T create0( final Class<T> clazz ) throws IllegalAccessException, InvocationTargetException, InstantiationException {
try {
} catch (NoSuchMethodException e) {
exp = new NoSuchMethodError(Messages.NO_DEFAULT_CONSTRUCTOR_IN_INNER_CLASS.format(clazz.getName()));
} else {
}
throw exp;
}
// attempt to make it work even if the constructor is not accessible
try {
cons.setAccessible(true);
} catch(SecurityException e) {
// but if we don't have a permission to do so, work gracefully.
throw e;
}
}
}
}
/**
* The same as {@link #create0} but with an error handling to make
* the instanciation error fatal.
*/
try {
} catch (InstantiationException e) {
throw new InstantiationError(e.toString());
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.toString());
} catch (InvocationTargetException e) {
// most likely an error on the user's code.
// just let it through for the ease of debugging
if(target instanceof RuntimeException)
throw (RuntimeException)target;
// error. just forward it for the ease of debugging
// a checked exception.
// not sure how we should report this error,
// but for now we just forward it by wrapping it into a runtime exception
throw new IllegalStateException(target);
}
}
/**
* Call a method in the factory class to get the object.
*/
try {
} catch (InvocationTargetException ive) {
if(target instanceof RuntimeException)
throw (RuntimeException)target;
throw new IllegalStateException(target);
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.toString());
} catch (IllegalArgumentException iae){
} catch (NullPointerException npe){
} catch (ExceptionInInitializerError eie){
}
throw exp;
}
/**
* Infers the instanciable implementation class that can be assigned to the given field type.
*
* @return null
* if inference fails.
*/
if(!fieldType.isInterface())
return fieldType;
}
// if we can't find an implementation class,
// let's just hope that we will never need to create a new object,
// and returns null
return null;
}
}