ReferenceUtil.java revision 0a18ea7f8b62e51945a79ac37802133a24c9a742
1/*
2 * Copyright 2012, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.dexlib2.util;
33
34import org.jf.dexlib2.iface.reference.*;
35import org.jf.util.StringUtils;
36
37import javax.annotation.Nullable;
38import java.io.IOException;
39import java.io.Writer;
40
41public final class ReferenceUtil {
42    public static String getShortMethodDescriptor(MethodReference methodReference) {
43        // TODO: try using a thread local StringBuilder
44        StringBuilder sb = new StringBuilder();
45        sb.append(methodReference.getName());
46        sb.append('(');
47        for (CharSequence paramType: methodReference.getParameterTypes()) {
48            sb.append(paramType);
49        }
50        sb.append(')');
51        sb.append(methodReference.getReturnType());
52        return sb.toString();
53    }
54
55    public static String getMethodDescriptor(MethodReference methodReference) {
56        // TODO: try using a thread local StringBuilder
57        StringBuilder sb = new StringBuilder();
58        sb.append(methodReference.getDefiningClass());
59        sb.append("->");
60        sb.append(methodReference.getName());
61        sb.append('(');
62        for (CharSequence paramType: methodReference.getParameterTypes()) {
63            sb.append(paramType);
64        }
65        sb.append(')');
66        sb.append(methodReference.getReturnType());
67        return sb.toString();
68    }
69
70    public static void writeMethodDescriptor(Writer writer, MethodReference methodReference) throws IOException {
71        writer.write(methodReference.getDefiningClass());
72        writer.write("->");
73        writer.write(methodReference.getName());
74        writer.write('(');
75        for (CharSequence paramType: methodReference.getParameterTypes()) {
76            writer.write(paramType.toString());
77        }
78        writer.write(')');
79        writer.write(methodReference.getReturnType());
80    }
81
82    public static String getFieldDescriptor(FieldReference fieldReference) {
83        // TODO: try using a thread local StringBuilder
84        StringBuilder sb = new StringBuilder();
85        sb.append(fieldReference.getDefiningClass());
86        sb.append("->");
87        sb.append(fieldReference.getName());
88        sb.append(':');
89        sb.append(fieldReference.getType());
90        return sb.toString();
91    }
92
93    public static String getShortFieldDescriptor(FieldReference fieldReference) {
94        // TODO: try using a thread local StringBuilder
95        StringBuilder sb = new StringBuilder();
96        sb.append(fieldReference.getName());
97        sb.append(':');
98        sb.append(fieldReference.getType());
99        return sb.toString();
100    }
101
102    public static void writeFieldDescriptor(Writer writer, FieldReference fieldReference) throws IOException {
103        writer.write(fieldReference.getDefiningClass());
104        writer.write("->");
105        writer.write(fieldReference.getName());
106        writer.write(':');
107        writer.write(fieldReference.getType());
108    }
109
110    @Nullable
111    public static String getReferenceString(Reference reference) {
112        if (reference instanceof StringReference) {
113            return String.format("\"%s\"", StringUtils.escapeString(((StringReference)reference).getString()));
114        }
115        if (reference instanceof TypeReference) {
116            return ((TypeReference)reference).getType();
117        }
118        if (reference instanceof FieldReference) {
119            return getFieldDescriptor((FieldReference)reference);
120        }
121        if (reference instanceof MethodReference) {
122            return getMethodDescriptor((MethodReference)reference);
123        }
124        return null;
125    }
126
127    private ReferenceUtil() {}
128}
129