All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.mmm.code.impl.java.JavaFactory Maven / Gradle / Ivy

There is a newer version: 0.9.10
Show newest version
package io.github.mmm.code.impl.java;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import io.github.mmm.code.api.expression.CodeExpression;
import io.github.mmm.code.base.BaseFactory;
import io.github.mmm.code.base.expression.BaseArrayInstatiation;
import io.github.mmm.code.base.expression.BaseExpression;
import io.github.mmm.code.impl.java.expression.constant.JavaConstant;

/**
 * Implementation of {@link BaseFactory} for Java.
 *
 * @since 1.0.0
 */
public class JavaFactory extends BaseFactory {

  @Override
  public BaseExpression createExpression(Object value, boolean primitive) {

    if (value != null) {
      Class valueClass = value.getClass();
      if (valueClass.isArray()) {
        Class componentType = valueClass.getComponentType();
        primitive = componentType.isPrimitive();
        List list;
        if (primitive) {
          int length = Array.getLength(value);
          list = new ArrayList<>(length);
          for (int i = 0; i < length; i++) {
            Object item = Array.get(value, i);
            list.add(createExpression(item, primitive));
          }
        } else {
          Object[] array = (Object[]) value;
          int length = array.length;
          list = new ArrayList<>(length);
          for (int i = 0; i < length; i++) {
            list.add(createExpression(array[i], primitive));
          }
        }
        return new BaseArrayInstatiation(list);
      }
    }
    return JavaConstant.of(value, primitive);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy