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
17package com.android.dexgen.rop;
18
19import com.android.dexgen.rop.cst.CstDouble;
20import com.android.dexgen.rop.cst.CstFloat;
21import com.android.dexgen.rop.cst.CstInteger;
22import com.android.dexgen.rop.cst.CstLong;
23import com.android.dexgen.rop.cst.CstString;
24import com.android.dexgen.rop.cst.TypedConstant;
25
26/**
27 * Attribute class for standard {@code ConstantValue} attributes.
28 */
29public final class AttConstantValue extends BaseAttribute {
30    /** {@code non-null;} attribute name for attributes of this type */
31    public static final String ATTRIBUTE_NAME = "ConstantValue";
32
33    /** {@code non-null;} the constant value */
34    private final TypedConstant constantValue;
35
36    /**
37     * Constructs an instance.
38     *
39     * @param constantValue {@code non-null;} the constant value, which must
40     * be an instance of one of: {@code CstString},
41     * {@code CstInteger}, {@code CstLong},
42     * {@code CstFloat}, or {@code CstDouble}
43     */
44    public AttConstantValue(TypedConstant constantValue) {
45        super(ATTRIBUTE_NAME);
46
47        if (!((constantValue instanceof CstString) ||
48               (constantValue instanceof CstInteger) ||
49               (constantValue instanceof CstLong) ||
50               (constantValue instanceof CstFloat) ||
51               (constantValue instanceof CstDouble))) {
52            if (constantValue == null) {
53                throw new NullPointerException("constantValue == null");
54            }
55            throw new IllegalArgumentException("bad type for constantValue");
56        }
57
58        this.constantValue = constantValue;
59    }
60
61    /** {@inheritDoc} */
62    public int byteLength() {
63        return 8;
64    }
65
66    /**
67     * Gets the constant value of this instance. The returned value
68     * is an instance of one of: {@code CstString},
69     * {@code CstInteger}, {@code CstLong},
70     * {@code CstFloat}, or {@code CstDouble}.
71     *
72     * @return {@code non-null;} the constant value
73     */
74    public TypedConstant getConstantValue() {
75        return constantValue;
76    }
77}
78