• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 package org.jf.dexlib2.util;
33 
34 import org.jf.dexlib2.iface.reference.*;
35 import org.jf.util.StringUtils;
36 
37 import javax.annotation.Nullable;
38 import java.io.IOException;
39 import java.io.Writer;
40 
41 public final class ReferenceUtil {
getShortMethodDescriptor(MethodReference methodReference)42     public static String getShortMethodDescriptor(MethodReference methodReference) {
43         StringBuilder sb = new StringBuilder();
44         sb.append(methodReference.getName());
45         sb.append('(');
46         for (CharSequence paramType: methodReference.getParameterTypes()) {
47             sb.append(paramType);
48         }
49         sb.append(')');
50         sb.append(methodReference.getReturnType());
51         return sb.toString();
52     }
53 
getMethodDescriptor(MethodReference methodReference)54     public static String getMethodDescriptor(MethodReference methodReference) {
55         StringBuilder sb = new StringBuilder();
56         sb.append(methodReference.getDefiningClass());
57         sb.append("->");
58         sb.append(methodReference.getName());
59         sb.append('(');
60         for (CharSequence paramType: methodReference.getParameterTypes()) {
61             sb.append(paramType);
62         }
63         sb.append(')');
64         sb.append(methodReference.getReturnType());
65         return sb.toString();
66     }
67 
writeMethodDescriptor(Writer writer, MethodReference methodReference)68     public static void writeMethodDescriptor(Writer writer, MethodReference methodReference) throws IOException {
69         writer.write(methodReference.getDefiningClass());
70         writer.write("->");
71         writer.write(methodReference.getName());
72         writer.write('(');
73         for (CharSequence paramType: methodReference.getParameterTypes()) {
74             writer.write(paramType.toString());
75         }
76         writer.write(')');
77         writer.write(methodReference.getReturnType());
78     }
79 
getFieldDescriptor(FieldReference fieldReference)80     public static String getFieldDescriptor(FieldReference fieldReference) {
81         StringBuilder sb = new StringBuilder();
82         sb.append(fieldReference.getDefiningClass());
83         sb.append("->");
84         sb.append(fieldReference.getName());
85         sb.append(':');
86         sb.append(fieldReference.getType());
87         return sb.toString();
88     }
89 
getShortFieldDescriptor(FieldReference fieldReference)90     public static String getShortFieldDescriptor(FieldReference fieldReference) {
91         StringBuilder sb = new StringBuilder();
92         sb.append(fieldReference.getName());
93         sb.append(':');
94         sb.append(fieldReference.getType());
95         return sb.toString();
96     }
97 
writeFieldDescriptor(Writer writer, FieldReference fieldReference)98     public static void writeFieldDescriptor(Writer writer, FieldReference fieldReference) throws IOException {
99         writer.write(fieldReference.getDefiningClass());
100         writer.write("->");
101         writer.write(fieldReference.getName());
102         writer.write(':');
103         writer.write(fieldReference.getType());
104     }
105 
106     @Nullable
getReferenceString(Reference reference)107     public static String getReferenceString(Reference reference) {
108         if (reference instanceof StringReference) {
109             return String.format("\"%s\"", StringUtils.escapeString(((StringReference)reference).getString()));
110         }
111         if (reference instanceof TypeReference) {
112             return ((TypeReference)reference).getType();
113         }
114         if (reference instanceof FieldReference) {
115             return getFieldDescriptor((FieldReference)reference);
116         }
117         if (reference instanceof MethodReference) {
118             return getMethodDescriptor((MethodReference)reference);
119         }
120         return null;
121     }
122 
ReferenceUtil()123     private ReferenceUtil() {}
124 }
125