VerifyJavaUtil.java

/**
 * Copyright (c) 2004-2025 Carnegie Mellon University and others. (see Contributors file).
 * All Rights Reserved.
 * 
 * NO WARRANTY. ALL MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE
 * OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT
 * MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
 * 
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Created, in part, with funding and support from the United States Government. (see Acknowledgments file).
 * 
 * This program includes and/or can make use of certain third party source code, object code, documentation and other
 * files ("Third Party Software"). The Third Party Software that is used by this program is dependent upon your system
 * configuration. By using this program, You agree to comply with any and all relevant Third Party Software terms and
 * conditions contained in any such Third Party Software or separate license file distributed with such Third Party
 * Software. The parties who own the Third Party Software ("Third Party Licensors") are intended third party benefici-
 * aries to this license with respect to the terms applicable to their Third Party Software. Third Party Software li-
 * censes only apply to the Third Party Software and not any other portion of this program or this program as a whole.
 */
package org.osate.verify.internal.util;

import com.google.common.base.Objects;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.osate.aadl2.Aadl2Package;
import org.osate.aadl2.AadlBoolean;
import org.osate.aadl2.AadlInteger;
import org.osate.aadl2.AadlReal;
import org.osate.aadl2.AadlString;
import org.osate.aadl2.BooleanLiteral;
import org.osate.aadl2.IntegerLiteral;
import org.osate.aadl2.NumberType;
import org.osate.aadl2.PropertyExpression;
import org.osate.aadl2.PropertyType;
import org.osate.aadl2.RealLiteral;
import org.osate.aadl2.StringLiteral;
import org.osate.aadl2.UnitLiteral;
import org.osate.aadl2.instance.ComponentInstance;
import org.osate.aadl2.instance.ConnectionInstance;
import org.osate.aadl2.instance.EndToEndFlowInstance;
import org.osate.aadl2.instance.FeatureInstance;
import org.osate.aadl2.instance.InstanceObject;
import org.osate.aadl2.instance.InstancePackage;
import org.osate.aadl2.instance.ModeInstance;
import org.osate.aadl2.instance.SystemInstance;
import org.osate.alisa.common.common.ModelRef;
import org.osate.alisa.common.common.PropertyRef;
import org.osate.alisa.common.common.TargetType;
import org.osate.alisa.common.common.TypeRef;
import org.osate.verify.verify.FormalParameter;
import org.osate.verify.verify.JavaMethod;
import org.osate.verify.verify.JavaParameter;
import org.osate.verify.verify.VerificationMethod;

/**
 * @since 4.0
 */
@SuppressWarnings("all")
public class VerifyJavaUtil {
  public static List<Object> getActualJavaObjects(final JavaMethod vm, final InstanceObject target, final List<PropertyExpression> parameters) {
    ArrayList<Object> _xblockexpression = null;
    {
      EObject _eContainer = vm.eContainer();
      final EList<FormalParameter> formalparameters = ((VerificationMethod) _eContainer).getFormals();
      final ArrayList<Object> objects = new ArrayList<Object>();
      objects.add(target);
      final Iterator<FormalParameter> fpiter = formalparameters.iterator();
      for (final PropertyExpression o : parameters) {
        {
          final FormalParameter fp = fpiter.next();
          objects.add(VerifyJavaUtil.toJavaActual(fp, o, vm));
        }
      }
      _xblockexpression = objects;
    }
    return _xblockexpression;
  }

  /**
   * Create list of Java objects as parameters to be used
   */
  public static List<Object> getActualJavaObjects(final EList<FormalParameter> formalparameters, final InstanceObject target, final List<PropertyExpression> parameters) {
    ArrayList<Object> _xblockexpression = null;
    {
      final ArrayList<Object> objects = new ArrayList<Object>();
      objects.add(target);
      final Iterator<FormalParameter> fpiter = formalparameters.iterator();
      for (final PropertyExpression actual : parameters) {
        {
          final FormalParameter fp = fpiter.next();
          objects.add(VerifyJavaUtil.convertActualToJavaObject(fp, actual));
        }
      }
      _xblockexpression = objects;
    }
    return _xblockexpression;
  }

  /**
   * returns collection of Java classes for parameters
   */
  public static ArrayList<Class<?>> getParameterClasses(final JavaMethod vm) {
    ArrayList<Class<?>> _xblockexpression = null;
    {
      EObject _eContainer = vm.eContainer();
      final EList<FormalParameter> parameters = ((VerificationMethod) _eContainer).getFormals();
      final ArrayList<Class<?>> newClasses = CollectionLiterals.<Class<?>>newArrayList();
      EObject _eContainer_1 = vm.eContainer();
      newClasses.add(VerifyJavaUtil.classForTargetType(((VerificationMethod) _eContainer_1)));
      for (final FormalParameter par : parameters) {
        {
          final Class<?> cl = VerifyJavaUtil.classForParameter(par, vm);
          newClasses.add(cl);
        }
      }
      _xblockexpression = newClasses;
    }
    return _xblockexpression;
  }

  /**
   * return Java class for target type
   */
  private static Class<?> classForTargetType(final VerificationMethod vm) {
    Class<? extends InstanceObject> _switchResult = null;
    TargetType _targetType = vm.getTargetType();
    if (_targetType != null) {
      switch (_targetType) {
        case FEATURE:
          _switchResult = FeatureInstance.class;
          break;
        case COMPONENT:
          _switchResult = ComponentInstance.class;
          break;
        case CONNECTION:
          _switchResult = ConnectionInstance.class;
          break;
        case ELEMENT:
          _switchResult = InstanceObject.class;
          break;
        case FLOW:
          _switchResult = EndToEndFlowInstance.class;
          break;
        case MODE:
          _switchResult = ModeInstance.class;
          break;
        case ROOT:
          _switchResult = SystemInstance.class;
          break;
        default:
          break;
      }
    }
    return _switchResult;
  }

  private static Class<?> classForParameter(final FormalParameter fp, final JavaMethod vm) {
    try {
      Class<?> _xblockexpression = null;
      {
        final EList<JavaParameter> jparameters = vm.getParams();
        for (final JavaParameter jp : jparameters) {
          boolean _equalsIgnoreCase = fp.getName().equalsIgnoreCase(jp.getName());
          if (_equalsIgnoreCase) {
            return VerifyJavaUtil.forName(jp.getParameterType());
          }
        }
        final PropertyType type = fp.getType();
        Class<?> _switchResult = null;
        boolean _matched = false;
        if (type instanceof AadlString) {
          _matched=true;
          return String.class;
        }
        if (!_matched) {
          if (type instanceof NumberType) {
            _matched=true;
            _switchResult = VerifyJavaUtil.classForNumberType(type, fp.getUnit());
          }
        }
        if (!_matched) {
          if (type instanceof AadlBoolean) {
            _matched=true;
            return Boolean.class;
          }
        }
        if (!_matched) {
          if (type instanceof ModelRef) {
            _matched=true;
            return EObject.class;
          }
        }
        if (!_matched) {
          if (type instanceof TypeRef) {
            _matched=true;
            final PropertyType pt = ((TypeRef)type).getRef();
            if ((pt instanceof NumberType)) {
              return VerifyJavaUtil.classForNumberType(pt, fp.getUnit());
            }
            return pt.getClass();
          }
        }
        if (!_matched) {
          if (type instanceof PropertyRef) {
            _matched=true;
            final PropertyType pt = ((PropertyRef)type).getRef().getPropertyType();
            if ((pt instanceof NumberType)) {
              return VerifyJavaUtil.classForNumberType(pt, fp.getUnit());
            }
            return pt.getClass();
          }
        }
        _xblockexpression = _switchResult;
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }

  private static Class<?> classForNumberType(final PropertyType type, final UnitLiteral inUnit) {
    boolean _matched = false;
    if (type instanceof AadlReal) {
      _matched=true;
      if ((inUnit == null)) {
        return RealLiteral.class;
      } else {
        return double.class;
      }
    }
    if (!_matched) {
      if (type instanceof AadlInteger) {
        _matched=true;
        if ((inUnit == null)) {
          return IntegerLiteral.class;
        } else {
          return long.class;
        }
      }
    }
    return null;
  }

  /**
   * return Java Class for class specified by name in JavaParameter
   */
  private static Class<?> forName(final String name) throws ClassNotFoundException {
    boolean _matched = false;
    String _name = void.class.getName();
    if (Objects.equal(name, _name)) {
      _matched=true;
      return void.class;
    }
    if (!_matched) {
      String _name_1 = boolean.class.getName();
      if (Objects.equal(name, _name_1)) {
        _matched=true;
        return boolean.class;
      }
    }
    if (!_matched) {
      String _name_2 = byte.class.getName();
      if (Objects.equal(name, _name_2)) {
        _matched=true;
        return byte.class;
      }
    }
    if (!_matched) {
      String _name_3 = char.class.getName();
      if (Objects.equal(name, _name_3)) {
        _matched=true;
        return char.class;
      }
    }
    if (!_matched) {
      String _name_4 = short.class.getName();
      if (Objects.equal(name, _name_4)) {
        _matched=true;
        return short.class;
      }
    }
    if (!_matched) {
      String _name_5 = int.class.getName();
      if (Objects.equal(name, _name_5)) {
        _matched=true;
        return int.class;
      }
    }
    if (!_matched) {
      String _name_6 = float.class.getName();
      if (Objects.equal(name, _name_6)) {
        _matched=true;
        return float.class;
      }
    }
    if (!_matched) {
      String _name_7 = long.class.getName();
      if (Objects.equal(name, _name_7)) {
        _matched=true;
        return long.class;
      }
    }
    if (!_matched) {
      String _name_8 = double.class.getName();
      if (Objects.equal(name, _name_8)) {
        _matched=true;
        return double.class;
      }
    }
    if (!_matched) {
      String _name_9 = String.class.getName();
      if (Objects.equal(name, _name_9)) {
        _matched=true;
        return String.class;
      }
    }
    if (!_matched) {
      if (Objects.equal(name, "Double")) {
        _matched=true;
        return Double.class;
      }
    }
    if (!_matched) {
      if (Objects.equal(name, "Long")) {
        _matched=true;
        return Long.class;
      }
    }
    if (!_matched) {
      String _name_10 = AadlReal.class.getName();
      if (Objects.equal(name, _name_10)) {
        _matched=true;
        return RealLiteral.class;
      }
    }
    if (!_matched) {
      String _name_11 = AadlInteger.class.getName();
      if (Objects.equal(name, _name_11)) {
        _matched=true;
        return IntegerLiteral.class;
      }
    }
    if (!_matched) {
      String _name_12 = AadlBoolean.class.getName();
      if (Objects.equal(name, _name_12)) {
        _matched=true;
        return BooleanLiteral.class;
      }
    }
    {
      EClassifier ecl = Aadl2Package.eINSTANCE.getEClassifier(name);
      if ((ecl == null)) {
        InstancePackage.eINSTANCE.getEClassifier(name);
      }
      if ((ecl != null)) {
        return ecl.getInstanceClass();
      }
      return Class.forName(name);
    }
  }

  /**
   * convert actual to Java object if JavaParameter is specified
   */
  private static Object toJavaActual(final FormalParameter formal, final PropertyExpression actual, final JavaMethod vm) {
    final EList<JavaParameter> jparameters = vm.getParams();
    for (final JavaParameter jp : jparameters) {
      boolean _equalsIgnoreCase = formal.getName().equalsIgnoreCase(jp.getName());
      if (_equalsIgnoreCase) {
        return VerifyJavaUtil.convertToJavaObject(jp, actual);
      }
    }
    return VerifyJavaUtil.convertActualToJavaObject(formal, actual);
  }

  /**
   * Convert AADL Property representation of string, boolean, integer, real to Java objects
   */
  private static Object convertActualToJavaObject(final FormalParameter formal, final PropertyExpression actual) {
    Object result = actual;
    boolean _matched = false;
    if (actual instanceof RealLiteral) {
      _matched=true;
      UnitLiteral _unit = formal.getUnit();
      boolean _tripleNotEquals = (_unit != null);
      if (_tripleNotEquals) {
        result = Double.valueOf(((RealLiteral)actual).getValue());
      }
    }
    if (!_matched) {
      if (actual instanceof IntegerLiteral) {
        _matched=true;
        UnitLiteral _unit = formal.getUnit();
        boolean _tripleNotEquals = (_unit != null);
        if (_tripleNotEquals) {
          result = Long.valueOf(((IntegerLiteral)actual).getValue());
        }
      }
    }
    if (!_matched) {
      if (actual instanceof StringLiteral) {
        _matched=true;
        result = ((StringLiteral)actual).getValue();
      }
    }
    if (!_matched) {
      if (actual instanceof BooleanLiteral) {
        _matched=true;
        result = Boolean.valueOf(((BooleanLiteral)actual).isValue());
      }
    }
    return result;
  }

  /**
   * Convert AADL Property representation of string, boolean, integer, real to Java objects accordin gto JavaParameter specification
   */
  private static Object convertToJavaObject(final JavaParameter formalParam, final PropertyExpression actual) {
    Object result = actual;
    boolean _matched = false;
    if (actual instanceof RealLiteral) {
      _matched=true;
      if ((formalParam.getParameterType().equalsIgnoreCase("double") || 
        formalParam.getParameterType().equalsIgnoreCase("real"))) {
        result = Double.valueOf(((RealLiteral)actual).getValue());
      }
    }
    if (!_matched) {
      if (actual instanceof IntegerLiteral) {
        _matched=true;
        boolean _equalsIgnoreCase = formalParam.getParameterType().equalsIgnoreCase("long");
        if (_equalsIgnoreCase) {
          result = Long.valueOf(((IntegerLiteral)actual).getValue());
        } else {
          boolean _equalsIgnoreCase_1 = formalParam.getParameterType().equalsIgnoreCase("int");
          if (_equalsIgnoreCase_1) {
            result = Integer.valueOf(Long.valueOf(((IntegerLiteral)actual).getValue()).intValue());
          }
        }
      }
    }
    if (!_matched) {
      if (actual instanceof StringLiteral) {
        _matched=true;
        boolean _equalsIgnoreCase = formalParam.getParameterType().equalsIgnoreCase("string");
        if (_equalsIgnoreCase) {
          result = ((StringLiteral)actual).getValue();
        }
      }
    }
    if (!_matched) {
      if (actual instanceof BooleanLiteral) {
        _matched=true;
        boolean _equalsIgnoreCase = formalParam.getParameterType().equalsIgnoreCase("boolean");
        if (_equalsIgnoreCase) {
          result = Boolean.valueOf(((BooleanLiteral)actual).isValue());
        }
      }
    }
    return result;
  }
}