Skip to content

Commit

Permalink
Refactor TryCodeGenerator to use BytecodeExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
haozhun committed Nov 21, 2016
1 parent 4629a69 commit 83aa2fa
Showing 1 changed file with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.sql.relational.CallExpression;
import com.facebook.presto.sql.relational.RowExpression;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Primitives;

import java.lang.invoke.MethodHandle;
Expand All @@ -45,6 +46,7 @@
import static com.facebook.presto.sql.gen.BytecodeUtils.boxPrimitiveIfNecessary;
import static com.facebook.presto.sql.gen.BytecodeUtils.invoke;
import static com.facebook.presto.sql.gen.BytecodeUtils.unboxPrimitiveIfNecessary;
import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
import static com.facebook.presto.util.Reflection.methodHandle;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
Expand Down Expand Up @@ -72,21 +74,15 @@ public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorCon
CallExpression innerCallExpression = (CallExpression) getOnlyElement(arguments);
checkState(tryMethodsMap.containsKey(innerCallExpression), "try methods map does not contain this try call");

BytecodeBlock bytecodeBlock = new BytecodeBlock()
.comment("load required variables")
.getVariable(context.getScope().getVariable("this"));

MethodDefinition definition = tryMethodsMap.get(innerCallExpression);

definition.getParameters().stream()
ImmutableList<Variable> invokeArguments = definition.getParameters().stream()
.map(parameter -> context.getScope().getVariable(parameter.getName()))
.forEach(bytecodeBlock::getVariable);
.collect(toImmutableList());

bytecodeBlock.comment("call dynamic try method: " + definition.getName())
.invokeVirtual(definition)
return new BytecodeBlock()
.append(context.getScope().getThis().invoke(definition, invokeArguments))
.append(unboxPrimitiveIfNecessary(context.getScope(), Primitives.wrap(innerCallExpression.getType().getJavaType())));

return bytecodeBlock;
}

public static MethodDefinition defineTryMethod(
Expand Down

0 comments on commit 83aa2fa

Please sign in to comment.