1 /* 2 * Copyright 2018, 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.writer.util; 32 33 import com.android.tools.smali.dexlib2.iface.Field; 34 import com.android.tools.smali.dexlib2.immutable.value.ImmutableEncodedValueFactory; 35 import com.android.tools.smali.dexlib2.util.EncodedValueUtils; 36 import com.android.tools.smali.util.AbstractForwardSequentialList; 37 import com.android.tools.smali.util.CollectionUtils; 38 import com.android.tools.smali.dexlib2.base.value.BaseArrayEncodedValue; 39 import com.android.tools.smali.dexlib2.iface.value.ArrayEncodedValue; 40 import com.android.tools.smali.dexlib2.iface.value.EncodedValue; 41 42 import javax.annotation.Nonnull; 43 import javax.annotation.Nullable; 44 import java.util.function.Function; 45 import java.util.function.Predicate; 46 import java.util.stream.Collectors; 47 import java.util.Iterator; 48 import java.util.List; 49 import java.util.SortedSet; 50 51 public class StaticInitializerUtil { 52 getStaticInitializers( @onnull SortedSet<? extends Field> sortedStaticFields)53 @Nullable public static ArrayEncodedValue getStaticInitializers( 54 @Nonnull SortedSet<? extends Field> sortedStaticFields) { 55 final int lastIndex = CollectionUtils.lastIndexOf(sortedStaticFields, HAS_INITIALIZER); 56 if (lastIndex > -1) { 57 return new BaseArrayEncodedValue() { 58 @Nonnull 59 @Override 60 public List<? extends EncodedValue> getValue() { 61 return new AbstractForwardSequentialList<EncodedValue>() { 62 @Nonnull @Override public Iterator<EncodedValue> iterator() { 63 return sortedStaticFields.stream().limit(lastIndex + 1) 64 .map(GET_INITIAL_VALUE).collect(Collectors.toList()).iterator(); 65 } 66 67 @Override public int size() { 68 return lastIndex+1; 69 } 70 }; 71 } 72 }; 73 } 74 return null; 75 } 76 77 private static final Predicate<Field> HAS_INITIALIZER = new Predicate<Field>() { 78 @Override 79 public boolean test(Field input) { 80 EncodedValue encodedValue = input.getInitialValue(); 81 return encodedValue != null && !EncodedValueUtils.isDefaultValue(encodedValue); 82 } 83 }; 84 85 private static final Function<Field, EncodedValue> GET_INITIAL_VALUE = new Function<Field, EncodedValue>() { 86 @Override 87 public EncodedValue apply(Field input) { 88 EncodedValue initialValue = input.getInitialValue(); 89 if (initialValue == null) { 90 return ImmutableEncodedValueFactory.defaultValueForType(input.getType()); 91 } 92 return initialValue; 93 } 94 }; 95 96 } 97