ClassTranslator.java revision 2942:08092deced3f
1/*
2 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.classfile;
27
28import java.util.Map;
29
30import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
31import com.sun.tools.classfile.ConstantPool.CONSTANT_Double_info;
32import com.sun.tools.classfile.ConstantPool.CONSTANT_Fieldref_info;
33import com.sun.tools.classfile.ConstantPool.CONSTANT_Float_info;
34import com.sun.tools.classfile.ConstantPool.CONSTANT_Integer_info;
35import com.sun.tools.classfile.ConstantPool.CONSTANT_InterfaceMethodref_info;
36import com.sun.tools.classfile.ConstantPool.CONSTANT_InvokeDynamic_info;
37import com.sun.tools.classfile.ConstantPool.CONSTANT_Long_info;
38import com.sun.tools.classfile.ConstantPool.CONSTANT_MethodHandle_info;
39import com.sun.tools.classfile.ConstantPool.CONSTANT_MethodType_info;
40import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info;
41import com.sun.tools.classfile.ConstantPool.CONSTANT_NameAndType_info;
42import com.sun.tools.classfile.ConstantPool.CONSTANT_String_info;
43import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
44import com.sun.tools.classfile.ConstantPool.CPInfo;
45
46/**
47 * Rewrites a class file using a map of translations.
48 *
49 *  <p><b>This is NOT part of any supported API.
50 *  If you write code that depends on this, you do so at your own risk.
51 *  This code and its internal interfaces are subject to change or
52 *  deletion without notice.</b>
53 */
54public class ClassTranslator
55        implements ConstantPool.Visitor<ConstantPool.CPInfo,Map<Object,Object>> {
56    /**
57     * Create a new ClassFile from {@code cf}, such that for all entries
58     * {@code k&nbsp;-\&gt;&nbsp;v} in {@code translations},
59     * each occurrence of {@code k} in {@code cf} will be replaced by {@code v}.
60     * in
61     * @param cf the class file to be processed
62     * @param translations the set of translations to be applied
63     * @return a copy of {@code} with the values in {@code translations} substituted
64     */
65    public ClassFile translate(ClassFile cf, Map<Object,Object> translations) {
66        ClassFile cf2 = (ClassFile) translations.get(cf);
67        if (cf2 == null) {
68            ConstantPool constant_pool2 = translate(cf.constant_pool, translations);
69            Field[] fields2 = translate(cf.fields, cf.constant_pool, translations);
70            Method[] methods2 = translateMethods(cf.methods, cf.constant_pool, translations);
71            Attributes attributes2 = translateAttributes(cf.attributes, cf.constant_pool,
72                    translations);
73
74            if (constant_pool2 == cf.constant_pool &&
75                    fields2 == cf.fields &&
76                    methods2 == cf.methods &&
77                    attributes2 == cf.attributes)
78                cf2 = cf;
79            else
80                cf2 = new ClassFile(
81                        cf.magic,
82                        cf.minor_version,
83                        cf.major_version,
84                        constant_pool2,
85                        cf.access_flags,
86                        cf.this_class,
87                        cf.super_class,
88                        cf.interfaces,
89                        fields2,
90                        methods2,
91                        attributes2);
92            translations.put(cf, cf2);
93        }
94        return cf2;
95    }
96
97    ConstantPool translate(ConstantPool cp, Map<Object,Object> translations) {
98        ConstantPool cp2 = (ConstantPool) translations.get(cp);
99        if (cp2 == null) {
100            ConstantPool.CPInfo[] pool2 = new ConstantPool.CPInfo[cp.size()];
101            boolean eq = true;
102            for (int i = 0; i < cp.size(); ) {
103                ConstantPool.CPInfo cpInfo;
104                try {
105                    cpInfo = cp.get(i);
106                } catch (ConstantPool.InvalidIndex e) {
107                    throw new IllegalStateException(e);
108                }
109                ConstantPool.CPInfo cpInfo2 = translate(cpInfo, translations);
110                eq &= (cpInfo == cpInfo2);
111                pool2[i] = cpInfo2;
112                if (cpInfo.getTag() != cpInfo2.getTag())
113                    throw new IllegalStateException();
114                i += cpInfo.size();
115            }
116
117            if (eq)
118                cp2 = cp;
119            else
120                cp2 = new ConstantPool(pool2);
121
122            translations.put(cp, cp2);
123        }
124        return cp2;
125    }
126
127    ConstantPool.CPInfo translate(ConstantPool.CPInfo cpInfo, Map<Object,Object> translations) {
128        ConstantPool.CPInfo cpInfo2 = (ConstantPool.CPInfo) translations.get(cpInfo);
129        if (cpInfo2 == null) {
130            cpInfo2 = cpInfo.accept(this, translations);
131            translations.put(cpInfo, cpInfo2);
132        }
133        return cpInfo2;
134    }
135
136    Field[] translate(Field[] fields, ConstantPool constant_pool, Map<Object,Object> translations) {
137        Field[] fields2 = (Field[]) translations.get(fields);
138        if (fields2 == null) {
139            fields2 = new Field[fields.length];
140            for (int i = 0; i < fields.length; i++)
141                fields2[i] = translate(fields[i], constant_pool, translations);
142            if (equal(fields, fields2))
143                fields2 = fields;
144            translations.put(fields, fields2);
145        }
146        return fields2;
147    }
148
149    Field translate(Field field, ConstantPool constant_pool, Map<Object,Object> translations) {
150        Field field2 = (Field) translations.get(field);
151        if (field2 == null) {
152            Attributes attributes2 = translateAttributes(field.attributes, constant_pool,
153                    translations);
154
155            if (attributes2 == field.attributes)
156                field2 = field;
157            else
158                field2 = new Field(
159                        field.access_flags,
160                        field.name_index,
161                        field.descriptor,
162                        attributes2);
163            translations.put(field, field2);
164        }
165        return field2;
166    }
167
168    Method[] translateMethods(Method[] methods, ConstantPool constant_pool, Map<Object,Object> translations) {
169        Method[] methods2 = (Method[]) translations.get(methods);
170        if (methods2 == null) {
171            methods2 = new Method[methods.length];
172            for (int i = 0; i < methods.length; i++)
173                methods2[i] = translate(methods[i], constant_pool, translations);
174            if (equal(methods, methods2))
175                methods2 = methods;
176            translations.put(methods, methods2);
177        }
178        return methods2;
179    }
180
181    Method translate(Method method, ConstantPool constant_pool, Map<Object,Object> translations) {
182        Method method2 = (Method) translations.get(method);
183        if (method2 == null) {
184            Attributes attributes2 = translateAttributes(method.attributes, constant_pool,
185                    translations);
186
187            if (attributes2 == method.attributes)
188                method2 = method;
189            else
190                method2 = new Method(
191                        method.access_flags,
192                        method.name_index,
193                        method.descriptor,
194                        attributes2);
195            translations.put(method, method2);
196        }
197        return method2;
198    }
199
200    Attributes translateAttributes(Attributes attributes,
201            ConstantPool constant_pool, Map<Object,Object> translations) {
202        Attributes attributes2 = (Attributes) translations.get(attributes);
203        if (attributes2 == null) {
204            Attribute[] attrArray2 = new Attribute[attributes.size()];
205            ConstantPool constant_pool2 = translate(constant_pool, translations);
206            boolean attrsEqual = true;
207            for (int i = 0; i < attributes.size(); i++) {
208                Attribute attr = attributes.get(i);
209                Attribute attr2 = translate(attr, translations);
210                if (attr2 != attr)
211                    attrsEqual = false;
212                attrArray2[i] = attr2;
213            }
214            if ((constant_pool2 == constant_pool) && attrsEqual)
215                attributes2 = attributes;
216            else
217                attributes2 = new Attributes(constant_pool2, attrArray2);
218            translations.put(attributes, attributes2);
219        }
220        return attributes2;
221    }
222
223    Attribute translate(Attribute attribute, Map<Object,Object> translations) {
224        Attribute attribute2 = (Attribute) translations.get(attribute);
225        if (attribute2 == null) {
226            attribute2 = attribute; // don't support translation within attributes yet
227                                    // (what about Code attribute)
228            translations.put(attribute, attribute2);
229        }
230        return attribute2;
231    }
232
233    private static <T> boolean equal(T[] a1, T[] a2) {
234        if (a1 == null || a2 == null)
235            return (a1 == a2);
236        if (a1.length != a2.length)
237            return false;
238        for (int i = 0; i < a1.length; i++) {
239            if (a1[i] != a2[i])
240                return false;
241        }
242        return true;
243    }
244
245    public CPInfo visitClass(CONSTANT_Class_info info, Map<Object, Object> translations) {
246        CONSTANT_Class_info info2 = (CONSTANT_Class_info) translations.get(info);
247        if (info2 == null) {
248            ConstantPool cp2 = translate(info.cp, translations);
249            if (cp2 == info.cp)
250                info2 = info;
251            else
252                info2 = new CONSTANT_Class_info(cp2, info.name_index);
253            translations.put(info, info2);
254        }
255        return info;
256    }
257
258    public CPInfo visitDouble(CONSTANT_Double_info info, Map<Object, Object> translations) {
259        CONSTANT_Double_info info2 = (CONSTANT_Double_info) translations.get(info);
260        if (info2 == null) {
261            info2 = info;
262            translations.put(info, info2);
263        }
264        return info;
265    }
266
267    public CPInfo visitFieldref(CONSTANT_Fieldref_info info, Map<Object, Object> translations) {
268        CONSTANT_Fieldref_info info2 = (CONSTANT_Fieldref_info) translations.get(info);
269        if (info2 == null) {
270            ConstantPool cp2 = translate(info.cp, translations);
271            if (cp2 == info.cp)
272                info2 = info;
273            else
274                info2 = new CONSTANT_Fieldref_info(cp2, info.class_index, info.name_and_type_index);
275            translations.put(info, info2);
276        }
277        return info;
278    }
279
280    public CPInfo visitFloat(CONSTANT_Float_info info, Map<Object, Object> translations) {
281        CONSTANT_Float_info info2 = (CONSTANT_Float_info) translations.get(info);
282        if (info2 == null) {
283            info2 = info;
284            translations.put(info, info2);
285        }
286        return info;
287    }
288
289    public CPInfo visitInteger(CONSTANT_Integer_info info, Map<Object, Object> translations) {
290        CONSTANT_Integer_info info2 = (CONSTANT_Integer_info) translations.get(info);
291        if (info2 == null) {
292            info2 = info;
293            translations.put(info, info2);
294        }
295        return info;
296    }
297
298    public CPInfo visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Map<Object, Object> translations) {
299        CONSTANT_InterfaceMethodref_info info2 = (CONSTANT_InterfaceMethodref_info) translations.get(info);
300        if (info2 == null) {
301            ConstantPool cp2 = translate(info.cp, translations);
302            if (cp2 == info.cp)
303                info2 = info;
304            else
305                info2 = new CONSTANT_InterfaceMethodref_info(cp2, info.class_index, info.name_and_type_index);
306            translations.put(info, info2);
307        }
308        return info;
309    }
310
311    public CPInfo visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, Map<Object, Object> translations) {
312        CONSTANT_InvokeDynamic_info info2 = (CONSTANT_InvokeDynamic_info) translations.get(info);
313        if (info2 == null) {
314            ConstantPool cp2 = translate(info.cp, translations);
315            if (cp2 == info.cp) {
316                info2 = info;
317            } else {
318                info2 = new CONSTANT_InvokeDynamic_info(cp2, info.bootstrap_method_attr_index, info.name_and_type_index);
319            }
320            translations.put(info, info2);
321        }
322        return info;
323    }
324
325    public CPInfo visitLong(CONSTANT_Long_info info, Map<Object, Object> translations) {
326        CONSTANT_Long_info info2 = (CONSTANT_Long_info) translations.get(info);
327        if (info2 == null) {
328            info2 = info;
329            translations.put(info, info2);
330        }
331        return info;
332    }
333
334    public CPInfo visitNameAndType(CONSTANT_NameAndType_info info, Map<Object, Object> translations) {
335        CONSTANT_NameAndType_info info2 = (CONSTANT_NameAndType_info) translations.get(info);
336        if (info2 == null) {
337            ConstantPool cp2 = translate(info.cp, translations);
338            if (cp2 == info.cp)
339                info2 = info;
340            else
341                info2 = new CONSTANT_NameAndType_info(cp2, info.name_index, info.type_index);
342            translations.put(info, info2);
343        }
344        return info;
345    }
346
347    public CPInfo visitMethodref(CONSTANT_Methodref_info info, Map<Object, Object> translations) {
348        CONSTANT_Methodref_info info2 = (CONSTANT_Methodref_info) translations.get(info);
349        if (info2 == null) {
350            ConstantPool cp2 = translate(info.cp, translations);
351            if (cp2 == info.cp)
352                info2 = info;
353            else
354                info2 = new CONSTANT_Methodref_info(cp2, info.class_index, info.name_and_type_index);
355            translations.put(info, info2);
356        }
357        return info;
358    }
359
360    public CPInfo visitMethodHandle(CONSTANT_MethodHandle_info info, Map<Object, Object> translations) {
361        CONSTANT_MethodHandle_info info2 = (CONSTANT_MethodHandle_info) translations.get(info);
362        if (info2 == null) {
363            ConstantPool cp2 = translate(info.cp, translations);
364            if (cp2 == info.cp) {
365                info2 = info;
366            } else {
367                info2 = new CONSTANT_MethodHandle_info(cp2, info.reference_kind, info.reference_index);
368            }
369            translations.put(info, info2);
370        }
371        return info;
372    }
373
374    public CPInfo visitMethodType(CONSTANT_MethodType_info info, Map<Object, Object> translations) {
375        CONSTANT_MethodType_info info2 = (CONSTANT_MethodType_info) translations.get(info);
376        if (info2 == null) {
377            ConstantPool cp2 = translate(info.cp, translations);
378            if (cp2 == info.cp) {
379                info2 = info;
380            } else {
381                info2 = new CONSTANT_MethodType_info(cp2, info.descriptor_index);
382            }
383            translations.put(info, info2);
384        }
385        return info;
386    }
387
388    public CPInfo visitString(CONSTANT_String_info info, Map<Object, Object> translations) {
389        CONSTANT_String_info info2 = (CONSTANT_String_info) translations.get(info);
390        if (info2 == null) {
391            ConstantPool cp2 = translate(info.cp, translations);
392            if (cp2 == info.cp)
393                info2 = info;
394            else
395                info2 = new CONSTANT_String_info(cp2, info.string_index);
396            translations.put(info, info2);
397        }
398        return info;
399    }
400
401    public CPInfo visitUtf8(CONSTANT_Utf8_info info, Map<Object, Object> translations) {
402        CONSTANT_Utf8_info info2 = (CONSTANT_Utf8_info) translations.get(info);
403        if (info2 == null) {
404            info2 = info;
405            translations.put(info, info2);
406        }
407        return info;
408    }
409
410}
411