Home | History | Annotate | Download | only in gen
      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 
     17 package com.android.dx.gen;
     18 
     19 import com.android.dx.rop.cst.CstBoolean;
     20 import com.android.dx.rop.cst.CstByte;
     21 import com.android.dx.rop.cst.CstChar;
     22 import com.android.dx.rop.cst.CstDouble;
     23 import com.android.dx.rop.cst.CstFloat;
     24 import com.android.dx.rop.cst.CstInteger;
     25 import com.android.dx.rop.cst.CstKnownNull;
     26 import com.android.dx.rop.cst.CstLong;
     27 import com.android.dx.rop.cst.CstShort;
     28 import com.android.dx.rop.cst.CstString;
     29 import com.android.dx.rop.cst.CstType;
     30 import com.android.dx.rop.cst.TypedConstant;
     31 
     32 /**
     33  * Factory for rop constants.
     34  */
     35 final 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