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