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