You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/09/02 14:40:05 UTC

[groovy] branch GROOVY_3_0_X updated (f02a643ac7 -> d66a8a4d2d)

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a change to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


 discard f02a643ac7 GROOVY-10744: STC: assignment of primitives to wrapper-type interfaces
     new d66a8a4d2d GROOVY-10744: STC: assignment of primitives to wrapper-type interfaces

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (f02a643ac7)
            \
             N -- N -- N   refs/heads/GROOVY_3_0_X (d66a8a4d2d)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../groovy/transform/stc/StaticTypeCheckingVisitor.java      |  6 +++---
 src/test/groovy/transform/stc/GenericsSTCTest.groovy         | 12 ++++++------
 2 files changed, 9 insertions(+), 9 deletions(-)


[groovy] 01/01: GROOVY-10744: STC: assignment of primitives to wrapper-type interfaces

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit d66a8a4d2d7195abab78a68ee68273eef6266d48
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Sep 2 07:37:58 2022 -0500

    GROOVY-10744: STC: assignment of primitives to wrapper-type interfaces
---
 .../transform/stc/StaticTypeCheckingSupport.java   |   7 +-
 .../transform/stc/StaticTypeCheckingVisitor.java   |  52 +++---
 .../groovy/transform/stc/GenericsSTCTest.groovy    |  12 +-
 .../groovy/transform/stc/STCAssignmentTest.groovy  | 201 +++++++++++++--------
 4 files changed, 158 insertions(+), 114 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 0faa278a93..d08be7835f 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -701,9 +701,6 @@ public abstract class StaticTypeCheckingSupport {
             return true;
         }
 
-        // simple check on being subclass
-        if (right.isDerivedFrom(left) || (left.isInterface() && right.implementsInterface(left))) return true;
-
         // if left and right are primitives or numbers allow
         if (isPrimitiveType(leftRedirect) && isPrimitiveType(rightRedirect)) return true;
         if (isNumberType(leftRedirect) && isNumberType(rightRedirect)) return true;
@@ -713,6 +710,10 @@ public abstract class StaticTypeCheckingSupport {
             return true;
         }
 
+        if (WideningCategories.implementsInterfaceOrSubclassOf(getWrapper(right), left)) {
+            return true;
+        }
+
         if (GROOVY_OBJECT_TYPE.equals(leftRedirect) && isBeingCompiled(right)) {
             return true;
         }
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 772e153e48..7154253555 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1297,21 +1297,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         checkGroovyConstructorMap(leftExpression, leftRedirect, mapExpression);
     }
 
-    private void checkTypeGenerics(final ClassNode leftExpressionType, final ClassNode wrappedRHS, final Expression rightExpression) {
-        // last, check generic type information to ensure that inferred types are compatible
-        if (!leftExpressionType.isUsingGenerics()) return;
-        // example of incomplete type info: "List<Type> list = new LinkedList()"
-        // we assume arity related errors are already handled here
-        if (missesGenericsTypes(wrappedRHS)) return;
-
-        GenericsType gt = GenericsUtils.buildWildcardType(leftExpressionType);
-        if (UNKNOWN_PARAMETER_TYPE.equals(wrappedRHS) ||
-                gt.isCompatibleWith(wrappedRHS) ||
-                isNullConstant(rightExpression)) return;
-
-        addStaticTypeError("Incompatible generic argument types. Cannot assign "
-                + wrappedRHS.toString(false)
-                + " to: " + leftExpressionType.toString(false), rightExpression);
+    private void checkTypeGenerics(final ClassNode leftExpressionType, final ClassNode rightExpressionType, final Expression rightExpression) {
+        if (leftExpressionType.isUsingGenerics()
+                && !missesGenericsTypes(rightExpressionType)
+                && !isNullConstant(rightExpression) && !UNKNOWN_PARAMETER_TYPE.equals(rightExpressionType)
+                && !GenericsUtils.buildWildcardType(leftExpressionType).isCompatibleWith(wrapTypeIfNecessary(rightExpressionType)))
+            addStaticTypeError("Incompatible generic argument types. Cannot assign " + prettyPrintType(rightExpressionType) + " to: " + prettyPrintType(leftExpressionType), rightExpression);
     }
 
     private boolean hasGStringStringError(final ClassNode leftExpressionType, final ClassNode wrappedRHS, final Expression rightExpression) {
@@ -1323,31 +1314,32 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         return false;
     }
 
-    protected void typeCheckAssignment(final BinaryExpression assignmentExpression, final Expression leftExpression, final ClassNode leftExpressionType, final Expression rightExpression, final ClassNode inferredRightExpressionTypeOrig) {
-        ClassNode inferredRightExpressionType = inferredRightExpressionTypeOrig;
+    protected void typeCheckAssignment(final BinaryExpression assignmentExpression, final Expression leftExpression, final ClassNode leftExpressionType, final Expression rightExpression, final ClassNode rightExpressionType) {
         if (!typeCheckMultipleAssignmentAndContinue(leftExpression, rightExpression)) return;
 
         // TODO: need errors for write-only too!
         if (addedReadOnlyPropertyError(leftExpression)) return;
 
-        ClassNode leftRedirect = leftExpressionType.redirect();
-        // see if instanceof applies
+        ClassNode rTypeInferred; // see if any instanceof exists
         if (rightExpression instanceof VariableExpression && hasInferredReturnType(rightExpression) && assignmentExpression.getOperation().getType() == EQUAL) {
-            inferredRightExpressionType = getInferredReturnType(rightExpression);
+            rTypeInferred = getInferredReturnType(rightExpression);
+        } else {
+            rTypeInferred = rightExpressionType;
         }
-        ClassNode wrappedRHS = adjustTypeForSpreading(inferredRightExpressionType, leftExpression);
+        ClassNode rTypeAdjusted = adjustTypeForSpreading(rTypeInferred, leftExpression);
 
-        // check types are compatible for assignment
-        if (!checkCompatibleAssignmentTypes(leftRedirect, wrappedRHS, rightExpression)) {
-            if (!extension.handleIncompatibleAssignment(leftExpressionType, inferredRightExpressionType, assignmentExpression)) {
-                addAssignmentError(leftExpressionType, inferredRightExpressionType, assignmentExpression.getRightExpression());
+        if (!checkCompatibleAssignmentTypes(leftExpressionType, rTypeAdjusted, rightExpression)) {
+            if (!extension.handleIncompatibleAssignment(leftExpressionType, rTypeAdjusted, assignmentExpression)) {
+                addAssignmentError(leftExpressionType, rTypeInferred, rightExpression);
             }
         } else {
-            addPrecisionErrors(leftRedirect, leftExpressionType, inferredRightExpressionType, rightExpression);
-            addListAssignmentConstructorErrors(leftRedirect, leftExpressionType, inferredRightExpressionType, rightExpression, assignmentExpression);
-            addMapAssignmentConstructorErrors(leftRedirect, leftExpression, rightExpression);
-            if (hasGStringStringError(leftExpressionType, wrappedRHS, rightExpression)) return;
-            checkTypeGenerics(leftExpressionType, wrappedRHS, rightExpression);
+            ClassNode lTypeRedirect = leftExpressionType.redirect();
+            addPrecisionErrors(lTypeRedirect, leftExpressionType, rTypeAdjusted, rightExpression);
+            addListAssignmentConstructorErrors(lTypeRedirect, leftExpressionType, rTypeInferred, rightExpression, assignmentExpression);
+            addMapAssignmentConstructorErrors(lTypeRedirect, leftExpression, rightExpression);
+            if (!hasGStringStringError(leftExpressionType, rTypeAdjusted, rightExpression)) {
+                checkTypeGenerics(leftExpressionType, rTypeAdjusted, rightExpression);
+            }
         }
     }
 
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 3d6950c8d7..bd966006fb 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -526,7 +526,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-8638
-    void testReturnTypeInferenceWithMethodGenerics18() {
+    void _testReturnTypeInferenceWithMethodGenerics18() {
         assertScript '''
             @Grab('com.google.guava:guava:31.1-jre')
             import com.google.common.collect.*
@@ -1311,15 +1311,15 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         }
     }
 
-    // GROOVY-10367
+    @NotYetImplemented // GROOVY-10367
     void testDiamondInferrenceFromConstructor26() {
         assertScript '''
             @groovy.transform.TupleConstructor(defaults=false)
             class C<X, Y extends X> { // works without Y
-              X x
+                X x
             }
             def <Z extends Number> void test(Z z) {
-              z = new C<>(z).x // Cannot assign value of type Object to variable of type Z
+                z = new C<>(z).x // Cannot assign value of type X to variable of type Z
             }
             test(null)
         '''
@@ -1967,7 +1967,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-10525
-    void testMethodCallWithClassParameterUnbounded2() {
+    void _testMethodCallWithClassParameterUnbounded2() {
         assertScript '''
             @Grab('javax.validation:validation-api:1.1.0.Final')
             import javax.validation.Validator
@@ -4161,7 +4161,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-6760
-    void testGenericsAtMethodLevelWithGenericsInTypeOfGenericType() {
+    void _testGenericsAtMethodLevelWithGenericsInTypeOfGenericType() {
         assertScript '''
             @Grab('com.netflix.rxjava:rxjava-core:0.18.1')
             import rx.Observable
diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
index b83c8eb5d5..3ab0838d50 100644
--- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
@@ -23,43 +23,49 @@ package groovy.transform.stc
  */
 class STCAssignmentTest extends StaticTypeCheckingTestCase {
 
-    void testAssignmentFailure() {
+    void testAssignmentFailure1() {
         shouldFailWithMessages '''
             int x = new Object()
-        ''', 'Cannot assign value of type java.lang.Object to variable of type int'
+        ''',
+        'Cannot assign value of type java.lang.Object to variable of type int'
     }
 
     void testAssignmentFailure2() {
         shouldFailWithMessages '''
             Set set = new Object()
-        ''', 'Cannot assign value of type java.lang.Object to variable of type java.util.Set'
+        ''',
+        'Cannot assign value of type java.lang.Object to variable of type java.util.Set'
     }
 
     void testAssignmentFailure3() {
         shouldFailWithMessages '''
             Set set = new Integer(2)
-        ''', 'Cannot assign value of type java.lang.Integer to variable of type java.util.Set'
+        ''',
+        'Cannot assign value of type java.lang.Integer to variable of type java.util.Set'
     }
 
-    void testIndirectAssignment() {
+    void testIndirectAssignment1() {
         shouldFailWithMessages '''
             def o = new Object()
             int x = o
-        ''', 'Cannot assign value of type java.lang.Object to variable of type int'
+        ''',
+        'Cannot assign value of type java.lang.Object to variable of type int'
     }
 
     void testIndirectAssignment2() {
         shouldFailWithMessages '''
             def o = new Object()
             Set set = o
-        ''', 'Cannot assign value of type java.lang.Object to variable of type java.util.Set'
+        ''',
+        'Cannot assign value of type java.lang.Object to variable of type java.util.Set'
     }
 
     void testIndirectAssignment3() {
         shouldFailWithMessages '''
             int x = 2
             Set set = x
-        ''', 'Cannot assign value of type int to variable of type java.util.Set'
+        ''',
+        'Cannot assign value of type int to variable of type java.util.Set'
     }
 
     void testAssignmentToEnum() {
@@ -69,14 +75,18 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
             e = 'a' // string to enum is implicit
             e = "${'a'}" // gstring to enum is implicit too
         '''
-    }
-
-    void testAssignmentToEnumFailure() {
         shouldFailWithMessages '''
             enum MyEnum { a, b, c }
             MyEnum e = MyEnum.a
             e = 1
-        ''', 'Cannot assign value of type int to variable of type MyEnum'
+        ''',
+        'Cannot assign value of type int to variable of type MyEnum'
+    }
+
+    void testAssignmentToClass() {
+        assertScript '''
+            Class test = 'java.lang.String'
+        '''
     }
 
     void testAssignmentToString() {
@@ -97,9 +107,23 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testAssignmentToClass() {
+    // GROOVY-10744
+    void testAssignmentToSerializable() {
+        // Number implements Serializable
         assertScript '''
-            Class test = 'java.lang.String'
+            Serializable x = 0
+            Serializable y = 1.2
+            Serializable z = Math.PI
+            assert x.class.name == 'java.lang.Integer'
+            assert y.class.name == 'java.math.BigDecimal'
+            assert z.class.name == 'java.lang.Double'
+        '''
+        // Boolean implements Serializable and Comparable<Boolean>
+        assertScript '''
+            Serializable x = true
+            Comparable<Boolean> y = false
+            assert x.class.name == 'java.lang.Boolean'
+            assert y.class.name == 'java.lang.Boolean'
         '''
     }
 
@@ -121,14 +145,16 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         shouldFailWithMessages '''
             int i = 0
             i += new Object()
-        ''', 'Cannot find matching method int#plus(java.lang.Object)'
+        ''',
+        'Cannot find matching method int#plus(java.lang.Object)'
     }
 
     void testIntMinusEqualsObject() {
         shouldFailWithMessages '''
             int i = 0
             i -= new Object()
-        ''', 'Cannot find matching method int#minus(java.lang.Object)'
+        ''',
+        'Cannot find matching method int#minus(java.lang.Object)'
     }
 
     void testStringPlusEqualsString() {
@@ -201,65 +227,69 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testPossibleLooseOfPrecision() {
+    void testPossibleLossOfPrecision1() {
         shouldFailWithMessages '''
             long a = Long.MAX_VALUE
             int b = a
-        ''', 'Possible loss of precision from long to int'
+        ''',
+        'Possible loss of precision from long to int'
     }
 
-    void testPossibleLooseOfPrecision2() {
+    void testPossibleLossOfPrecision2() {
         assertScript '''
             int b = 0L
         '''
     }
 
-    void testPossibleLooseOfPrecision3() {
+    void testPossibleLossOfPrecision3() {
         assertScript '''
             byte b = 127
         '''
     }
 
-    void testPossibleLooseOfPrecision4() {
+    void testPossibleLossOfPrecision4() {
         shouldFailWithMessages '''
             byte b = 128 // will not fit in a byte
-        ''', 'Possible loss of precision from int to byte'
+        ''',
+        'Possible loss of precision from int to byte'
     }
 
-    void testPossibleLooseOfPrecision5() {
+    void testPossibleLossOfPrecision5() {
         assertScript '''
             short b = 128
         '''
     }
 
-    void testPossibleLooseOfPrecision6() {
+    void testPossibleLossOfPrecision6() {
         shouldFailWithMessages '''
             short b = 32768 // will not fit in a short
-        ''', 'Possible loss of precision from int to short'
+        ''',
+        'Possible loss of precision from int to short'
     }
 
-    void testPossibleLooseOfPrecision7() {
+    void testPossibleLossOfPrecision7() {
         assertScript '''
             int b = 32768L // mark it as a long, but it fits into an int
         '''
     }
 
-    void testPossibleLooseOfPrecision8() {
+    void testPossibleLossOfPrecision8() {
         assertScript '''
             int b = 32768.0f // mark it as a float, but it fits into an int
         '''
     }
 
-    void testPossibleLooseOfPrecision9() {
+    void testPossibleLossOfPrecision9() {
         assertScript '''
             int b = 32768.0d // mark it as a double, but it fits into an int
         '''
     }
 
-    void testPossibleLooseOfPrecision10() {
+    void testPossibleLossOfPrecision10() {
         shouldFailWithMessages '''
             int b = 32768.1d
-        ''', 'Possible loss of precision from double to int'
+        ''',
+        'Possible loss of precision from double to int'
     }
 
     void testCastIntToShort() {
@@ -276,8 +306,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
 
     void testCompatibleTypeCast() {
         assertScript '''
-        String s = 'Hello'
-        ((CharSequence) s)
+            String s = 'Hello'
+            ((CharSequence) s)
         '''
     }
 
@@ -285,7 +315,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         shouldFailWithMessages '''
             String s = 'Hello'
             ((Set) s)
-        ''', 'Inconvertible types: cannot cast java.lang.String to java.util.Set'
+        ''',
+        'Inconvertible types: cannot cast java.lang.String to java.util.Set'
     }
 
     void testIncompatibleTypeCastWithAsType() {
@@ -301,7 +332,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
             def s = 'Hello'
             s = 1
             ((Set) s)
-        ''', 'Inconvertible types: cannot cast int to java.util.Set'
+        ''',
+        'Inconvertible types: cannot cast int to java.util.Set'
     }
 
     void testArrayLength() {
@@ -334,7 +366,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
             List x
             List y
             (x,y) = [1,2]
-        ''', 'Cannot assign value of type int to variable of type java.util.List'
+        ''',
+        'Cannot assign value of type int to variable of type java.util.List'
     }
 
     void testMultipleAssignmentWithoutEnoughArgs() {
@@ -342,7 +375,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
             int x
             int y
             (x,y) = [1]
-        ''', 'Incorrect number of values. Expected:2 Was:1'
+        ''',
+        'Incorrect number of values. Expected:2 Was:1'
     }
 
     void testMultipleAssignmentTooManyArgs() {
@@ -359,25 +393,26 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         shouldFailWithMessages '''
             def list = [1,2,3]
             def (x,y) = list
-        ''', 'Multiple assignments without list expressions on the right hand side are unsupported in static type checking mode'
+        ''',
+        'Multiple assignments without list expressions on the right hand side are unsupported in static type checking mode'
     }
 
     void testAssignmentToInterface() {
         assertScript '''
-            Serializable ser = 'Hello'
+            Comparable<String> x = 'x'
+            CharSequence y = 'y'
         '''
-    }
-
-    void testAssignmentToIncompatibleInterface() {
         shouldFailWithMessages '''
-            Collection ser = 'Hello'
-        ''', 'Cannot assign value of type java.lang.String to variable of type java.util.Collection'
+            Collection z = 'z'
+        ''',
+        'Cannot assign value of type java.lang.String to variable of type java.util.Collection'
     }
 
     void testTernaryOperatorAssignmentShouldFailBecauseOfIncompatibleGenericTypes() {
         shouldFailWithMessages '''
             List<Integer> foo = true?new LinkedList<String>():new LinkedList<Integer>();
-        ''', 'Incompatible generic argument types. Cannot assign java.util.LinkedList <? extends java.io.Serializable <? extends java.io.Serializable>> to: java.util.List <Integer>'
+        ''',
+        'Incompatible generic argument types. Cannot assign java.util.LinkedList <? extends java.io.Serializable <? extends java.io.Serializable>> to: java.util.List <Integer>'
     }
 
     void testCastStringToChar() {
@@ -389,13 +424,15 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
     void testCastStringLongerThan1CharToChar() {
         shouldFailWithMessages '''
             char c = 'aa'
-        ''','Cannot assign value of type java.lang.String to variable of type char'
+        ''',
+        'Cannot assign value of type java.lang.String to variable of type char'
     }
 
     void testCastNullToChar() {
         shouldFailWithMessages '''
             char c = null
-        ''', 'Cannot assign value of type java.lang.Object to variable of type char'
+        ''',
+        'Cannot assign value of type java.lang.Object to variable of type char'
     }
 
     // GROOVY-6577
@@ -423,7 +460,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
     void testCastStringLongerThan1CharToCharacter() {
         shouldFailWithMessages '''
             Character c = 'aa'
-        ''','Cannot assign value of type java.lang.String to variable of type java.lang.Character'
+        ''',
+        'Cannot assign value of type java.lang.String to variable of type java.lang.Character'
     }
 
     void testAssignNullToCharacter() {
@@ -457,13 +495,15 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
     void testCastStringLongerThan1ToCharWithCast() {
         shouldFailWithMessages '''
             def c = (char) 'aa'
-        ''', 'Inconvertible types: cannot cast java.lang.String to char'
+        ''',
+        'Inconvertible types: cannot cast java.lang.String to char'
     }
 
     void testCastNullToCharWithCast() {
         shouldFailWithMessages '''
             def c = (char) null
-        ''', 'Inconvertible types: cannot cast java.lang.Object to char'
+        ''',
+        'Inconvertible types: cannot cast java.lang.Object to char'
     }
 
     void testCastStringToCharacterWithCast() {
@@ -475,7 +515,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
     void testCastStringLongerThan1ToCharacterWithCast() {
         shouldFailWithMessages '''
             def c = (Character) 'aa'
-        ''', 'Inconvertible types: cannot cast java.lang.String to java.lang.Character'
+        ''',
+        'Inconvertible types: cannot cast java.lang.String to java.lang.Character'
     }
 
     void testCastNullToCharacterWithCast() {
@@ -487,10 +528,7 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
     void testCastObjectToSubclass() {
         assertScript '''
             Object o = null
-            try {
-                ((Integer)o).intValue()
-            } catch (NullPointerException e) {
-            }
+            ((Integer) o)?.intValue()
         '''
     }
 
@@ -504,7 +542,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
                 x = '123'
             }
             x.toInteger()
-        ''', 'Cannot find matching method java.io.Serializable#toInteger()'
+        ''',
+        'Cannot find matching method java.io.Serializable#toInteger()'
     }
 
     void testIfElseBranchParameter() {
@@ -519,7 +558,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
                 x.toInteger()
             }
             foo('bar')
-        ''', 'Cannot find matching method java.lang.Object#toInteger()'
+        ''',
+        'Cannot find matching method java.lang.Object#toInteger()'
     }
 
     void testIfOnly() {
@@ -530,7 +570,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
                 x = new HashSet()
             }
             x.toInteger()
-        ''', 'Cannot find matching method java.io.Serializable#toInteger()'
+        ''',
+        'Cannot find matching method java.io.Serializable#toInteger()'
     }
 
     void testIfOnlyParameter() {
@@ -544,7 +585,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
                 x.toInteger()
             }
             foo('123')
-        ''', 'Cannot find matching method java.lang.Object#toInteger()'
+        ''',
+        'Cannot find matching method java.lang.Object#toInteger()'
     }
 
     void testIfWithCommonInterface() {
@@ -808,7 +850,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
             BigInteger a = 333
             double b = 2d
             BigInteger c = a * b
-        ''', 'Cannot assign value of type java.math.BigDecimal to variable of type java.math.BigInteger'
+        ''',
+        'Cannot assign value of type java.math.BigDecimal to variable of type java.math.BigInteger'
     }
 
     void testBigIntegerMultInteger() {
@@ -875,18 +918,21 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
                 void setInteger(Character c) { integer = (c as int) }
             }
             new Pogo().integer++
-        ''', 'Cannot assign value of type java.lang.Integer to variable of type java.lang.Character'
+        ''',
+        'Cannot assign value of type java.lang.Integer to variable of type java.lang.Character'
     }
 
     void testPostfixOnObject() {
         shouldFailWithMessages '''
             Object o = new Object()
             o++
-        ''', 'Cannot find matching method java.lang.Object#next()'
+        ''',
+        'Cannot find matching method java.lang.Object#next()'
         shouldFailWithMessages '''
             Object o = new Object()
             o--
-        ''', 'Cannot find matching method java.lang.Object#previous()'
+        ''',
+        'Cannot find matching method java.lang.Object#previous()'
     }
 
     void testPrefixOnInt() {
@@ -915,11 +961,13 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         shouldFailWithMessages '''
             Object o = new Object()
             ++o
-        ''', 'Cannot find matching method java.lang.Object#next()'
+        ''',
+        'Cannot find matching method java.lang.Object#next()'
         shouldFailWithMessages '''
             Object o = new Object()
             --o
-        ''', 'Cannot find matching method java.lang.Object#previous()'
+        ''',
+        'Cannot find matching method java.lang.Object#previous()'
     }
 
     void testAssignArray() {
@@ -940,13 +988,15 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         shouldFailWithMessages '''
             String[] src = ['a','b','c']
             (Set[]) src
-        ''', 'Inconvertible types: cannot cast java.lang.String[] to java.util.Set[]'
+        ''',
+        'Inconvertible types: cannot cast java.lang.String[] to java.util.Set[]'
     }
 
     void testIncompatibleToArray() {
         shouldFailWithMessages '''
             (Set[]) ['a','b','c'].toArray(new String[3])
-        ''', 'Inconvertible types: cannot cast java.lang.String[] to java.util.Set[]'
+        ''',
+        'Inconvertible types: cannot cast java.lang.String[] to java.util.Set[]'
     }
 
     // GROOVY-5535
@@ -994,7 +1044,8 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         shouldFailWithMessages '''
             Object o
             int[] array = o
-        ''', 'Cannot assign value of type java.lang.Object to variable of type int[]'
+        ''',
+        'Cannot assign value of type java.lang.Object to variable of type int[]'
     }
 
     // GROOVY-7015
@@ -1034,19 +1085,19 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
 
     void testIntegerArraySmartType() {
         assertScript '''
-        def m() {
-            def a  = 1
-            Integer[] b = [a]
-        }
+            def m() {
+                def a  = 1
+                Integer[] b = [a]
+            }
         '''
     }
 
     void testIntegerSecondDimArraySmartType() {
         assertScript '''
-        def m() {
-            def a = new int[5]
-            int[][] b = [a]
-        }
+            def m() {
+                def a = new int[5]
+                int[][] b = [a]
+            }
         '''
     }