src/share/jaxws_classes/com/sun/xml/internal/bind/v2/bytecode/ClassTailor.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.bytecode;
aoqi@0 27
aoqi@0 28 import java.io.ByteArrayOutputStream;
aoqi@0 29 import java.io.DataInputStream;
aoqi@0 30 import java.io.DataOutputStream;
aoqi@0 31 import java.io.IOException;
aoqi@0 32 import java.io.InputStream;
aoqi@0 33 import java.util.logging.Level;
aoqi@0 34 import java.util.logging.Logger;
aoqi@0 35
aoqi@0 36 import com.sun.xml.internal.bind.Util;
aoqi@0 37
aoqi@0 38 /**
aoqi@0 39 * Replaces a few constant pool tokens from a class "template" and then loads it into the VM.
aoqi@0 40 *
aoqi@0 41 * @author Kohsuke Kawaguchi
aoqi@0 42 */
aoqi@0 43 public final class ClassTailor {
aoqi@0 44
aoqi@0 45 private ClassTailor() {} // no instanciation please
aoqi@0 46
aoqi@0 47 private static final Logger logger = Util.getClassLogger();
aoqi@0 48
aoqi@0 49 /**
aoqi@0 50 * Returns the class name in the JVM format (such as "java/lang/String")
aoqi@0 51 */
aoqi@0 52 public static String toVMClassName( Class c ) {
aoqi@0 53 assert !c.isPrimitive();
aoqi@0 54 if(c.isArray())
aoqi@0 55 // I have no idea why it is designed like this, but javap says so.
aoqi@0 56 return toVMTypeName(c);
aoqi@0 57 return c.getName().replace('.','/');
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 public static String toVMTypeName( Class c ) {
aoqi@0 61 if(c.isArray()) {
aoqi@0 62 // TODO: study how an array type is encoded.
aoqi@0 63 return '['+toVMTypeName(c.getComponentType());
aoqi@0 64 }
aoqi@0 65 if(c.isPrimitive()) {
aoqi@0 66 if(c==Boolean.TYPE) return "Z";
aoqi@0 67 if(c==Character.TYPE) return "C";
aoqi@0 68 if(c==Byte.TYPE) return "B";
aoqi@0 69 if(c==Double.TYPE) return "D";
aoqi@0 70 if(c==Float.TYPE) return "F";
aoqi@0 71 if(c==Integer.TYPE) return "I";
aoqi@0 72 if(c==Long.TYPE) return "J";
aoqi@0 73 if(c==Short.TYPE) return "S";
aoqi@0 74
aoqi@0 75 throw new IllegalArgumentException(c.getName());
aoqi@0 76 }
aoqi@0 77 return 'L'+c.getName().replace('.','/')+';';
aoqi@0 78 }
aoqi@0 79
aoqi@0 80
aoqi@0 81
aoqi@0 82 public static byte[] tailor( Class templateClass, String newClassName, String... replacements ) {
aoqi@0 83 String vmname = toVMClassName(templateClass);
aoqi@0 84 return tailor(
aoqi@0 85 SecureLoader.getClassClassLoader(templateClass).getResourceAsStream(vmname+".class"),
aoqi@0 86 vmname, newClassName, replacements );
aoqi@0 87 }
aoqi@0 88
aoqi@0 89
aoqi@0 90 /**
aoqi@0 91 * Customizes a class file by replacing constant pools.
aoqi@0 92 *
aoqi@0 93 * @param image
aoqi@0 94 * The image of the template class.
aoqi@0 95 * @param replacements
aoqi@0 96 * A list of pair of strings that specify the substitution
aoqi@0 97 * {@code String[]{search_0, replace_0, search_1, replace_1, ..., search_n, replace_n }}
aoqi@0 98 *
aoqi@0 99 * The search strings found in the constant pool will be replaced by the corresponding
aoqi@0 100 * replacement string.
aoqi@0 101 */
aoqi@0 102 public static byte[] tailor( InputStream image, String templateClassName, String newClassName, String... replacements ) {
aoqi@0 103 DataInputStream in = new DataInputStream(image);
aoqi@0 104
aoqi@0 105 try {
aoqi@0 106 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
aoqi@0 107 DataOutputStream out = new DataOutputStream(baos);
aoqi@0 108
aoqi@0 109 // skip until the constant pool count
aoqi@0 110 long l = in.readLong();
aoqi@0 111 out.writeLong(l);
aoqi@0 112
aoqi@0 113 // read the constant pool size
aoqi@0 114 short count = in.readShort();
aoqi@0 115 out.writeShort(count);
aoqi@0 116
aoqi@0 117 // replace constant pools
aoqi@0 118 for( int i=0; i<count; i++ ) {
aoqi@0 119 byte tag = in.readByte();
aoqi@0 120 out.writeByte(tag);
aoqi@0 121 switch(tag) {
aoqi@0 122 case 0:
aoqi@0 123 // this isn't described in the spec,
aoqi@0 124 // but class files often seem to have this '0' tag.
aoqi@0 125 // we can apparently just ignore it, but not sure
aoqi@0 126 // what this really means.
aoqi@0 127 break;
aoqi@0 128
aoqi@0 129 case 1: // CONSTANT_UTF8
aoqi@0 130 {
aoqi@0 131 String value = in.readUTF();
aoqi@0 132 if(value.equals(templateClassName))
aoqi@0 133 value = newClassName;
aoqi@0 134 else {
aoqi@0 135 for( int j=0; j<replacements.length; j+=2 )
aoqi@0 136 if(value.equals(replacements[j])) {
aoqi@0 137 value = replacements[j+1];
aoqi@0 138 break;
aoqi@0 139 }
aoqi@0 140 }
aoqi@0 141 out.writeUTF(value);
aoqi@0 142 }
aoqi@0 143 break;
aoqi@0 144
aoqi@0 145 case 3: // CONSTANT_Integer
aoqi@0 146 case 4: // CONSTANT_Float
aoqi@0 147 out.writeInt(in.readInt());
aoqi@0 148 break;
aoqi@0 149
aoqi@0 150 case 5: // CONSTANT_Long
aoqi@0 151 case 6: // CONSTANT_Double
aoqi@0 152 i++; // doubles and longs take two entries
aoqi@0 153 out.writeLong(in.readLong());
aoqi@0 154 break;
aoqi@0 155
aoqi@0 156 case 7: // CONSTANT_Class
aoqi@0 157 case 8: // CONSTANT_String
aoqi@0 158 out.writeShort(in.readShort());
aoqi@0 159 break;
aoqi@0 160
aoqi@0 161 case 9: // CONSTANT_Fieldref
aoqi@0 162 case 10: // CONSTANT_Methodref
aoqi@0 163 case 11: // CONSTANT_InterfaceMethodref
aoqi@0 164 case 12: // CONSTANT_NameAndType
aoqi@0 165 out.writeInt(in.readInt());
aoqi@0 166 break;
aoqi@0 167
aoqi@0 168 default:
aoqi@0 169 throw new IllegalArgumentException("Unknown constant type "+tag);
aoqi@0 170 }
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 // then copy the rest
aoqi@0 174 byte[] buf = new byte[512];
aoqi@0 175 int len;
aoqi@0 176 while((len=in.read(buf))>0)
aoqi@0 177 out.write(buf,0,len);
aoqi@0 178
aoqi@0 179 in.close();
aoqi@0 180 out.close();
aoqi@0 181
aoqi@0 182 // by now we got the properly tailored class file image
aoqi@0 183 return baos.toByteArray();
aoqi@0 184
aoqi@0 185 } catch( IOException e ) {
aoqi@0 186 // never happen
aoqi@0 187 logger.log(Level.WARNING,"failed to tailor",e);
aoqi@0 188 return null;
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191 }

mercurial