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.ByteCodeExpressions.constantInt;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.Iterables.transform;

A ByteCodeExpression is chain of Java like expressions that results in at most a single value being pushed on the stack. The chain starts with a constant, local variable, static field, static method or invoke dynamic followed followed by zero or more invocations, field dereferences, array element fetches, or casts. The expression can optionally be terminated by a set expression, and in this case no value is pushed on the stack. A ByteCodeExpression is a ByteCodeNode so it works with tools like tree dump. This abstraction makes it easy to write generic byte code generators that can work with data that may come from a parameter, field or the result of a method invocation.
 
 public abstract class ByteCodeExpression
         implements ByteCodeNode
 {
     private final ParameterizedType type;
 
     protected ByteCodeExpression(ParameterizedType type)
     {
         this. = checkNotNull(type"type is null");
     }
 
     public final ParameterizedType getType()
     {
         return ;
     }
 
     public abstract ByteCodeNode getByteCode();
 
     protected abstract String formatOneLine();
 
     @Override
     public final String toString()
     {
         return formatOneLine() + (.getPrimitiveType() == void.class ? ";" : "");
     }
 
     public final ByteCodeExpression getField(Class<?> declaringClassString name)
     {
         return new GetFieldByteCodeExpression(thisdeclaringClassname);
     }
 
     public final ByteCodeExpression getField(String nameClass<?> type)
     {
         return new GetFieldByteCodeExpression(thisthis.getType(), nametype(type));
     }
 
     public final ByteCodeExpression getField(Field field)
     {
         return new GetFieldByteCodeExpression(thisfield);
     }
 
     public final ByteCodeExpression getField(FieldDefinition field)
     {
         return new GetFieldByteCodeExpression(thisfield);
     }
 
     public final ByteCodeExpression getField(ParameterizedType declaringClassString nameParameterizedType type)
     {
         return new GetFieldByteCodeExpression(thisdeclaringClassnametype);
     }
 
     public final ByteCodeExpression setField(String nameByteCodeExpression value)
     {
         return new SetFieldByteCodeExpression(thisthis.getType(), namevalue);
     }
 
     public final ByteCodeExpression setField(Field fieldByteCodeExpression value)
    {
        return new SetFieldByteCodeExpression(thisfieldvalue);
    }
    public final ByteCodeExpression setField(FieldDefinition fieldByteCodeExpression value)
    {
        return new SetFieldByteCodeExpression(thisfieldvalue);
    }
    public final ByteCodeExpression cast(Class<?> type)
    {
        return new CastByteCodeExpression(thistype(type));
    }
    public final ByteCodeExpression cast(ParameterizedType type)
    {
        return new CastByteCodeExpression(thistype);
    }
    public final ByteCodeExpression invoke(String methodNameClass<?> returnTypeByteCodeExpression... parameters)
    {
        return invoke(methodNametype(returnType), ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public final ByteCodeExpression invoke(String methodNameClass<?> returnTypeIterable<? extends ByteCodeExpressionparameters)
    {
        return invoke(methodNametype(returnType), parameters);
    }
    public final ByteCodeExpression invoke(String methodNameParameterizedType returnTypeIterable<? extends ByteCodeExpressionparameters)
    {
        checkNotNull(parameters"parameters is null");
        return invoke(methodName,
                returnType,
                ImmutableList.copyOf(transform(parameters, ByteCodeExpression::getType)),
                parameters);
    }
    public final ByteCodeExpression invoke(String methodNameClass<?> returnTypeIterable<? extends Class<?>> parameterTypesByteCodeExpression... parameters)
    {
        return invoke(methodNametype(returnType), transform(parameterTypes, ParameterizedType::type), ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public final ByteCodeExpression invoke(String methodNameParameterizedType returnTypeIterable<ParameterizedTypeparameterTypesByteCodeExpression... parameters)
    {
        return invoke(methodNamereturnTypeparameterTypes, ImmutableList.copyOf(checkNotNull(parameters"parameters is null")));
    }
    public final ByteCodeExpression invoke(
            String methodName,
            ParameterizedType returnType,
            Iterable<ParameterizedTypeparameterTypes,
            Iterable<? extends ByteCodeExpressionparameters)
    {
        return InvokeByteCodeExpression.createInvoke(
                this,
                methodName,
                returnType,
                parameterTypes,
                parameters);
    }
    public final ByteCodeExpression getElement(int index)
    {
        return new GetElementByteCodeExpression(thisconstantInt(index));
    }
    public final ByteCodeExpression getElement(ByteCodeExpression index)
    {
        return new GetElementByteCodeExpression(thisindex);
    }
    public final ByteCodeExpression ret()
    {
        return new ReturnByteCodeExpression(this);
    }
    public final ByteCodeExpression pop()
    {
        if (this.getType().getPrimitiveType() == void.class) {
            return this;
        }
        return new PopByteCodeExpression(this);
    }
    @Override
    public final void accept(MethodVisitor visitor)
    {
        getByteCode().accept(visitor);
    }
    @Override
    public final <T> T accept(ByteCodeNode parentByteCodeVisitor<T> visitor)
    {
        return visitor.visitByteCodeExpression(parentthis);
    }
New to GrepCode? Check out our FAQ X