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.CstDouble;
20 import com.android.dx.rop.cst.CstFloat;
21 import com.android.dx.rop.cst.CstInteger;
22 import com.android.dx.rop.cst.CstLong;
23 import com.android.dx.rop.cst.CstString;
24 import com.android.dx.rop.cst.TypedConstant;
25 
26 /**
27  * Attribute class for standard {@code ConstantValue} attributes.
28  */
29 public 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      */
AttConstantValue(TypedConstant constantValue)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     @Override
byteLength()63     public int byteLength() {
64         return 8;
65     }
66 
67     /**
68      * Gets the constant value of this instance. The returned value
69      * is an instance of one of: {@code CstString},
70      * {@code CstInteger}, {@code CstLong},
71      * {@code CstFloat}, or {@code CstDouble}.
72      *
73      * @return {@code non-null;} the constant value
74      */
getConstantValue()75     public TypedConstant getConstantValue() {
76         return constantValue;
77     }
78 }
79