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