jjg@46: /* jjg@46: * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. jjg@46: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@46: * jjg@46: * This code is free software; you can redistribute it and/or modify it jjg@46: * under the terms of the GNU General Public License version 2 only, as jjg@46: * published by the Free Software Foundation. Sun designates this jjg@46: * particular file as subject to the "Classpath" exception as provided jjg@46: * by Sun in the LICENSE file that accompanied this code. jjg@46: * jjg@46: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@46: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@46: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@46: * version 2 for more details (a copy is included in the LICENSE file that jjg@46: * accompanied this code). jjg@46: * jjg@46: * You should have received a copy of the GNU General Public License version jjg@46: * 2 along with this work; if not, write to the Free Software Foundation, jjg@46: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@46: * jjg@46: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@46: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@46: * have any questions. jjg@46: */ jjg@46: jjg@46: package com.sun.tools.classfile; jjg@46: jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_Double_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_Fieldref_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_Float_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_Integer_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_InterfaceMethodref_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_Long_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_NameAndType_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_String_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info; jjg@46: import com.sun.tools.classfile.ConstantPool.CPInfo; jjg@46: import java.util.Map; jjg@46: jjg@46: /** jjg@46: * Rewrites a class file using a map of translations. jjg@46: * jjg@46: *

This is NOT part of any API supported by Sun Microsystems. If jjg@46: * you write code that depends on this, you do so at your own risk. jjg@46: * This code and its internal interfaces are subject to change or jjg@46: * deletion without notice. jjg@46: */ jjg@46: public class ClassTranslator jjg@46: implements ConstantPool.Visitor> { jjg@46: /** jjg@46: * Create a new ClassFile from {@code cf}, such that for all entries jjg@46: * {@code k -\> v} in {@code translations}, jjg@46: * each occurrence of {@code k} in {@code cf} will be replaced by {@code v}. jjg@46: * in jjg@46: * @param cf the class file to be processed jjg@46: * @param translations the set of translations to be applied jjg@46: * @return a copy of {@code} with the values in {@code translations} substituted jjg@46: */ jjg@46: public ClassFile translate(ClassFile cf, Map translations) { jjg@46: ClassFile cf2 = (ClassFile) translations.get(cf); jjg@46: if (cf2 == null) { jjg@46: ConstantPool constant_pool2 = translate(cf.constant_pool, translations); jjg@46: Field[] fields2 = translate(cf.fields, cf.constant_pool, translations); jjg@46: Method[] methods2 = translateMethods(cf.methods, cf.constant_pool, translations); jjg@46: Attributes attributes2 = translateAttributes(cf.attributes, cf.constant_pool, jjg@46: translations); jjg@46: jjg@46: if (constant_pool2 == cf.constant_pool && jjg@46: fields2 == cf.fields && jjg@46: methods2 == cf.methods && jjg@46: attributes2 == cf.attributes) jjg@46: cf2 = cf; jjg@46: else jjg@46: cf2 = new ClassFile( jjg@46: cf.magic, jjg@46: cf.minor_version, jjg@46: cf.major_version, jjg@46: constant_pool2, jjg@46: cf.access_flags, jjg@46: cf.this_class, jjg@46: cf.super_class, jjg@46: cf.interfaces, jjg@46: fields2, jjg@46: methods2, jjg@46: attributes2); jjg@46: translations.put(cf, cf2); jjg@46: } jjg@46: return cf2; jjg@46: } jjg@46: jjg@46: ConstantPool translate(ConstantPool cp, Map translations) { jjg@46: ConstantPool cp2 = (ConstantPool) translations.get(cp); jjg@46: if (cp2 == null) { jjg@46: ConstantPool.CPInfo[] pool2 = new ConstantPool.CPInfo[cp.size()]; jjg@46: boolean eq = true; jjg@46: for (int i = 0; i < cp.size(); i++) { jjg@46: ConstantPool.CPInfo cpInfo; jjg@46: try { jjg@46: cpInfo = cp.get(i); jjg@46: } catch (ConstantPool.InvalidIndex e) { jjg@46: throw new IllegalStateException(e); jjg@46: } jjg@46: ConstantPool.CPInfo cpInfo2 = translate(cpInfo, translations); jjg@46: eq &= (cpInfo == cpInfo2); jjg@46: pool2[i] = cpInfo2; jjg@46: if (cpInfo.getTag() != cpInfo2.getTag()) jjg@46: throw new IllegalStateException(); jjg@46: switch (cpInfo.getTag()) { jjg@46: case ConstantPool.CONSTANT_Double: jjg@46: case ConstantPool.CONSTANT_Long: jjg@46: i += 1; jjg@46: } jjg@46: } jjg@46: jjg@46: if (eq) jjg@46: cp2 = cp; jjg@46: else jjg@46: cp2 = new ConstantPool(pool2); jjg@46: jjg@46: translations.put(cp, cp2); jjg@46: } jjg@46: return cp2; jjg@46: } jjg@46: jjg@46: ConstantPool.CPInfo translate(ConstantPool.CPInfo cpInfo, Map translations) { jjg@46: ConstantPool.CPInfo cpInfo2 = (ConstantPool.CPInfo) translations.get(cpInfo); jjg@46: if (cpInfo2 == null) { jjg@46: cpInfo2 = cpInfo.accept(this, translations); jjg@46: translations.put(cpInfo, cpInfo2); jjg@46: } jjg@46: return cpInfo2; jjg@46: } jjg@46: jjg@46: Field[] translate(Field[] fields, ConstantPool constant_pool, Map translations) { jjg@46: Field[] fields2 = (Field[]) translations.get(fields); jjg@46: if (fields2 == null) { jjg@46: fields2 = new Field[fields.length]; jjg@46: for (int i = 0; i < fields.length; i++) jjg@46: fields2[i] = translate(fields[i], constant_pool, translations); jjg@46: if (equal(fields, fields2)) jjg@46: fields2 = fields; jjg@46: translations.put(fields, fields2); jjg@46: } jjg@46: return fields2; jjg@46: } jjg@46: jjg@46: Field translate(Field field, ConstantPool constant_pool, Map translations) { jjg@46: Field field2 = (Field) translations.get(field); jjg@46: if (field2 == null) { jjg@46: Attributes attributes2 = translateAttributes(field.attributes, constant_pool, jjg@46: translations); jjg@46: jjg@46: if (attributes2 == field.attributes) jjg@46: field2 = field; jjg@46: else jjg@46: field2 = new Field( jjg@46: field.access_flags, jjg@46: field.name_index, jjg@46: field.descriptor, jjg@46: attributes2); jjg@46: translations.put(field, field2); jjg@46: } jjg@46: return field2; jjg@46: } jjg@46: jjg@46: Method[] translateMethods(Method[] methods, ConstantPool constant_pool, Map translations) { jjg@46: Method[] methods2 = (Method[]) translations.get(methods); jjg@46: if (methods2 == null) { jjg@46: methods2 = new Method[methods.length]; jjg@46: for (int i = 0; i < methods.length; i++) jjg@46: methods2[i] = translate(methods[i], constant_pool, translations); jjg@46: if (equal(methods, methods2)) jjg@46: methods2 = methods; jjg@46: translations.put(methods, methods2); jjg@46: } jjg@46: return methods2; jjg@46: } jjg@46: jjg@46: Method translate(Method method, ConstantPool constant_pool, Map translations) { jjg@46: Method method2 = (Method) translations.get(method); jjg@46: if (method2 == null) { jjg@46: Attributes attributes2 = translateAttributes(method.attributes, constant_pool, jjg@46: translations); jjg@46: jjg@46: if (attributes2 == method.attributes) jjg@46: method2 = method; jjg@46: else jjg@46: method2 = new Method( jjg@46: method.access_flags, jjg@46: method.name_index, jjg@46: method.descriptor, jjg@46: attributes2); jjg@46: translations.put(method, method2); jjg@46: } jjg@46: return method2; jjg@46: } jjg@46: jjg@46: Attributes translateAttributes(Attributes attributes, jjg@46: ConstantPool constant_pool, Map translations) { jjg@46: Attributes attributes2 = (Attributes) translations.get(attributes); jjg@46: if (attributes2 == null) { jjg@46: Attribute[] attrArray2 = new Attribute[attributes.size()]; jjg@46: ConstantPool constant_pool2 = translate(constant_pool, translations); jjg@46: boolean attrsEqual = true; jjg@46: for (int i = 0; i < attributes.size(); i++) { jjg@46: Attribute attr = attributes.get(i); jjg@46: Attribute attr2 = translate(attr, translations); jjg@46: if (attr2 != attr) jjg@46: attrsEqual = false; jjg@46: attrArray2[i] = attr2; jjg@46: } jjg@46: if ((constant_pool2 == constant_pool) && attrsEqual) jjg@46: attributes2 = attributes; jjg@46: else jjg@46: attributes2 = new Attributes(constant_pool2, attrArray2); jjg@46: translations.put(attributes, attributes2); jjg@46: } jjg@46: return attributes2; jjg@46: } jjg@46: jjg@46: Attribute translate(Attribute attribute, Map translations) { jjg@46: Attribute attribute2 = (Attribute) translations.get(attribute); jjg@46: if (attribute2 == null) { jjg@46: attribute2 = attribute; // don't support translation within attributes yet jjg@46: // (what about Code attribute) jjg@46: translations.put(attribute, attribute2); jjg@46: } jjg@46: return attribute2; jjg@46: } jjg@46: jjg@46: private static boolean equal(T[] a1, T[] a2) { jjg@46: if (a1 == null || a2 == null) jjg@46: return (a1 == a2); jjg@46: if (a1.length != a2.length) jjg@46: return false; jjg@46: for (int i = 0; i < a1.length; i++) { jjg@46: if (a1[i] != a2[i]) jjg@46: return false; jjg@46: } jjg@46: return true; jjg@46: } jjg@46: jjg@46: public CPInfo visitClass(CONSTANT_Class_info info, Map translations) { jjg@46: CONSTANT_Class_info info2 = (CONSTANT_Class_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: ConstantPool cp2 = translate(info.cp, translations); jjg@46: if (cp2 == info.cp) jjg@46: info2 = info; jjg@46: else jjg@46: info2 = new CONSTANT_Class_info(cp2, info.name_index); jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitDouble(CONSTANT_Double_info info, Map translations) { jjg@46: CONSTANT_Double_info info2 = (CONSTANT_Double_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: info2 = info; jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitFieldref(CONSTANT_Fieldref_info info, Map translations) { jjg@46: CONSTANT_Fieldref_info info2 = (CONSTANT_Fieldref_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: ConstantPool cp2 = translate(info.cp, translations); jjg@46: if (cp2 == info.cp) jjg@46: info2 = info; jjg@46: else jjg@46: info2 = new CONSTANT_Fieldref_info(cp2, info.class_index, info.name_and_type_index); jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitFloat(CONSTANT_Float_info info, Map translations) { jjg@46: CONSTANT_Float_info info2 = (CONSTANT_Float_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: info2 = info; jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitInteger(CONSTANT_Integer_info info, Map translations) { jjg@46: CONSTANT_Integer_info info2 = (CONSTANT_Integer_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: info2 = info; jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Map translations) { jjg@46: CONSTANT_InterfaceMethodref_info info2 = (CONSTANT_InterfaceMethodref_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: ConstantPool cp2 = translate(info.cp, translations); jjg@46: if (cp2 == info.cp) jjg@46: info2 = info; jjg@46: else jjg@46: info2 = new CONSTANT_InterfaceMethodref_info(cp2, info.class_index, info.name_and_type_index); jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitLong(CONSTANT_Long_info info, Map translations) { jjg@46: CONSTANT_Long_info info2 = (CONSTANT_Long_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: info2 = info; jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitNameAndType(CONSTANT_NameAndType_info info, Map translations) { jjg@46: CONSTANT_NameAndType_info info2 = (CONSTANT_NameAndType_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: ConstantPool cp2 = translate(info.cp, translations); jjg@46: if (cp2 == info.cp) jjg@46: info2 = info; jjg@46: else jjg@46: info2 = new CONSTANT_NameAndType_info(cp2, info.name_index, info.type_index); jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitMethodref(CONSTANT_Methodref_info info, Map translations) { jjg@46: CONSTANT_Methodref_info info2 = (CONSTANT_Methodref_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: ConstantPool cp2 = translate(info.cp, translations); jjg@46: if (cp2 == info.cp) jjg@46: info2 = info; jjg@46: else jjg@46: info2 = new CONSTANT_Methodref_info(cp2, info.class_index, info.name_and_type_index); jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitString(CONSTANT_String_info info, Map translations) { jjg@46: CONSTANT_String_info info2 = (CONSTANT_String_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: ConstantPool cp2 = translate(info.cp, translations); jjg@46: if (cp2 == info.cp) jjg@46: info2 = info; jjg@46: else jjg@46: info2 = new CONSTANT_String_info(cp2, info.string_index); jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: public CPInfo visitUtf8(CONSTANT_Utf8_info info, Map translations) { jjg@46: CONSTANT_Utf8_info info2 = (CONSTANT_Utf8_info) translations.get(info); jjg@46: if (info2 == null) { jjg@46: info2 = info; jjg@46: translations.put(info, info2); jjg@46: } jjg@46: return info; jjg@46: } jjg@46: jjg@46: }