Home | History | Annotate | Download | only in attrib
      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.dx.cf.attrib;
     18 
     19 import com.android.dx.rop.cst.ConstantPool;
     20 import com.android.dx.util.ByteArray;
     21 
     22 /**
     23  * Raw attribute, for holding onto attributes that are unrecognized.
     24  */
     25 public final class RawAttribute extends BaseAttribute {
     26     /** {@code non-null;} attribute data */
     27     private final ByteArray data;
     28 
     29     /**
     30      * {@code null-ok;} constant pool to use for resolution of cpis in {@link
     31      * #data}
     32      */
     33     private final ConstantPool pool;
     34 
     35     /**
     36      * Constructs an instance.
     37      *
     38      * @param name {@code non-null;} attribute name
     39      * @param data {@code non-null;} attribute data
     40      * @param pool {@code null-ok;} constant pool to use for cpi resolution
     41      */
     42     public RawAttribute(String name, ByteArray data, ConstantPool pool) {
     43         super(name);
     44 
     45         if (data == null) {
     46             throw new NullPointerException("data == null");
     47         }
     48 
     49         this.data = data;
     50         this.pool = pool;
     51     }
     52 
     53     /**
     54      * Constructs an instance from a sub-array of a {@link ByteArray}.
     55      *
     56      * @param name {@code non-null;} attribute name
     57      * @param data {@code non-null;} array containing the attribute data
     58      * @param offset offset in {@code data} to the attribute data
     59      * @param length length of the attribute data, in bytes
     60      * @param pool {@code null-ok;} constant pool to use for cpi resolution
     61      */
     62     public RawAttribute(String name, ByteArray data, int offset,
     63                         int length, ConstantPool pool) {
     64         this(name, data.slice(offset, offset + length), pool);
     65     }
     66 
     67     /**
     68      * Get the raw data of the attribute.
     69      *
     70      * @return {@code non-null;} the data
     71      */
     72     public ByteArray getData() {
     73         return data;
     74     }
     75 
     76     /** {@inheritDoc} */
     77     public int byteLength() {
     78         return data.size() + 6;
     79     }
     80 
     81     /**
     82      * Gets the constant pool to use for cpi resolution, if any. It
     83      * presumably came from the class file that this attribute came
     84      * from.
     85      *
     86      * @return {@code null-ok;} the constant pool
     87      */
     88     public ConstantPool getPool() {
     89         return pool;
     90     }
     91 }
     92