Skip to content

Commit

Permalink
[lang] Adding Java cast on the returned value into the generated code…
Browse files Browse the repository at this point in the history
… of the synthetic functions for default-valued parameters.

close #1035

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 23, 2020
1 parent 39a4b69 commit 863a215
Show file tree
Hide file tree
Showing 4 changed files with 681 additions and 7 deletions.
Expand Up @@ -45,6 +45,8 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.base.Objects;
import com.google.common.base.Predicate;
Expand Down Expand Up @@ -1849,14 +1851,17 @@ protected void transform(final XtendFunction source, final JvmGenericType contai
.cloneWithProxies(exception));
}

final JvmTypeReference returnType2;
if (selectedReturnType instanceof XComputedTypeReference) {
operation2.setReturnType(this.typeReferences.createDelegateTypeReference(selectedReturnType));
returnType2 = this.typeReferences.createDelegateTypeReference(selectedReturnType);
} else {
operation2.setReturnType(cloneWithTypeParametersAndProxies(selectedReturnType, operation2));
returnType2 = cloneWithTypeParametersAndProxies(selectedReturnType, operation2);
}
operation2.setReturnType(returnType2);

final List<String> args = translateSarlFormalParametersForSyntheticOperation(
operation2, container, isVarArgs, otherSignature.getValue());
operation2, container, isVarArgs,
otherSignature.getValue());

if (context.isAtLeastJava8()) {
operation2.setDefault(container.isInterface());
Expand All @@ -1866,6 +1871,16 @@ protected void transform(final XtendFunction source, final JvmGenericType contai
if (!SARLJvmModelInferrer.this.typeReferences.is(type, void.class)) {
it.append("return "); //$NON-NLS-1$
}
final LightweightTypeReference ltr = Utils.toLightweightTypeReference(returnType2, this.services);
if (Utils.containsGenericType(ltr)) {
final String typeId = ltr.getRawTypeReference().getType().getQualifiedName('.');
final String javaId = ltr.getRawTypeReference().getJavaIdentifier();
final String fullId = ltr.getJavaIdentifier().replaceAll(
Pattern.quote(javaId), Matcher.quoteReplacement(typeId));
it.append("("); //$NON-NLS-1$
it.append(fullId);
it.append(")"); //$NON-NLS-1$
}
it.append(sourceName);
it.append("("); //$NON-NLS-1$
it.append(IterableExtensions.join(args, ", ")); //$NON-NLS-1$
Expand Down Expand Up @@ -3154,7 +3169,8 @@ protected void translateSarlFormalParameters(
}
}

/** Generate a list of formal parameters with annotations for the default values.
/** Generate a list arguments from the formal parameters in order to be used for a call into
* a synthetic operation, such as default-valued parameter function.
*
* @param owner the JVM element to change.
* @param actionContainer the container of the action.
Expand All @@ -3169,7 +3185,8 @@ protected List<String> translateSarlFormalParametersForSyntheticOperation(JvmExe
final JvmTypeReference paramType = parameterSpec.getType();
if (parameterSpec instanceof InferredValuedParameter) {
final StringBuilder argumentValue = new StringBuilder();
if (paramType.getType() instanceof JvmTypeParameter) {
final JvmType jtype = paramType.getType();
if (jtype instanceof JvmTypeParameter) {
argumentValue.append("("); //$NON-NLS-1$
argumentValue.append(paramType.getSimpleName());
argumentValue.append(") "); //$NON-NLS-1$
Expand Down
35 changes: 35 additions & 0 deletions main/coreplugins/io.sarl.lang/src/io/sarl/lang/util/Utils.java
Expand Up @@ -1843,5 +1843,40 @@ private static boolean isLocalEntity(XAbstractFeatureCall featureCall, XExpressi
return false;
}

/** Replies if a generic type parameter is declared into the given type.
*
* @param type is the type to explore.
* @return {@code code} if the {@code type} contains a generic type.
*/
public static boolean containsGenericType(LightweightTypeReference type) {
if (type == null) {
return false;
}
final JvmType jtype = type.getType();
if (jtype instanceof JvmTypeParameter) {
return true;
}
for (final LightweightTypeReference atype : type.getTypeArguments()) {
if (containsGenericType(atype)) {
return true;
}
}
switch (type.getKind()) {
case LightweightTypeReference.KIND_WILDCARD_TYPE_REFERENCE:
if (containsGenericType(type.getLowerBoundSubstitute()) || containsGenericType(type.getUpperBoundSubstitute())) {
return true;
}
break;
case LightweightTypeReference.KIND_ARRAY_TYPE_REFERENCE:
if (containsGenericType(type.getComponentType())) {
return true;
}
break;
default:
break;
}
return false;
}

}

Expand Up @@ -92,7 +92,7 @@ public class Bug723Test extends AbstractSarlTest {
" @SyntheticMember",
" @Pure",
" public static <S extends Object> Class<? extends S> getSystemPropertyAsClass(final Class<S> type, final String name) {",
" return getSystemPropertyAsClass(type, name, $DEFAULT_VALUE$GETSYSTEMPROPERTYASCLASS_0);",
" return (java.lang.Class<? extends S>)getSystemPropertyAsClass(type, name, $DEFAULT_VALUE$GETSYSTEMPROPERTYASCLASS_0);",
" }",
" ",
" @SyntheticMember",
Expand Down Expand Up @@ -144,7 +144,7 @@ public class Bug723Test extends AbstractSarlTest {
" @SyntheticMember",
" @Pure",
" public static <S extends Enum<S>> S getSystemPropertyAsEnum(final Class<S> type, final String name) {",
" return getSystemPropertyAsEnum(type, name, (S) $DEFAULT_VALUE$GETSYSTEMPROPERTYASENUM_0);",
" return (S)getSystemPropertyAsEnum(type, name, (S) $DEFAULT_VALUE$GETSYSTEMPROPERTYASENUM_0);",
" }",
" ",
" @SyntheticMember",
Expand Down

0 comments on commit 863a215

Please sign in to comment.