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.io.InputStream; 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.bytecode.ClassTailor; ohair@286: ohair@286: /** ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: class AccessorInjector { ohair@286: ohair@286: private static final Logger logger = Util.getClassLogger(); ohair@286: ohair@286: protected static final boolean noOptimize = ohair@286: Util.getSystemProperty(ClassTailor.class.getName()+".noOptimize")!=null; ohair@286: ohair@286: static { ohair@286: if(noOptimize) ohair@286: logger.info("The optimized code generation is disabled"); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Loads the optimized class and returns it. ohair@286: * ohair@286: * @return null ohair@286: * if it fails for some reason. ohair@286: */ ohair@286: public static Class prepare( ohair@286: Class beanClass, String templateClassName, String newClassName, String... replacements ) { ohair@286: ohair@286: if(noOptimize) ohair@286: return null; ohair@286: ohair@286: try { ohair@286: ClassLoader cl = SecureLoader.getClassClassLoader(beanClass); ohair@286: if(cl==null) return null; // how do I inject classes to this "null" class loader? for now, back off. ohair@286: mkos@408: Class c = Injector.find(cl,newClassName); mkos@408: if (c==null) { mkos@408: byte[] image = tailor(templateClassName,newClassName,replacements); mkos@408: if (image==null) { mkos@408: return null; mkos@408: } mkos@408: c = Injector.inject(cl,newClassName,image); mkos@408: if (c == null) { mkos@408: Injector.find(cl, newClassName); ohair@286: } ohair@286: } ohair@286: return c; ohair@286: } catch(SecurityException e) { ohair@286: // we don't have enough permission to do this ohair@286: logger.log(Level.INFO,"Unable to create an optimized TransducedAccessor ",e); ohair@286: return null; ohair@286: } ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Customizes a class file by replacing constant pools. ohair@286: * ohair@286: * @param templateClassName alanb@368: * The resource that contains the template class file. ohair@286: * @param replacements ohair@286: * A list of pair of strings that specify the substitution ohair@286: * {@code String[]{search_0, replace_0, search_1, replace_1, ..., search_n, replace_n } ohair@286: * ohair@286: * The search strings found in the constant pool will be replaced by the corresponding ohair@286: * replacement string. ohair@286: */ ohair@286: private static byte[] tailor( String templateClassName, String newClassName, String... replacements ) { ohair@286: InputStream resource; ohair@286: if(CLASS_LOADER!=null) ohair@286: resource = CLASS_LOADER.getResourceAsStream(templateClassName+".class"); ohair@286: else ohair@286: resource = ClassLoader.getSystemResourceAsStream(templateClassName+".class"); ohair@286: if(resource==null) ohair@286: return null; ohair@286: ohair@286: return ClassTailor.tailor(resource,templateClassName,newClassName,replacements); ohair@286: } ohair@286: ohair@286: private static final ClassLoader CLASS_LOADER = SecureLoader.getClassClassLoader(AccessorInjector.class); mkos@408: ohair@286: }