1 /* 2 * Copyright 2021, Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google LLC nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 package com.android.tools.smali.dexlib2.formatter; 32 33 import com.android.tools.smali.dexlib2.iface.reference.CallSiteReference; 34 import com.android.tools.smali.dexlib2.iface.reference.FieldReference; 35 import com.android.tools.smali.dexlib2.iface.reference.MethodHandleReference; 36 import com.android.tools.smali.dexlib2.iface.reference.MethodProtoReference; 37 import com.android.tools.smali.dexlib2.iface.reference.MethodReference; 38 import com.android.tools.smali.dexlib2.iface.reference.Reference; 39 import com.android.tools.smali.dexlib2.iface.value.EncodedValue; 40 41 import java.io.IOException; 42 import java.io.StringWriter; 43 import java.io.Writer; 44 45 /** 46 * This class handles formatting and getting strings for various types of items in a dex file. 47 */ 48 public class DexFormatter { 49 50 public static final DexFormatter INSTANCE = new DexFormatter(); 51 52 /** 53 * Gets a {@link DexFormattedWriter} for writing formatted strings to a {@link Writer}, with the same settings as this Formatter. 54 * 55 * @param writer The {@link Writer} that the {@link DexFormattedWriter} will write to. 56 */ getWriter(Writer writer)57 public DexFormattedWriter getWriter(Writer writer) { 58 return new DexFormattedWriter(writer); 59 } 60 getMethodDescriptor(MethodReference methodReference)61 public String getMethodDescriptor(MethodReference methodReference) { 62 StringWriter writer = new StringWriter(); 63 try { 64 getWriter(writer).writeMethodDescriptor(methodReference); 65 } catch (IOException e) { 66 throw new AssertionError("Unexpected IOException"); 67 } 68 return writer.toString(); 69 } 70 getShortMethodDescriptor(MethodReference methodReference)71 public String getShortMethodDescriptor(MethodReference methodReference) { 72 StringWriter writer = new StringWriter(); 73 try { 74 getWriter(writer).writeShortMethodDescriptor(methodReference); 75 } catch (IOException e) { 76 throw new AssertionError("Unexpected IOException"); 77 } 78 return writer.toString(); 79 } 80 getMethodProtoDescriptor(MethodProtoReference protoReference)81 public String getMethodProtoDescriptor(MethodProtoReference protoReference) { 82 StringWriter writer = new StringWriter(); 83 try { 84 getWriter(writer).writeMethodProtoDescriptor(protoReference); 85 } catch (IOException e) { 86 throw new AssertionError("Unexpected IOException"); 87 } 88 return writer.toString(); 89 } 90 getFieldDescriptor(FieldReference fieldReference)91 public String getFieldDescriptor(FieldReference fieldReference) { 92 StringWriter writer = new StringWriter(); 93 try { 94 getWriter(writer).writeFieldDescriptor(fieldReference); 95 } catch (IOException e) { 96 throw new AssertionError("Unexpected IOException"); 97 } 98 return writer.toString(); 99 } 100 getShortFieldDescriptor(FieldReference fieldReference)101 public String getShortFieldDescriptor(FieldReference fieldReference) { 102 StringWriter writer = new StringWriter(); 103 try { 104 getWriter(writer).writeShortFieldDescriptor(fieldReference); 105 } catch (IOException e) { 106 throw new AssertionError("Unexpected IOException"); 107 } 108 return writer.toString(); 109 } 110 getMethodHandle(MethodHandleReference methodHandleReference)111 public String getMethodHandle(MethodHandleReference methodHandleReference) { 112 StringWriter writer = new StringWriter(); 113 try { 114 getWriter(writer).writeMethodHandle(methodHandleReference); 115 } catch (IOException e) { 116 throw new AssertionError("Unexpected IOException"); 117 } 118 return writer.toString(); 119 } 120 getCallSite(CallSiteReference callSiteReference)121 public String getCallSite(CallSiteReference callSiteReference) { 122 StringWriter writer = new StringWriter(); 123 try { 124 getWriter(writer).writeCallSite(callSiteReference); 125 } catch (IOException e) { 126 throw new AssertionError("Unexpected IOException"); 127 } 128 return writer.toString(); 129 } 130 getType(CharSequence type)131 public String getType(CharSequence type) { 132 StringWriter writer = new StringWriter(); 133 try { 134 getWriter(writer).writeType(type); 135 } catch (IOException e) { 136 throw new AssertionError("Unexpected IOException"); 137 } 138 return writer.toString(); 139 } 140 getQuotedString(CharSequence string)141 public String getQuotedString(CharSequence string) { 142 StringWriter writer = new StringWriter(); 143 try { 144 getWriter(writer).writeQuotedString(string); 145 } catch (IOException e) { 146 throw new AssertionError("Unexpected IOException"); 147 } 148 return writer.toString(); 149 } 150 getEncodedValue(EncodedValue encodedValue)151 public String getEncodedValue(EncodedValue encodedValue) { 152 StringWriter writer = new StringWriter(); 153 try { 154 getWriter(writer).writeEncodedValue(encodedValue); 155 } catch (IOException e) { 156 throw new AssertionError("Unexpected IOException"); 157 } 158 return writer.toString(); 159 } 160 getReference(Reference reference)161 public String getReference(Reference reference) { 162 StringWriter writer = new StringWriter(); 163 try { 164 getWriter(writer).writeReference(reference); 165 } catch (IOException e) { 166 throw new AssertionError("Unexpected IOException"); 167 } 168 return writer.toString(); 169 } 170 } 171