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.ParameterizedType.type;
 import static com.facebook.presto.byteCode.expression.ArithmeticByteCodeExpression.createArithmeticByteCodeExpression;
 import static com.facebook.presto.byteCode.instruction.Constant.loadBoolean;
 import static com.facebook.presto.byteCode.instruction.Constant.loadClass;
 import static com.facebook.presto.byteCode.instruction.Constant.loadDouble;
 import static com.facebook.presto.byteCode.instruction.Constant.loadFloat;
 import static com.facebook.presto.byteCode.instruction.Constant.loadInt;
 import static com.facebook.presto.byteCode.instruction.Constant.loadLong;
 import static com.facebook.presto.byteCode.instruction.Constant.loadNull;
 import static com.facebook.presto.byteCode.instruction.Constant.loadString;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.Iterables.transform;
 import static java.util.Objects.requireNonNull;
 
 public final class ByteCodeExpressions
 {
     private ByteCodeExpressions()
     {
     }
 
     //
     // Constants
     //
 
     public static ByteCodeExpression constantTrue()
     {
         return new ConstantByteCodeExpression(boolean.classloadBoolean(true));
     }
 
     public static ByteCodeExpression constantFalse()
     {
         return new ConstantByteCodeExpression(boolean.classloadBoolean(false));
     }
 
     public static ByteCodeExpression constantBoolean(boolean value)
     {
         return new ConstantByteCodeExpression(boolean.classloadBoolean(value));
     }
 
     public static ByteCodeExpression constantClass(Class<?> value)
     {
         return new ConstantByteCodeExpression(Class.classloadClass(value));
     }
 
     public static ByteCodeExpression constantClass(ParameterizedType value)
     {
         return new ConstantByteCodeExpression(Class.classloadClass(value));
     }
 
     public static ByteCodeExpression constantDouble(double value)
     {
         return new ConstantByteCodeExpression(double.classloadDouble(value));
     }
 
     public static ByteCodeExpression constantFloat(float value)
     {
         return new ConstantByteCodeExpression(float.classloadFloat(value));
     }
 
     public static ByteCodeExpression constantInt(int value)
     {
         return new ConstantByteCodeExpression(int.classloadInt(value));
     }
 
     public static ByteCodeExpression constantLong(long value)
     {
         return new ConstantByteCodeExpression(long.classloadLong(value));
     }
 
     public static ByteCodeExpression constantNumber(Number value)
     {
         if (value instanceof Byte) {
             return constantInt((value).intValue()).cast(byte.class);
         }
        if (value instanceof Short) {
            return constantInt((value).intValue()).cast(short.class);
        }
        if (value instanceof Integer) {
            return constantInt((Integervalue);
        }
        if (value instanceof Long) {
            return constantLong((Longvalue);
        }
        if (value instanceof Float) {
            return constantFloat((Floatvalue);
        }
        if (value instanceof Double) {
            return constantDouble((Doublevalue);
        }
        throw new IllegalStateException("Unsupported number type " + value.getClass().getSimpleName());
    }
    public static ByteCodeExpression constantNull(Class<?> type)
    {
        return new ConstantByteCodeExpression(typeloadNull());
    }
    public static ByteCodeExpression constantNull(ParameterizedType type)
    {
        return new ConstantByteCodeExpression(typeloadNull());
    }
    public static ByteCodeExpression constantString(String value)
    {
        return new ConstantByteCodeExpression(String.classloadString(value));
    }
    public static ByteCodeExpression defaultValue(Class<?> type)
    {
        requireNonNull(type"type is null");
        if (type == boolean.class) {
            return constantInt(0).cast(boolean.class);
        }
        if (type == byte.class) {
            return constantInt(0).cast(byte.class);
        }
        if (type == int.class) {
            return constantInt(0);
        }
        if (type == short.class) {
            return constantInt(0).cast(short.class);
        }
        if (type == long.class) {
            return constantLong(0L);
        }
        if (type == float.class) {
            return constantFloat(0.0f);
        }
        if (type == double.class) {
            return constantDouble(0.0d);
        }
        checkArgument(!type.isPrimitive(), "Unsupported type %s"type);
        return constantNull(type);
    }
    //
    // Get static field
    //
    public static ByteCodeExpression getStatic(Class<?> declaringClassString name)
    {
        return new GetFieldByteCodeExpression(nulldeclaringClassname);
    }
    public static ByteCodeExpression getStatic(Field staticField)
    {
        return new GetFieldByteCodeExpression(nullstaticField);
    }
    public static ByteCodeExpression getStatic(FieldDefinition staticField)
    {
        return new GetFieldByteCodeExpression(nullstaticField);
    }
    public static ByteCodeExpression getStatic(ParameterizedType declaringClassString nameParameterizedType type)
    {
        return new GetFieldByteCodeExpression(nulldeclaringClassnametype);
    }
    //
    // Set static field
    //
    public static ByteCodeExpression setStatic(Class<?> declaringClassString nameByteCodeExpression value)
    {
        return new SetFieldByteCodeExpression(nulldeclaringClassnamevalue);
    }
    public static ByteCodeExpression setStatic(Field staticFieldByteCodeExpression value)
    {
        return new SetFieldByteCodeExpression(nullstaticFieldvalue);
    }
    public static ByteCodeExpression setStatic(FieldDefinition staticFieldByteCodeExpression value)
    {
        return new SetFieldByteCodeExpression(nullstaticFieldvalue);
    }
    public static ByteCodeExpression setStatic(ParameterizedType declaringClassString nameByteCodeExpression value)
    {
        return new SetFieldByteCodeExpression(nulldeclaringClassnamevalue);
    }
    //
    // New instance
    //
    public static ByteCodeExpression newInstance(Class<?> returnTypeByteCodeExpression... parameters)
    {
        return newInstance(type(returnType), ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public static ByteCodeExpression newInstance(Class<?> returnTypeIterable<? extends ByteCodeExpressionparameters)
    {
        return newInstance(type(returnType), parameters);
    }
    public static ByteCodeExpression newInstance(ParameterizedType returnTypeByteCodeExpression... parameters)
    {
        checkNotNull(parameters"parameters is null");
        return newInstance(returnType, ImmutableList.copyOf(parameters));
    }
    public static ByteCodeExpression newInstance(ParameterizedType returnTypeIterable<? extends ByteCodeExpressionparameters)
    {
        checkNotNull(parameters"parameters is null");
        return newInstance(
                returnType,
                ImmutableList.copyOf(transform(parameters, ByteCodeExpression::getType)),
                parameters);
    }
    public static ByteCodeExpression newInstance(Class<?> returnTypeIterable<? extends Class<?>> parameterTypesByteCodeExpression... parameters)
    {
        return newInstance(type(returnType), transform(parameterTypes, ParameterizedType::type), ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public static ByteCodeExpression newInstance(ParameterizedType returnTypeIterable<ParameterizedTypeparameterTypesByteCodeExpression... parameters)
    {
        return newInstance(returnTypeparameterTypes, ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public static ByteCodeExpression newInstance(
            ParameterizedType type,
            Iterable<ParameterizedTypeparameterTypes,
            Iterable<? extends ByteCodeExpressionparameters)
    {
        return new NewInstanceByteCodeExpression(typeparameterTypesparameters);
    }
    //
    // Invoke static method
    //
    public static ByteCodeExpression invokeStatic(Method method,  ByteCodeExpression... parameters)
    {
        return invokeStatic(method, ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public static ByteCodeExpression invokeStatic(Method method,  Iterable<? extends ByteCodeExpressionparameters)
    {
        return invokeStatic(method.getDeclaringClass(), method.getName(), method.getReturnType(), parameters);
    }
    public static ByteCodeExpression invokeStatic(Class<?> methodTargetTypeString methodNameClass<?> returnTypeByteCodeExpression... parameters)
    {
        return invokeStatic(methodTargetTypemethodNamereturnType, ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public static ByteCodeExpression invokeStatic(
            Class<?> methodTargetType,
            String methodName,
            Class<?> returnType,
            Iterable<? extends ByteCodeExpressionparameters)
    {
        checkNotNull(methodTargetType"methodTargetType is null");
        checkNotNull(returnType"returnType is null");
        checkNotNull(parameters"parameters is null");
        return invokeStatic(
                type(methodTargetType),
                methodName,
                type(returnType),
                ImmutableList.copyOf(transform(parameters, ByteCodeExpression::getType)),
                parameters);
    }
    public static ByteCodeExpression invokeStatic(
            Class<?> methodTargetType,
            String methodName,
            Class<?> returnType,
            Iterable<? extends Class<?>> parameterTypes,
            ByteCodeExpression... parameters)
    {
        checkNotNull(methodTargetType"methodTargetType is null");
        checkNotNull(returnType"returnType is null");
        checkNotNull(parameterTypes"parameterTypes is null");
        checkNotNull(parameters"parameters is null");
        return invokeStatic(
                type(methodTargetType),
                methodName,
                type(returnType),
                transform(parameterTypes, ParameterizedType::type),
                ImmutableList.copyOf(parameters));
    }
    public static ByteCodeExpression invokeStatic(
            ParameterizedType methodTargetType,
            String methodName,
            ParameterizedType returnType,
            Iterable<ParameterizedTypeparameterTypes,
            ByteCodeExpression... parameters)
    {
        return invokeStatic(methodTargetTypemethodNamereturnTypeparameterTypes, ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public static ByteCodeExpression invokeStatic(
            ParameterizedType methodTargetType,
            String methodName,
            ParameterizedType returnType,
            Iterable<ParameterizedTypeparameterTypes,
            Iterable<? extends ByteCodeExpressionparameters)
    {
        return new InvokeByteCodeExpression(
                null,
                methodTargetType,
                methodName,
                returnType,
                parameterTypes,
                parameters);
    }
    //
    // Invoke dynamic
    //
    public static ByteCodeExpression invokeDynamic(
            Method bootstrapMethod,
            Iterable<? extends ObjectbootstrapArgs,
            String methodName,
            Class<?> returnType,
            ByteCodeExpression... parameters)
    {
        return invokeDynamic(bootstrapMethodbootstrapArgsmethodNamereturnType, ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public static ByteCodeExpression invokeDynamic(
            Method bootstrapMethod,
            Iterable<? extends ObjectbootstrapArgs,
            String methodName,
            Class<?> returnType,
            Iterable<? extends ByteCodeExpressionparameters)
    {
        checkNotNull(returnType"returnType is null");
        checkNotNull(parameters"parameters is null");
        return invokeDynamic(
                bootstrapMethod,
                bootstrapArgs,
                methodName,
                type(returnType),
                ImmutableList.copyOf(transform(parameters, ByteCodeExpression::getType)),
                parameters);
    }
    public static ByteCodeExpression invokeDynamic(
            Method bootstrapMethod,
            Iterable<? extends ObjectbootstrapArgs,
            String methodName,
            ParameterizedType returnType,
            ByteCodeExpression... parameters)
    {
        return invokeDynamic(bootstrapMethodbootstrapArgsmethodNamereturnType, ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public static ByteCodeExpression invokeDynamic(
            Method bootstrapMethod,
            Iterable<? extends ObjectbootstrapArgs,
            String methodName,
            ParameterizedType returnType,
            Iterable<? extends ByteCodeExpressionparameters)
    {
        checkNotNull(returnType"returnType is null");
        checkNotNull(parameters"parameters is null");
        return invokeDynamic(
                bootstrapMethod,
                bootstrapArgs,
                methodName,
                returnType,
                ImmutableList.copyOf(transform(parameters, ByteCodeExpression::getType)),
                parameters);
    }
    public static ByteCodeExpression invokeDynamic(
            Method bootstrapMethod,
            Iterable<? extends ObjectbootstrapArgs,
            String methodName,
            MethodType methodType,
            ByteCodeExpression... parameters)
    {
        checkNotNull(methodType"methodType is null");
        checkNotNull(parameters"parameters is null");
        return invokeDynamic(bootstrapMethodbootstrapArgsmethodNamemethodType, ImmutableList.copyOf(parameters));
    }
    public static ByteCodeExpression invokeDynamic(
            Method bootstrapMethod,
            Iterable<? extends ObjectbootstrapArgs,
            String methodName,
            MethodType methodType,
            Iterable<? extends ByteCodeExpressionparameters)
    {
        return invokeDynamic(
                bootstrapMethod,
                bootstrapArgs,
                methodName,
                type(methodType.returnType()),
                transform(methodType.parameterList(), ParameterizedType::type),
                ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public static ByteCodeExpression invokeDynamic(
            Method bootstrapMethod,
            Iterable<? extends ObjectbootstrapArgs,
            String methodName,
            ParameterizedType returnType,
            Iterable<ParameterizedTypeparameterTypes,
            Iterable<? extends ByteCodeExpressionparameters)
    {
        return new InvokeDynamicByteCodeExpression(
                bootstrapMethod,
                bootstrapArgs,
                methodName,
                returnType,
                parameters,
                parameterTypes);
    }
    //
    // Arithmetic operations
    //
    public static ByteCodeExpression add(ByteCodeExpression leftByteCodeExpression right)
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    public static ByteCodeExpression subtract(ByteCodeExpression leftByteCodeExpression right)
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    public static ByteCodeExpression multiply(ByteCodeExpression leftByteCodeExpression right)
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    public static ByteCodeExpression divide(ByteCodeExpression leftByteCodeExpression right)
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    public static ByteCodeExpression remainder(ByteCodeExpression leftByteCodeExpression right)
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    public static ByteCodeExpression bitwiseOr(ByteCodeExpression leftByteCodeExpression right)
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    public static ByteCodeExpression shiftLeft(ByteCodeExpression leftByteCodeExpression right)
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    {
        return createArithmeticByteCodeExpression(.leftright);
    }
    public static ByteCodeExpression negate(ByteCodeExpression value)
    {
        return new NegateByteCodeExpression(value);
    }
    //
    // Comparison operations
    //
    public static ByteCodeExpression lessThan(ByteCodeExpression leftByteCodeExpression right)
    {
        return ComparisonByteCodeExpression.lessThan(leftright);
    }
    {
        return ComparisonByteCodeExpression.greaterThan(leftright);
    }
    {
        return ComparisonByteCodeExpression.lessThanOrEqual(leftright);
    }
    {
        return ComparisonByteCodeExpression.greaterThanOrEqual(leftright);
    }
    public static ByteCodeExpression equal(ByteCodeExpression leftByteCodeExpression right)
    {
        return ComparisonByteCodeExpression.equal(leftright);
    }
    public static ByteCodeExpression notEqual(ByteCodeExpression leftByteCodeExpression right)
    {
        return ComparisonByteCodeExpression.notEqual(leftright);
    }
    //
    // Logical binary operations
    //
    public static ByteCodeExpression and(ByteCodeExpression leftByteCodeExpression right)
    {
        return new AndByteCodeExpression(leftright);
    }
    public static ByteCodeExpression or(ByteCodeExpression leftByteCodeExpression right)
    {
        return new OrByteCodeExpression(leftright);
    }
    public static ByteCodeExpression not(ByteCodeExpression value)
    {
        return new NotByteCodeExpression(value);
    }
    //
    // Complex expressions
    //
    public static ByteCodeExpression inlineIf(ByteCodeExpression conditionByteCodeExpression ifTrueByteCodeExpression ifFalse)
    {
        return new InlineIfByteCodeExpression(conditionifTrueifFalse);
    }
New to GrepCode? Check out our FAQ X