1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2010 Ben Gruver (JesusFreke)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29package org.jf.dexlib.EncodedValue;
30
31import org.jf.dexlib.DexFile;
32import org.jf.dexlib.MethodIdItem;
33import org.jf.dexlib.Util.AnnotatedOutput;
34import org.jf.dexlib.Util.EncodedValueUtils;
35import org.jf.dexlib.Util.Input;
36
37public class MethodEncodedValue extends EncodedValue {
38    public final MethodIdItem value;
39
40    /**
41     * Constructs a new <code>MethodEncodedValue</code> by reading the method index from the given <code>Input</code>
42     * object. The <code>Input</code>'s cursor should be set to the 2nd byte of the encoded value, and the high 3 bits
43     * of the first byte should be passed as the valueArg parameter
44     * @param dexFile The <code>DexFile</code> that is being read in
45     * @param in The <code>Input</code> object to read from
46     * @param valueArg The high 3 bits of the first byte of this encoded value
47     */
48    protected MethodEncodedValue(DexFile dexFile, Input in, byte valueArg) {
49        int index = (int) EncodedValueUtils.decodeUnsignedIntegralValue(in.readBytes(valueArg+1));
50        value = dexFile.MethodIdsSection.getItemByIndex(index);
51    }
52
53    /**
54     * Constructs a new <code>MethodEncodedValue</code> with the given <code>MethodIdItem</code> value
55     * @param value The <code>MethodIdItem</code> value
56     */
57    public MethodEncodedValue(MethodIdItem value) {
58        this.value = value;
59    }
60
61    /** {@inheritDoc} */
62    public void writeValue(AnnotatedOutput out) {
63        byte[] bytes = EncodedValueUtils.encodeUnsignedIntegralValue(value.getIndex());
64
65        if (out.annotates()) {
66            out.annotate(1, "value_type=" + ValueType.VALUE_METHOD.name() + ",value_arg=" + (bytes.length - 1));
67            out.annotate(bytes.length, "value: " + value.getMethodString());
68        }
69
70        out.writeByte(ValueType.VALUE_METHOD.value | ((bytes.length - 1) << 5));
71        out.write(bytes);
72    }
73
74    /** {@inheritDoc} */
75    public int placeValue(int offset) {
76        return offset + EncodedValueUtils.getRequiredBytesForUnsignedIntegralValue(value.getIndex()) + 1;
77    }
78
79    /** {@inheritDoc} */
80    protected int compareValue(EncodedValue o) {
81        MethodEncodedValue other = (MethodEncodedValue)o;
82
83        return value.compareTo(other.value);
84    }
85
86    /** {@inheritDoc} */
87    public ValueType getValueType() {
88        return ValueType.VALUE_METHOD;
89    }
90
91    @Override
92    public int hashCode() {
93        return value.hashCode();
94    }
95}
96