diff --git a/main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/scoping/extensions/cast/PrimitiveCastExtensions.java b/main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/scoping/extensions/cast/PrimitiveCastExtensions.java index ac0a84be2..41e7293b1 100644 --- a/main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/scoping/extensions/cast/PrimitiveCastExtensions.java +++ b/main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/scoping/extensions/cast/PrimitiveCastExtensions.java @@ -121,6 +121,30 @@ public static byte byteValue(CharSequence value) { return 0; } + /** Decodes a {@code CharSequence} into a {@code Byte}. + * + *

In opposite to the functions of {@link Byte}, this function is + * null-safe and does not generate a {@link NumberFormatException}. + * If the given string cannot by parsed, {@code 0} is replied. + * + *

See {@link Byte#decode(String)} for details on the accepted formats + * for the input string of characters. + * + * @param value a value of {@code CharSequence} type. + * @return the equivalent value to {@code value} of {@code Byte} type. + * @since 0.12 + * @see Byte#decode(String) + */ + @Pure + public static byte toByte(CharSequence value) { + try { + return Byte.decode(value.toString()); + } catch (Throwable exception) { + // Silent exception. + } + return 0; + } + /** Decodes a {@code CharSequence} into a {@code short}. * *

In opposite to the functions of {@link Short}, this function is @@ -145,7 +169,31 @@ public static short shortValue(CharSequence value) { return 0; } - /** Decodes a {@code CharSequence} into a {@code int}. + /** Decodes a {@code CharSequence} into a {@code Short}. + * + *

In opposite to the functions of {@link Short}, this function is + * null-safe and does not generate a {@link NumberFormatException}. + * If the given string cannot by parsed, {@code 0} is replied. + * + *

See {@link Short#decode(String)} for details on the accepted formats + * for the input string of characters. + * + * @param value a value of {@code CharSequence} type. + * @return the equivalent value to {@code value} of {@code Short} type. + * @since 0.12 + * @see Short#decode(String) + */ + @Pure + public static Short toShort(CharSequence value) { + try { + return Short.decode(value.toString()); + } catch (Throwable exception) { + // Silent exception. + } + return 0; + } + + /** Decodes a {@code CharSequence} into an {@code int}. * *

In opposite to the functions of {@link Integer}, this function is * null-safe and does not generate a {@link NumberFormatException}. @@ -169,6 +217,30 @@ public static int intValue(CharSequence value) { return 0; } + /** Decodes a {@code CharSequence} into an {@code Integer}. + * + *

In opposite to the functions of {@link Integer}, this function is + * null-safe and does not generate a {@link NumberFormatException}. + * If the given string cannot by parsed, {@code 0} is replied. + * + *

See {@link Integer#decode(String)} for details on the accepted formats + * for the input string of characters. + * + * @param value a value of {@code CharSequence} type. + * @return the equivalent value to {@code value} of {@code Integer} type. + * @since 0.12 + * @see Integer#decode(String) + */ + @Pure + public static Integer toInteger(CharSequence value) { + try { + return Integer.decode(value.toString()); + } catch (Throwable exception) { + // Silent exception. + } + return 0; + } + /** Decodes a {@code CharSequence} into a {@code char}. * * @param value a value of {@code CharSequence} type. @@ -176,9 +248,29 @@ public static int intValue(CharSequence value) { * @since 0.12 */ @Pure - @Inline("($1).charAt(0)") public static char charValue(CharSequence value) { - return value.charAt(0); + try { + return value.charAt(0); + } catch (Throwable exception) { + // + } + return '\0'; + } + + /** Decodes a {@code CharSequence} into a {@code Character}. + * + * @param value a value of {@code CharSequence} type. + * @return the equivalent value to {@code value} of {@code Character} type. + * @since 0.12 + */ + @Pure + public static Character toCharacter(CharSequence value) { + try { + return value.charAt(0); + } catch (Throwable exception) { + // + } + return '\0'; } /** Decodes a {@code CharSequence} into a {@code long}. @@ -205,6 +297,30 @@ public static long longValue(CharSequence value) { return 0; } + /** Decodes a {@code CharSequence} into a {@code Long}. + * + *

In opposite to the functions of {@link Long}, this function is + * null-safe and does not generate a {@link NumberFormatException}. + * If the given string cannot by parsed, {@code 0} is replied. + * + *

See {@link Long#decode(String)} for details on the accepted formats + * for the input string of characters. + * + * @param value a value of {@code CharSequence} type. + * @return the equivalent value to {@code value} of {@code Long} type. + * @since 0.9 + * @see Long#decode(String) + */ + @Pure + public static Long toLong(CharSequence value) { + try { + return Long.decode(value.toString()); + } catch (Throwable exception) { + // Silent exception. + } + return 0L; + } + /** Decodes a {@code CharSequence} into a {@code float}. * *

In opposite to the functions of {@link Float}, this function is @@ -229,6 +345,30 @@ public static float floatValue(CharSequence value) { return 0; } + /** Decodes a {@code CharSequence} into a {@code Float}. + * + *

In opposite to the functions of {@link Float}, this function is + * null-safe and does not generate a {@link NumberFormatException}. + * If the given string cannot by parsed, {@code 0} is replied. + * + *

See {@link Float#valueOf(String)} for details on the accepted formats + * for the input string of characters. + * + * @param value a value of {@code CharSequence} type. + * @return the equivalent value to {@code value} of {@code Float} type. + * @since 0.9 + * @see Float#valueOf(String) + */ + @Pure + public static Float toFloat(CharSequence value) { + try { + return Float.parseFloat(value.toString()); + } catch (Throwable exception) { + // Silent exception. + } + return 0f; + } + /** Decodes a {@code CharSequence} into a {@code double}. * *

In opposite to the functions of {@link Double}, this function is @@ -253,6 +393,30 @@ public static double doubleValue(CharSequence value) { return 0; } + /** Decodes a {@code CharSequence} into a {@code Double}. + * + *

In opposite to the functions of {@link Double}, this function is + * null-safe and does not generate a {@link NumberFormatException}. + * If the given string cannot by parsed, {@code 0} is replied. + * + *

See {@link Double#valueOf(String)} for details on the accepted formats + * for the input string of characters. + * + * @param value a value of {@code CharSequence} type. + * @return the equivalent value to {@code value} of {@code Double} type. + * @since 0.12 + * @see Double#valueOf(String) + */ + @Pure + public static Double toDouble(CharSequence value) { + try { + return Double.parseDouble(value.toString()); + } catch (Throwable exception) { + // Silent exception. + } + return 0.0; + } + /** Decodes a {@code CharSequence} into a {@code AtomicBoolean}. * * diff --git a/tests/io.sarl.lang.core.tests/src/test/java/io/sarl/lang/core/tests/scoping/extensions/cast/CodeTest.java b/tests/io.sarl.lang.core.tests/src/test/java/io/sarl/lang/core/tests/scoping/extensions/cast/CodeTest.java index 78aa6f66c..9a9f5d4d6 100644 --- a/tests/io.sarl.lang.core.tests/src/test/java/io/sarl/lang/core/tests/scoping/extensions/cast/CodeTest.java +++ b/tests/io.sarl.lang.core.tests/src/test/java/io/sarl/lang/core/tests/scoping/extensions/cast/CodeTest.java @@ -85,6 +85,28 @@ public void toString_char() { assertEquals("a", PrimitiveCastExtensions.toString('a')); } + @Test + public void charValue_CharSequence() { + assertEquals('4', PrimitiveCastExtensions.charValue("4")); + assertEquals('0', PrimitiveCastExtensions.charValue("0xa")); + assertEquals('-', PrimitiveCastExtensions.charValue("-4")); + assertEquals('-', PrimitiveCastExtensions.charValue("-0xa")); + assertEquals('\0', PrimitiveCastExtensions.charValue("")); + assertEquals('z', PrimitiveCastExtensions.charValue("z")); + assertEquals('\0', PrimitiveCastExtensions.charValue(null)); + } + + @Test + public void toCharacter_CharSequence() { + assertEquals('4', PrimitiveCastExtensions.toCharacter("4")); + assertEquals('0', PrimitiveCastExtensions.toCharacter("0xa")); + assertEquals('-', PrimitiveCastExtensions.toCharacter("-4")); + assertEquals('-', PrimitiveCastExtensions.toCharacter("-0xa")); + assertEquals('\0', PrimitiveCastExtensions.toCharacter("")); + assertEquals('z', PrimitiveCastExtensions.toCharacter("z")); + assertEquals('\0', PrimitiveCastExtensions.toCharacter(null)); + } + @Test public void byteValue_CharSequence() { assertEquals(4, PrimitiveCastExtensions.byteValue("4")); @@ -96,6 +118,17 @@ public void byteValue_CharSequence() { assertEquals(0, PrimitiveCastExtensions.byteValue(null)); } + @Test + public void toByte_CharSequence() { + assertEquals(4, PrimitiveCastExtensions.toByte("4")); + assertEquals(10, PrimitiveCastExtensions.toByte("0xa")); + assertEquals(-4, PrimitiveCastExtensions.toByte("-4")); + assertEquals(-10, PrimitiveCastExtensions.toByte("-0xa")); + assertEquals(0, PrimitiveCastExtensions.toByte("")); + assertEquals(0, PrimitiveCastExtensions.toByte("z")); + assertEquals(0, PrimitiveCastExtensions.toByte(null)); + } + @Test public void shortValue_CharSequence() { assertEquals(4, PrimitiveCastExtensions.shortValue("4")); @@ -107,6 +140,17 @@ public void shortValue_CharSequence() { assertEquals(0, PrimitiveCastExtensions.shortValue(null)); } + @Test + public void toShort_CharSequence() { + assertEquals((short) 4, PrimitiveCastExtensions.toShort("4")); + assertEquals((short) 10, PrimitiveCastExtensions.toShort("0xa")); + assertEquals((short) -4, PrimitiveCastExtensions.toShort("-4")); + assertEquals((short) -10, PrimitiveCastExtensions.toShort("-0xa")); + assertEquals((short) 0, PrimitiveCastExtensions.toShort("")); + assertEquals((short) 0, PrimitiveCastExtensions.toShort("z")); + assertEquals((short) 0, PrimitiveCastExtensions.toShort(null)); + } + @Test public void intValue_CharSequence() { assertEquals(4, PrimitiveCastExtensions.intValue("4")); @@ -118,6 +162,17 @@ public void intValue_CharSequence() { assertEquals(0, PrimitiveCastExtensions.intValue(null)); } + @Test + public void toInteger_CharSequence() { + assertEquals(4, PrimitiveCastExtensions.toInteger("4")); + assertEquals(10, PrimitiveCastExtensions.toInteger("0xa")); + assertEquals(-4, PrimitiveCastExtensions.toInteger("-4")); + assertEquals(-10, PrimitiveCastExtensions.toInteger("-0xa")); + assertEquals(0, PrimitiveCastExtensions.toInteger("")); + assertEquals(0, PrimitiveCastExtensions.toInteger("z")); + assertEquals(0, PrimitiveCastExtensions.toInteger(null)); + } + @Test public void longValue_CharSequence() { assertEquals(4l, PrimitiveCastExtensions.longValue("4")); @@ -129,6 +184,17 @@ public void longValue_CharSequence() { assertEquals(0l, PrimitiveCastExtensions.longValue(null)); } + @Test + public void toLong_CharSequence() { + assertEquals(4l, PrimitiveCastExtensions.toLong("4")); + assertEquals(10l, PrimitiveCastExtensions.toLong("0xa")); + assertEquals(-4l, PrimitiveCastExtensions.toLong("-4")); + assertEquals(-10l, PrimitiveCastExtensions.toLong("-0xa")); + assertEquals(0l, PrimitiveCastExtensions.toLong("")); + assertEquals(0l, PrimitiveCastExtensions.toLong("z")); + assertEquals(0l, PrimitiveCastExtensions.toLong(null)); + } + @Test public void floatValue_CharSequence() { assertEpsilonEquals(4f, PrimitiveCastExtensions.floatValue("4")); @@ -140,6 +206,17 @@ public void floatValue_CharSequence() { assertEpsilonEquals(0f, PrimitiveCastExtensions.floatValue(null)); } + @Test + public void toFloat_CharSequence() { + assertEpsilonEquals(4f, PrimitiveCastExtensions.toFloat("4")); + assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat("0xa")); + assertEpsilonEquals(-4f, PrimitiveCastExtensions.toFloat("-4")); + assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat("-0xa")); + assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat("")); + assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat("z")); + assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat(null)); + } + @Test public void doubleValue_CharSequence() { assertEpsilonEquals(4., PrimitiveCastExtensions.doubleValue("4")); @@ -151,6 +228,17 @@ public void doubleValue_CharSequence() { assertEpsilonEquals(0., PrimitiveCastExtensions.doubleValue(null)); } + @Test + public void toDoubleCharSequence() { + assertEpsilonEquals(4., PrimitiveCastExtensions.toDouble("4")); + assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble("0xa")); + assertEpsilonEquals(-4., PrimitiveCastExtensions.toDouble("-4")); + assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble("-0xa")); + assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble("")); + assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble("z")); + assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble(null)); + } + @Test public void toAtomicBoolean_CharSequence() { assertFalse(PrimitiveCastExtensions.toAtomicBoolean("4").get()); diff --git a/tests/io.sarl.lang.core.tests/src/test/java/io/sarl/lang/core/tests/scoping/extensions/cast/CompilerTest.java b/tests/io.sarl.lang.core.tests/src/test/java/io/sarl/lang/core/tests/scoping/extensions/cast/CompilerTest.java index 8f1d4be55..843a03d49 100644 --- a/tests/io.sarl.lang.core.tests/src/test/java/io/sarl/lang/core/tests/scoping/extensions/cast/CompilerTest.java +++ b/tests/io.sarl.lang.core.tests/src/test/java/io/sarl/lang/core/tests/scoping/extensions/cast/CompilerTest.java @@ -255,6 +255,58 @@ public void string_as_byte(ResourceSetGlobalCompilationContext ctx) throws Excep ctx.compileTo(STRING_AS_BYTE_SARL, STRING_AS_BYTE_JAVA); } + private static final String STRING_AS_BYTEOBJ_SARL = multilineString( + "class A {", + " def fct(left : String) : Byte {", + " left as Byte", + " }", + "}"); + + private static final String STRING_AS_BYTEOBJ_JAVA = multilineString( + "import io.sarl.lang.annotation.SarlElementType;", + "import io.sarl.lang.annotation.SarlSpecification;", + "import io.sarl.lang.annotation.SyntheticMember;", + "import io.sarl.lang.scoping.extensions.cast.PrimitiveCastExtensions;", + "import org.eclipse.xtext.xbase.lib.Pure;", + "", + "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", + "@SarlElementType(" + SarlPackage.SARL_CLASS + ")", + "@SuppressWarnings(\"all\")", + "public class A {", + " @Pure", + " public Byte fct(final String left) {", + " return (left == null ? 0 : PrimitiveCastExtensions.toByte(left));", + " }", + " ", + " @SyntheticMember", + " public A() {", + " super();", + " }", + "}", + ""); + + @Test + @Tag("sarlValidation") + public void string_as_Byte_issues() throws Exception { + validate(getValidationHelper(), getInjector(), file(getParseHelper(), STRING_AS_BYTEOBJ_SARL)) + .assertNoErrors( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.INVALID_CAST) + .assertNoWarnings( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.OBSOLETE_CAST) + .assertWarning( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + IssueCodes.POTENTIAL_INEFFICIENT_VALUE_CONVERSION, + "'byteValue'"); + } + + @GlobalCompilationTestContribution + @Tag("compileToJava") + public void string_as_Byte(ResourceSetGlobalCompilationContext ctx) throws Exception { + ctx.compileTo(STRING_AS_BYTEOBJ_SARL, STRING_AS_BYTEOBJ_JAVA); + } + private static final String STRING_AS_SHORT_SARL = multilineString( "class A {", " def fct(left : String) : short {", @@ -307,6 +359,58 @@ public void string_as_short(ResourceSetGlobalCompilationContext ctx) throws Exce ctx.compileTo(STRING_AS_SHORT_SARL, STRING_AS_SHORT_JAVA); } + private static final String STRING_AS_SHORTOBJ_SARL = multilineString( + "class A {", + " def fct(left : String) : Short {", + " left as Short", + " }", + "}"); + + private static final String STRING_AS_SHORTOBJ_JAVA = multilineString( + "import io.sarl.lang.annotation.SarlElementType;", + "import io.sarl.lang.annotation.SarlSpecification;", + "import io.sarl.lang.annotation.SyntheticMember;", + "import io.sarl.lang.scoping.extensions.cast.PrimitiveCastExtensions;", + "import org.eclipse.xtext.xbase.lib.Pure;", + "", + "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", + "@SarlElementType(" + SarlPackage.SARL_CLASS + ")", + "@SuppressWarnings(\"all\")", + "public class A {", + " @Pure", + " public Short fct(final String left) {", + " return (left == null ? 0 : PrimitiveCastExtensions.toShort(left));", + " }", + " ", + " @SyntheticMember", + " public A() {", + " super();", + " }", + "}", + ""); + + @Test + @Tag("sarlValidation") + public void string_as_Short_issues() throws Exception { + validate(getValidationHelper(), getInjector(), file(getParseHelper(), STRING_AS_SHORTOBJ_SARL)) + .assertNoErrors( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.INVALID_CAST) + .assertNoWarnings( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.OBSOLETE_CAST) + .assertWarning( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + IssueCodes.POTENTIAL_INEFFICIENT_VALUE_CONVERSION, + "'toShort'"); + } + + @GlobalCompilationTestContribution + @Tag("compileToJava") + public void string_as_Short(ResourceSetGlobalCompilationContext ctx) throws Exception { + ctx.compileTo(STRING_AS_SHORTOBJ_SARL, STRING_AS_SHORTOBJ_JAVA); + } + private static final String STRING_AS_INT_SARL = multilineString( "class A {", " def fct(left : String) : int {", @@ -353,10 +457,57 @@ public void string_as_int_issues() throws Exception { "'intValue'"); } + + private static final String STRING_AS_INTEGER_SARL = multilineString( + "class A {", + " def fct(left : String) : Integer {", + " left as Integer", + " }", + "}"); + + private static final String STRING_AS_INTEGER_JAVA = multilineString( + "import io.sarl.lang.annotation.SarlElementType;", + "import io.sarl.lang.annotation.SarlSpecification;", + "import io.sarl.lang.annotation.SyntheticMember;", + "import io.sarl.lang.scoping.extensions.cast.PrimitiveCastExtensions;", + "import org.eclipse.xtext.xbase.lib.Pure;", + "", + "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", + "@SarlElementType(" + SarlPackage.SARL_CLASS + ")", + "@SuppressWarnings(\"all\")", + "public class A {", + " @Pure", + " public Integer fct(final String left) {", + " return (left == null ? 0 : PrimitiveCastExtensions.toInteger(left));", + " }", + " ", + " @SyntheticMember", + " public A() {", + " super();", + " }", + "}", + ""); + + @Test + @Tag("sarlValidation") + public void string_as_integer_issues() throws Exception { + validate(getValidationHelper(), getInjector(), file(getParseHelper(), STRING_AS_INTEGER_SARL)) + .assertNoErrors( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.INVALID_CAST) + .assertNoWarnings( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.OBSOLETE_CAST) + .assertWarning( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + IssueCodes.POTENTIAL_INEFFICIENT_VALUE_CONVERSION, + "'toInteger'"); + } + @GlobalCompilationTestContribution @Tag("compileToJava") - public void string_as_int(ResourceSetGlobalCompilationContext ctx) throws Exception { - ctx.compileTo(STRING_AS_INT_SARL, STRING_AS_INT_JAVA); + public void string_as_integer(ResourceSetGlobalCompilationContext ctx) throws Exception { + ctx.compileTo(STRING_AS_INTEGER_SARL, STRING_AS_INTEGER_JAVA); } private static final String STRING_AS_LONG_SARL = multilineString( @@ -411,6 +562,59 @@ public void string_as_long(ResourceSetGlobalCompilationContext ctx) throws Excep ctx.compileTo(STRING_AS_LONG_SARL, STRING_AS_LONG_JAVA); } + + private static final String STRING_AS_LONGOBJ_SARL = multilineString( + "class A {", + " def fct(left : String) : Long {", + " left as Long", + " }", + "}"); + + private static final String STRING_AS_LONGOBJ_JAVA = multilineString( + "import io.sarl.lang.annotation.SarlElementType;", + "import io.sarl.lang.annotation.SarlSpecification;", + "import io.sarl.lang.annotation.SyntheticMember;", + "import io.sarl.lang.scoping.extensions.cast.PrimitiveCastExtensions;", + "import org.eclipse.xtext.xbase.lib.Pure;", + "", + "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", + "@SarlElementType(" + SarlPackage.SARL_CLASS + ")", + "@SuppressWarnings(\"all\")", + "public class A {", + " @Pure", + " public Long fct(final String left) {", + " return (left == null ? 0 : PrimitiveCastExtensions.toLong(left));", + " }", + " ", + " @SyntheticMember", + " public A() {", + " super();", + " }", + "}", + ""); + + @Test + @Tag("sarlValidation") + public void string_as_Long_issues() throws Exception { + validate(getValidationHelper(), getInjector(), file(getParseHelper(), STRING_AS_LONGOBJ_SARL)) + .assertNoErrors( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.INVALID_CAST) + .assertNoWarnings( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.OBSOLETE_CAST) + .assertWarning( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + IssueCodes.POTENTIAL_INEFFICIENT_VALUE_CONVERSION, + "'toLong'"); + } + + @GlobalCompilationTestContribution + @Tag("compileToJava") + public void string_as_Long(ResourceSetGlobalCompilationContext ctx) throws Exception { + ctx.compileTo(STRING_AS_LONGOBJ_SARL, STRING_AS_LONGOBJ_JAVA); + } + private static final String STRING_AS_FLOAT_SARL = multilineString( "class A {", " def fct(left : String) : float {", @@ -463,6 +667,58 @@ public void string_as_float(ResourceSetGlobalCompilationContext ctx) throws Exce ctx.compileTo(STRING_AS_FLOAT_SARL, STRING_AS_FLOAT_JAVA); } + private static final String STRING_AS_FLOATOBJ_SARL = multilineString( + "class A {", + " def fct(left : String) : Float {", + " left as Float", + " }", + "}"); + + private static final String STRING_AS_FLOATOBJ_JAVA = multilineString( + "import io.sarl.lang.annotation.SarlElementType;", + "import io.sarl.lang.annotation.SarlSpecification;", + "import io.sarl.lang.annotation.SyntheticMember;", + "import io.sarl.lang.scoping.extensions.cast.PrimitiveCastExtensions;", + "import org.eclipse.xtext.xbase.lib.Pure;", + "", + "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", + "@SarlElementType(" + SarlPackage.SARL_CLASS + ")", + "@SuppressWarnings(\"all\")", + "public class A {", + " @Pure", + " public Float fct(final String left) {", + " return (left == null ? 0 : PrimitiveCastExtensions.toFloat(left));", + " }", + " ", + " @SyntheticMember", + " public A() {", + " super();", + " }", + "}", + ""); + + @Test + @Tag("sarlValidation") + public void string_as_Float_issues() throws Exception { + validate(getValidationHelper(), getInjector(), file(getParseHelper(), STRING_AS_FLOATOBJ_SARL)) + .assertNoErrors( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.INVALID_CAST) + .assertNoWarnings( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.OBSOLETE_CAST) + .assertWarning( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + IssueCodes.POTENTIAL_INEFFICIENT_VALUE_CONVERSION, + "'toFloat'"); + } + + @GlobalCompilationTestContribution + @Tag("compileToJava") + public void string_as_Float(ResourceSetGlobalCompilationContext ctx) throws Exception { + ctx.compileTo(STRING_AS_FLOATOBJ_SARL, STRING_AS_FLOATOBJ_JAVA); + } + private static final String STRING_AS_DOUBLE_SARL = multilineString( "class A {", " def fct(left : String) : double {", @@ -515,6 +771,58 @@ public void string_as_double(ResourceSetGlobalCompilationContext ctx) throws Exc ctx.compileTo(STRING_AS_DOUBLE_SARL, STRING_AS_DOUBLE_JAVA); } + private static final String STRING_AS_DOUBLEOBJ_SARL = multilineString( + "class A {", + " def fct(left : String) : Double {", + " left as Double", + " }", + "}"); + + private static final String STRING_AS_DOUBLEOBJ_JAVA = multilineString( + "import io.sarl.lang.annotation.SarlElementType;", + "import io.sarl.lang.annotation.SarlSpecification;", + "import io.sarl.lang.annotation.SyntheticMember;", + "import io.sarl.lang.scoping.extensions.cast.PrimitiveCastExtensions;", + "import org.eclipse.xtext.xbase.lib.Pure;", + "", + "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", + "@SarlElementType(" + SarlPackage.SARL_CLASS + ")", + "@SuppressWarnings(\"all\")", + "public class A {", + " @Pure", + " public Double fct(final String left) {", + " return (left == null ? 0 : PrimitiveCastExtensions.toDouble(left));", + " }", + " ", + " @SyntheticMember", + " public A() {", + " super();", + " }", + "}", + ""); + + @Test + @Tag("sarlValidation") + public void string_as_Double_issues() throws Exception { + validate(getValidationHelper(), getInjector(), file(getParseHelper(), STRING_AS_DOUBLEOBJ_SARL)) + .assertNoErrors( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.INVALID_CAST) + .assertNoWarnings( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + org.eclipse.xtext.xbase.validation.IssueCodes.OBSOLETE_CAST) + .assertWarning( + TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(), + IssueCodes.POTENTIAL_INEFFICIENT_VALUE_CONVERSION, + "'toDouble'"); + } + + @GlobalCompilationTestContribution + @Tag("compileToJava") + public void string_as_Double(ResourceSetGlobalCompilationContext ctx) throws Exception { + ctx.compileTo(STRING_AS_DOUBLEOBJ_SARL, STRING_AS_DOUBLEOBJ_JAVA); + } + private static final String STRING_AS_ATOMICINTEGER_SARL = multilineString( "import java.util.concurrent.atomic.AtomicInteger", "class A {",