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.dex.MixedSectionCollection; 8 import java.util.Arrays; 9 10 public class DexEncodedAnnotation extends DexItem { 11 12 private static final int UNSORTED = 0; 13 14 public final DexType type; 15 public final DexAnnotationElement[] elements; 16 17 private int sorted = UNSORTED; 18 19 public DexEncodedAnnotation(DexType type, DexAnnotationElement[] elements) { 20 this.type = type; 21 this.elements = elements; 22 } 23 24 @Override 25 public void collectIndexedItems(IndexedItemCollection indexedItems) { 26 type.collectIndexedItems(indexedItems); 27 collectAll(indexedItems, elements); 28 } 29 30 @Override 31 void collectMixedSectionItems(MixedSectionCollection mixedItems) { 32 // Should never be called. 33 assert false; 34 } 35 36 public String toString() { 37 return "Encoded annotation " + type + " " + Arrays.toString(elements); 38 } 39 40 @Override 41 public int hashCode() { 42 return type.hashCode() * 7 + Arrays.hashCode(elements); 43 } 44 45 @Override 46 public boolean equals(Object other) { 47 if (this == other) { 48 return true; 49 } 50 if (other instanceof DexEncodedAnnotation) { 51 DexEncodedAnnotation that = (DexEncodedAnnotation) other; 52 return that.type.equals(type) && Arrays.equals(that.elements, elements); 53 } 54 return false; 55 } 56 57 public void sort() { 58 if (sorted != UNSORTED) { 59 assert sorted == sortedHashCode(); 60 return; 61 } 62 Arrays.sort(elements, (a, b) -> a.name.compareTo(b.name)); 63 for (DexAnnotationElement element : elements) { 64 element.value.sort(); 65 } 66 sorted = sortedHashCode(); 67 } 68 69 private int sortedHashCode() { 70 int hashCode = hashCode(); 71 return hashCode == UNSORTED ? 1 : hashCode; 72 } 73 } 74