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/bytecode/ClassTailor.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,191 @@ 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.bytecode; 1.30 + 1.31 +import java.io.ByteArrayOutputStream; 1.32 +import java.io.DataInputStream; 1.33 +import java.io.DataOutputStream; 1.34 +import java.io.IOException; 1.35 +import java.io.InputStream; 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 + 1.41 +/** 1.42 + * Replaces a few constant pool tokens from a class "template" and then loads it into the VM. 1.43 + * 1.44 + * @author Kohsuke Kawaguchi 1.45 + */ 1.46 +public final class ClassTailor { 1.47 + 1.48 + private ClassTailor() {} // no instanciation please 1.49 + 1.50 + private static final Logger logger = Util.getClassLogger(); 1.51 + 1.52 + /** 1.53 + * Returns the class name in the JVM format (such as "java/lang/String") 1.54 + */ 1.55 + public static String toVMClassName( Class c ) { 1.56 + assert !c.isPrimitive(); 1.57 + if(c.isArray()) 1.58 + // I have no idea why it is designed like this, but javap says so. 1.59 + return toVMTypeName(c); 1.60 + return c.getName().replace('.','/'); 1.61 + } 1.62 + 1.63 + public static String toVMTypeName( Class c ) { 1.64 + if(c.isArray()) { 1.65 + // TODO: study how an array type is encoded. 1.66 + return '['+toVMTypeName(c.getComponentType()); 1.67 + } 1.68 + if(c.isPrimitive()) { 1.69 + if(c==Boolean.TYPE) return "Z"; 1.70 + if(c==Character.TYPE) return "C"; 1.71 + if(c==Byte.TYPE) return "B"; 1.72 + if(c==Double.TYPE) return "D"; 1.73 + if(c==Float.TYPE) return "F"; 1.74 + if(c==Integer.TYPE) return "I"; 1.75 + if(c==Long.TYPE) return "J"; 1.76 + if(c==Short.TYPE) return "S"; 1.77 + 1.78 + throw new IllegalArgumentException(c.getName()); 1.79 + } 1.80 + return 'L'+c.getName().replace('.','/')+';'; 1.81 + } 1.82 + 1.83 + 1.84 + 1.85 + public static byte[] tailor( Class templateClass, String newClassName, String... replacements ) { 1.86 + String vmname = toVMClassName(templateClass); 1.87 + return tailor( 1.88 + SecureLoader.getClassClassLoader(templateClass).getResourceAsStream(vmname+".class"), 1.89 + vmname, newClassName, replacements ); 1.90 + } 1.91 + 1.92 + 1.93 + /** 1.94 + * Customizes a class file by replacing constant pools. 1.95 + * 1.96 + * @param image 1.97 + * The image of the template class. 1.98 + * @param replacements 1.99 + * A list of pair of strings that specify the substitution 1.100 + * {@code String[]{search_0, replace_0, search_1, replace_1, ..., search_n, replace_n }} 1.101 + * 1.102 + * The search strings found in the constant pool will be replaced by the corresponding 1.103 + * replacement string. 1.104 + */ 1.105 + public static byte[] tailor( InputStream image, String templateClassName, String newClassName, String... replacements ) { 1.106 + DataInputStream in = new DataInputStream(image); 1.107 + 1.108 + try { 1.109 + ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); 1.110 + DataOutputStream out = new DataOutputStream(baos); 1.111 + 1.112 + // skip until the constant pool count 1.113 + long l = in.readLong(); 1.114 + out.writeLong(l); 1.115 + 1.116 + // read the constant pool size 1.117 + short count = in.readShort(); 1.118 + out.writeShort(count); 1.119 + 1.120 + // replace constant pools 1.121 + for( int i=0; i<count; i++ ) { 1.122 + byte tag = in.readByte(); 1.123 + out.writeByte(tag); 1.124 + switch(tag) { 1.125 + case 0: 1.126 + // this isn't described in the spec, 1.127 + // but class files often seem to have this '0' tag. 1.128 + // we can apparently just ignore it, but not sure 1.129 + // what this really means. 1.130 + break; 1.131 + 1.132 + case 1: // CONSTANT_UTF8 1.133 + { 1.134 + String value = in.readUTF(); 1.135 + if(value.equals(templateClassName)) 1.136 + value = newClassName; 1.137 + else { 1.138 + for( int j=0; j<replacements.length; j+=2 ) 1.139 + if(value.equals(replacements[j])) { 1.140 + value = replacements[j+1]; 1.141 + break; 1.142 + } 1.143 + } 1.144 + out.writeUTF(value); 1.145 + } 1.146 + break; 1.147 + 1.148 + case 3: // CONSTANT_Integer 1.149 + case 4: // CONSTANT_Float 1.150 + out.writeInt(in.readInt()); 1.151 + break; 1.152 + 1.153 + case 5: // CONSTANT_Long 1.154 + case 6: // CONSTANT_Double 1.155 + i++; // doubles and longs take two entries 1.156 + out.writeLong(in.readLong()); 1.157 + break; 1.158 + 1.159 + case 7: // CONSTANT_Class 1.160 + case 8: // CONSTANT_String 1.161 + out.writeShort(in.readShort()); 1.162 + break; 1.163 + 1.164 + case 9: // CONSTANT_Fieldref 1.165 + case 10: // CONSTANT_Methodref 1.166 + case 11: // CONSTANT_InterfaceMethodref 1.167 + case 12: // CONSTANT_NameAndType 1.168 + out.writeInt(in.readInt()); 1.169 + break; 1.170 + 1.171 + default: 1.172 + throw new IllegalArgumentException("Unknown constant type "+tag); 1.173 + } 1.174 + } 1.175 + 1.176 + // then copy the rest 1.177 + byte[] buf = new byte[512]; 1.178 + int len; 1.179 + while((len=in.read(buf))>0) 1.180 + out.write(buf,0,len); 1.181 + 1.182 + in.close(); 1.183 + out.close(); 1.184 + 1.185 + // by now we got the properly tailored class file image 1.186 + return baos.toByteArray(); 1.187 + 1.188 + } catch( IOException e ) { 1.189 + // never happen 1.190 + logger.log(Level.WARNING,"failed to tailor",e); 1.191 + return null; 1.192 + } 1.193 + } 1.194 +}