1 /* 2 * [The "BSD licence"] 3 * Copyright (c) 2010 Ben Gruver (JesusFreke) 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 package org.jf.baksmali.Adaptors.EncodedValue; 30 31 import org.jf.baksmali.Adaptors.ReferenceFormatter; 32 import org.jf.baksmali.Renderers.*; 33 import org.jf.dexlib2.ValueType; 34 import org.jf.dexlib2.iface.value.*; 35 import org.jf.dexlib2.util.ReferenceUtil; 36 import org.jf.util.IndentingWriter; 37 38 import javax.annotation.Nonnull; 39 import javax.annotation.Nullable; 40 import java.io.IOException; 41 42 public abstract class EncodedValueAdaptor { writeTo(@onnull IndentingWriter writer, @Nonnull EncodedValue encodedValue, @Nullable String containingClass)43 public static void writeTo(@Nonnull IndentingWriter writer, @Nonnull EncodedValue encodedValue, 44 @Nullable String containingClass) 45 throws IOException { 46 switch (encodedValue.getValueType()) { 47 case ValueType.ANNOTATION: 48 AnnotationEncodedValueAdaptor.writeTo(writer, (AnnotationEncodedValue)encodedValue, containingClass); 49 return; 50 case ValueType.ARRAY: 51 ArrayEncodedValueAdaptor.writeTo(writer, (ArrayEncodedValue)encodedValue, containingClass); 52 return; 53 case ValueType.BOOLEAN: 54 BooleanRenderer.writeTo(writer, ((BooleanEncodedValue)encodedValue).getValue()); 55 return; 56 case ValueType.BYTE: 57 ByteRenderer.writeTo(writer, ((ByteEncodedValue)encodedValue).getValue()); 58 return; 59 case ValueType.CHAR: 60 CharRenderer.writeTo(writer, ((CharEncodedValue)encodedValue).getValue()); 61 return; 62 case ValueType.DOUBLE: 63 DoubleRenderer.writeTo(writer, ((DoubleEncodedValue)encodedValue).getValue()); 64 return; 65 case ValueType.ENUM: 66 EnumEncodedValue enumEncodedValue = (EnumEncodedValue)encodedValue; 67 boolean useImplicitReference = false; 68 if (enumEncodedValue.getValue().getDefiningClass().equals(containingClass)) { 69 useImplicitReference = true; 70 } 71 writer.write(".enum "); 72 ReferenceUtil.writeFieldDescriptor(writer, enumEncodedValue.getValue(), useImplicitReference); 73 return; 74 case ValueType.FIELD: 75 FieldEncodedValue fieldEncodedValue = (FieldEncodedValue)encodedValue; 76 useImplicitReference = false; 77 if (fieldEncodedValue.getValue().getDefiningClass().equals(containingClass)) { 78 useImplicitReference = true; 79 } 80 ReferenceUtil.writeFieldDescriptor(writer, fieldEncodedValue.getValue(), useImplicitReference); 81 return; 82 case ValueType.FLOAT: 83 FloatRenderer.writeTo(writer, ((FloatEncodedValue)encodedValue).getValue()); 84 return; 85 case ValueType.INT: 86 IntegerRenderer.writeTo(writer, ((IntEncodedValue)encodedValue).getValue()); 87 return; 88 case ValueType.LONG: 89 LongRenderer.writeTo(writer, ((LongEncodedValue)encodedValue).getValue()); 90 return; 91 case ValueType.METHOD: 92 MethodEncodedValue methodEncodedValue = (MethodEncodedValue)encodedValue; 93 useImplicitReference = false; 94 if (methodEncodedValue.getValue().getDefiningClass().equals(containingClass)) { 95 useImplicitReference = true; 96 } 97 ReferenceUtil.writeMethodDescriptor(writer, methodEncodedValue.getValue(), useImplicitReference); 98 return; 99 case ValueType.NULL: 100 writer.write("null"); 101 return; 102 case ValueType.SHORT: 103 ShortRenderer.writeTo(writer, ((ShortEncodedValue)encodedValue).getValue()); 104 return; 105 case ValueType.STRING: 106 ReferenceFormatter.writeStringReference(writer, ((StringEncodedValue)encodedValue).getValue()); 107 return; 108 case ValueType.TYPE: 109 writer.write(((TypeEncodedValue)encodedValue).getValue()); 110 } 111 } 112 } 113