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/AccessorInjector.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,112 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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.io.InputStream; 1.32 +import java.util.logging.Level; 1.33 +import java.util.logging.Logger; 1.34 + 1.35 +import com.sun.xml.internal.bind.Util; 1.36 +import com.sun.xml.internal.bind.v2.bytecode.ClassTailor; 1.37 + 1.38 +/** 1.39 + * @author Kohsuke Kawaguchi 1.40 + */ 1.41 +class AccessorInjector { 1.42 + 1.43 + private static final Logger logger = Util.getClassLogger(); 1.44 + 1.45 + protected static final boolean noOptimize = 1.46 + Util.getSystemProperty(ClassTailor.class.getName()+".noOptimize")!=null; 1.47 + 1.48 + static { 1.49 + if(noOptimize) 1.50 + logger.info("The optimized code generation is disabled"); 1.51 + } 1.52 + 1.53 + /** 1.54 + * Loads the optimized class and returns it. 1.55 + * 1.56 + * @return null 1.57 + * if it fails for some reason. 1.58 + */ 1.59 + public static Class<?> prepare( 1.60 + Class beanClass, String templateClassName, String newClassName, String... replacements ) { 1.61 + 1.62 + if(noOptimize) 1.63 + return null; 1.64 + 1.65 + try { 1.66 + ClassLoader cl = SecureLoader.getClassClassLoader(beanClass); 1.67 + if(cl==null) return null; // how do I inject classes to this "null" class loader? for now, back off. 1.68 + 1.69 + Class c = Injector.find(cl,newClassName); 1.70 + if (c==null) { 1.71 + byte[] image = tailor(templateClassName,newClassName,replacements); 1.72 + if (image==null) { 1.73 + return null; 1.74 + } 1.75 + c = Injector.inject(cl,newClassName,image); 1.76 + if (c == null) { 1.77 + Injector.find(cl, newClassName); 1.78 + } 1.79 + } 1.80 + return c; 1.81 + } catch(SecurityException e) { 1.82 + // we don't have enough permission to do this 1.83 + logger.log(Level.INFO,"Unable to create an optimized TransducedAccessor ",e); 1.84 + return null; 1.85 + } 1.86 + } 1.87 + 1.88 + 1.89 + /** 1.90 + * Customizes a class file by replacing constant pools. 1.91 + * 1.92 + * @param templateClassName 1.93 + * The resource that contains the template class file. 1.94 + * @param replacements 1.95 + * A list of pair of strings that specify the substitution 1.96 + * {@code String[]{search_0, replace_0, search_1, replace_1, ..., search_n, replace_n } 1.97 + * 1.98 + * The search strings found in the constant pool will be replaced by the corresponding 1.99 + * replacement string. 1.100 + */ 1.101 + private static byte[] tailor( String templateClassName, String newClassName, String... replacements ) { 1.102 + InputStream resource; 1.103 + if(CLASS_LOADER!=null) 1.104 + resource = CLASS_LOADER.getResourceAsStream(templateClassName+".class"); 1.105 + else 1.106 + resource = ClassLoader.getSystemResourceAsStream(templateClassName+".class"); 1.107 + if(resource==null) 1.108 + return null; 1.109 + 1.110 + return ClassTailor.tailor(resource,templateClassName,newClassName,replacements); 1.111 + } 1.112 + 1.113 + private static final ClassLoader CLASS_LOADER = SecureLoader.getClassClassLoader(AccessorInjector.class); 1.114 + 1.115 +}