1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.dx.dex.file; 18 19 import com.android.dx.rop.annotation.Annotations; 20 import com.android.dx.rop.cst.CstFieldRef; 21 import com.android.dx.util.AnnotatedOutput; 22 import com.android.dx.util.Hex; 23 import com.android.dx.util.ToHuman; 24 25 /** 26 * Association of a field and its annotations. 27 */ 28 public final class FieldAnnotationStruct 29 implements ToHuman, Comparable<FieldAnnotationStruct> { 30 /** {@code non-null;} the field in question */ 31 private final CstFieldRef field; 32 33 /** {@code non-null;} the associated annotations */ 34 private AnnotationSetItem annotations; 35 36 /** 37 * Constructs an instance. 38 * 39 * @param field {@code non-null;} the field in question 40 * @param annotations {@code non-null;} the associated annotations 41 */ 42 public FieldAnnotationStruct(CstFieldRef field, 43 AnnotationSetItem annotations) { 44 if (field == null) { 45 throw new NullPointerException("field == null"); 46 } 47 48 if (annotations == null) { 49 throw new NullPointerException("annotations == null"); 50 } 51 52 this.field = field; 53 this.annotations = annotations; 54 } 55 56 /** {@inheritDoc} */ 57 @Override 58 public int hashCode() { 59 return field.hashCode(); 60 } 61 62 /** {@inheritDoc} */ 63 @Override 64 public boolean equals(Object other) { 65 if (! (other instanceof FieldAnnotationStruct)) { 66 return false; 67 } 68 69 return field.equals(((FieldAnnotationStruct) other).field); 70 } 71 72 /** {@inheritDoc} */ 73 @Override 74 public int compareTo(FieldAnnotationStruct other) { 75 return field.compareTo(other.field); 76 } 77 78 /** {@inheritDoc} */ 79 public void addContents(DexFile file) { 80 FieldIdsSection fieldIds = file.getFieldIds(); 81 MixedItemSection wordData = file.getWordData(); 82 83 fieldIds.intern(field); 84 annotations = wordData.intern(annotations); 85 } 86 87 /** {@inheritDoc} */ 88 public void writeTo(DexFile file, AnnotatedOutput out) { 89 int fieldIdx = file.getFieldIds().indexOf(field); 90 int annotationsOff = annotations.getAbsoluteOffset(); 91 92 if (out.annotates()) { 93 out.annotate(0, " " + field.toHuman()); 94 out.annotate(4, " field_idx: " + Hex.u4(fieldIdx)); 95 out.annotate(4, " annotations_off: " + 96 Hex.u4(annotationsOff)); 97 } 98 99 out.writeInt(fieldIdx); 100 out.writeInt(annotationsOff); 101 } 102 103 /** {@inheritDoc} */ 104 @Override 105 public String toHuman() { 106 return field.toHuman() + ": " + annotations; 107 } 108 109 /** 110 * Gets the field this item is for. 111 * 112 * @return {@code non-null;} the field 113 */ 114 public CstFieldRef getField() { 115 return field; 116 } 117 118 /** 119 * Gets the associated annotations. 120 * 121 * @return {@code non-null;} the annotations 122 */ 123 public Annotations getAnnotations() { 124 return annotations.getAnnotations(); 125 } 126 } 127