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