Skip to content

Commit

Permalink
Add byte code expression for instance of
Browse files Browse the repository at this point in the history
  • Loading branch information
nileema committed Dec 3, 2015
1 parent 19a8604 commit 0141317
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,9 @@ public final <T> T accept(ByteCodeNode parent, ByteCodeVisitor<T> visitor)
{
return visitor.visitByteCodeExpression(parent, this);
}

public ByteCodeExpression instanceOf(Class<?> type)
{
return InstanceOfByteCodeExpression.instanceOf(this, type);
}
}
Original file line number Diff line number Diff line change
@@ -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<ByteCodeNode> getChildNodes()
{
return ImmutableList.of();
}
}

0 comments on commit 0141317

Please sign in to comment.