You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/10/01 08:29:19 UTC

[groovy] branch master updated: Prefer asSubclass instead of casting the result of newInstance

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

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


The following commit(s) were added to refs/heads/master by this push:
     new f16c7da  Prefer asSubclass instead of casting the result of newInstance
f16c7da is described below

commit f16c7da3cfc114ff3ab91af2a8e9aab24823701f
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Oct 1 16:12:44 2020 +1000

    Prefer asSubclass instead of casting the result of newInstance
---
 .../groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java    | 2 +-
 subprojects/groovy-test/src/main/java/groovy/test/AllTestSuite.java     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java b/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java
index a164e19..c37ad9f 100644
--- a/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java
+++ b/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java
@@ -387,7 +387,7 @@ public class TemplateServlet extends AbstractHttpServlet {
             return new SimpleTemplateEngine();
         }
         try {
-            return (TemplateEngine) Class.forName(name).getDeclaredConstructor().newInstance();
+            return Class.forName(name).asSubclass(TemplateEngine.class).getDeclaredConstructor().newInstance();
         } catch (InstantiationException | InvocationTargetException e) {
             log("Could not instantiate template engine: " + name, e);
         } catch (IllegalAccessException e) {
diff --git a/subprojects/groovy-test/src/main/java/groovy/test/AllTestSuite.java b/subprojects/groovy-test/src/main/java/groovy/test/AllTestSuite.java
index 2ec63ac..757be72 100644
--- a/subprojects/groovy-test/src/main/java/groovy/test/AllTestSuite.java
+++ b/subprojects/groovy-test/src/main/java/groovy/test/AllTestSuite.java
@@ -88,7 +88,7 @@ public class AllTestSuite extends TestSuite {
     static { // this is only needed since the Groovy Build compiles *.groovy files after *.java files
         try {
             // TODO: dk: make FileNameFinder injectable
-            finder = (IFileNameFinder) Class.forName("groovy.ant.FileNameFinder").getDeclaredConstructor().newInstance();
+            finder = Class.forName("groovy.ant.FileNameFinder").asSubclass(IFileNameFinder.class).getDeclaredConstructor().newInstance();
         } catch (Exception e) {
             throw new RuntimeException("Cannot find and instantiate class FileNameFinder", e);
         }