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 17package com.android.dx.dex.file; 18 19import com.android.dx.rop.annotation.Annotations; 20import com.android.dx.rop.cst.CstMethodRef; 21import com.android.dx.util.AnnotatedOutput; 22import com.android.dx.util.Hex; 23import com.android.dx.util.ToHuman; 24 25/** 26 * Association of a method and its annotations. 27 */ 28public final class MethodAnnotationStruct 29 implements ToHuman, Comparable<MethodAnnotationStruct> { 30 /** {@code non-null;} the method in question */ 31 private final CstMethodRef method; 32 33 /** {@code non-null;} the associated annotations */ 34 private AnnotationSetItem annotations; 35 36 /** 37 * Constructs an instance. 38 * 39 * @param method {@code non-null;} the method in question 40 * @param annotations {@code non-null;} the associated annotations 41 */ 42 public MethodAnnotationStruct(CstMethodRef method, 43 AnnotationSetItem annotations) { 44 if (method == null) { 45 throw new NullPointerException("method == null"); 46 } 47 48 if (annotations == null) { 49 throw new NullPointerException("annotations == null"); 50 } 51 52 this.method = method; 53 this.annotations = annotations; 54 } 55 56 /** {@inheritDoc} */ 57 public int hashCode() { 58 return method.hashCode(); 59 } 60 61 /** {@inheritDoc} */ 62 public boolean equals(Object other) { 63 if (! (other instanceof MethodAnnotationStruct)) { 64 return false; 65 } 66 67 return method.equals(((MethodAnnotationStruct) other).method); 68 } 69 70 /** {@inheritDoc} */ 71 public int compareTo(MethodAnnotationStruct other) { 72 return method.compareTo(other.method); 73 } 74 75 /** {@inheritDoc} */ 76 public void addContents(DexFile file) { 77 MethodIdsSection methodIds = file.getMethodIds(); 78 MixedItemSection wordData = file.getWordData(); 79 80 methodIds.intern(method); 81 annotations = wordData.intern(annotations); 82 } 83 84 /** {@inheritDoc} */ 85 public void writeTo(DexFile file, AnnotatedOutput out) { 86 int methodIdx = file.getMethodIds().indexOf(method); 87 int annotationsOff = annotations.getAbsoluteOffset(); 88 89 if (out.annotates()) { 90 out.annotate(0, " " + method.toHuman()); 91 out.annotate(4, " method_idx: " + Hex.u4(methodIdx)); 92 out.annotate(4, " annotations_off: " + 93 Hex.u4(annotationsOff)); 94 } 95 96 out.writeInt(methodIdx); 97 out.writeInt(annotationsOff); 98 } 99 100 /** {@inheritDoc} */ 101 public String toHuman() { 102 return method.toHuman() + ": " + annotations; 103 } 104 105 /** 106 * Gets the method this item is for. 107 * 108 * @return {@code non-null;} the method 109 */ 110 public CstMethodRef getMethod() { 111 return method; 112 } 113 114 /** 115 * Gets the associated annotations. 116 * 117 * @return {@code non-null;} the annotations 118 */ 119 public Annotations getAnnotations() { 120 return annotations.getAnnotations(); 121 } 122} 123