1 /* 2 * Copyright (C) 2007 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.dexgen.rop; 18 19 import com.android.dexgen.util.FixedSizeList; 20 21 /** 22 * Standard implementation of {@link AttributeList}, which directly stores 23 * an array of {@link Attribute} objects and can be made immutable. 24 */ 25 public final class StdAttributeList extends FixedSizeList 26 implements AttributeList { 27 /** 28 * Constructs an instance. All indices initially contain {@code null}. 29 * 30 * @param size the size of the list 31 */ StdAttributeList(int size)32 public StdAttributeList(int size) { 33 super(size); 34 } 35 36 /** {@inheritDoc} */ get(int n)37 public Attribute get(int n) { 38 return (Attribute) get0(n); 39 } 40 41 /** {@inheritDoc} */ byteLength()42 public int byteLength() { 43 int sz = size(); 44 int result = 2; // u2 attributes_count 45 46 for (int i = 0; i < sz; i++) { 47 result += get(i).byteLength(); 48 } 49 50 return result; 51 } 52 53 /** {@inheritDoc} */ findFirst(String name)54 public Attribute findFirst(String name) { 55 int sz = size(); 56 57 for (int i = 0; i < sz; i++) { 58 Attribute att = get(i); 59 if (att.getName().equals(name)) { 60 return att; 61 } 62 } 63 64 return null; 65 } 66 67 /** {@inheritDoc} */ findNext(Attribute attrib)68 public Attribute findNext(Attribute attrib) { 69 int sz = size(); 70 int at; 71 72 outer: { 73 for (at = 0; at < sz; at++) { 74 Attribute att = get(at); 75 if (att == attrib) { 76 break outer; 77 } 78 } 79 80 return null; 81 } 82 83 String name = attrib.getName(); 84 85 for (at++; at < sz; at++) { 86 Attribute att = get(at); 87 if (att.getName().equals(name)) { 88 return att; 89 } 90 } 91 92 return null; 93 } 94 95 /** 96 * Sets the attribute at the given index. 97 * 98 * @param n {@code >= 0, < size();} which attribute 99 * @param attribute {@code null-ok;} the attribute object 100 */ set(int n, Attribute attribute)101 public void set(int n, Attribute attribute) { 102 set0(n, attribute); 103 } 104 } 105