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 32 package org.jf.dexlib2.writer.builder; 33 34 import com.google.common.base.Functions; 35 import com.google.common.collect.*; 36 import org.jf.dexlib2.base.reference.BaseTypeReference; 37 import org.jf.dexlib2.iface.ClassDef; 38 import org.jf.dexlib2.util.FieldUtil; 39 import org.jf.dexlib2.util.MethodUtil; 40 import org.jf.dexlib2.writer.DexWriter; 41 42 import javax.annotation.Nonnull; 43 import javax.annotation.Nullable; 44 import java.util.*; 45 46 public 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 List<String> getInterfaces() { 101 return Lists.transform(this.interfaces, Functions.toStringFunction()); 102 } 103 104 @Nonnull @Override public Collection<BuilderField> getFields() { 105 return new AbstractCollection<BuilderField>() { 106 @Nonnull @Override public Iterator<BuilderField> iterator() { 107 return Iterators.mergeSorted( 108 ImmutableList.of(staticFields.iterator(), instanceFields.iterator()), 109 Ordering.natural()); 110 } 111 112 @Override public int size() { 113 return staticFields.size() + instanceFields.size(); 114 } 115 }; 116 } 117 118 @Nonnull @Override public Collection<BuilderMethod> getMethods() { 119 return new AbstractCollection<BuilderMethod>() { 120 @Nonnull @Override public Iterator<BuilderMethod> iterator() { 121 return Iterators.mergeSorted( 122 ImmutableList.of(directMethods.iterator(), virtualMethods.iterator()), 123 Ordering.natural()); 124 } 125 126 @Override public int size() { 127 return directMethods.size() + virtualMethods.size(); 128 } 129 }; 130 } 131 } 132