ParameterKey.java
/*
** Module : ParameterKey.java
** Abstract : wrapper for a parameter signature in a method.
**
** Copyright (c) 2018-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA 20181229 First version.
** 002 CA 20190423 WrappedResource must be treated as a handle type. Normalized DataSet type's case.
** 003 CA 20190508 Changes to solve collisions in converted Java signature collisions for overloaded methods.
** 004 CA 20220516 Fixed 'toJava' for OO extent parameters.
** GES 20220520 Added typeEquivalent(), other helpers and the way Java types are rendered.
** 005 CA 20230426 For arguments, mark those which are from an expression (i.e. using operators).
** 006 CA 20231007 Arguments for direct method calls which can't be emitted as direct java calls (as they
** are considered 'dynamic poly') must be wrapped in a 'polyArg' to check the type.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.uast;
import com.goldencode.p2j.util.*;
import java.util.*;
import static com.goldencode.p2j.uast.ProgressParserTokenTypes.*;
/**
* Container for a parameter's key, including its data type and mode. Used both at caller and
* at the OO method definition.
*/
public class ParameterKey
{
/** The parameter mode. May be <code>null</code> in case of BUFFER.*/
final Integer mode;
/** The parameter's data type.*/
final String type;
/** Flag indicating if the caller's argument is an expression. */
final boolean fromExpression;
/** Flag indicating if the caller's argument is from a direct method call with poly arguments. */
final boolean dynamicPoly;
/**
* Create a new parameter key.
*
* @param mode
* The parameter mode.
* @param type
* The parameter type.
*/
public ParameterKey(Integer mode, String type)
{
this(mode, type, false, false);
}
/**
* Create a new parameter key.
*
* @param mode
* The parameter mode.
* @param type
* The parameter type.
* @param fromExpression
* Flag indicating if the caller's argument is an expression.
* @param dynamicPoly
* Flag indicating if the caller's argument is from a direct method call with poly arguments.
*/
public ParameterKey(Integer mode, String type, boolean fromExpression, boolean dynamicPoly)
{
if ("WrappedResource".equals(type))
{
type = "handle";
}
else if ("DataSet".equals(type))
{
type = type.toUpperCase();
}
this.mode = mode;
this.type = type;
this.fromExpression = fromExpression;
this.dynamicPoly = dynamicPoly;
}
/**
* Create a new parameter key.
*
* @param lparam
* The legacy parameter annotation.
*/
public ParameterKey(LegacyParameter lparam)
{
String pmode = lparam.mode();
if ("INPUT".equalsIgnoreCase(pmode))
{
this.mode = KW_INPUT;
}
else if ("OUTPUT".equalsIgnoreCase(pmode))
{
this.mode = KW_OUTPUT;
}
else if ("INPUT-OUTPUT".equalsIgnoreCase(pmode))
{
this.mode = KW_IN_OUT;
}
else if ("RETURN".equalsIgnoreCase(pmode))
{
this.mode = KW_RETURN;
}
else
{
this.mode = UNKNOWN_TOKEN;
}
String txt = lparam.type().toLowerCase();
if ("bdt".equalsIgnoreCase(txt))
{
// OO legacy classes which have POLY parameters have BDT as their type, at the Java implementation.
txt = "Object";
}
if (!lparam.qualified().isEmpty())
{
txt += "<? extends " + lparam.qualified().toLowerCase() + ">";
}
else if ("object".equalsIgnoreCase(lparam.type()))
{
txt += "<? extends progress.lang.object>";
}
if (lparam.extent() == -1)
{
txt += "[]";
}
else if (lparam.extent() != -2)
{
txt += "[" + lparam.extent() + "]";
}
this.type = txt;
this.fromExpression = false;
this.dynamicPoly = false;
}
/**
* Report if the type of the two parameters is the same. The mode is not considered.
*
* @param other
* The instance to compare against.
*
* @return {@code true} if the types are the same or are both {@code null}. If they are different
* (including if only one is {@code null}) then {@code false} will be returned.
*/
public boolean typeEquivalent(ParameterKey other)
{
return Objects.equals(this.type, other.type);
}
/**
* Report the type without any encoded extent.
*
* @return The type with any extent portion removed.
*/
public String scalarType()
{
int idx = type.indexOf('[');
return (idx < 0) ? type : type.substring(0, idx);
}
/**
* Check if the argument is from an expression.
*
* @return See above.
*/
public boolean isFromExpression()
{
return fromExpression;
}
/**
* Test {@code other} object for equality.
*
* @param other
* The object to compare to.
*
* @return {@code true} only if the {@code other} object is equal to this one.
*/
@Override
public boolean equals(Object other)
{
if (this == other)
{
return true;
}
if (other == null || !(other instanceof ParameterKey))
{
return false;
}
ParameterKey otherKey = (ParameterKey) other;
return Objects.equals(this.mode, otherKey.mode) && typeEquivalent(otherKey);
}
/**
* Computes the hash code for this object.
*
* @return The computed hash-code.
*/
@Override
public int hashCode()
{
int result = 17;
result = 37 * result + Objects.hashCode(mode);
result = 37 * result + Objects.hashCode(type);
return result;
}
/**
* Get a string representation of this parameter key.
*
* @return See above.
*/
@Override
public String toString()
{
return renderMode() + " " + type;
}
/**
* Describe the instance as a 4GL parameter.
*
* @return See above.
*/
public String render()
{
return renderMode() + " " + renderType();
}
/**
* Check if this parameter is extent.
*
* @return See above.
*/
public boolean isExtent()
{
return type.indexOf("[") > 0;
}
/**
* Convert this parameter key to its converted Java equivalent.
*
* @return See above.
*/
public String toJava()
{
String t = type.toLowerCase();
// "dataset <signature>"
if (t.startsWith("dataset "))
{
switch (mode)
{
case KW_INPUT:
return "InputDataSetParameter";
case KW_OUTPUT:
return "OutputDataSetParameter";
case KW_IN_OUT:
return "InputOutputDataSetParameter";
}
}
if (t.equals("dataset-handle"))
{
switch (mode)
{
case KW_INPUT:
return "InputDataSetHandle";
case KW_OUTPUT:
return "OutputDataSetHandle";
case KW_IN_OUT:
return "InputOutputDataSetHandle";
}
}
// "temp-table <signature>"
if (t.startsWith("temp-table "))
{
switch (mode)
{
case KW_INPUT:
return "InputTableParameter";
case KW_OUTPUT:
return "OutputTableParameter";
case KW_IN_OUT:
return "InputOutputTableParameter";
}
}
if (t.equals("table-handle"))
{
switch (mode)
{
case KW_INPUT:
return "InputTableHandle";
case KW_OUTPUT:
return "OutputTableHandle";
case KW_IN_OUT:
return "InputOutputTableHandle";
}
}
// we must process the "dataset <...|int[]>" and "temp-table <...|int[]>" cases above first; by
// the time we get here, we are dealing with BDT cases only
if (type.indexOf('[') > 0)
{
switch (mode)
{
case KW_INPUT:
return type.startsWith("object")
? "object[]"
: type.substring(0, type.indexOf('[')) + "[]"; // extent is lost
case KW_OUTPUT:
return "OutputExtentParameter";
case KW_IN_OUT:
return "InputOutputExtentParameter";
}
}
if (t.startsWith("object"))
{
return "object";
}
return type;
}
/**
* Get a string representation of the mode.
*
* @return The mode.
*/
private String renderType()
{
String text = type;
// note the trailing space, it is important to avoid matching "DATASET-HANDLE"
if (type.startsWith("DATASET "))
{
text = "DATASET";
}
else if (type.startsWith("TEMP-TABLE"))
{
text = "TEMP-TABLE";
}
return text;
}
/**
* Get a string representation of the mode.
*
* @return The mode.
*/
private String renderMode()
{
return mode == null ? "" : ProgressParser.lookupTokenName(mode);
}
}