Class ReflectionOperationDescriptor

  • All Implemented Interfaces:
    ILazyDescriptor, IMetaOperation

    public class ReflectionOperationDescriptor
    extends OperationDescriptor
    implements ILazyDescriptor
    Represents an individual operation available to the VIL languages based on reflection analysis of the underlying class. This class works on the default type registry. Please note that named arguments are passed in terms of a map as the last parameter of the respective method. If the underlying Java method takes such a parameter, the descriptor will count one parameter less!
    Author:
    Holger Eichelberger
    • Field Detail

      • REFLECTION_EQUALITIES

        private static final java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> REFLECTION_EQUALITIES
        Stores implicit type equalities handled by boxing/unboxing in Java reflection.
      • method

        private java.lang.reflect.Method method
      • returnGenericParameterIndex

        private int returnGenericParameterIndex
      • returnParameterIndex

        private int returnParameterIndex
      • storeArtifactsBeforeExecution

        private boolean storeArtifactsBeforeExecution
      • trace

        private boolean trace
    • Constructor Detail

      • ReflectionOperationDescriptor

        public ReflectionOperationDescriptor​(TypeDescriptor<?> declaringType,
                                             java.lang.reflect.Method method,
                                             boolean isConstructor)
        Creates a new operation descriptor.
        Parameters:
        declaringType - the declaring type
        method - the reflection method to be called
        isConstructor - whether this descriptor shall act as constructor
      • ReflectionOperationDescriptor

        public ReflectionOperationDescriptor​(TypeDescriptor<?> declaringType,
                                             java.lang.reflect.Method method,
                                             java.lang.String name,
                                             boolean isConstructor)
        Creates a new operation descriptor as an alias.
        Parameters:
        declaringType - the declaring type
        method - the reflection method to be called
        name - the alias name (may be null if the original name of method shall be used)
        isConstructor - whether this descriptor shall act as a constructor
    • Method Detail

      • addReflectionEquality

        private static final void addReflectionEquality​(java.lang.Class<?> cls1,
                                                        java.lang.Class<?> cls2)
        Adds a new bi-directional reflection equality.
        Parameters:
        cls1 - a class to be considered equivalent / assignment compatible to cls2
        cls2 - a class to be considered equivalent / assignment compatible to cls1
      • forceInitialization

        public void forceInitialization()
        Description copied from interface: ILazyDescriptor
        Forces the initialization of the typically lazily deferred parameters and return types.
        Specified by:
        forceInitialization in interface ILazyDescriptor
      • considerNamedParameters

        protected boolean considerNamedParameters()
        Returns whether named parameters shall be considered for this descriptor.
        Returns:
        true if named parameters shall be considered, false else
      • getDefaultValues

        private java.util.Map<java.lang.String,​java.lang.Object> getDefaultValues()
        Returns the default values for this method.
        Returns:
        the default values
      • checkForNamedParameter

        private java.util.Map<java.lang.String,​java.lang.Object> checkForNamedParameter​(TypeDescriptor<?> type,
                                                                                              int index,
                                                                                              java.lang.annotation.Annotation[][] annotations,
                                                                                              java.util.Map<java.lang.String,​java.lang.Object> defltValues,
                                                                                              int paramCount)
        Checks the current parameter for a named parameter.
        Parameters:
        type - the VIL type of the parameter
        index - the 0-based index of the parameter
        annotations - the parameter annotations for the method (may be null)
        defltValues - the default values (global or local, may be null)
        paramCount - the total number of parameters
        Returns:
        defltValues, a new instance if defltValues is null or null
      • specializeFor

        public OperationDescriptor specializeFor​(TypeDescriptor<?> declaringType)
        Description copied from class: OperationDescriptor
        Tries to specialize this operation descriptor for the given type, e.g., to consider the specific generic parameters of that type.
        Overrides:
        specializeFor in class OperationDescriptor
        Parameters:
        declaringType - the declaring type to specialize for
        Returns:
        either this if no specialization is needed or a specializing instance
      • nullify

        private static java.lang.Object nullify​(java.lang.Object value)
        Nullifies value, i.e., checks whether it is an IVML null value and turns it into null.
        Parameters:
        value - the value to check
        Returns:
        value or null
      • tryEvaluateEquality

        private java.lang.Object tryEvaluateEquality​(java.lang.Object[] args,
                                                     java.lang.Object[] callArgs)
                                              throws VilException
        Tries to evaluate equality/unequality if the actual parameters are incompatible for a regular call. This allows evaluating null comparisons against primitive types (decision variables declare a specific operation for that).
        Parameters:
        args - the original call arguments
        callArgs - the actual call arguments prepared for a reflective call
        Returns:
        the evaluation result
        Throws:
        VilException - in case that the arguments are still considered incompatible
      • isCompatible

        public IMetaOperation.CompatibilityResult isCompatible​(java.lang.Class<?> retType,
                                                               java.lang.Object... params)
        Returns whether the operation represented by this instance is compatible to the given return type and parameters. This method does not consider possible unnamed parameter rather than the underlying Java parameter.
        Specified by:
        isCompatible in interface IMetaOperation
        Parameters:
        retType - the return type (may be null in order to ignore this parameter)
        params - the parameters (may be null if there are none, may be classes)
        Returns:
        an instance of IMetaOperation.CompatibilityResult denoting the actual compatibility level
      • isStatic

        public boolean isStatic()
        Description copied from interface: IMetaOperation
        Returns whether this operation is static.
        Specified by:
        isStatic in interface IMetaOperation
        Returns:
        true if it is static, false else
      • isFirstParameterOperand

        public boolean isFirstParameterOperand()
        Description copied from interface: IMetaOperation
        Returns whether the first parameter is the operand.
        Specified by:
        isFirstParameterOperand in interface IMetaOperation
        Returns:
        true if the first parameter is the operand, false else
      • getMethod

        protected java.lang.reflect.Method getMethod()
        Returns the wrapped method.
        Returns:
        the method
      • getParameterGenerics

        protected java.lang.Class<?>[] getParameterGenerics​(int index)
        Determines the type of a parameter.
        Parameters:
        index - the index of the parameter
        Returns:
        the parameter generics (null if none are specified)
        Throws:
        java.lang.IndexOutOfBoundsException - if index < 0 || index >= OperationDescriptor.getParameterCount()
      • getReturnGenerics

        protected java.lang.Class<?>[] getReturnGenerics()
        Determines the return type generics.
        Returns:
        the return type generics (null if none are specified)
      • isPlaceholder

        public boolean isPlaceholder()
        Description copied from interface: IMetaOperation
        Returns whether this operation is valid or whether it is a placeholder operation in case that the original operation cannot be resolved but the script shall remain executable.
        Specified by:
        isPlaceholder in interface IMetaOperation
        Returns:
        true if this operation is a placeholder, false else
      • useGenericParameterAsReturn

        public int useGenericParameterAsReturn()
        Description copied from class: OperationDescriptor
        Returns whether a generc parameter of the operand shall be used as return type.
        Specified by:
        useGenericParameterAsReturn in class OperationDescriptor
        Returns:
        the parameter to be used as index number, negative if none
      • useParameterAsReturn

        public int useParameterAsReturn()
        Description copied from class: OperationDescriptor
        Returns whether a parameter shall be used as return type.
        Specified by:
        useParameterAsReturn in class OperationDescriptor
        Returns:
        the parameter to be used as index number, negative if none
      • storeArtifactsBeforeExecution

        public boolean storeArtifactsBeforeExecution()
        Description copied from class: OperationDescriptor
        Returns whether artifacts shall be stored before execution of this operation.
        Specified by:
        storeArtifactsBeforeExecution in class OperationDescriptor
        Returns:
        true if artifacts shall be stored, false else
      • requiresDynamicExpressionProcessing

        public boolean requiresDynamicExpressionProcessing()
        Description copied from class: OperationDescriptor
        Indicates whether this function requires dynamic expression processing and may cause problems with serialized models in standalone settings without xText.
        Overrides:
        requiresDynamicExpressionProcessing in class OperationDescriptor
        Returns:
        true if this function requires dynamic exception processing, false else
      • trace

        public boolean trace()
        Description copied from class: OperationDescriptor
        Returns whether an execution of this descriptor shall be traced. Conversions shall not be traced by default as well as some operation types.
        Overrides:
        trace in class OperationDescriptor
        Returns:
        true if it shall be traced, false
      • isOclCompliant

        public boolean isOclCompliant()
        Description copied from class: OperationDescriptor
        Returns whether this operation is OCL compliant or flagged as not compliant.
        Overrides:
        isOclCompliant in class OperationDescriptor
        Returns:
        true for compliant, false else
      • useAny

        public boolean useAny()
        Description copied from class: OperationDescriptor
        When resolving expression types, use any as return type if object is given and do not try to override this.
        Overrides:
        useAny in class OperationDescriptor
        Returns:
        true for use any, false else (default)
      • flatten

        public boolean flatten()
        Description copied from class: OperationDescriptor
        When resolving expression types, flatten the final return type.
        Overrides:
        flatten in class OperationDescriptor
        Returns:
        true for use any, false else (default)
      • useOperandTypeAsParameter

        public boolean useOperandTypeAsParameter()
        Description copied from class: OperationDescriptor
        When resolving the return type, use the actual operand type as return generics.
        Overrides:
        useOperandTypeAsParameter in class OperationDescriptor
        Returns:
        true for use return type, false else (default)