src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/reflect/opt/OptimizedAccessorFactory.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

ohair@286 1 /*
mkos@408 2 * Copyright (c) 1997, 2013, 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);
mkos@408 130 if (acc!=null) {
mkos@408 131 if (logger.isLoggable(Level.FINE)) {
mkos@408 132 logger.log(Level.FINE, "Using optimized Accessor for {0} and {1}", new Object[]{getter, setter});
mkos@408 133 }
mkos@408 134 }
ohair@286 135 return acc;
ohair@286 136 }
ohair@286 137
ohair@286 138
ohair@286 139 /**
alanb@368 140 * Gets the optimized {@link Accessor} that accesses the given field.
ohair@286 141 *
ohair@286 142 * @return null
ohair@286 143 * if for some reason it fails to create an optimized version.
ohair@286 144 */
ohair@286 145 public static final <B,V> Accessor<B,V> get(Field field) {
ohair@286 146 int mods = field.getModifiers();
ohair@286 147 if(Modifier.isPrivate(mods) || Modifier.isFinal(mods))
ohair@286 148 // we can't access private fields
ohair@286 149 return null;
ohair@286 150
ohair@286 151 String newClassName = toVMClassName(field.getDeclaringClass())+"$JaxbAccessorF_"+field.getName();
ohair@286 152
ohair@286 153 Class opt;
ohair@286 154
ohair@286 155 if(field.getType().isPrimitive())
ohair@286 156 opt = AccessorInjector.prepare( field.getDeclaringClass(),
ohair@286 157 fieldTemplateName+RuntimeUtil.primitiveToBox.get(field.getType()).getSimpleName(),
ohair@286 158 newClassName,
ohair@286 159 toVMClassName(Bean.class),
ohair@286 160 toVMClassName(field.getDeclaringClass()),
ohair@286 161 "f_"+field.getType().getName(),
ohair@286 162 field.getName() );
ohair@286 163 else
ohair@286 164 opt = AccessorInjector.prepare( field.getDeclaringClass(),
ohair@286 165 fieldTemplateName+"Ref",
ohair@286 166 newClassName,
ohair@286 167 toVMClassName(Bean.class),
ohair@286 168 toVMClassName(field.getDeclaringClass()),
ohair@286 169 toVMClassName(Ref.class),
ohair@286 170 toVMClassName(field.getType()),
ohair@286 171 toVMTypeName(Ref.class),
ohair@286 172 toVMTypeName(field.getType()),
ohair@286 173 "f_ref",
ohair@286 174 field.getName() );
ohair@286 175
ohair@286 176 if(opt==null)
ohair@286 177 return null;
ohair@286 178
ohair@286 179 Accessor<B,V> acc = instanciate(opt);
mkos@408 180 if (acc!=null) {
mkos@408 181 if (logger.isLoggable(Level.FINE)) {
mkos@408 182 logger.log(Level.FINE, "Using optimized Accessor for {0}", field);
mkos@408 183 }
mkos@408 184 }
ohair@286 185 return acc;
ohair@286 186 }
ohair@286 187
ohair@286 188 private static <B,V> Accessor<B,V> instanciate(Class opt) {
ohair@286 189 try {
ohair@286 190 return (Accessor<B,V>)opt.newInstance();
ohair@286 191 } catch (InstantiationException e) {
ohair@286 192 logger.log(Level.INFO,"failed to load an optimized Accessor",e);
ohair@286 193 } catch (IllegalAccessException e) {
ohair@286 194 logger.log(Level.INFO,"failed to load an optimized Accessor",e);
ohair@286 195 } catch (SecurityException e) {
ohair@286 196 logger.log(Level.INFO,"failed to load an optimized Accessor",e);
ohair@286 197 }
ohair@286 198 return null;
ohair@286 199 }
ohair@286 200 }

mercurial