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
32package org.jf.dexlib2.writer.pool;
33
34import org.jf.dexlib2.iface.Annotation;
35import org.jf.dexlib2.iface.AnnotationElement;
36import org.jf.dexlib2.iface.value.EncodedValue;
37import org.jf.dexlib2.writer.AnnotationSection;
38
39import javax.annotation.Nonnull;
40import java.util.Collection;
41
42public class AnnotationPool extends BaseOffsetPool<Annotation>
43        implements AnnotationSection<CharSequence, CharSequence, Annotation, AnnotationElement, EncodedValue> {
44    @Nonnull StringPool stringPool;
45    @Nonnull TypePool typePool;
46    @Nonnull FieldPool fieldPool;
47    @Nonnull MethodPool methodPool;
48
49    public AnnotationPool(@Nonnull StringPool stringPool, @Nonnull TypePool typePool,
50                          @Nonnull FieldPool fieldPool, @Nonnull MethodPool methodPool) {
51        this.stringPool = stringPool;
52        this.typePool = typePool;
53        this.fieldPool = fieldPool;
54        this.methodPool = methodPool;
55    }
56
57    public void intern(@Nonnull Annotation annotation) {
58        Integer prev = internedItems.put(annotation, 0);
59        if (prev == null) {
60            typePool.intern(annotation.getType());
61            for (AnnotationElement element: annotation.getElements()) {
62                stringPool.intern(element.getName());
63                DexPool.internEncodedValue(element.getValue(), stringPool, typePool, fieldPool, methodPool);
64            }
65        }
66    }
67
68    @Override public int getVisibility(@Nonnull Annotation annotation) {
69        return annotation.getVisibility();
70    }
71
72    @Nonnull @Override public CharSequence getType(@Nonnull Annotation annotation) {
73        return annotation.getType();
74    }
75
76    @Nonnull @Override public Collection<? extends AnnotationElement> getElements(@Nonnull Annotation annotation) {
77        return annotation.getElements();
78    }
79
80    @Nonnull @Override public CharSequence getElementName(@Nonnull AnnotationElement annotationElement) {
81        return annotationElement.getName();
82    }
83
84    @Nonnull @Override public EncodedValue getElementValue(@Nonnull AnnotationElement annotationElement) {
85        return annotationElement.getValue();
86    }
87}
88