Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, 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.runtime.reflect.opt; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.reflect.Field; |
aoqi@0 | 29 | import java.lang.reflect.Method; |
aoqi@0 | 30 | import java.lang.reflect.Modifier; |
aoqi@0 | 31 | import java.util.logging.Level; |
aoqi@0 | 32 | import java.util.logging.Logger; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import com.sun.xml.internal.bind.Util; |
aoqi@0 | 35 | import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; |
aoqi@0 | 36 | import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import static com.sun.xml.internal.bind.v2.bytecode.ClassTailor.toVMClassName; |
aoqi@0 | 39 | import static com.sun.xml.internal.bind.v2.bytecode.ClassTailor.toVMTypeName; |
aoqi@0 | 40 | |
aoqi@0 | 41 | /** |
aoqi@0 | 42 | * @author Kohsuke Kawaguchi |
aoqi@0 | 43 | */ |
aoqi@0 | 44 | public abstract class OptimizedAccessorFactory { |
aoqi@0 | 45 | private OptimizedAccessorFactory() {} // no instanciation please |
aoqi@0 | 46 | |
aoqi@0 | 47 | private static final Logger logger = Util.getClassLogger(); |
aoqi@0 | 48 | |
aoqi@0 | 49 | |
aoqi@0 | 50 | private static final String fieldTemplateName; |
aoqi@0 | 51 | private static final String methodTemplateName; |
aoqi@0 | 52 | |
aoqi@0 | 53 | static { |
aoqi@0 | 54 | String s = FieldAccessor_Byte.class.getName(); |
aoqi@0 | 55 | fieldTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/'); |
aoqi@0 | 56 | |
aoqi@0 | 57 | s = MethodAccessor_Byte.class.getName(); |
aoqi@0 | 58 | methodTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/'); |
aoqi@0 | 59 | } |
aoqi@0 | 60 | |
aoqi@0 | 61 | /** |
aoqi@0 | 62 | * Gets the optimized {@link Accessor} that accesses the given getter/setter. |
aoqi@0 | 63 | * |
aoqi@0 | 64 | * @return null |
aoqi@0 | 65 | * if for some reason it fails to create an optimized version. |
aoqi@0 | 66 | */ |
aoqi@0 | 67 | public static final <B,V> Accessor<B,V> get(Method getter, Method setter) { |
aoqi@0 | 68 | // make sure the method signatures are what we expect |
aoqi@0 | 69 | if(getter.getParameterTypes().length!=0) |
aoqi@0 | 70 | return null; |
aoqi@0 | 71 | Class<?>[] sparams = setter.getParameterTypes(); |
aoqi@0 | 72 | if(sparams.length!=1) |
aoqi@0 | 73 | return null; |
aoqi@0 | 74 | if(sparams[0]!=getter.getReturnType()) |
aoqi@0 | 75 | return null; |
aoqi@0 | 76 | if(setter.getReturnType()!=Void.TYPE) |
aoqi@0 | 77 | return null; |
aoqi@0 | 78 | if(getter.getDeclaringClass()!=setter.getDeclaringClass()) |
aoqi@0 | 79 | return null; |
aoqi@0 | 80 | if(Modifier.isPrivate(getter.getModifiers()) || Modifier.isPrivate(setter.getModifiers())) |
aoqi@0 | 81 | // we can't access private fields |
aoqi@0 | 82 | return null; |
aoqi@0 | 83 | |
aoqi@0 | 84 | Class t = sparams[0]; |
aoqi@0 | 85 | String typeName = t.getName().replace('.','_'); |
aoqi@0 | 86 | if (t.isArray()) { |
aoqi@0 | 87 | typeName = "AOf_"; |
aoqi@0 | 88 | String compName = t.getComponentType().getName().replace('.','_'); |
aoqi@0 | 89 | while (compName.startsWith("[L")) { |
aoqi@0 | 90 | compName = compName.substring(2); |
aoqi@0 | 91 | typeName += "AOf_"; |
aoqi@0 | 92 | } |
aoqi@0 | 93 | typeName = typeName + compName; |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | String newClassName = toVMClassName(getter.getDeclaringClass())+"$JaxbAccessorM_"+getter.getName()+'_'+setter.getName()+'_'+typeName; |
aoqi@0 | 97 | Class opt; |
aoqi@0 | 98 | |
aoqi@0 | 99 | if(t.isPrimitive()) |
aoqi@0 | 100 | opt = AccessorInjector.prepare( getter.getDeclaringClass(), |
aoqi@0 | 101 | methodTemplateName+RuntimeUtil.primitiveToBox.get(t).getSimpleName(), |
aoqi@0 | 102 | newClassName, |
aoqi@0 | 103 | toVMClassName(Bean.class), |
aoqi@0 | 104 | toVMClassName(getter.getDeclaringClass()), |
aoqi@0 | 105 | "get_"+t.getName(), |
aoqi@0 | 106 | getter.getName(), |
aoqi@0 | 107 | "set_"+t.getName(), |
aoqi@0 | 108 | setter.getName()); |
aoqi@0 | 109 | else |
aoqi@0 | 110 | opt = AccessorInjector.prepare( getter.getDeclaringClass(), |
aoqi@0 | 111 | methodTemplateName+"Ref", |
aoqi@0 | 112 | newClassName, |
aoqi@0 | 113 | toVMClassName(Bean.class), |
aoqi@0 | 114 | toVMClassName(getter.getDeclaringClass()), |
aoqi@0 | 115 | toVMClassName(Ref.class), |
aoqi@0 | 116 | toVMClassName(t), |
aoqi@0 | 117 | "()"+toVMTypeName(Ref.class), |
aoqi@0 | 118 | "()"+toVMTypeName(t), |
aoqi@0 | 119 | '('+toVMTypeName(Ref.class)+")V", |
aoqi@0 | 120 | '('+toVMTypeName(t)+")V", |
aoqi@0 | 121 | "get_ref", |
aoqi@0 | 122 | getter.getName(), |
aoqi@0 | 123 | "set_ref", |
aoqi@0 | 124 | setter.getName()); |
aoqi@0 | 125 | |
aoqi@0 | 126 | if(opt==null) |
aoqi@0 | 127 | return null; |
aoqi@0 | 128 | |
aoqi@0 | 129 | Accessor<B,V> acc = instanciate(opt); |
aoqi@0 | 130 | if (acc!=null) { |
aoqi@0 | 131 | if (logger.isLoggable(Level.FINE)) { |
aoqi@0 | 132 | logger.log(Level.FINE, "Using optimized Accessor for {0} and {1}", new Object[]{getter, setter}); |
aoqi@0 | 133 | } |
aoqi@0 | 134 | } |
aoqi@0 | 135 | return acc; |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | |
aoqi@0 | 139 | /** |
aoqi@0 | 140 | * Gets the optimized {@link Accessor} that accesses the given field. |
aoqi@0 | 141 | * |
aoqi@0 | 142 | * @return null |
aoqi@0 | 143 | * if for some reason it fails to create an optimized version. |
aoqi@0 | 144 | */ |
aoqi@0 | 145 | public static final <B,V> Accessor<B,V> get(Field field) { |
aoqi@0 | 146 | int mods = field.getModifiers(); |
aoqi@0 | 147 | if(Modifier.isPrivate(mods) || Modifier.isFinal(mods)) |
aoqi@0 | 148 | // we can't access private fields |
aoqi@0 | 149 | return null; |
aoqi@0 | 150 | |
aoqi@0 | 151 | String newClassName = toVMClassName(field.getDeclaringClass())+"$JaxbAccessorF_"+field.getName(); |
aoqi@0 | 152 | |
aoqi@0 | 153 | Class opt; |
aoqi@0 | 154 | |
aoqi@0 | 155 | if(field.getType().isPrimitive()) |
aoqi@0 | 156 | opt = AccessorInjector.prepare( field.getDeclaringClass(), |
aoqi@0 | 157 | fieldTemplateName+RuntimeUtil.primitiveToBox.get(field.getType()).getSimpleName(), |
aoqi@0 | 158 | newClassName, |
aoqi@0 | 159 | toVMClassName(Bean.class), |
aoqi@0 | 160 | toVMClassName(field.getDeclaringClass()), |
aoqi@0 | 161 | "f_"+field.getType().getName(), |
aoqi@0 | 162 | field.getName() ); |
aoqi@0 | 163 | else |
aoqi@0 | 164 | opt = AccessorInjector.prepare( field.getDeclaringClass(), |
aoqi@0 | 165 | fieldTemplateName+"Ref", |
aoqi@0 | 166 | newClassName, |
aoqi@0 | 167 | toVMClassName(Bean.class), |
aoqi@0 | 168 | toVMClassName(field.getDeclaringClass()), |
aoqi@0 | 169 | toVMClassName(Ref.class), |
aoqi@0 | 170 | toVMClassName(field.getType()), |
aoqi@0 | 171 | toVMTypeName(Ref.class), |
aoqi@0 | 172 | toVMTypeName(field.getType()), |
aoqi@0 | 173 | "f_ref", |
aoqi@0 | 174 | field.getName() ); |
aoqi@0 | 175 | |
aoqi@0 | 176 | if(opt==null) |
aoqi@0 | 177 | return null; |
aoqi@0 | 178 | |
aoqi@0 | 179 | Accessor<B,V> acc = instanciate(opt); |
aoqi@0 | 180 | if (acc!=null) { |
aoqi@0 | 181 | if (logger.isLoggable(Level.FINE)) { |
aoqi@0 | 182 | logger.log(Level.FINE, "Using optimized Accessor for {0}", field); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | } |
aoqi@0 | 185 | return acc; |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | private static <B,V> Accessor<B,V> instanciate(Class opt) { |
aoqi@0 | 189 | try { |
aoqi@0 | 190 | return (Accessor<B,V>)opt.newInstance(); |
aoqi@0 | 191 | } catch (InstantiationException e) { |
aoqi@0 | 192 | logger.log(Level.INFO,"failed to load an optimized Accessor",e); |
aoqi@0 | 193 | } catch (IllegalAccessException e) { |
aoqi@0 | 194 | logger.log(Level.INFO,"failed to load an optimized Accessor",e); |
aoqi@0 | 195 | } catch (SecurityException e) { |
aoqi@0 | 196 | logger.log(Level.INFO,"failed to load an optimized Accessor",e); |
aoqi@0 | 197 | } |
aoqi@0 | 198 | return null; |
aoqi@0 | 199 | } |
aoqi@0 | 200 | } |