• 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.iface.Annotation;
34 import com.android.tools.smali.dexlib2.writer.AnnotationSection;
35 import com.android.tools.smali.dexlib2.writer.builder.BuilderEncodedValues.BuilderEncodedValue;
36 
37 import javax.annotation.Nonnull;
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 BuilderAnnotationPool extends BaseBuilderPool implements AnnotationSection<BuilderStringReference,
44         BuilderTypeReference, BuilderAnnotation, BuilderAnnotationElement, BuilderEncodedValue> {
45     @Nonnull private final ConcurrentMap<Annotation, BuilderAnnotation> internedItems =
46             new ConcurrentHashMap<>();
47 
BuilderAnnotationPool(@onnull DexBuilder dexBuilder)48     public BuilderAnnotationPool(@Nonnull DexBuilder dexBuilder) {
49         super(dexBuilder);
50     }
51 
internAnnotation(@onnull Annotation annotation)52     @Nonnull public BuilderAnnotation internAnnotation(@Nonnull Annotation annotation) {
53         BuilderAnnotation ret = internedItems.get(annotation);
54         if (ret != null) {
55             return ret;
56         }
57 
58         BuilderAnnotation dexBuilderAnnotation = new BuilderAnnotation(
59                 annotation.getVisibility(),
60                 dexBuilder.typeSection.internType(annotation.getType()),
61                 dexBuilder.internAnnotationElements(annotation.getElements()));
62         ret = internedItems.putIfAbsent(dexBuilderAnnotation, dexBuilderAnnotation);
63         return ret==null?dexBuilderAnnotation:ret;
64     }
65 
getVisibility(@onnull BuilderAnnotation key)66     @Override public int getVisibility(@Nonnull BuilderAnnotation key) {
67         return key.visibility;
68     }
69 
getType(@onnull BuilderAnnotation key)70     @Nonnull @Override public BuilderTypeReference getType(@Nonnull BuilderAnnotation key) {
71         return key.type;
72     }
73 
74     @Nonnull @Override
getElements(@onnull BuilderAnnotation key)75     public Collection<? extends BuilderAnnotationElement> getElements(@Nonnull BuilderAnnotation key) {
76         return key.elements;
77     }
78 
79     @Nonnull @Override
getElementName(@onnull BuilderAnnotationElement element)80     public BuilderStringReference getElementName(@Nonnull BuilderAnnotationElement element) {
81         return element.name;
82     }
83 
84     @Nonnull @Override
getElementValue(@onnull BuilderAnnotationElement element)85     public BuilderEncodedValue getElementValue(@Nonnull BuilderAnnotationElement element) {
86         return element.value;
87     }
88 
getItemOffset(@onnull BuilderAnnotation key)89     @Override public int getItemOffset(@Nonnull BuilderAnnotation key) {
90         return key.offset;
91     }
92 
getItems()93     @Nonnull @Override public Collection<? extends Entry<? extends BuilderAnnotation, Integer>> getItems() {
94         return new BuilderMapEntryCollection<BuilderAnnotation>(internedItems.values()) {
95             @Override protected int getValue(@Nonnull BuilderAnnotation key) {
96                 return key.offset;
97             }
98 
99             @Override protected int setValue(@Nonnull BuilderAnnotation key, int value) {
100                 int prev = key.offset;
101                 key.offset = value;
102                 return prev;
103             }
104         };
105     }
106 }
107