Home | History | Annotate | Download | only in io
      1 /*
      2  * Copyright (C) 2011 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.io;
     18 
     19 import com.android.dx.util.Unsigned;
     20 
     21 /**
     22  * An annotation.
     23  */
     24 public final class Annotation implements Comparable<Annotation> {
     25     private final DexBuffer buffer;
     26     private final byte visibility;
     27     private final int typeIndex;
     28     private final int[] names;
     29     private final EncodedValue[] values;
     30 
     31     public Annotation(DexBuffer buffer, byte visibility, int typeIndex, int[] names,
     32             EncodedValue[] values) {
     33         this.buffer = buffer;
     34         this.visibility = visibility;
     35         this.typeIndex = typeIndex;
     36         this.names = names;
     37         this.values = values;
     38     }
     39 
     40     public byte getVisibility() {
     41         return visibility;
     42     }
     43 
     44     public int getTypeIndex() {
     45         return typeIndex;
     46     }
     47 
     48     public int[] getNames() {
     49         return names;
     50     }
     51 
     52     public EncodedValue[] getValues() {
     53         return values;
     54     }
     55 
     56     public void writeTo(DexBuffer.Section out) {
     57         out.writeByte(visibility);
     58         out.writeUleb128(typeIndex);
     59         out.writeUleb128(names.length);
     60         for (int i = 0; i < names.length; i++) {
     61             out.writeUleb128(names[i]);
     62             values[i].writeTo(out);
     63         }
     64     }
     65 
     66     @Override public int compareTo(Annotation other) {
     67         if (typeIndex != other.typeIndex) {
     68             return Unsigned.compare(typeIndex, other.typeIndex);
     69         }
     70         int size = Math.min(names.length, other.names.length);
     71         for (int i = 0; i < size; i++) {
     72             if (names[i] != other.names[i]) {
     73                 return Unsigned.compare(names[i], other.names[i]);
     74             }
     75             int compare = values[i].compareTo(other.values[i]);
     76             if (compare != 0) {
     77                 return compare;
     78             }
     79         }
     80         return names.length - other.names.length;
     81     }
     82 
     83     @Override public String toString() {
     84         if (buffer == null) {
     85             return visibility + " " + typeIndex;
     86         }
     87 
     88         StringBuilder result = new StringBuilder();
     89         result.append(visibility);
     90         result.append(" ");
     91         result.append(buffer.typeNames().get(typeIndex));
     92         result.append("[");
     93         for (int i = 0; i < names.length; i++) {
     94             if (i > 0) {
     95                 result.append(", ");
     96             }
     97             result.append(buffer.strings().get(names[i]));
     98             result.append("=");
     99             result.append(values[i]);
    100         }
    101         result.append("]");
    102         return result.toString();
    103     }
    104 }
    105