Home | History | Annotate | Download | only in builder
      1 /*
      2  * Copyright 2013, 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.builder;
     33 
     34 import com.google.common.base.Function;
     35 import com.google.common.collect.ImmutableSet;
     36 import com.google.common.collect.Iterators;
     37 import com.google.common.collect.Maps;
     38 import org.jf.dexlib2.iface.Annotation;
     39 import org.jf.dexlib2.writer.AnnotationSetSection;
     40 import org.jf.dexlib2.writer.DexWriter;
     41 
     42 import javax.annotation.Nonnull;
     43 import javax.annotation.Nullable;
     44 import java.util.Collection;
     45 import java.util.Map.Entry;
     46 import java.util.Set;
     47 import java.util.concurrent.ConcurrentMap;
     48 
     49 class BuilderAnnotationSetPool extends BaseBuilderPool
     50         implements AnnotationSetSection<BuilderAnnotation, BuilderAnnotationSet> {
     51     @Nonnull private final ConcurrentMap<Set<? extends Annotation>, BuilderAnnotationSet> internedItems =
     52             Maps.newConcurrentMap();
     53 
     54     public BuilderAnnotationSetPool(@Nonnull DexBuilder dexBuilder) {
     55         super(dexBuilder);
     56     }
     57 
     58     @Nonnull public BuilderAnnotationSet internAnnotationSet(@Nullable Set<? extends Annotation> annotations) {
     59         if (annotations == null) {
     60             return BuilderAnnotationSet.EMPTY;
     61         }
     62 
     63         BuilderAnnotationSet ret = internedItems.get(annotations);
     64         if (ret != null) {
     65             return ret;
     66         }
     67 
     68         BuilderAnnotationSet annotationSet = new BuilderAnnotationSet(
     69                 ImmutableSet.copyOf(Iterators.transform(annotations.iterator(),
     70                         new Function<Annotation, BuilderAnnotation>() {
     71                             @Nullable @Override public BuilderAnnotation apply(Annotation input) {
     72                                 return dexBuilder.annotationSection.internAnnotation(input);
     73                             }
     74                         })));
     75 
     76         ret = internedItems.putIfAbsent(annotationSet, annotationSet);
     77         return ret==null?annotationSet:ret;
     78     }
     79 
     80     @Nonnull @Override
     81     public Collection<? extends BuilderAnnotation> getAnnotations(@Nonnull BuilderAnnotationSet key) {
     82         return key.annotations;
     83     }
     84 
     85     @Override public int getNullableItemOffset(@Nullable BuilderAnnotationSet key) {
     86         return key==null?DexWriter.NO_OFFSET:key.offset;
     87     }
     88 
     89     @Override public int getItemOffset(@Nonnull BuilderAnnotationSet key) {
     90         return key.offset;
     91     }
     92 
     93     @Nonnull @Override public Collection<? extends Entry<? extends BuilderAnnotationSet, Integer>> getItems() {
     94         return new BuilderMapEntryCollection<BuilderAnnotationSet>(internedItems.values()) {
     95             @Override protected int getValue(@Nonnull BuilderAnnotationSet key) {
     96                 return key.offset;
     97             }
     98 
     99             @Override protected int setValue(@Nonnull BuilderAnnotationSet key, int value) {
    100                 int prev = key.offset;
    101                 key.offset = value;
    102                 return prev;
    103             }
    104         };
    105     }
    106 }
    107