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