BuilderClassDef.java revision 6926ece0cd35d0b34ca66db6f64fdb45e2fac544
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.builder;
33
34import com.google.common.base.Functions;
35import com.google.common.collect.*;
36import org.jf.dexlib2.base.reference.BaseTypeReference;
37import org.jf.dexlib2.iface.ClassDef;
38import org.jf.dexlib2.util.FieldUtil;
39import org.jf.dexlib2.util.MethodUtil;
40import org.jf.dexlib2.writer.DexWriter;
41
42import javax.annotation.Nonnull;
43import javax.annotation.Nullable;
44import java.util.*;
45
46public class BuilderClassDef extends BaseTypeReference implements ClassDef {
47    @Nonnull final BuilderTypeReference type;
48    final int accessFlags;
49    @Nullable final BuilderTypeReference superclass;
50    @Nonnull final BuilderTypeList interfaces;
51    @Nullable final BuilderStringReference sourceFile;
52    @Nonnull final BuilderAnnotationSet annotations;
53    @Nonnull final SortedSet<BuilderField> staticFields;
54    @Nonnull final SortedSet<BuilderField> instanceFields;
55    @Nonnull final SortedSet<BuilderMethod> directMethods;
56    @Nonnull final SortedSet<BuilderMethod> virtualMethods;
57
58    int classDefIndex = DexWriter.NO_INDEX;
59    int encodedArrayOffset = DexWriter.NO_OFFSET;
60    int annotationDirectoryOffset = DexWriter.NO_OFFSET;
61
62    BuilderClassDef(@Nonnull BuilderTypeReference type,
63                    int accessFlags,
64                    @Nullable BuilderTypeReference superclass,
65                    @Nonnull BuilderTypeList interfaces,
66                    @Nullable BuilderStringReference sourceFile,
67                    @Nonnull BuilderAnnotationSet annotations,
68                    @Nullable Iterable<? extends BuilderField> fields,
69                    @Nullable Iterable<? extends BuilderMethod> methods) {
70        if (fields == null) {
71            fields = ImmutableList.of();
72        }
73        if (methods == null) {
74            methods = ImmutableList.of();
75        }
76
77        this.type = type;
78        this.accessFlags = accessFlags;
79        this.superclass = superclass;
80        this.interfaces = interfaces;
81        this.sourceFile = sourceFile;
82        this.annotations = annotations;
83        this.staticFields = ImmutableSortedSet.copyOf(Iterables.filter(fields, FieldUtil.FIELD_IS_STATIC));
84        this.instanceFields = ImmutableSortedSet.copyOf(Iterables.filter(fields, FieldUtil.FIELD_IS_INSTANCE));
85        this.directMethods = ImmutableSortedSet.copyOf(Iterables.filter(methods, MethodUtil.METHOD_IS_DIRECT));
86        this.virtualMethods = ImmutableSortedSet.copyOf(Iterables.filter(methods, MethodUtil.METHOD_IS_VIRTUAL));
87    }
88
89    @Nonnull @Override public String getType() { return type.getType(); }
90    @Override public int getAccessFlags() { return accessFlags; }
91    @Nullable @Override public String getSuperclass() { return superclass==null?null:superclass.getType(); }
92    @Nullable @Override public String getSourceFile() { return sourceFile==null?null:sourceFile.getString(); }
93    @Nonnull @Override public BuilderAnnotationSet getAnnotations() { return annotations; }
94    @Nonnull @Override public SortedSet<BuilderField> getStaticFields() { return staticFields; }
95    @Nonnull @Override public SortedSet<BuilderField> getInstanceFields() { return instanceFields; }
96    @Nonnull @Override public SortedSet<BuilderMethod> getDirectMethods() { return directMethods; }
97    @Nonnull @Override public SortedSet<BuilderMethod> getVirtualMethods() { return virtualMethods; }
98
99    @Nonnull @Override
100    public Set<String> getInterfaces() {
101        return new AbstractSet<String>() {
102            @Nonnull @Override public Iterator<String> iterator() {
103                return Iterators.transform(interfaces.iterator(), Functions.toStringFunction());
104            }
105
106            @Override public int size() {
107                return interfaces.size();
108            }
109        };
110    }
111
112    @Nonnull @Override public Collection<BuilderField> getFields() {
113        return new AbstractCollection<BuilderField>() {
114            @Nonnull @Override public Iterator<BuilderField> iterator() {
115                return Iterators.mergeSorted(
116                        ImmutableList.of(staticFields.iterator(), instanceFields.iterator()),
117                        Ordering.natural());
118            }
119
120            @Override public int size() {
121                return staticFields.size() + instanceFields.size();
122            }
123        };
124    }
125
126    @Nonnull @Override public Collection<BuilderMethod> getMethods() {
127        return new AbstractCollection<BuilderMethod>() {
128            @Nonnull @Override public Iterator<BuilderMethod> iterator() {
129                return Iterators.mergeSorted(
130                        ImmutableList.of(directMethods.iterator(), virtualMethods.iterator()),
131                        Ordering.natural());
132            }
133
134            @Override public int size() {
135                return directMethods.size() + virtualMethods.size();
136            }
137        };
138    }
139}
140