content: /*
   * 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 io.airlift.bytecode.expression;
  
  import com.google.common.base.Joiner;
  import com.google.common.collect.ImmutableList;
  import io.airlift.bytecode.BytecodeBlock;
  import io.airlift.bytecode.BytecodeNode;
  import io.airlift.bytecode.MethodGenerationContext;
  import io.airlift.bytecode.ParameterizedType;
  import io.airlift.bytecode.instruction.TypeInstruction;
  
  import javax.annotation.Nullable;
  
  import java.util.List;
  
  import static com.google.common.base.Preconditions.checkArgument;
  import static io.airlift.bytecode.ArrayOpCode.getArrayOpCode;
  import static io.airlift.bytecode.expression.BytecodeExpressions.constantInt;
  import static java.util.Objects.requireNonNull;
  
  class NewArrayBytecodeExpression
          extends BytecodeExpression
  {
      private final BytecodeExpression length;
      private final ParameterizedType elementType;
  
      @Nullable
      private final List<BytecodeExpression> elements;
  
      public NewArrayBytecodeExpression(ParameterizedType type, int length)
      {
          this(type, constantInt(length));
      }
  
      public NewArrayBytecodeExpression(ParameterizedType type, BytecodeExpression length)
      {
          this(type, length, null);
      }
  
      public NewArrayBytecodeExpression(ParameterizedType type, List<BytecodeExpression> elements)
      {
          this(type, constantInt(elements.size()), elements);
      }
  
      private NewArrayBytecodeExpression(ParameterizedType type, BytecodeExpression length, List<BytecodeExpression> elements)
      {
          super(type);
          requireNonNull(type, "type is null");
          checkArgument(type.getArrayComponentType() != null, "type %s must be array type", type);
          this.elementType = type.getArrayComponentType();
          this.length = requireNonNull(length, "length is null");
          this.elements = (elements == null) ? null : ImmutableList.copyOf(elements);
      }
  
      @Override
      public BytecodeNode getBytecode(MethodGenerationContext generationContext)
      {
          BytecodeBlock bytecodeBlock;
          if (elementType.isPrimitive()) {
              bytecodeBlock = new BytecodeBlock()
                      .append(length)
                      .append(TypeInstruction.newPrimitiveArray(elementType));
          }
          else {
              bytecodeBlock = new BytecodeBlock()
                      .append(length)
                      .append(TypeInstruction.newObjectArray(elementType));
          }
          if (elements != null) {
              for (int i = 0; i < elements.size(); i++) {
                  BytecodeExpression element = elements.get(i);
                  bytecodeBlock
                          .dup()
                          .append(constantInt(i))
                          .append(element)
                          .append(getArrayOpCode(elementType).getStore());
              }
          }
          return bytecodeBlock;
      }
  
      @Override
      protected String formatOneLine()
      {
          if (elements == null) {
              return "new " + elementType.getSimpleName() + "[" + length + "]";
          }
          return "new " + elementType.getSimpleName() + "[] {" + Joiner.on(", ").join(elements) + "}";
      }
  
      @Override
      public List<BytecodeNode> getChildNodes()
      {
          return ImmutableList.of();
      }
  }
  