aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, 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.bytecode; aoqi@0: aoqi@0: import java.io.ByteArrayOutputStream; aoqi@0: import java.io.DataInputStream; aoqi@0: import java.io.DataOutputStream; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; 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: aoqi@0: /** aoqi@0: * Replaces a few constant pool tokens from a class "template" and then loads it into the VM. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class ClassTailor { aoqi@0: aoqi@0: private ClassTailor() {} // no instanciation please aoqi@0: aoqi@0: private static final Logger logger = Util.getClassLogger(); aoqi@0: aoqi@0: /** aoqi@0: * Returns the class name in the JVM format (such as "java/lang/String") aoqi@0: */ aoqi@0: public static String toVMClassName( Class c ) { aoqi@0: assert !c.isPrimitive(); aoqi@0: if(c.isArray()) aoqi@0: // I have no idea why it is designed like this, but javap says so. aoqi@0: return toVMTypeName(c); aoqi@0: return c.getName().replace('.','/'); aoqi@0: } aoqi@0: aoqi@0: public static String toVMTypeName( Class c ) { aoqi@0: if(c.isArray()) { aoqi@0: // TODO: study how an array type is encoded. aoqi@0: return '['+toVMTypeName(c.getComponentType()); aoqi@0: } aoqi@0: if(c.isPrimitive()) { aoqi@0: if(c==Boolean.TYPE) return "Z"; aoqi@0: if(c==Character.TYPE) return "C"; aoqi@0: if(c==Byte.TYPE) return "B"; aoqi@0: if(c==Double.TYPE) return "D"; aoqi@0: if(c==Float.TYPE) return "F"; aoqi@0: if(c==Integer.TYPE) return "I"; aoqi@0: if(c==Long.TYPE) return "J"; aoqi@0: if(c==Short.TYPE) return "S"; aoqi@0: aoqi@0: throw new IllegalArgumentException(c.getName()); aoqi@0: } aoqi@0: return 'L'+c.getName().replace('.','/')+';'; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: public static byte[] tailor( Class templateClass, String newClassName, String... replacements ) { aoqi@0: String vmname = toVMClassName(templateClass); aoqi@0: return tailor( aoqi@0: SecureLoader.getClassClassLoader(templateClass).getResourceAsStream(vmname+".class"), aoqi@0: vmname, newClassName, replacements ); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Customizes a class file by replacing constant pools. aoqi@0: * aoqi@0: * @param image aoqi@0: * The image of the template class. aoqi@0: * @param replacements aoqi@0: * A list of pair of strings that specify the substitution aoqi@0: * {@code String[]{search_0, replace_0, search_1, replace_1, ..., search_n, replace_n }} aoqi@0: * aoqi@0: * The search strings found in the constant pool will be replaced by the corresponding aoqi@0: * replacement string. aoqi@0: */ aoqi@0: public static byte[] tailor( InputStream image, String templateClassName, String newClassName, String... replacements ) { aoqi@0: DataInputStream in = new DataInputStream(image); aoqi@0: aoqi@0: try { aoqi@0: ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); aoqi@0: DataOutputStream out = new DataOutputStream(baos); aoqi@0: aoqi@0: // skip until the constant pool count aoqi@0: long l = in.readLong(); aoqi@0: out.writeLong(l); aoqi@0: aoqi@0: // read the constant pool size aoqi@0: short count = in.readShort(); aoqi@0: out.writeShort(count); aoqi@0: aoqi@0: // replace constant pools aoqi@0: for( int i=0; i0) aoqi@0: out.write(buf,0,len); aoqi@0: aoqi@0: in.close(); aoqi@0: out.close(); aoqi@0: aoqi@0: // by now we got the properly tailored class file image aoqi@0: return baos.toByteArray(); aoqi@0: aoqi@0: } catch( IOException e ) { aoqi@0: // never happen aoqi@0: logger.log(Level.WARNING,"failed to tailor",e); aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: }