Add annotation lookup functionality to AnnotationDirectoryItem

This commit is contained in:
Ben Gruver
2012-06-05 17:44:54 -07:00
parent 1ffc028a3b
commit e5466fee23
5 changed files with 145 additions and 65 deletions

View File

@ -36,9 +36,7 @@ import org.jf.dexlib.Util.ReadOnlyArrayList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
@Nullable
@ -375,6 +373,58 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
return ReadOnlyArrayList.of(parameterAnnotations);
}
/**
* Gets the field annotations for the given field, or null if no annotations are defined for that field
* @param fieldIdItem The field to get the annotations for
* @return An <code>AnnotationSetItem</code> containing the field annotations, or null if none are found
*/
@Nullable
public AnnotationSetItem getFieldAnnotations(FieldIdItem fieldIdItem) {
if (fieldAnnotations == null) {
return null;
}
int index = Arrays.binarySearch(fieldAnnotations, fieldIdItem);
if (index < 0) {
return null;
}
return fieldAnnotations[index].annotationSet;
}
/**
* Gets the method annotations for the given method, or null if no annotations are defined for that method
* @param methodIdItem The method to get the annotations for
* @return An <code>AnnotationSetItem</code> containing the method annotations, or null if none are found
*/
@Nullable
public AnnotationSetItem getMethodAnnotations(MethodIdItem methodIdItem) {
if (methodAnnotations == null) {
return null;
}
int index = Arrays.binarySearch(methodAnnotations, methodIdItem);
if (index < 0) {
return null;
}
return methodAnnotations[index].annotationSet;
}
/**
* Gets the parameter annotations for the given method, or null if no parameter annotations are defined for that
* method
* @param methodIdItem The method to get the parameter annotations for
* @return An <code>AnnotationSetRefList</code> containing the parameter annotations, or null if none are found
*/
@Nullable
public AnnotationSetRefList getParameterAnnotations(MethodIdItem methodIdItem) {
if (parameterAnnotations == null) {
return null;
}
int index = Arrays.binarySearch(parameterAnnotations, methodIdItem);
if (index < 0) {
return null;
}
return parameterAnnotations[index].annotationSet;
}
/**
* @return The number of field annotations in this <code>AnnotationDirectoryItem</code>
*/
@ -454,7 +504,7 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
return (this.compareTo(other) == 0);
}
public static class FieldAnnotation implements Comparable<FieldAnnotation> {
public static class FieldAnnotation implements Comparable<Convertible<FieldIdItem>>, Convertible<FieldIdItem> {
public final FieldIdItem field;
public final AnnotationSetItem annotationSet;
@ -463,8 +513,8 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
this.annotationSet = annotationSet;
}
public int compareTo(FieldAnnotation other) {
return field.compareTo(other.field);
public int compareTo(Convertible<FieldIdItem> other) {
return field.compareTo(other.convert());
}
@Override
@ -479,9 +529,13 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
public int hashCode() {
return field.hashCode() + 31 * annotationSet.hashCode();
}
public FieldIdItem convert() {
return field;
}
}
public static class MethodAnnotation implements Comparable<MethodAnnotation> {
public static class MethodAnnotation implements Comparable<Convertible<MethodIdItem>>, Convertible<MethodIdItem> {
public final MethodIdItem method;
public final AnnotationSetItem annotationSet;
@ -490,8 +544,8 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
this.annotationSet = annotationSet;
}
public int compareTo(MethodAnnotation other) {
return method.compareTo(other.method);
public int compareTo(Convertible<MethodIdItem> other) {
return method.compareTo(other.convert());
}
@Override
@ -506,9 +560,14 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
public int hashCode() {
return method.hashCode() + 31 * annotationSet.hashCode();
}
public MethodIdItem convert() {
return method;
}
}
public static class ParameterAnnotation implements Comparable<ParameterAnnotation> {
public static class ParameterAnnotation implements Comparable<Convertible<MethodIdItem>>,
Convertible<MethodIdItem> {
public final MethodIdItem method;
public final AnnotationSetRefList annotationSet;
@ -517,8 +576,8 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
this.annotationSet = annotationSet;
}
public int compareTo(ParameterAnnotation other) {
return method.compareTo(other.method);
public int compareTo(Convertible<MethodIdItem> other) {
return method.compareTo(other.convert());
}
@Override
@ -533,5 +592,9 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
public int hashCode() {
return method.hashCode() + 31 * annotationSet.hashCode();
}
public MethodIdItem convert() {
return method;
}
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib;
/**
* Describes an object that can be converted to a different type
*/
public interface Convertible<T> {
T convert();
}

View File

@ -31,7 +31,7 @@ package org.jf.dexlib;
import org.jf.dexlib.Util.AnnotatedOutput;
import org.jf.dexlib.Util.Input;
public class FieldIdItem extends Item<FieldIdItem> {
public class FieldIdItem extends Item<FieldIdItem> implements Convertible<FieldIdItem> {
private int hashCode = 0;
private TypeIdItem classType;
@ -237,4 +237,8 @@ public class FieldIdItem extends Item<FieldIdItem> {
fieldType == other.fieldType &&
fieldName == other.fieldName);
}
public FieldIdItem convert() {
return this;
}
}

View File

@ -31,7 +31,7 @@ package org.jf.dexlib;
import org.jf.dexlib.Util.AnnotatedOutput;
import org.jf.dexlib.Util.Input;
public class MethodIdItem extends Item<MethodIdItem> {
public class MethodIdItem extends Item<MethodIdItem> implements Convertible<MethodIdItem> {
private int hashCode = 0;
private TypeIdItem classType;
@ -249,4 +249,8 @@ public class MethodIdItem extends Item<MethodIdItem> {
methodPrototype == other.methodPrototype &&
methodName == other.methodName);
}
public MethodIdItem convert() {
return this;
}
}