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

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/reflect/opt/OptimizedTransducedAccessorFactory.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,167 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.runtime.reflect.opt;
    1.30 +
    1.31 +import java.lang.reflect.Field;
    1.32 +import java.lang.reflect.Modifier;
    1.33 +import java.lang.reflect.Type;
    1.34 +import java.util.HashMap;
    1.35 +import java.util.Map;
    1.36 +import java.util.logging.Level;
    1.37 +import java.util.logging.Logger;
    1.38 +
    1.39 +import com.sun.xml.internal.bind.Util;
    1.40 +import com.sun.xml.internal.bind.v2.model.core.TypeInfo;
    1.41 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeClassInfo;
    1.42 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
    1.43 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
    1.44 +import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor;
    1.45 +
    1.46 +import static com.sun.xml.internal.bind.v2.bytecode.ClassTailor.toVMClassName;
    1.47 +
    1.48 +/**
    1.49 + * Prepares optimized {@link TransducedAccessor} from templates.
    1.50 + *
    1.51 + * @author Kohsuke Kawaguchi
    1.52 + */
    1.53 +public abstract class OptimizedTransducedAccessorFactory {
    1.54 +    private OptimizedTransducedAccessorFactory() {} // no instanciation please
    1.55 +
    1.56 +    // http://java.sun.com/docs/books/vmspec/2nd-edition/html/ConstantPool.doc.html#75929
    1.57 +    // "same runtime package"
    1.58 +
    1.59 +    private static final Logger logger = Util.getClassLogger();
    1.60 +
    1.61 +    private static final String fieldTemplateName;
    1.62 +    private static final String methodTemplateName;
    1.63 +
    1.64 +    static {
    1.65 +        String s = TransducedAccessor_field_Byte.class.getName();
    1.66 +        fieldTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/');
    1.67 +
    1.68 +        s = TransducedAccessor_method_Byte.class.getName();
    1.69 +        methodTemplateName = s.substring(0,s.length()-"Byte".length()).replace('.','/');
    1.70 +    }
    1.71 +
    1.72 +    /**
    1.73 +     * Gets the optimized {@link TransducedAccessor} if possible.
    1.74 +     *
    1.75 +     * @return null
    1.76 +     *      if for some reason it fails to create an optimized version.
    1.77 +     */
    1.78 +    public static final TransducedAccessor get(RuntimePropertyInfo prop) {
    1.79 +        Accessor acc = prop.getAccessor();
    1.80 +
    1.81 +        // consider using an optimized TransducedAccessor implementations.
    1.82 +        Class opt=null;
    1.83 +
    1.84 +        TypeInfo<Type,Class> parent = prop.parent();
    1.85 +        if(!(parent instanceof RuntimeClassInfo))
    1.86 +            return null;
    1.87 +
    1.88 +        Class dc = ((RuntimeClassInfo)parent).getClazz();
    1.89 +        String newClassName = toVMClassName(dc)+"_JaxbXducedAccessor_"+prop.getName();
    1.90 +
    1.91 +
    1.92 +        if(acc instanceof Accessor.FieldReflection) {
    1.93 +            // TODO: we also need to make sure that the default xducer is used.
    1.94 +            Accessor.FieldReflection racc = (Accessor.FieldReflection) acc;
    1.95 +            Field field = racc.f;
    1.96 +
    1.97 +            int mods = field.getModifiers();
    1.98 +            if(Modifier.isPrivate(mods) || Modifier.isFinal(mods))
    1.99 +                // we can't access private fields.
   1.100 +                // TODO: think about how to improve this case
   1.101 +                return null;
   1.102 +
   1.103 +            Class<?> t = field.getType();
   1.104 +            if(t.isPrimitive())
   1.105 +                opt = AccessorInjector.prepare( dc,
   1.106 +                    fieldTemplateName+suffixMap.get(t),
   1.107 +                    newClassName,
   1.108 +                    toVMClassName(Bean.class),
   1.109 +                    toVMClassName(dc),
   1.110 +                    "f_"+t.getName(),
   1.111 +                    field.getName() );
   1.112 +        }
   1.113 +
   1.114 +        if(acc.getClass()==Accessor.GetterSetterReflection.class) {
   1.115 +            Accessor.GetterSetterReflection gacc = (Accessor.GetterSetterReflection) acc;
   1.116 +
   1.117 +            if(gacc.getter==null || gacc.setter==null)
   1.118 +                return null;    // incomplete
   1.119 +
   1.120 +            Class<?> t = gacc.getter.getReturnType();
   1.121 +
   1.122 +            if(Modifier.isPrivate(gacc.getter.getModifiers())
   1.123 +            || Modifier.isPrivate(gacc.setter.getModifiers()))
   1.124 +                // we can't access private methods.
   1.125 +                return null;
   1.126 +
   1.127 +
   1.128 +            if(t.isPrimitive())
   1.129 +                opt = AccessorInjector.prepare( dc,
   1.130 +                    methodTemplateName+suffixMap.get(t),
   1.131 +                    newClassName,
   1.132 +                    toVMClassName(Bean.class),
   1.133 +                    toVMClassName(dc),
   1.134 +                    "get_"+t.getName(),
   1.135 +                    gacc.getter.getName(),
   1.136 +                    "set_"+t.getName(),
   1.137 +                    gacc.setter.getName());
   1.138 +        }
   1.139 +
   1.140 +        if(opt==null)
   1.141 +            return null;
   1.142 +
   1.143 +        logger.log(Level.FINE,"Using optimized TransducedAccessor for "+prop.displayName());
   1.144 +
   1.145 +
   1.146 +        try {
   1.147 +            return (TransducedAccessor)opt.newInstance();
   1.148 +        } catch (InstantiationException e) {
   1.149 +            logger.log(Level.INFO,"failed to load an optimized TransducedAccessor",e);
   1.150 +        } catch (IllegalAccessException e) {
   1.151 +            logger.log(Level.INFO,"failed to load an optimized TransducedAccessor",e);
   1.152 +        } catch (SecurityException e) {
   1.153 +            logger.log(Level.INFO,"failed to load an optimized TransducedAccessor",e);
   1.154 +        }
   1.155 +        return null;
   1.156 +    }
   1.157 +
   1.158 +    private static final Map<Class,String> suffixMap = new HashMap<Class, String>();
   1.159 +
   1.160 +    static {
   1.161 +        suffixMap.put(Byte.TYPE,"Byte");
   1.162 +        suffixMap.put(Short.TYPE,"Short");
   1.163 +        suffixMap.put(Integer.TYPE,"Integer");
   1.164 +        suffixMap.put(Long.TYPE,"Long");
   1.165 +        suffixMap.put(Boolean.TYPE,"Boolean");
   1.166 +        suffixMap.put(Float.TYPE,"Float");
   1.167 +        suffixMap.put(Double.TYPE,"Double");
   1.168 +    }
   1.169 +
   1.170 +}

mercurial