aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime.reflect.opt; aoqi@0: aoqi@0: import java.lang.reflect.Field; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.lang.reflect.Modifier; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.Util; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil; aoqi@0: aoqi@0: import static com.sun.xml.internal.bind.v2.bytecode.ClassTailor.toVMClassName; aoqi@0: import static com.sun.xml.internal.bind.v2.bytecode.ClassTailor.toVMTypeName; aoqi@0: aoqi@0: /** aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public abstract class OptimizedAccessorFactory { aoqi@0: private OptimizedAccessorFactory() {} // no instanciation please aoqi@0: aoqi@0: private static final Logger logger = Util.getClassLogger(); aoqi@0: aoqi@0: aoqi@0: private static final String fieldTemplateName; aoqi@0: private static final String methodTemplateName; aoqi@0: aoqi@0: static { aoqi@0: String s = FieldAccessor_Byte.class.getName(); aoqi@0: fieldTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/'); aoqi@0: aoqi@0: s = MethodAccessor_Byte.class.getName(); aoqi@0: methodTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/'); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the optimized {@link Accessor} that accesses the given getter/setter. aoqi@0: * aoqi@0: * @return null aoqi@0: * if for some reason it fails to create an optimized version. aoqi@0: */ aoqi@0: public static final Accessor get(Method getter, Method setter) { aoqi@0: // make sure the method signatures are what we expect aoqi@0: if(getter.getParameterTypes().length!=0) aoqi@0: return null; aoqi@0: Class[] sparams = setter.getParameterTypes(); aoqi@0: if(sparams.length!=1) aoqi@0: return null; aoqi@0: if(sparams[0]!=getter.getReturnType()) aoqi@0: return null; aoqi@0: if(setter.getReturnType()!=Void.TYPE) aoqi@0: return null; aoqi@0: if(getter.getDeclaringClass()!=setter.getDeclaringClass()) aoqi@0: return null; aoqi@0: if(Modifier.isPrivate(getter.getModifiers()) || Modifier.isPrivate(setter.getModifiers())) aoqi@0: // we can't access private fields aoqi@0: return null; aoqi@0: aoqi@0: Class t = sparams[0]; aoqi@0: String typeName = t.getName().replace('.','_'); aoqi@0: if (t.isArray()) { aoqi@0: typeName = "AOf_"; aoqi@0: String compName = t.getComponentType().getName().replace('.','_'); aoqi@0: while (compName.startsWith("[L")) { aoqi@0: compName = compName.substring(2); aoqi@0: typeName += "AOf_"; aoqi@0: } aoqi@0: typeName = typeName + compName; aoqi@0: } aoqi@0: aoqi@0: String newClassName = toVMClassName(getter.getDeclaringClass())+"$JaxbAccessorM_"+getter.getName()+'_'+setter.getName()+'_'+typeName; aoqi@0: Class opt; aoqi@0: aoqi@0: if(t.isPrimitive()) aoqi@0: opt = AccessorInjector.prepare( getter.getDeclaringClass(), aoqi@0: methodTemplateName+RuntimeUtil.primitiveToBox.get(t).getSimpleName(), aoqi@0: newClassName, aoqi@0: toVMClassName(Bean.class), aoqi@0: toVMClassName(getter.getDeclaringClass()), aoqi@0: "get_"+t.getName(), aoqi@0: getter.getName(), aoqi@0: "set_"+t.getName(), aoqi@0: setter.getName()); aoqi@0: else aoqi@0: opt = AccessorInjector.prepare( getter.getDeclaringClass(), aoqi@0: methodTemplateName+"Ref", aoqi@0: newClassName, aoqi@0: toVMClassName(Bean.class), aoqi@0: toVMClassName(getter.getDeclaringClass()), aoqi@0: toVMClassName(Ref.class), aoqi@0: toVMClassName(t), aoqi@0: "()"+toVMTypeName(Ref.class), aoqi@0: "()"+toVMTypeName(t), aoqi@0: '('+toVMTypeName(Ref.class)+")V", aoqi@0: '('+toVMTypeName(t)+")V", aoqi@0: "get_ref", aoqi@0: getter.getName(), aoqi@0: "set_ref", aoqi@0: setter.getName()); aoqi@0: aoqi@0: if(opt==null) aoqi@0: return null; aoqi@0: aoqi@0: Accessor acc = instanciate(opt); aoqi@0: if (acc!=null) { aoqi@0: if (logger.isLoggable(Level.FINE)) { aoqi@0: logger.log(Level.FINE, "Using optimized Accessor for {0} and {1}", new Object[]{getter, setter}); aoqi@0: } aoqi@0: } aoqi@0: return acc; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Gets the optimized {@link Accessor} that accesses the given field. aoqi@0: * aoqi@0: * @return null aoqi@0: * if for some reason it fails to create an optimized version. aoqi@0: */ aoqi@0: public static final Accessor get(Field field) { aoqi@0: int mods = field.getModifiers(); aoqi@0: if(Modifier.isPrivate(mods) || Modifier.isFinal(mods)) aoqi@0: // we can't access private fields aoqi@0: return null; aoqi@0: aoqi@0: String newClassName = toVMClassName(field.getDeclaringClass())+"$JaxbAccessorF_"+field.getName(); aoqi@0: aoqi@0: Class opt; aoqi@0: aoqi@0: if(field.getType().isPrimitive()) aoqi@0: opt = AccessorInjector.prepare( field.getDeclaringClass(), aoqi@0: fieldTemplateName+RuntimeUtil.primitiveToBox.get(field.getType()).getSimpleName(), aoqi@0: newClassName, aoqi@0: toVMClassName(Bean.class), aoqi@0: toVMClassName(field.getDeclaringClass()), aoqi@0: "f_"+field.getType().getName(), aoqi@0: field.getName() ); aoqi@0: else aoqi@0: opt = AccessorInjector.prepare( field.getDeclaringClass(), aoqi@0: fieldTemplateName+"Ref", aoqi@0: newClassName, aoqi@0: toVMClassName(Bean.class), aoqi@0: toVMClassName(field.getDeclaringClass()), aoqi@0: toVMClassName(Ref.class), aoqi@0: toVMClassName(field.getType()), aoqi@0: toVMTypeName(Ref.class), aoqi@0: toVMTypeName(field.getType()), aoqi@0: "f_ref", aoqi@0: field.getName() ); aoqi@0: aoqi@0: if(opt==null) aoqi@0: return null; aoqi@0: aoqi@0: Accessor acc = instanciate(opt); aoqi@0: if (acc!=null) { aoqi@0: if (logger.isLoggable(Level.FINE)) { aoqi@0: logger.log(Level.FINE, "Using optimized Accessor for {0}", field); aoqi@0: } aoqi@0: } aoqi@0: return acc; aoqi@0: } aoqi@0: aoqi@0: private static Accessor instanciate(Class opt) { aoqi@0: try { aoqi@0: return (Accessor)opt.newInstance(); aoqi@0: } catch (InstantiationException e) { aoqi@0: logger.log(Level.INFO,"failed to load an optimized Accessor",e); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: logger.log(Level.INFO,"failed to load an optimized Accessor",e); aoqi@0: } catch (SecurityException e) { aoqi@0: logger.log(Level.INFO,"failed to load an optimized Accessor",e); aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: }