/*
 * 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.sql.gen;

import com.facebook.presto.bytecode.Binding;
import com.facebook.presto.bytecode.BytecodeNode;
import com.facebook.presto.bytecode.CallSiteBinder;
import com.facebook.presto.bytecode.MethodGenerationContext;
import com.facebook.presto.bytecode.expression.BytecodeExpression;
import com.facebook.presto.bytecode.instruction.InvokeInstruction;
import com.facebook.presto.common.type.Type;
import io.airlift.slice.Slice;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

import static com.facebook.presto.bytecode.BytecodeUtils.Bootstrap.BOOTSTRAP_METHOD;
import static com.facebook.presto.bytecode.ParameterizedType.type;
import static java.util.Objects.requireNonNull;

public class SqlTypeBytecodeExpression
        extends BytecodeExpression
{
    public static SqlTypeBytecodeExpression constantType(CallSiteBinder callSiteBinder, Type type)
    {
        requireNonNull(callSiteBinder, "callSiteBinder is null");
        requireNonNull(type, "type is null");

        Binding binding = callSiteBinder.bind(type, Type.class);
        return new SqlTypeBytecodeExpression(type, binding, BOOTSTRAP_METHOD);
    }

    private static String generateName(Type type)
    {
        String name = type.getTypeSignature().toString();
        if (name.length() > 20) {
            // Use type base to reduce the identifier size in generated code
            name = type.getTypeSignature().getBase();
        }
        return name.replaceAll("\\W+", "_");
    }

    private final Type type;
    private final Binding binding;
    private final Method bootstrapMethod;

    private SqlTypeBytecodeExpression(Type type, Binding binding, Method bootstrapMethod)
    {
        super(type(Type.class));
        this.type = requireNonNull(type, "type is null");
        this.binding = requireNonNull(binding, "binding is null");
        this.bootstrapMethod = requireNonNull(bootstrapMethod, "bootstrapMethod is null");
    }

    @Override
    public BytecodeNode getBytecode(MethodGenerationContext generationContext)
    {
        return InvokeInstruction.invokeDynamic(generateName(type), binding.getType(), bootstrapMethod, binding.getBindingId());
    }

    @Override
    public List<BytecodeNode> getChildNodes()
    {
        return Collections.emptyList();
    }

    @Override
    protected String formatOneLine()
    {
        return type.getTypeSignature().toString();
    }

    public BytecodeExpression getValue(BytecodeExpression block, BytecodeExpression position)
    {
        Class<?> fromJavaElementType = type.getJavaType();

        if (fromJavaElementType == boolean.class) {
            return invoke("getBoolean", boolean.class, block, position);
        }
        if (fromJavaElementType == long.class) {
            return invoke("getLong", long.class, block, position);
        }
        if (fromJavaElementType == double.class) {
            return invoke("getDouble", double.class, block, position);
        }
        if (fromJavaElementType == Slice.class) {
            return invoke("getSlice", Slice.class, block, position);
        }
        return invoke("getObject", Object.class, block, position).cast(fromJavaElementType);
    }

    public BytecodeExpression writeValue(BytecodeExpression blockBuilder, BytecodeExpression value)
    {
        Class<?> fromJavaElementType = type.getJavaType();

        if (fromJavaElementType == boolean.class) {
            return invoke("writeBoolean", void.class, blockBuilder, value);
        }
        if (fromJavaElementType == long.class) {
            return invoke("writeLong", void.class, blockBuilder, value);
        }
        if (fromJavaElementType == double.class) {
            return invoke("writeDouble", void.class, blockBuilder, value);
        }
        if (fromJavaElementType == Slice.class) {
            return invoke("writeSlice", void.class, blockBuilder, value);
        }
        return invoke("writeObject", void.class, blockBuilder, value.cast(Object.class));
    }
}

@@ -642,58 +642,8 @@
 ng;%0A
-import com.facebook.presto.bytecode.BytecodeNode;%0A
 impo
@@ -730,162 +730,33 @@
 ode.
-MethodGenerationContext;%0Aimport com.facebook.presto.bytecode.expression.BytecodeExpression;%0Aimport com.facebook.presto.bytecode.instruction.InvokeInstruct
+StaticTypeBytecodeExpress
 ion;
@@ -804,39 +804,8 @@
 ype;
-%0Aimport io.airlift.slice.Slice;
 %0A%0Aim
@@ -838,61 +838,8 @@
 hod;
-%0Aimport java.util.Collections;%0Aimport java.util.List;
 %0A%0Aim
@@ -925,75 +925,8 @@
 OD;%0A
-import static com.facebook.presto.bytecode.ParameterizedType.type;%0A
 impo
@@ -1025,16 +1025,26 @@
 extends 
+StaticType
 Bytecode
@@ -1782,2491 +1782,181 @@
 ate 
-final Type type;%0A    private final Binding binding;%0A    private final Method bootstrapMethod;%0A%0A    private SqlTypeBytecodeExpression(Type type, Binding binding, Method bootstrapMethod)%0A    %7B%0A        super(type(Type.class));%0A        this.type = requireNonNull(type, %22type is null%22);%0A        this.binding = requireNonNull(binding, %22binding is null%22);%0A        this.bootstrapMethod = requireNonNull(bootstrapMethod, %22bootstrapMethod is null%22);%0A    %7D%0A%0A    @Override%0A    public BytecodeNode getBytecode(MethodGenerationContext generationContext)%0A    %7B%0A        return InvokeInstruction.invokeDynamic(generateName(type), binding.getType(), bootstrapMethod, binding.getBindingId());%0A    %7D%0A%0A    @Override%0A    public List%3CBytecodeNode%3E getChildNodes()%0A    %7B%0A        return Collections.emptyList();%0A    %7D%0A%0A    @Override%0A    protected String formatOneLine()%0A    %7B%0A        return type.getTypeSignature().toString();%0A    %7D%0A%0A    public BytecodeExpression getValue(BytecodeExpression block, BytecodeExpression position)%0A    %7B%0A        Class%3C?%3E fromJavaElementType = type.getJavaType();%0A%0A        if (fromJavaElementType == boolean.class) %7B%0A            return invoke(%22getBoolean%22, boolean.class, block, position);%0A        %7D%0A        if (fromJavaElementType == long.class) %7B%0A            return invoke(%22getLong%22, long.class, block, position);%0A        %7D%0A        if (fromJavaElementType == double.class) %7B%0A            return invoke(%22getDouble%22, double.class, block, position);%0A        %7D%0A        if (fromJavaElementType == Slice.class) %7B%0A            return invoke(%22getSlice%22, Slice.class, block, position);%0A        %7D%0A        return invoke(%22getObject%22, Object.class, block, position).cast(fromJavaElementType);%0A    %7D%0A%0A    public BytecodeExpression writeValue(BytecodeExpression blockBuilder, BytecodeExpression value)%0A    %7B%0A        Class%3C?%3E fromJavaElementType = type.getJavaType();%0A%0A        if (fromJavaElementType == boolean.class) %7B%0A            return invoke(%22writeBoolean%22, void.class, blockBuilder, value);%0A        %7D%0A        if (fromJavaElementType == long.class) %7B%0A            return invoke(%22writeLong%22, void.class, blockBuilder, value);%0A        %7D%0A        if (fromJavaElementType == double.class) %7B%0A            return invoke(%22writeDouble%22, void.class, blockBuilder, value);%0A        %7D%0A        if (fromJavaElementType == Slice.class) %7B%0A            return invoke(%22writeSlice%22, void.class, blockBuilder, value);%0A        %7D%0A        return invoke(%22writeObject%22, void.class, blockBuilder, value.cast(Object.class
+SqlTypeBytecodeExpression(Type type, Binding binding, Method bootstrapMethod)%0A    %7B%0A        super(binding, bootstrapMethod, Type.class, generateName(type), type.getJavaType(
 ));%0A

Refactor SqlTypeBytecodeExpression