ImmutableClassDef.java revision 22c3185bb7c8618437eabe6c597549e0989ec4e6
1/*
2 * Copyright 2012, 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.immutable;
33
34import com.google.common.collect.ImmutableSet;
35import org.jf.dexlib2.base.reference.BaseTypeReference;
36import org.jf.dexlib2.iface.Annotation;
37import org.jf.dexlib2.iface.ClassDef;
38import org.jf.dexlib2.iface.Field;
39import org.jf.dexlib2.iface.Method;
40import org.jf.util.ImmutableConverter;
41import org.jf.util.ImmutableUtils;
42
43import javax.annotation.Nonnull;
44import javax.annotation.Nullable;
45import java.util.Collection;
46
47public class ImmutableClassDef extends BaseTypeReference implements ClassDef {
48    @Nonnull protected final String type;
49    protected final int accessFlags;
50    @Nullable protected final String superclass;
51    @Nonnull protected final ImmutableSet<String> interfaces;
52    @Nullable protected final String sourceFile;
53    @Nonnull protected final ImmutableSet<? extends ImmutableAnnotation> annotations;
54    @Nonnull protected final ImmutableSet<? extends ImmutableField> fields;
55    @Nonnull protected final ImmutableSet<? extends ImmutableMethod> methods;
56
57    public ImmutableClassDef(@Nonnull String type,
58                             int accessFlags,
59                             @Nullable String superclass,
60                             @Nullable Collection<String> interfaces,
61                             @Nullable String sourceFile,
62                             @Nullable Collection<? extends Annotation> annotations,
63                             @Nullable Collection<? extends Field> fields,
64                             @Nullable Collection<? extends Method> methods) {
65        this.type = type;
66        this.accessFlags = accessFlags;
67        this.superclass = superclass;
68        this.interfaces = interfaces==null ? ImmutableSet.<String>of() : ImmutableSet.copyOf(interfaces);
69        this.sourceFile = sourceFile;
70        this.annotations = ImmutableAnnotation.immutableSetOf(annotations);
71        this.fields = ImmutableField.immutableSetOf(fields);
72        this.methods = ImmutableMethod.immutableSetOf(methods);
73    }
74
75    public ImmutableClassDef(@Nonnull String type,
76                             int accessFlags,
77                             @Nullable String superclass,
78                             @Nullable ImmutableSet<String> interfaces,
79                             @Nullable String sourceFile,
80                             @Nullable ImmutableSet<? extends ImmutableAnnotation> annotations,
81                             @Nullable ImmutableSet<? extends ImmutableField> fields,
82                             @Nullable ImmutableSet<? extends ImmutableMethod> methods) {
83        this.type = type;
84        this.accessFlags = accessFlags;
85        this.superclass = superclass;
86        this.interfaces = ImmutableUtils.nullToEmptySet(interfaces);
87        this.sourceFile = sourceFile;
88        this.annotations = ImmutableUtils.nullToEmptySet(annotations);
89        this.fields = ImmutableUtils.nullToEmptySet(fields);
90        this.methods = ImmutableUtils.nullToEmptySet(methods);
91    }
92
93    public static ImmutableClassDef of(ClassDef classDef) {
94        if (classDef instanceof ImmutableClassDef) {
95            return (ImmutableClassDef)classDef;
96        }
97        return new ImmutableClassDef(
98                classDef.getType(),
99                classDef.getAccessFlags(),
100                classDef.getSuperclass(),
101                classDef.getInterfaces(),
102                classDef.getSourceFile(),
103                classDef.getAnnotations(),
104                classDef.getFields(),
105                classDef.getMethods());
106    }
107
108    @Nonnull @Override public String getType() { return type; }
109    @Override public int getAccessFlags() { return accessFlags; }
110    @Nullable @Override public String getSuperclass() { return superclass; }
111    @Nonnull @Override public ImmutableSet<String> getInterfaces() { return interfaces; }
112    @Nullable @Override public String getSourceFile() { return sourceFile; }
113    @Nonnull @Override public ImmutableSet<? extends ImmutableAnnotation> getAnnotations() { return annotations; }
114    @Nonnull @Override public ImmutableSet<? extends ImmutableField> getFields() { return fields; }
115    @Nonnull @Override public ImmutableSet<? extends ImmutableMethod> getMethods() { return methods; }
116
117    @Nonnull
118    public static ImmutableSet<ImmutableClassDef> immutableSetOf(@Nullable Iterable<? extends ClassDef> iterable) {
119        return CONVERTER.toSet(iterable);
120    }
121
122    private static final ImmutableConverter<ImmutableClassDef, ClassDef> CONVERTER =
123            new ImmutableConverter<ImmutableClassDef, ClassDef>() {
124                @Override
125                protected boolean isImmutable(@Nonnull ClassDef item) {
126                    return item instanceof ImmutableClassDef;
127                }
128
129                @Nonnull
130                @Override
131                protected ImmutableClassDef makeImmutable(@Nonnull ClassDef item) {
132                    return ImmutableClassDef.of(item);
133                }
134            };
135}
136