Constants.java revision 579d7739c53a2707ad711a2d2cae46d7d782f061
1/*
2 * Copyright (C) 2011 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.google.dexmaker;
18
19import com.android.dx.rop.cst.CstBoolean;
20import com.android.dx.rop.cst.CstByte;
21import com.android.dx.rop.cst.CstChar;
22import com.android.dx.rop.cst.CstDouble;
23import com.android.dx.rop.cst.CstFloat;
24import com.android.dx.rop.cst.CstInteger;
25import com.android.dx.rop.cst.CstKnownNull;
26import com.android.dx.rop.cst.CstLong;
27import com.android.dx.rop.cst.CstShort;
28import com.android.dx.rop.cst.CstString;
29import com.android.dx.rop.cst.CstType;
30import com.android.dx.rop.cst.TypedConstant;
31
32/**
33 * Factory for rop constants.
34 */
35final class Constants {
36    private Constants() {}
37
38    /**
39     * Returns a rop constant for the specified value.
40     *
41     * @param value null, a boxed primitive, String, Class, or Type.
42     */
43    static TypedConstant getConstant(Object value) {
44        if (value == null) {
45            return CstKnownNull.THE_ONE;
46        } else if (value instanceof Boolean) {
47            return CstBoolean.make((Boolean) value);
48        } else if (value instanceof Byte) {
49            return CstByte.make((Byte) value);
50        } else if (value instanceof Character) {
51            return CstChar.make((Character) value);
52        } else if (value instanceof Double) {
53            return CstDouble.make(Double.doubleToLongBits((Double) value));
54        } else if (value instanceof Float) {
55            return CstFloat.make(Float.floatToIntBits((Float) value));
56        } else if (value instanceof Integer) {
57            return CstInteger.make((Integer) value);
58        } else if (value instanceof Long) {
59            return CstLong.make((Long) value);
60        } else if (value instanceof Short) {
61            return CstShort.make((Short) value);
62        } else if (value instanceof String) {
63            return new CstString((String) value);
64        } else if (value instanceof Class) {
65            return new CstType(Type.get((Class<?>) value).ropType);
66        } else if (value instanceof Type) {
67            return new CstType(((Type) value).ropType);
68        } else {
69            throw new UnsupportedOperationException("Not a constant: " + value);
70        }
71    }
72}
73