Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.byteCode.expression;
 
 
 import java.util.List;
 
 import static com.facebook.presto.byteCode.ParameterizedType.type;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.primitives.Primitives.wrap;
 import static java.lang.String.format;
 
         extends ByteCodeExpression
 {
     private final ByteCodeExpression instance;
 
     {
         super(type);
 
         this. = checkNotNull(instance"instance is null");
 
         checkArgument(type.getPrimitiveType() != void.class"Type %s can not be cast to %s"instance.getType(), type);
 
         // if we have a primitive to object or object to primitive conversion, it must be an exact boxing or unboxing conversion
         if (instance.getType().isPrimitive() != type.isPrimitive()) {
             checkArgument(unwrapPrimitiveType(instance.getType()) == unwrapPrimitiveType(type), "Type %s can not be cast to %s"instance.getType(), type);
         }
     }
 
     @Override
     public ByteCodeNode getByteCode(MethodGenerationContext generationContext)
     {
         Block block = new Block().append(.getByteCode(generationContext));
 
         if (.getType().isPrimitive()) {
             Class<?> sourceType = .getType().getPrimitiveType();
             castPrimitiveToPrimitive(blocksourceTypeunwrapPrimitiveType(getType()));
 
             // insert boxing conversion
             if (!getType().isPrimitive()) {
                 Class<?> primitiveTargetType = unwrapPrimitiveType(getType());
                 return block.invokeStatic(getType(), "valueOf"getType(), type(primitiveTargetType));
             }
 
             return block;
         }
         else if (getType().isPrimitive()) {
             // unbox
             Class<?> targetType = getType().getPrimitiveType();
             return block.invokeVirtual(wrap(targetType), targetType.getSimpleName() + "Value"targetType);
         }
         else {
             block.checkCast(getType());
         }
         return block;
     }
 
     private static Block castPrimitiveToPrimitive(Block blockClass<?> sourceTypeClass<?> targetType)
     {
         if (sourceType == byte.class) {
             if (targetType == byte.class) {
                 return block;
             }
             if (targetType == char.class) {
                 return block;
             }
             if (targetType == short.class) {
                 return block;
             }
             if (targetType == int.class) {
                 return block;
             }
             if (targetType == long.class) {
                 return block.append(.);
             }
             if (targetType == float.class) {
                 return block.append(.);
             }
             if (targetType == double.class) {
                return block.append(.);
            }
        }
        if (sourceType == char.class) {
            if (targetType == byte.class) {
                return block.append(.);
            }
            if (targetType == char.class) {
                return block;
            }
            if (targetType == short.class) {
                return block;
            }
            if (targetType == int.class) {
                return block;
            }
            if (targetType == long.class) {
                return block.append(.);
            }
            if (targetType == float.class) {
                return block.append(.);
            }
            if (targetType == double.class) {
                return block.append(.);
            }
        }
        if (sourceType == short.class) {
            if (targetType == byte.class) {
                return block.append(.);
            }
            if (targetType == char.class) {
                return block.append(.);
            }
            if (targetType == short.class) {
                return block;
            }
            if (targetType == int.class) {
                return block;
            }
            if (targetType == long.class) {
                return block.append(.);
            }
            if (targetType == float.class) {
                return block.append(.);
            }
            if (targetType == double.class) {
                return block.append(.);
            }
        }
        if (sourceType == int.class) {
            if (targetType == boolean.class) {
                return block;
            }
            if (targetType == byte.class) {
                return block.append(.);
            }
            if (targetType == char.class) {
                return block.append(.);
            }
            if (targetType == short.class) {
                return block.append(.);
            }
            if (targetType == int.class) {
                return block;
            }
            if (targetType == long.class) {
                return block.append(.);
            }
            if (targetType == float.class) {
                return block.append(.);
            }
            if (targetType == double.class) {
                return block.append(.);
            }
        }
        if (sourceType == long.class) {
            if (targetType == byte.class) {
                return block.append(.).append(.);
            }
            if (targetType == char.class) {
                return block.append(.).append(.);
            }
            if (targetType == short.class) {
                return block.append(.).append(.);
            }
            if (targetType == int.class) {
                return block.append(.);
            }
            if (targetType == long.class) {
                return block;
            }
            if (targetType == float.class) {
                return block.append(.);
            }
            if (targetType == double.class) {
                return block.append(.);
            }
        }
        if (sourceType == float.class) {
            if (targetType == byte.class) {
                return block.append(.).append(.);
            }
            if (targetType == char.class) {
                return block.append(.).append(.);
            }
            if (targetType == short.class) {
                return block.append(.).append(.);
            }
            if (targetType == int.class) {
                return block.append(.);
            }
            if (targetType == long.class) {
                return block.append(.);
            }
            if (targetType == float.class) {
                return block;
            }
            if (targetType == double.class) {
                return block.append(.);
            }
        }
        if (sourceType == double.class) {
            if (targetType == byte.class) {
                return block.append(.).append(.);
            }
            if (targetType == char.class) {
                return block.append(.).append(.);
            }
            if (targetType == short.class) {
                return block.append(.).append(.);
            }
            if (targetType == int.class) {
                return block.append(.);
            }
            if (targetType == long.class) {
                return block.append(.);
            }
            if (targetType == float.class) {
                return block.append(.);
            }
            if (targetType == double.class) {
                return block;
            }
        }
        throw new IllegalArgumentException(format("Type %s can not be cast to %s"sourceTypetargetType));
    }
    private static Class<?> unwrapPrimitiveType(ParameterizedType type)
    {
        if (type.isPrimitive()) {
            return type.getPrimitiveType();
        }
        switch (type.getJavaClassName()) {
            case "java.lang.Byte":
                return byte.class;
            case "java.lang.Character":
                return char.class;
            case "java.lang.Short":
                return short.class;
            case "java.lang.Integer":
                return int.class;
            case "java.lang.Long":
                return long.class;
            case "java.lang.Float":
                return float.class;
            case "java.lang.Double":
                return double.class;
            default:
                return null;
        }
    }
    @Override
    protected String formatOneLine()
    {
        return "((" + getType().getSimpleName() + ") " +  + ")";
    }
    @Override
    public List<ByteCodeNodegetChildNodes()
    {
        return ImmutableList.<ByteCodeNode>of();
    }
New to GrepCode? Check out our FAQ X