• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013, 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.builder;
32 
33 import com.android.tools.smali.dexlib2.writer.DexWriter;
34 import com.android.tools.smali.dexlib2.writer.TypeSection;
35 
36 import javax.annotation.Nonnull;
37 import javax.annotation.Nullable;
38 import java.util.Collection;
39 import java.util.Map.Entry;
40 import java.util.concurrent.ConcurrentHashMap;
41 import java.util.concurrent.ConcurrentMap;
42 
43 class BuilderTypePool extends BaseBuilderPool
44         implements TypeSection<BuilderStringReference, BuilderTypeReference, BuilderTypeReference> {
45     @Nonnull private final ConcurrentMap<String, BuilderTypeReference> internedItems = new ConcurrentHashMap<>();
46 
BuilderTypePool(@onnull DexBuilder dexBuilder)47     public BuilderTypePool(@Nonnull DexBuilder dexBuilder) {
48         super(dexBuilder);
49     }
50 
internType(@onnull String type)51     @Nonnull public BuilderTypeReference internType(@Nonnull String type) {
52         BuilderTypeReference ret = internedItems.get(type);
53         if (ret != null) {
54             return ret;
55         }
56         BuilderStringReference stringRef = dexBuilder.stringSection.internString(type);
57         BuilderTypeReference typeReference = new BuilderTypeReference(stringRef);
58         ret = internedItems.putIfAbsent(type, typeReference);
59         return ret==null?typeReference:ret;
60     }
61 
internNullableType(@ullable String type)62     @Nullable public BuilderTypeReference internNullableType(@Nullable String type) {
63         if (type == null) {
64             return null;
65         }
66         return internType(type);
67     }
68 
getString(@onnull BuilderTypeReference key)69     @Nonnull @Override public BuilderStringReference getString(@Nonnull BuilderTypeReference key) {
70         return key.stringReference;
71     }
72 
getNullableItemIndex(@ullable BuilderTypeReference key)73     @Override public int getNullableItemIndex(@Nullable BuilderTypeReference key) {
74         return key==null? DexWriter.NO_INDEX:key.index;
75     }
76 
getItemIndex(@onnull BuilderTypeReference key)77     @Override public int getItemIndex(@Nonnull BuilderTypeReference key) {
78         return key.getIndex();
79     }
80 
getItems()81     @Nonnull @Override public Collection<? extends Entry<? extends BuilderTypeReference, Integer>> getItems() {
82         return new BuilderMapEntryCollection<BuilderTypeReference>(internedItems.values()) {
83             @Override protected int getValue(@Nonnull BuilderTypeReference key) {
84                 return key.index;
85             }
86 
87             @Override protected int setValue(@Nonnull BuilderTypeReference key, int value) {
88                 int prev = key.index;
89                 key.index = value;
90                 return prev;
91             }
92         };
93     }
94 
95     @Override public int getItemCount() {
96         return internedItems.size();
97     }
98 }
99