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.AnnotationSetSection; 35 import com.android.tools.smali.dexlib2.writer.DexWriter; 36 37 import javax.annotation.Nonnull; 38 import javax.annotation.Nullable; 39 import java.util.Collection; 40 import java.util.Collections; 41 import java.util.Map.Entry; 42 import java.util.Set; 43 import java.util.concurrent.ConcurrentHashMap; 44 import java.util.concurrent.ConcurrentMap; 45 import java.util.stream.Collectors; 46 47 class BuilderAnnotationSetPool extends BaseBuilderPool 48 implements AnnotationSetSection<BuilderAnnotation, BuilderAnnotationSet> { 49 @Nonnull private final ConcurrentMap<Set<? extends Annotation>, BuilderAnnotationSet> internedItems = 50 new ConcurrentHashMap<>(); 51 BuilderAnnotationSetPool(@onnull DexBuilder dexBuilder)52 public BuilderAnnotationSetPool(@Nonnull DexBuilder dexBuilder) { 53 super(dexBuilder); 54 } 55 internAnnotationSet(@ullable Set<? extends Annotation> annotations)56 @Nonnull public BuilderAnnotationSet internAnnotationSet(@Nullable Set<? extends Annotation> annotations) { 57 if (annotations == null) { 58 return BuilderAnnotationSet.EMPTY; 59 } 60 61 BuilderAnnotationSet ret = internedItems.get(annotations); 62 if (ret != null) { 63 return ret; 64 } 65 66 BuilderAnnotationSet annotationSet = new BuilderAnnotationSet( 67 Collections.unmodifiableSet(annotations.stream() 68 .map(annotation -> dexBuilder.annotationSection.internAnnotation(annotation)) 69 .collect(Collectors.toSet()))); 70 71 ret = internedItems.putIfAbsent(annotationSet, annotationSet); 72 return ret==null?annotationSet:ret; 73 } 74 75 @Nonnull @Override getAnnotations(@onnull BuilderAnnotationSet key)76 public Collection<? extends BuilderAnnotation> getAnnotations(@Nonnull BuilderAnnotationSet key) { 77 return key.annotations; 78 } 79 getNullableItemOffset(@ullable BuilderAnnotationSet key)80 @Override public int getNullableItemOffset(@Nullable BuilderAnnotationSet key) { 81 return key==null? DexWriter.NO_OFFSET:key.offset; 82 } 83 getItemOffset(@onnull BuilderAnnotationSet key)84 @Override public int getItemOffset(@Nonnull BuilderAnnotationSet key) { 85 return key.offset; 86 } 87 getItems()88 @Nonnull @Override public Collection<? extends Entry<? extends BuilderAnnotationSet, Integer>> getItems() { 89 return new BuilderMapEntryCollection<BuilderAnnotationSet>(internedItems.values()) { 90 @Override protected int getValue(@Nonnull BuilderAnnotationSet key) { 91 return key.offset; 92 } 93 94 @Override protected int setValue(@Nonnull BuilderAnnotationSet key, int value) { 95 int prev = key.offset; 96 key.offset = value; 97 return prev; 98 } 99 }; 100 } 101 } 102