Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.reflect.Constructor; |
aoqi@0 | 29 | import java.lang.reflect.InvocationTargetException; |
aoqi@0 | 30 | import java.lang.reflect.Method; |
aoqi@0 | 31 | import java.lang.reflect.Modifier; |
aoqi@0 | 32 | import java.lang.ref.WeakReference; |
aoqi@0 | 33 | import java.util.Map; |
aoqi@0 | 34 | import java.util.WeakHashMap; |
aoqi@0 | 35 | import java.util.logging.Level; |
aoqi@0 | 36 | import java.util.logging.Logger; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import com.sun.xml.internal.bind.Util; |
aoqi@0 | 39 | |
aoqi@0 | 40 | /** |
aoqi@0 | 41 | * Creates new instances of classes. |
aoqi@0 | 42 | * |
aoqi@0 | 43 | * <p> |
aoqi@0 | 44 | * This code handles the case where the class is not public or the constructor is |
aoqi@0 | 45 | * not public. |
aoqi@0 | 46 | * |
aoqi@0 | 47 | * @since 2.0 |
aoqi@0 | 48 | * @author Kohsuke Kawaguchi |
aoqi@0 | 49 | */ |
aoqi@0 | 50 | public final class ClassFactory { |
aoqi@0 | 51 | private static final Class[] emptyClass = new Class[0]; |
aoqi@0 | 52 | private static final Object[] emptyObject = new Object[0]; |
aoqi@0 | 53 | |
aoqi@0 | 54 | private static final Logger logger = Util.getClassLogger(); |
aoqi@0 | 55 | |
aoqi@0 | 56 | /** |
aoqi@0 | 57 | * Cache from a class to its default constructor. |
aoqi@0 | 58 | * |
aoqi@0 | 59 | * To avoid synchronization among threads, we use {@link ThreadLocal}. |
aoqi@0 | 60 | */ |
aoqi@0 | 61 | private static final ThreadLocal<Map<Class, WeakReference<Constructor>>> tls = new ThreadLocal<Map<Class,WeakReference<Constructor>>>() { |
aoqi@0 | 62 | @Override |
aoqi@0 | 63 | public Map<Class,WeakReference<Constructor>> initialValue() { |
aoqi@0 | 64 | return new WeakHashMap<Class,WeakReference<Constructor>>(); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | }; |
aoqi@0 | 67 | |
aoqi@0 | 68 | public static void cleanCache() { |
aoqi@0 | 69 | if (tls != null) { |
aoqi@0 | 70 | try { |
aoqi@0 | 71 | tls.remove(); |
aoqi@0 | 72 | } catch (Exception e) { |
aoqi@0 | 73 | logger.log(Level.WARNING, "Unable to clean Thread Local cache of classes used in Unmarshaller: {0}", e.getLocalizedMessage()); |
aoqi@0 | 74 | } |
aoqi@0 | 75 | } |
aoqi@0 | 76 | } |
aoqi@0 | 77 | |
aoqi@0 | 78 | /** |
aoqi@0 | 79 | * Creates a new instance of the class but throw exceptions without catching it. |
aoqi@0 | 80 | */ |
aoqi@0 | 81 | public static <T> T create0( final Class<T> clazz ) throws IllegalAccessException, InvocationTargetException, InstantiationException { |
aoqi@0 | 82 | Map<Class,WeakReference<Constructor>> m = tls.get(); |
aoqi@0 | 83 | Constructor<T> cons = null; |
aoqi@0 | 84 | WeakReference<Constructor> consRef = m.get(clazz); |
aoqi@0 | 85 | if(consRef!=null) |
aoqi@0 | 86 | cons = consRef.get(); |
aoqi@0 | 87 | if(cons==null) { |
aoqi@0 | 88 | try { |
aoqi@0 | 89 | cons = clazz.getDeclaredConstructor(emptyClass); |
aoqi@0 | 90 | } catch (NoSuchMethodException e) { |
aoqi@0 | 91 | logger.log(Level.INFO,"No default constructor found on "+clazz,e); |
aoqi@0 | 92 | NoSuchMethodError exp; |
aoqi@0 | 93 | if(clazz.getDeclaringClass()!=null && !Modifier.isStatic(clazz.getModifiers())) { |
aoqi@0 | 94 | exp = new NoSuchMethodError(Messages.NO_DEFAULT_CONSTRUCTOR_IN_INNER_CLASS.format(clazz.getName())); |
aoqi@0 | 95 | } else { |
aoqi@0 | 96 | exp = new NoSuchMethodError(e.getMessage()); |
aoqi@0 | 97 | } |
aoqi@0 | 98 | exp.initCause(e); |
aoqi@0 | 99 | throw exp; |
aoqi@0 | 100 | } |
aoqi@0 | 101 | |
aoqi@0 | 102 | int classMod = clazz.getModifiers(); |
aoqi@0 | 103 | |
aoqi@0 | 104 | if(!Modifier.isPublic(classMod) || !Modifier.isPublic(cons.getModifiers())) { |
aoqi@0 | 105 | // attempt to make it work even if the constructor is not accessible |
aoqi@0 | 106 | try { |
aoqi@0 | 107 | cons.setAccessible(true); |
aoqi@0 | 108 | } catch(SecurityException e) { |
aoqi@0 | 109 | // but if we don't have a permission to do so, work gracefully. |
aoqi@0 | 110 | logger.log(Level.FINE,"Unable to make the constructor of "+clazz+" accessible",e); |
aoqi@0 | 111 | throw e; |
aoqi@0 | 112 | } |
aoqi@0 | 113 | } |
aoqi@0 | 114 | |
aoqi@0 | 115 | m.put(clazz,new WeakReference<Constructor>(cons)); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | return cons.newInstance(emptyObject); |
aoqi@0 | 119 | } |
aoqi@0 | 120 | |
aoqi@0 | 121 | /** |
aoqi@0 | 122 | * The same as {@link #create0} but with an error handling to make |
aoqi@0 | 123 | * the instantiation error fatal. |
aoqi@0 | 124 | */ |
aoqi@0 | 125 | public static <T> T create( Class<T> clazz ) { |
aoqi@0 | 126 | try { |
aoqi@0 | 127 | return create0(clazz); |
aoqi@0 | 128 | } catch (InstantiationException e) { |
aoqi@0 | 129 | logger.log(Level.INFO,"failed to create a new instance of "+clazz,e); |
aoqi@0 | 130 | throw new InstantiationError(e.toString()); |
aoqi@0 | 131 | } catch (IllegalAccessException e) { |
aoqi@0 | 132 | logger.log(Level.INFO,"failed to create a new instance of "+clazz,e); |
aoqi@0 | 133 | throw new IllegalAccessError(e.toString()); |
aoqi@0 | 134 | } catch (InvocationTargetException e) { |
aoqi@0 | 135 | Throwable target = e.getTargetException(); |
aoqi@0 | 136 | |
aoqi@0 | 137 | // most likely an error on the user's code. |
aoqi@0 | 138 | // just let it through for the ease of debugging |
aoqi@0 | 139 | if(target instanceof RuntimeException) |
aoqi@0 | 140 | throw (RuntimeException)target; |
aoqi@0 | 141 | |
aoqi@0 | 142 | // error. just forward it for the ease of debugging |
aoqi@0 | 143 | if(target instanceof Error) |
aoqi@0 | 144 | throw (Error)target; |
aoqi@0 | 145 | |
aoqi@0 | 146 | // a checked exception. |
aoqi@0 | 147 | // not sure how we should report this error, |
aoqi@0 | 148 | // but for now we just forward it by wrapping it into a runtime exception |
aoqi@0 | 149 | throw new IllegalStateException(target); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | /** |
aoqi@0 | 154 | * Call a method in the factory class to get the object. |
aoqi@0 | 155 | */ |
aoqi@0 | 156 | public static Object create(Method method) { |
aoqi@0 | 157 | Throwable errorMsg; |
aoqi@0 | 158 | try { |
aoqi@0 | 159 | return method.invoke(null, emptyObject); |
aoqi@0 | 160 | } catch (InvocationTargetException ive) { |
aoqi@0 | 161 | Throwable target = ive.getTargetException(); |
aoqi@0 | 162 | |
aoqi@0 | 163 | if(target instanceof RuntimeException) |
aoqi@0 | 164 | throw (RuntimeException)target; |
aoqi@0 | 165 | |
aoqi@0 | 166 | if(target instanceof Error) |
aoqi@0 | 167 | throw (Error)target; |
aoqi@0 | 168 | |
aoqi@0 | 169 | throw new IllegalStateException(target); |
aoqi@0 | 170 | } catch (IllegalAccessException e) { |
aoqi@0 | 171 | logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),e); |
aoqi@0 | 172 | throw new IllegalAccessError(e.toString()); |
aoqi@0 | 173 | } catch (IllegalArgumentException iae){ |
aoqi@0 | 174 | logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),iae); |
aoqi@0 | 175 | errorMsg = iae; |
aoqi@0 | 176 | } catch (NullPointerException npe){ |
aoqi@0 | 177 | logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),npe); |
aoqi@0 | 178 | errorMsg = npe; |
aoqi@0 | 179 | } catch (ExceptionInInitializerError eie){ |
aoqi@0 | 180 | logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),eie); |
aoqi@0 | 181 | errorMsg = eie; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | NoSuchMethodError exp; |
aoqi@0 | 185 | exp = new NoSuchMethodError(errorMsg.getMessage()); |
aoqi@0 | 186 | exp.initCause(errorMsg); |
aoqi@0 | 187 | throw exp; |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | /** |
aoqi@0 | 191 | * Infers the instanciable implementation class that can be assigned to the given field type. |
aoqi@0 | 192 | * |
aoqi@0 | 193 | * @return null |
aoqi@0 | 194 | * if inference fails. |
aoqi@0 | 195 | */ |
aoqi@0 | 196 | public static <T> Class<? extends T> inferImplClass(Class<T> fieldType, Class[] knownImplClasses) { |
aoqi@0 | 197 | if(!fieldType.isInterface()) |
aoqi@0 | 198 | return fieldType; |
aoqi@0 | 199 | |
aoqi@0 | 200 | for( Class<?> impl : knownImplClasses ) { |
aoqi@0 | 201 | if(fieldType.isAssignableFrom(impl)) |
aoqi@0 | 202 | return impl.asSubclass(fieldType); |
aoqi@0 | 203 | } |
aoqi@0 | 204 | |
aoqi@0 | 205 | // if we can't find an implementation class, |
aoqi@0 | 206 | // let's just hope that we will never need to create a new object, |
aoqi@0 | 207 | // and returns null |
aoqi@0 | 208 | return null; |
aoqi@0 | 209 | } |
aoqi@0 | 210 | } |