Home | History | Annotate | Download | only in file
      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.dex.file;
     18 
     19 import com.android.dexgen.rop.cst.CstUtf8;
     20 import com.android.dexgen.util.AnnotatedOutput;
     21 import com.android.dexgen.util.Hex;
     22 
     23 /**
     24  * Representation of a string inside a Dalvik file.
     25  */
     26 public final class StringIdItem
     27         extends IndexedItem implements Comparable {
     28     /** size of instances when written out to a file, in bytes */
     29     public static final int WRITE_SIZE = 4;
     30 
     31     /** {@code non-null;} the string value */
     32     private final CstUtf8 value;
     33 
     34     /** {@code null-ok;} associated string data object, if known */
     35     private StringDataItem data;
     36 
     37     /**
     38      * Constructs an instance.
     39      *
     40      * @param value {@code non-null;} the string value
     41      */
     42     public StringIdItem(CstUtf8 value) {
     43         if (value == null) {
     44             throw new NullPointerException("value == null");
     45         }
     46 
     47         this.value = value;
     48         this.data = null;
     49     }
     50 
     51     /** {@inheritDoc} */
     52     @Override
     53     public boolean equals(Object other) {
     54         if (!(other instanceof StringIdItem)) {
     55             return false;
     56         }
     57 
     58         StringIdItem otherString = (StringIdItem) other;
     59         return value.equals(otherString.value);
     60     }
     61 
     62     /** {@inheritDoc} */
     63     @Override
     64     public int hashCode() {
     65         return value.hashCode();
     66     }
     67 
     68     /** {@inheritDoc} */
     69     public int compareTo(Object other) {
     70         StringIdItem otherString = (StringIdItem) other;
     71         return value.compareTo(otherString.value);
     72     }
     73 
     74     /** {@inheritDoc} */
     75     @Override
     76     public ItemType itemType() {
     77         return ItemType.TYPE_STRING_ID_ITEM;
     78     }
     79 
     80     /** {@inheritDoc} */
     81     @Override
     82     public int writeSize() {
     83         return WRITE_SIZE;
     84     }
     85 
     86     /** {@inheritDoc} */
     87     @Override
     88     public void addContents(DexFile file) {
     89         if (data == null) {
     90             // The string data hasn't yet been added, so add it.
     91             MixedItemSection stringData = file.getStringData();
     92             data = new StringDataItem(value);
     93             stringData.add(data);
     94         }
     95     }
     96 
     97     /** {@inheritDoc} */
     98     @Override
     99     public void writeTo(DexFile file, AnnotatedOutput out) {
    100         int dataOff = data.getAbsoluteOffset();
    101 
    102         if (out.annotates()) {
    103             out.annotate(0, indexString() + ' ' + value.toQuoted(100));
    104             out.annotate(4, "  string_data_off: " + Hex.u4(dataOff));
    105         }
    106 
    107         out.writeInt(dataOff);
    108     }
    109 
    110     /**
    111      * Gets the string value.
    112      *
    113      * @return {@code non-null;} the value
    114      */
    115     public CstUtf8 getValue() {
    116         return value;
    117     }
    118 
    119     /**
    120      * Gets the associated data object for this instance, if known.
    121      *
    122      * @return {@code null-ok;} the associated data object or {@code null}
    123      * if not yet known
    124      */
    125     public StringDataItem getData() {
    126         return data;
    127     }
    128 }
    129