ImmutableClassDef.java revision b0383884fa44aa649cabb706325e922ee167c94a
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.base.Objects;
35import com.google.common.collect.ImmutableList;
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.ImmutableListConverter;
41import org.jf.util.ImmutableListUtils;
42
43import javax.annotation.Nonnull;
44import javax.annotation.Nullable;
45import java.util.List;
46
47public class ImmutableClassDef implements ClassDef {
48    @Nonnull public final String name;
49    public final int accessFlags;
50    @Nullable public final String superclass;
51    @Nonnull public final ImmutableList<String> interfaces;
52    @Nullable public final String sourceFile;
53    @Nonnull public final ImmutableList<? extends ImmutableAnnotation> annotations;
54    @Nonnull public final ImmutableList<? extends ImmutableField> fields;
55    @Nonnull public final ImmutableList<? extends ImmutableMethod> methods;
56
57    public ImmutableClassDef(@Nonnull String name,
58                             int accessFlags,
59                             @Nullable String superclass,
60                             @Nullable List<String> interfaces,
61                             @Nullable String sourceFile,
62                             @Nullable List<? extends Annotation> annotations,
63                             @Nullable List<? extends Field> fields,
64                             @Nullable List<? extends Method> methods) {
65        this.name = name;
66        this.accessFlags = accessFlags;
67        this.superclass = superclass;
68        this.interfaces = interfaces==null ? ImmutableList.<String>of() : ImmutableList.copyOf(interfaces);
69        this.sourceFile = sourceFile;
70        this.annotations = ImmutableAnnotation.immutableListOf(annotations);
71        this.fields = ImmutableField.immutableListOf(fields);
72        this.methods = ImmutableMethod.immutableListOf(methods);
73    }
74
75    public ImmutableClassDef(@Nonnull String name,
76                             int accessFlags,
77                             @Nullable String superclass,
78                             @Nullable ImmutableList<String> interfaces,
79                             @Nullable String sourceFile,
80                             @Nullable ImmutableList<? extends ImmutableAnnotation> annotations,
81                             @Nullable ImmutableList<? extends ImmutableField> fields,
82                             @Nullable ImmutableList<? extends ImmutableMethod> methods) {
83        this.name = name;
84        this.accessFlags = accessFlags;
85        this.superclass = superclass;
86        this.interfaces = ImmutableListUtils.nullToEmptyList(interfaces);
87        this.sourceFile = sourceFile;
88        this.annotations = ImmutableListUtils.nullToEmptyList(annotations);
89        this.fields = ImmutableListUtils.nullToEmptyList(fields);
90        this.methods = ImmutableListUtils.nullToEmptyList(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.getName(),
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 getName() { return name; }
109    @Override public int getAccessFlags() { return accessFlags; }
110    @Nullable @Override public String getSuperclass() { return superclass; }
111    @Nonnull @Override public ImmutableList<String> getInterfaces() { return interfaces; }
112    @Nullable @Override public String getSourceFile() { return sourceFile; }
113    @Nonnull @Override public ImmutableList<? extends ImmutableAnnotation> getAnnotations() { return annotations; }
114    @Nonnull @Override public ImmutableList<? extends ImmutableField> getFields() { return fields; }
115    @Nonnull @Override public ImmutableList<? extends ImmutableMethod> getMethods() { return methods; }
116
117    @Nonnull
118    public static ImmutableList<ImmutableClassDef> immutableListOf(@Nullable List<? extends ClassDef> list) {
119        return CONVERTER.convert(list);
120    }
121
122    private static final ImmutableListConverter<ImmutableClassDef, ClassDef> CONVERTER =
123            new ImmutableListConverter<ImmutableClassDef, ClassDef>() {
124                @Override
125                protected boolean isImmutable(ClassDef item) {
126                    return item instanceof ImmutableClassDef;
127                }
128
129                @Override
130                protected ImmutableClassDef makeImmutable(ClassDef item) {
131                    return ImmutableClassDef.of(item);
132                }
133            };
134}
135