src/share/jaxws_classes/com/sun/xml/internal/bind/v2/ClassFactory.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial