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 static com.facebook.presto.byteCode.expression.ByteCodeExpressionAssertions.assertByteCodeExpression;
 import static com.facebook.presto.byteCode.expression.ByteCodeExpressions.getStatic;
 import static org.testng.Assert.assertEquals;
 
 {
     public static final Object OBJECT_FIELD = "foo";
     public static final byte BYTE_FIELD = 99;
     public static final char CHAR_FIELD = 11;
     public static final short SHORT_FIELD = 22;
     public static final int INT_FIELD = 33;
     public static final long LONG_FIELD = 44;
     public static final float FLOAT_FIELD = 3.3f;
     public static final double DOUBLE_FIELD = 4.4;
 
     @Test
     public void testDownCastObject()
             throws Exception
     {
         assertByteCodeExpression(getStatic(getClass(), "OBJECT_FIELD").cast(String.class).invoke("length"int.class),
                 ((String).length(),
                 "((String) " + getClass().getSimpleName() + ".OBJECT_FIELD).length()");
     }
 
     @Test
     public void testCastPrimitive()
             throws Exception
     {
         assertPrimitiveCast("BYTE_FIELD"byte.class);
         assertPrimitiveCast("BYTE_FIELD"char.class, (char);
         assertPrimitiveCast("BYTE_FIELD"short.class, (short);
         assertPrimitiveCast("BYTE_FIELD"int.class, (int);
         assertPrimitiveCast("BYTE_FIELD"long.class, (long);
         assertPrimitiveCast("BYTE_FIELD"float.class, (float);
         assertPrimitiveCast("BYTE_FIELD"double.class, (double);
 
         assertPrimitiveCast("CHAR_FIELD"byte.class, (byte);
         assertPrimitiveCast("CHAR_FIELD"char.class);
         assertPrimitiveCast("CHAR_FIELD"short.class, (short);
         assertPrimitiveCast("CHAR_FIELD"int.class, (int);
         assertPrimitiveCast("CHAR_FIELD"long.class, (long);
         assertPrimitiveCast("CHAR_FIELD"float.class, (float);
         assertPrimitiveCast("CHAR_FIELD"double.class, (double);
 
         assertPrimitiveCast("SHORT_FIELD"byte.class, (byte);
         assertPrimitiveCast("SHORT_FIELD"char.class, (char);
         assertPrimitiveCast("SHORT_FIELD"short.class);
         assertPrimitiveCast("SHORT_FIELD"int.class, (int);
         assertPrimitiveCast("SHORT_FIELD"long.class, (long);
         assertPrimitiveCast("SHORT_FIELD"float.class, (float);
         assertPrimitiveCast("SHORT_FIELD"double.class, (double);
 
         assertPrimitiveCast("INT_FIELD"byte.class, (byte);
         assertPrimitiveCast("INT_FIELD"char.class, (char);
         assertPrimitiveCast("INT_FIELD"short.class, (short);
         assertPrimitiveCast("INT_FIELD"int.class);
         assertPrimitiveCast("INT_FIELD"long.class, (long);
         assertPrimitiveCast("INT_FIELD"float.class, (float);
         assertPrimitiveCast("INT_FIELD"double.class, (double);
 
         assertPrimitiveCast("LONG_FIELD"byte.class, (byte);
         assertPrimitiveCast("LONG_FIELD"char.class, (char);
         assertPrimitiveCast("LONG_FIELD"short.class, (short);
         assertPrimitiveCast("LONG_FIELD"int.class, (int);
         assertPrimitiveCast("LONG_FIELD"long.class);
         assertPrimitiveCast("LONG_FIELD"float.class, (float);
         assertPrimitiveCast("LONG_FIELD"double.class, (double);
 
         assertPrimitiveCast("FLOAT_FIELD"byte.class, (byte);
         assertPrimitiveCast("FLOAT_FIELD"char.class, (char);
         assertPrimitiveCast("FLOAT_FIELD"short.class, (short);
         assertPrimitiveCast("FLOAT_FIELD"int.class, (int);
         assertPrimitiveCast("FLOAT_FIELD"long.class, (long);
         assertPrimitiveCast("FLOAT_FIELD"float.class);
         assertPrimitiveCast("FLOAT_FIELD"double.class, (double);
 
         assertPrimitiveCast("DOUBLE_FIELD"byte.class, (byte);
         assertPrimitiveCast("DOUBLE_FIELD"char.class, (char);
         assertPrimitiveCast("DOUBLE_FIELD"short.class, (short);
         assertPrimitiveCast("DOUBLE_FIELD"int.class, (int);
         assertPrimitiveCast("DOUBLE_FIELD"long.class, (long);
        assertPrimitiveCast("DOUBLE_FIELD"float.class, (float);
        assertPrimitiveCast("DOUBLE_FIELD"double.class);
    }
    public void assertPrimitiveCast(String fieldNameClass<?> castToTypeObject expected)
            throws Exception
    {
        // simple cast
        ByteCodeExpression baseExpression = getStatic(getClass(), fieldName);
        assertCast(baseExpressionexpectedcastToType);
        // box result
        baseExpression = baseExpression.cast(castToType);
        Class<?> boxedType = Primitives.wrap(castToType);
        assertCast(baseExpressionexpectedboxedType);
        // unbox the boxed result
        baseExpression = baseExpression.cast(boxedType);
        assertCast(baseExpressionexpectedcastToType);
    }
    public static void assertCast(ByteCodeExpression expressionObject expectedValueClass<?> castToType)
            throws Exception
    {
        ByteCodeExpression castExpression = expression.cast(castToType);
        assertByteCodeExpression(castExpressionexpectedValueexpectedCastRendering(expression.toString(), castToType));
        assertEquals(castExpression.getType().getJavaClassName(), castToType.getName());
    }
    public static String expectedCastRendering(String expectedRenderingClass<?> castToType)
    {
        return "((" + castToType.getSimpleName() + ") " + expectedRendering + ")";
    }
New to GrepCode? Check out our FAQ X