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/runtime/reflect/opt/OptimizedAccessorFactory.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,200 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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.runtime.reflect.opt; 1.30 + 1.31 +import java.lang.reflect.Field; 1.32 +import java.lang.reflect.Method; 1.33 +import java.lang.reflect.Modifier; 1.34 +import java.util.logging.Level; 1.35 +import java.util.logging.Logger; 1.36 + 1.37 +import com.sun.xml.internal.bind.Util; 1.38 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; 1.39 +import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil; 1.40 + 1.41 +import static com.sun.xml.internal.bind.v2.bytecode.ClassTailor.toVMClassName; 1.42 +import static com.sun.xml.internal.bind.v2.bytecode.ClassTailor.toVMTypeName; 1.43 + 1.44 +/** 1.45 + * @author Kohsuke Kawaguchi 1.46 + */ 1.47 +public abstract class OptimizedAccessorFactory { 1.48 + private OptimizedAccessorFactory() {} // no instanciation please 1.49 + 1.50 + private static final Logger logger = Util.getClassLogger(); 1.51 + 1.52 + 1.53 + private static final String fieldTemplateName; 1.54 + private static final String methodTemplateName; 1.55 + 1.56 + static { 1.57 + String s = FieldAccessor_Byte.class.getName(); 1.58 + fieldTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/'); 1.59 + 1.60 + s = MethodAccessor_Byte.class.getName(); 1.61 + methodTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/'); 1.62 + } 1.63 + 1.64 + /** 1.65 + * Gets the optimized {@link Accessor} that accesses the given getter/setter. 1.66 + * 1.67 + * @return null 1.68 + * if for some reason it fails to create an optimized version. 1.69 + */ 1.70 + public static final <B,V> Accessor<B,V> get(Method getter, Method setter) { 1.71 + // make sure the method signatures are what we expect 1.72 + if(getter.getParameterTypes().length!=0) 1.73 + return null; 1.74 + Class<?>[] sparams = setter.getParameterTypes(); 1.75 + if(sparams.length!=1) 1.76 + return null; 1.77 + if(sparams[0]!=getter.getReturnType()) 1.78 + return null; 1.79 + if(setter.getReturnType()!=Void.TYPE) 1.80 + return null; 1.81 + if(getter.getDeclaringClass()!=setter.getDeclaringClass()) 1.82 + return null; 1.83 + if(Modifier.isPrivate(getter.getModifiers()) || Modifier.isPrivate(setter.getModifiers())) 1.84 + // we can't access private fields 1.85 + return null; 1.86 + 1.87 + Class t = sparams[0]; 1.88 + String typeName = t.getName().replace('.','_'); 1.89 + if (t.isArray()) { 1.90 + typeName = "AOf_"; 1.91 + String compName = t.getComponentType().getName().replace('.','_'); 1.92 + while (compName.startsWith("[L")) { 1.93 + compName = compName.substring(2); 1.94 + typeName += "AOf_"; 1.95 + } 1.96 + typeName = typeName + compName; 1.97 + } 1.98 + 1.99 + String newClassName = toVMClassName(getter.getDeclaringClass())+"$JaxbAccessorM_"+getter.getName()+'_'+setter.getName()+'_'+typeName; 1.100 + Class opt; 1.101 + 1.102 + if(t.isPrimitive()) 1.103 + opt = AccessorInjector.prepare( getter.getDeclaringClass(), 1.104 + methodTemplateName+RuntimeUtil.primitiveToBox.get(t).getSimpleName(), 1.105 + newClassName, 1.106 + toVMClassName(Bean.class), 1.107 + toVMClassName(getter.getDeclaringClass()), 1.108 + "get_"+t.getName(), 1.109 + getter.getName(), 1.110 + "set_"+t.getName(), 1.111 + setter.getName()); 1.112 + else 1.113 + opt = AccessorInjector.prepare( getter.getDeclaringClass(), 1.114 + methodTemplateName+"Ref", 1.115 + newClassName, 1.116 + toVMClassName(Bean.class), 1.117 + toVMClassName(getter.getDeclaringClass()), 1.118 + toVMClassName(Ref.class), 1.119 + toVMClassName(t), 1.120 + "()"+toVMTypeName(Ref.class), 1.121 + "()"+toVMTypeName(t), 1.122 + '('+toVMTypeName(Ref.class)+")V", 1.123 + '('+toVMTypeName(t)+")V", 1.124 + "get_ref", 1.125 + getter.getName(), 1.126 + "set_ref", 1.127 + setter.getName()); 1.128 + 1.129 + if(opt==null) 1.130 + return null; 1.131 + 1.132 + Accessor<B,V> acc = instanciate(opt); 1.133 + if (acc!=null) { 1.134 + if (logger.isLoggable(Level.FINE)) { 1.135 + logger.log(Level.FINE, "Using optimized Accessor for {0} and {1}", new Object[]{getter, setter}); 1.136 + } 1.137 + } 1.138 + return acc; 1.139 + } 1.140 + 1.141 + 1.142 + /** 1.143 + * Gets the optimized {@link Accessor} that accesses the given field. 1.144 + * 1.145 + * @return null 1.146 + * if for some reason it fails to create an optimized version. 1.147 + */ 1.148 + public static final <B,V> Accessor<B,V> get(Field field) { 1.149 + int mods = field.getModifiers(); 1.150 + if(Modifier.isPrivate(mods) || Modifier.isFinal(mods)) 1.151 + // we can't access private fields 1.152 + return null; 1.153 + 1.154 + String newClassName = toVMClassName(field.getDeclaringClass())+"$JaxbAccessorF_"+field.getName(); 1.155 + 1.156 + Class opt; 1.157 + 1.158 + if(field.getType().isPrimitive()) 1.159 + opt = AccessorInjector.prepare( field.getDeclaringClass(), 1.160 + fieldTemplateName+RuntimeUtil.primitiveToBox.get(field.getType()).getSimpleName(), 1.161 + newClassName, 1.162 + toVMClassName(Bean.class), 1.163 + toVMClassName(field.getDeclaringClass()), 1.164 + "f_"+field.getType().getName(), 1.165 + field.getName() ); 1.166 + else 1.167 + opt = AccessorInjector.prepare( field.getDeclaringClass(), 1.168 + fieldTemplateName+"Ref", 1.169 + newClassName, 1.170 + toVMClassName(Bean.class), 1.171 + toVMClassName(field.getDeclaringClass()), 1.172 + toVMClassName(Ref.class), 1.173 + toVMClassName(field.getType()), 1.174 + toVMTypeName(Ref.class), 1.175 + toVMTypeName(field.getType()), 1.176 + "f_ref", 1.177 + field.getName() ); 1.178 + 1.179 + if(opt==null) 1.180 + return null; 1.181 + 1.182 + Accessor<B,V> acc = instanciate(opt); 1.183 + if (acc!=null) { 1.184 + if (logger.isLoggable(Level.FINE)) { 1.185 + logger.log(Level.FINE, "Using optimized Accessor for {0}", field); 1.186 + } 1.187 + } 1.188 + return acc; 1.189 + } 1.190 + 1.191 + private static <B,V> Accessor<B,V> instanciate(Class opt) { 1.192 + try { 1.193 + return (Accessor<B,V>)opt.newInstance(); 1.194 + } catch (InstantiationException e) { 1.195 + logger.log(Level.INFO,"failed to load an optimized Accessor",e); 1.196 + } catch (IllegalAccessException e) { 1.197 + logger.log(Level.INFO,"failed to load an optimized Accessor",e); 1.198 + } catch (SecurityException e) { 1.199 + logger.log(Level.INFO,"failed to load an optimized Accessor",e); 1.200 + } 1.201 + return null; 1.202 + } 1.203 +}