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.immutable.value; 33 34 import com.google.common.base.Preconditions; 35 import com.google.common.collect.ImmutableList; 36 import org.jf.dexlib2.ValueType; 37 import org.jf.dexlib2.iface.value.*; 38 import org.jf.util.ExceptionWithContext; 39 import org.jf.util.ImmutableConverter; 40 41 import javax.annotation.Nonnull; 42 import javax.annotation.Nullable; 43 44 public class ImmutableEncodedValueFactory { 45 @Nonnull of(@onnull EncodedValue encodedValue)46 public static ImmutableEncodedValue of(@Nonnull EncodedValue encodedValue) { 47 switch (encodedValue.getValueType()) { 48 case ValueType.BYTE: 49 return ImmutableByteEncodedValue.of((ByteEncodedValue)encodedValue); 50 case ValueType.SHORT: 51 return ImmutableShortEncodedValue.of((ShortEncodedValue)encodedValue); 52 case ValueType.CHAR: 53 return ImmutableCharEncodedValue.of((CharEncodedValue)encodedValue); 54 case ValueType.INT: 55 return ImmutableIntEncodedValue.of((IntEncodedValue)encodedValue); 56 case ValueType.LONG: 57 return ImmutableLongEncodedValue.of((LongEncodedValue)encodedValue); 58 case ValueType.FLOAT: 59 return ImmutableFloatEncodedValue.of((FloatEncodedValue)encodedValue); 60 case ValueType.DOUBLE: 61 return ImmutableDoubleEncodedValue.of((DoubleEncodedValue)encodedValue); 62 case ValueType.STRING: 63 return ImmutableStringEncodedValue.of((StringEncodedValue)encodedValue); 64 case ValueType.TYPE: 65 return ImmutableTypeEncodedValue.of((TypeEncodedValue)encodedValue); 66 case ValueType.FIELD: 67 return ImmutableFieldEncodedValue.of((FieldEncodedValue)encodedValue); 68 case ValueType.METHOD: 69 return ImmutableMethodEncodedValue.of((MethodEncodedValue)encodedValue); 70 case ValueType.ENUM: 71 return ImmutableEnumEncodedValue.of((EnumEncodedValue)encodedValue); 72 case ValueType.ARRAY: 73 return ImmutableArrayEncodedValue.of((ArrayEncodedValue)encodedValue); 74 case ValueType.ANNOTATION: 75 return ImmutableAnnotationEncodedValue.of((AnnotationEncodedValue)encodedValue); 76 case ValueType.NULL: 77 return ImmutableNullEncodedValue.INSTANCE; 78 case ValueType.BOOLEAN: 79 return ImmutableBooleanEncodedValue.of((BooleanEncodedValue)encodedValue); 80 case ValueType.METHOD_HANDLE: 81 return ImmutableMethodHandleEncodedValue.of((MethodHandleEncodedValue) encodedValue); 82 case ValueType.METHOD_TYPE: 83 return ImmutableMethodTypeEncodedValue.of((MethodTypeEncodedValue) encodedValue); 84 default: 85 Preconditions.checkArgument(false); 86 return null; 87 } 88 } 89 90 @Nonnull defaultValueForType(String type)91 public static EncodedValue defaultValueForType(String type) { 92 switch (type.charAt(0)) { 93 case 'Z': 94 return ImmutableBooleanEncodedValue.FALSE_VALUE; 95 case 'B': 96 return new ImmutableByteEncodedValue((byte)0); 97 case 'S': 98 return new ImmutableShortEncodedValue((short)0); 99 case 'C': 100 return new ImmutableCharEncodedValue((char)0); 101 case 'I': 102 return new ImmutableIntEncodedValue(0); 103 case 'J': 104 return new ImmutableLongEncodedValue(0); 105 case 'F': 106 return new ImmutableFloatEncodedValue(0); 107 case 'D': 108 return new ImmutableDoubleEncodedValue(0); 109 case 'L': 110 case '[': 111 return ImmutableNullEncodedValue.INSTANCE; 112 default: 113 throw new ExceptionWithContext("Unrecognized type: %s", type); 114 } 115 } 116 117 @Nullable ofNullable(@ullable EncodedValue encodedValue)118 public static ImmutableEncodedValue ofNullable(@Nullable EncodedValue encodedValue) { 119 if (encodedValue == null) { 120 return null; 121 } 122 return of(encodedValue); 123 } 124 125 @Nonnull immutableListOf(@ullable Iterable<? extends EncodedValue> list)126 public static ImmutableList<ImmutableEncodedValue> immutableListOf 127 (@Nullable Iterable<? extends EncodedValue> list) { 128 return CONVERTER.toList(list); 129 } 130 131 private static final ImmutableConverter<ImmutableEncodedValue, EncodedValue> CONVERTER = 132 new ImmutableConverter<ImmutableEncodedValue, EncodedValue>() { 133 @Override 134 protected boolean isImmutable(@Nonnull EncodedValue item) { 135 return item instanceof ImmutableEncodedValue; 136 } 137 138 @Nonnull 139 @Override 140 protected ImmutableEncodedValue makeImmutable(@Nonnull EncodedValue item) { 141 return of(item); 142 } 143 }; 144 } 145