diff --git a/presto-main/src/main/java/com/facebook/presto/byteCode/expression/ByteCodeExpression.java b/presto-main/src/main/java/com/facebook/presto/byteCode/expression/ByteCodeExpression.java index fea9c41b293c..be531603784e 100644 --- a/presto-main/src/main/java/com/facebook/presto/byteCode/expression/ByteCodeExpression.java +++ b/presto-main/src/main/java/com/facebook/presto/byteCode/expression/ByteCodeExpression.java @@ -216,4 +216,9 @@ public final T accept(ByteCodeNode parent, ByteCodeVisitor visitor) { return visitor.visitByteCodeExpression(parent, this); } + + public ByteCodeExpression instanceOf(Class type) + { + return InstanceOfByteCodeExpression.instanceOf(this, type); + } } diff --git a/presto-main/src/main/java/com/facebook/presto/byteCode/expression/InstanceOfByteCodeExpression.java b/presto-main/src/main/java/com/facebook/presto/byteCode/expression/InstanceOfByteCodeExpression.java new file mode 100644 index 000000000000..5dd3ec208062 --- /dev/null +++ b/presto-main/src/main/java/com/facebook/presto/byteCode/expression/InstanceOfByteCodeExpression.java @@ -0,0 +1,64 @@ +/* + * 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 com.facebook.presto.byteCode.ByteCodeBlock; +import com.facebook.presto.byteCode.ByteCodeNode; +import com.facebook.presto.byteCode.MethodGenerationContext; +import com.google.common.collect.ImmutableList; + +import java.util.List; + +import static com.facebook.presto.byteCode.ParameterizedType.type; +import static java.util.Objects.requireNonNull; + +class InstanceOfByteCodeExpression + extends ByteCodeExpression +{ + private final ByteCodeExpression instance; + private final Class type; + + public InstanceOfByteCodeExpression(ByteCodeExpression instance, Class type) + { + super(type(boolean.class)); + + this.instance = requireNonNull(instance, "instance is null"); + this.type = requireNonNull(type, "type is null"); + } + + public static ByteCodeExpression instanceOf(ByteCodeExpression instance, Class type) + { + return new InstanceOfByteCodeExpression(instance, type); + } + + @Override + public ByteCodeNode getByteCode(MethodGenerationContext generationContext) + { + return new ByteCodeBlock() + .append(instance) + .isInstanceOf(type); + } + + @Override + protected String formatOneLine() + { + return instance + " instanceof " + type; + } + + @Override + public List getChildNodes() + { + return ImmutableList.of(); + } +}