/* * [The "BSD licence"] * Copyright (c) 2009 Ben Gruver * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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; import org.jf.dexlib.EncodedValue.AnnotationEncodedSubValue; import org.jf.dexlib.Util.ArrayUtils; import org.jf.dexlib.Util.Input; import org.jf.dexlib.Util.AnnotatedOutput; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class AnnotationDirectoryItem extends Item { private AnnotationSetItem classAnnotations; private FieldIdItem[] fieldAnnotationFields; private AnnotationSetItem[] fieldAnnotations; private MethodIdItem[] methodAnnotationMethods; private AnnotationSetItem[] methodAnnotations; private MethodIdItem[] parameterAnnotationMethods; private AnnotationSetRefList[] parameterAnnotations; /** * typically each AnnotationDirectoryItem will have a distinct parent. The only case that isn't true is when * the AnnotationDirectoryItem *only* contains class annotations, with no other type of annotation. In that * case, the same AnnotationDirectoryItem could be referenced from multiple classes. * This isn't a problem though, because this field is only used in compareTo to determine the sort order, * which handles it as a special case */ private ClassDefItem parent = null; /** * Creates a new uninitialized AnnotationDirectoryItem * @param dexFile The DexFile that this item belongs to */ protected AnnotationDirectoryItem(DexFile dexFile) { super(dexFile); } /** * Creates a new AnnotationDirectoryItem with the given values * @param dexFile The DexFile that this item belongs to * @param classAnnotations The annotations associated with the overall class * @param fieldAnnotationFields An array of FieldIdItem objects that the annotations in * fieldAnnotations are associated with * @param fieldAnnotations An array of AnnotationSetItem objects that contain the annotations for the * fields in fieldAnnotationFields * @param methodAnnotationMethods An array of MethodIdItem objects that the annotations in * methodAnnotations are associated with * @param methodAnnotations An array of AnnotationSetItem objects that contain the annotations for the * methods in methodAnnotationMethods * @param parameterAnnotationMethods An array of MethodIdItem objects that the annotations in * parameterAnnotations are associated with * @param parameterAnnotations An array of AnnotationSetRefList objects that contain the parameter * annotations for the methods in parameterAnnotationMethods */ private AnnotationDirectoryItem(DexFile dexFile, AnnotationSetItem classAnnotations, FieldIdItem[] fieldAnnotationFields, AnnotationSetItem[] fieldAnnotations, MethodIdItem[] methodAnnotationMethods, AnnotationSetItem[] methodAnnotations, MethodIdItem[] parameterAnnotationMethods, AnnotationSetRefList[] parameterAnnotations) { super(dexFile); this.classAnnotations = classAnnotations; this.fieldAnnotationFields = fieldAnnotationFields; this.fieldAnnotations = fieldAnnotations; this.methodAnnotationMethods = methodAnnotationMethods; this.methodAnnotations = methodAnnotations; this.parameterAnnotationMethods = parameterAnnotationMethods; this.parameterAnnotations = parameterAnnotations; } /** * Returns an AnnotationDirectoryItem for the given values, and that has been interned into the given * DexFile * @param dexFile The DexFile that this item belongs to * @param classAnnotations The annotations associated with the class * @param fieldAnnotationFields An array of FieldIdItem objects that the annotations in * fieldAnnotations are associated with * @param fieldAnnotations An array of AnnotationSetItem objects that contain the annotations for the * fields in fieldAnnotationFields * @param methodAnnotationMethods An array of MethodIdItem objects that the annotations in * methodAnnotations are associated with * @param methodAnnotations An array of AnnotationSetItem objects that contain the annotations for the * methods in methodAnnotationMethods * @param parameterAnnotationMethods An array of MethodIdItem objects that the annotations in * parameterAnnotations are associated with * @param parameterAnnotations An array of AnnotationSetRefList objects that contain the parameter * annotations for the methods in parameterAnnotationMethods * @return an AnnotationItem for the given values, and that has been interned into the given * DexFile */ public static AnnotationDirectoryItem getInternedAnnotationDirectoryItem(DexFile dexFile, AnnotationSetItem classAnnotations, FieldIdItem[] fieldAnnotationFields, AnnotationSetItem[] fieldAnnotations, MethodIdItem[] methodAnnotationMethods, AnnotationSetItem[] methodAnnotations, MethodIdItem[] parameterAnnotationMethods, AnnotationSetRefList[] parameterAnnotations) { AnnotationDirectoryItem annotationDirectoryItem = new AnnotationDirectoryItem(dexFile, classAnnotations, fieldAnnotationFields, fieldAnnotations, methodAnnotationMethods, methodAnnotations, parameterAnnotationMethods, parameterAnnotations); return dexFile.AnnotationDirectoriesSection.intern(annotationDirectoryItem); } /** {@inheritDoc} */ protected void readItem(Input in, ReadContext readContext) { readContext.getOffsettedItemByOffset(ItemType.TYPE_ANNOTATION_SET_ITEM, in.readInt()); fieldAnnotationFields = new FieldIdItem[in.readInt()]; fieldAnnotations = new AnnotationSetItem[fieldAnnotationFields.length]; methodAnnotationMethods = new MethodIdItem[in.readInt()]; methodAnnotations = new AnnotationSetItem[methodAnnotationMethods.length]; parameterAnnotationMethods = new MethodIdItem[in.readInt()]; parameterAnnotations = new AnnotationSetRefList[parameterAnnotationMethods.length]; for (int i=0; iAnnotationDirectoryItem is internable. It is only internable if it has * only class annotations, but no field, method or parameter annotations */ private boolean isInternable() { return classAnnotations != null && fieldAnnotations.length == 0 && methodAnnotations.length == 0 && parameterAnnotations.length == 0; } /** * Sets the ClassDefItem that this AnnotationDirectoryItem is associated with. * This is only applicable if this AnnotationDirectoryItem contains only class annotations, and no field, method * or parameter annotations. * @param classDefItem the ClassDefItem that this AnnotationDirectoryItem is associated * with */ protected void setParent(ClassDefItem classDefItem) { this.parent = classDefItem; } @Override public int hashCode() { //an instance is only internable if it has only class annotations, but //no other type of annotation if (!isInternable()) { return super.hashCode(); } return classAnnotations.hashCode(); } @Override public boolean equals(Object o) { if (this==o) { return true; } if (o==null || !this.getClass().equals(o.getClass())) { return false; } AnnotationDirectoryItem other = (AnnotationDirectoryItem)o; return (this.compareTo(other) == 0); } }