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