• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012, 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.immutable;
32 
33 import com.android.tools.smali.dexlib2.base.BaseAnnotation;
34 import com.android.tools.smali.dexlib2.iface.Annotation;
35 import com.android.tools.smali.dexlib2.iface.AnnotationElement;
36 import com.android.tools.smali.util.ImmutableConverter;
37 import com.android.tools.smali.util.ImmutableUtils;
38 
39 import javax.annotation.Nonnull;
40 import javax.annotation.Nullable;
41 import java.util.Collection;
42 import java.util.Set;
43 
44 public class ImmutableAnnotation extends BaseAnnotation {
45     protected final int visibility;
46     @Nonnull protected final String type;
47     @Nonnull protected final Set<? extends ImmutableAnnotationElement> elements;
48 
ImmutableAnnotation(int visibility, @Nonnull String type, @Nullable Collection<? extends AnnotationElement> elements)49     public ImmutableAnnotation(int visibility,
50                                @Nonnull String type,
51                                @Nullable Collection<? extends AnnotationElement> elements) {
52         this.visibility = visibility;
53         this.type = type;
54         this.elements = ImmutableAnnotationElement.immutableSetOf(elements);
55     }
56 
ImmutableAnnotation(int visibility, @Nonnull String type, @Nullable Set<? extends ImmutableAnnotationElement> elements)57     public ImmutableAnnotation(int visibility,
58                                @Nonnull String type,
59                                @Nullable Set<? extends ImmutableAnnotationElement> elements) {
60         this.visibility = visibility;
61         this.type = type;
62         this.elements = ImmutableUtils.nullToEmptySet(elements);
63     }
64 
of(Annotation annotation)65     public static ImmutableAnnotation of(Annotation annotation) {
66         if (annotation instanceof  ImmutableAnnotation) {
67             return (ImmutableAnnotation)annotation;
68         }
69         return new ImmutableAnnotation(
70                 annotation.getVisibility(),
71                 annotation.getType(),
72                 annotation.getElements());
73     }
74 
getVisibility()75     @Override public int getVisibility() { return visibility; }
getType()76     @Nonnull @Override public String getType() { return type; }
getElements()77     @Nonnull @Override public Set<? extends ImmutableAnnotationElement> getElements() { return elements; }
78 
79     @Nonnull
immutableSetOf(@ullable Iterable<? extends Annotation> list)80     public static Set<ImmutableAnnotation> immutableSetOf(@Nullable Iterable<? extends Annotation> list) {
81         return CONVERTER.toSet(list);
82     }
83 
84     private static final ImmutableConverter<ImmutableAnnotation, Annotation> CONVERTER =
85             new ImmutableConverter<ImmutableAnnotation, Annotation>() {
86                 @Override
87                 protected boolean isImmutable(@Nonnull Annotation item) {
88                     return item instanceof ImmutableAnnotation;
89                 }
90 
91                 @Nonnull
92                 @Override
93                 protected ImmutableAnnotation makeImmutable(@Nonnull Annotation item) {
94                     return ImmutableAnnotation.of(item);
95                 }
96             };
97 }
98