1 // Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file 2 // for details. All rights reserved. Use of this source code is governed by a 3 // BSD-style license that can be found in the LICENSE file. 4 package com.android.tools.r8.graph; 5 6 import com.android.tools.r8.dex.IndexedItemCollection; 7 import com.android.tools.r8.errors.CompilationError; 8 import com.android.tools.r8.naming.NamingLens; 9 10 public class DexField extends Descriptor<DexEncodedField, DexField> implements 11 PresortedComparable<DexField> { 12 13 public final DexType clazz; 14 public final DexType type; 15 public final DexString name; 16 DexField(DexType clazz, DexType type, DexString name)17 DexField(DexType clazz, DexType type, DexString name) { 18 this.clazz = clazz; 19 this.type = type; 20 this.name = name; 21 if (!name.isValidFieldName()) { 22 throw new CompilationError( 23 "Field name '" + name.toString() + "' cannot be represented in dex format."); 24 } 25 } 26 27 @Override computeHashCode()28 public int computeHashCode() { 29 return clazz.hashCode() 30 + type.hashCode() * 7 31 + name.hashCode() * 31; 32 } 33 34 @Override computeEquals(Object other)35 public boolean computeEquals(Object other) { 36 if (other instanceof DexField) { 37 DexField o = (DexField) other; 38 return clazz.equals(o.clazz) 39 && type.equals(o.type) 40 && name.equals(o.name); 41 } 42 return false; 43 } 44 45 @Override toString()46 public String toString() { 47 return "Field " + type + " " + clazz + "." + name; 48 } 49 50 @Override collectIndexedItems(IndexedItemCollection indexedItems)51 public void collectIndexedItems(IndexedItemCollection indexedItems) { 52 if (indexedItems.addField(this)) { 53 clazz.collectIndexedItems(indexedItems); 54 type.collectIndexedItems(indexedItems); 55 indexedItems.getRenamedName(this).collectIndexedItems(indexedItems); 56 } 57 } 58 59 @Override getOffset(ObjectToOffsetMapping mapping)60 public int getOffset(ObjectToOffsetMapping mapping) { 61 return mapping.getOffsetFor(this); 62 } 63 64 @Override compareTo(DexField other)65 public int compareTo(DexField other) { 66 return sortedCompareTo(other.getSortedIndex()); 67 } 68 69 @Override slowCompareTo(DexField other)70 public int slowCompareTo(DexField other) { 71 int result = clazz.slowCompareTo(other.clazz); 72 if (result != 0) { 73 return result; 74 } 75 result = name.slowCompareTo(other.name); 76 if (result != 0) { 77 return result; 78 } 79 return type.slowCompareTo(other.type); 80 } 81 82 @Override slowCompareTo(DexField other, NamingLens namingLens)83 public int slowCompareTo(DexField other, NamingLens namingLens) { 84 int result = clazz.slowCompareTo(other.clazz, namingLens); 85 if (result != 0) { 86 return result; 87 } 88 result = namingLens.lookupName(this).slowCompareTo(namingLens.lookupName(other)); 89 if (result != 0) { 90 return result; 91 } 92 return type.slowCompareTo(other.type, namingLens); 93 } 94 95 @Override layeredCompareTo(DexField other, NamingLens namingLens)96 public int layeredCompareTo(DexField other, NamingLens namingLens) { 97 int result = clazz.compareTo(other.clazz); 98 if (result != 0) { 99 return result; 100 } 101 result = namingLens.lookupName(this).compareTo(namingLens.lookupName(other)); 102 if (result != 0) { 103 return result; 104 } 105 return type.compareTo(other.type); 106 } 107 108 @Override match(DexEncodedField entry)109 public boolean match(DexEncodedField entry) { 110 return entry.field.name == name && entry.field.type == type; 111 } 112 113 @Override getHolder()114 public DexType getHolder() { 115 return clazz; 116 } 117 toSmaliString()118 public String toSmaliString() { 119 return clazz.toSmaliString() + "->" + name + ":" + type.toSmaliString(); 120 } 121 122 @Override toSourceString()123 public String toSourceString() { 124 return type.toSourceString() + " " + clazz.toSourceString() + "." + name.toSourceString(); 125 } 126 } 127