1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/ClassFactory.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,210 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2; 1.30 + 1.31 +import java.lang.reflect.Constructor; 1.32 +import java.lang.reflect.InvocationTargetException; 1.33 +import java.lang.reflect.Method; 1.34 +import java.lang.reflect.Modifier; 1.35 +import java.lang.ref.WeakReference; 1.36 +import java.util.Map; 1.37 +import java.util.WeakHashMap; 1.38 +import java.util.logging.Level; 1.39 +import java.util.logging.Logger; 1.40 + 1.41 +import com.sun.xml.internal.bind.Util; 1.42 + 1.43 +/** 1.44 + * Creates new instances of classes. 1.45 + * 1.46 + * <p> 1.47 + * This code handles the case where the class is not public or the constructor is 1.48 + * not public. 1.49 + * 1.50 + * @since 2.0 1.51 + * @author Kohsuke Kawaguchi 1.52 + */ 1.53 +public final class ClassFactory { 1.54 + private static final Class[] emptyClass = new Class[0]; 1.55 + private static final Object[] emptyObject = new Object[0]; 1.56 + 1.57 + private static final Logger logger = Util.getClassLogger(); 1.58 + 1.59 + /** 1.60 + * Cache from a class to its default constructor. 1.61 + * 1.62 + * To avoid synchronization among threads, we use {@link ThreadLocal}. 1.63 + */ 1.64 + private static final ThreadLocal<Map<Class, WeakReference<Constructor>>> tls = new ThreadLocal<Map<Class,WeakReference<Constructor>>>() { 1.65 + @Override 1.66 + public Map<Class,WeakReference<Constructor>> initialValue() { 1.67 + return new WeakHashMap<Class,WeakReference<Constructor>>(); 1.68 + } 1.69 + }; 1.70 + 1.71 + public static void cleanCache() { 1.72 + if (tls != null) { 1.73 + try { 1.74 + tls.remove(); 1.75 + } catch (Exception e) { 1.76 + logger.log(Level.WARNING, "Unable to clean Thread Local cache of classes used in Unmarshaller: {0}", e.getLocalizedMessage()); 1.77 + } 1.78 + } 1.79 + } 1.80 + 1.81 + /** 1.82 + * Creates a new instance of the class but throw exceptions without catching it. 1.83 + */ 1.84 + public static <T> T create0( final Class<T> clazz ) throws IllegalAccessException, InvocationTargetException, InstantiationException { 1.85 + Map<Class,WeakReference<Constructor>> m = tls.get(); 1.86 + Constructor<T> cons = null; 1.87 + WeakReference<Constructor> consRef = m.get(clazz); 1.88 + if(consRef!=null) 1.89 + cons = consRef.get(); 1.90 + if(cons==null) { 1.91 + try { 1.92 + cons = clazz.getDeclaredConstructor(emptyClass); 1.93 + } catch (NoSuchMethodException e) { 1.94 + logger.log(Level.INFO,"No default constructor found on "+clazz,e); 1.95 + NoSuchMethodError exp; 1.96 + if(clazz.getDeclaringClass()!=null && !Modifier.isStatic(clazz.getModifiers())) { 1.97 + exp = new NoSuchMethodError(Messages.NO_DEFAULT_CONSTRUCTOR_IN_INNER_CLASS.format(clazz.getName())); 1.98 + } else { 1.99 + exp = new NoSuchMethodError(e.getMessage()); 1.100 + } 1.101 + exp.initCause(e); 1.102 + throw exp; 1.103 + } 1.104 + 1.105 + int classMod = clazz.getModifiers(); 1.106 + 1.107 + if(!Modifier.isPublic(classMod) || !Modifier.isPublic(cons.getModifiers())) { 1.108 + // attempt to make it work even if the constructor is not accessible 1.109 + try { 1.110 + cons.setAccessible(true); 1.111 + } catch(SecurityException e) { 1.112 + // but if we don't have a permission to do so, work gracefully. 1.113 + logger.log(Level.FINE,"Unable to make the constructor of "+clazz+" accessible",e); 1.114 + throw e; 1.115 + } 1.116 + } 1.117 + 1.118 + m.put(clazz,new WeakReference<Constructor>(cons)); 1.119 + } 1.120 + 1.121 + return cons.newInstance(emptyObject); 1.122 + } 1.123 + 1.124 + /** 1.125 + * The same as {@link #create0} but with an error handling to make 1.126 + * the instantiation error fatal. 1.127 + */ 1.128 + public static <T> T create( Class<T> clazz ) { 1.129 + try { 1.130 + return create0(clazz); 1.131 + } catch (InstantiationException e) { 1.132 + logger.log(Level.INFO,"failed to create a new instance of "+clazz,e); 1.133 + throw new InstantiationError(e.toString()); 1.134 + } catch (IllegalAccessException e) { 1.135 + logger.log(Level.INFO,"failed to create a new instance of "+clazz,e); 1.136 + throw new IllegalAccessError(e.toString()); 1.137 + } catch (InvocationTargetException e) { 1.138 + Throwable target = e.getTargetException(); 1.139 + 1.140 + // most likely an error on the user's code. 1.141 + // just let it through for the ease of debugging 1.142 + if(target instanceof RuntimeException) 1.143 + throw (RuntimeException)target; 1.144 + 1.145 + // error. just forward it for the ease of debugging 1.146 + if(target instanceof Error) 1.147 + throw (Error)target; 1.148 + 1.149 + // a checked exception. 1.150 + // not sure how we should report this error, 1.151 + // but for now we just forward it by wrapping it into a runtime exception 1.152 + throw new IllegalStateException(target); 1.153 + } 1.154 + } 1.155 + 1.156 + /** 1.157 + * Call a method in the factory class to get the object. 1.158 + */ 1.159 + public static Object create(Method method) { 1.160 + Throwable errorMsg; 1.161 + try { 1.162 + return method.invoke(null, emptyObject); 1.163 + } catch (InvocationTargetException ive) { 1.164 + Throwable target = ive.getTargetException(); 1.165 + 1.166 + if(target instanceof RuntimeException) 1.167 + throw (RuntimeException)target; 1.168 + 1.169 + if(target instanceof Error) 1.170 + throw (Error)target; 1.171 + 1.172 + throw new IllegalStateException(target); 1.173 + } catch (IllegalAccessException e) { 1.174 + logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),e); 1.175 + throw new IllegalAccessError(e.toString()); 1.176 + } catch (IllegalArgumentException iae){ 1.177 + logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),iae); 1.178 + errorMsg = iae; 1.179 + } catch (NullPointerException npe){ 1.180 + logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),npe); 1.181 + errorMsg = npe; 1.182 + } catch (ExceptionInInitializerError eie){ 1.183 + logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),eie); 1.184 + errorMsg = eie; 1.185 + } 1.186 + 1.187 + NoSuchMethodError exp; 1.188 + exp = new NoSuchMethodError(errorMsg.getMessage()); 1.189 + exp.initCause(errorMsg); 1.190 + throw exp; 1.191 + } 1.192 + 1.193 + /** 1.194 + * Infers the instanciable implementation class that can be assigned to the given field type. 1.195 + * 1.196 + * @return null 1.197 + * if inference fails. 1.198 + */ 1.199 + public static <T> Class<? extends T> inferImplClass(Class<T> fieldType, Class[] knownImplClasses) { 1.200 + if(!fieldType.isInterface()) 1.201 + return fieldType; 1.202 + 1.203 + for( Class<?> impl : knownImplClasses ) { 1.204 + if(fieldType.isAssignableFrom(impl)) 1.205 + return impl.asSubclass(fieldType); 1.206 + } 1.207 + 1.208 + // if we can't find an implementation class, 1.209 + // let's just hope that we will never need to create a new object, 1.210 + // and returns null 1.211 + return null; 1.212 + } 1.213 +}