This is an automated email from the ASF dual-hosted git repository. paulk pushed a commit to branch GROOVY_4_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
commit dc63ea353104b8f626e77479340966099326a40e Author: Paul King <[email protected]> AuthorDate: Wed Apr 13 23:09:40 2022 +1000 remove deprecation warnings warning --- src/main/java/groovy/lang/GroovyShell.java | 4 ++-- src/main/java/groovy/util/FactoryBuilderSupport.java | 7 ++++++- src/main/java/groovy/util/ObjectGraphBuilder.java | 8 +++++++- src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java | 8 ++++---- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/groovy/lang/GroovyShell.java b/src/main/java/groovy/lang/GroovyShell.java index 2efc40ce92..57fa108c60 100644 --- a/src/main/java/groovy/lang/GroovyShell.java +++ b/src/main/java/groovy/lang/GroovyShell.java @@ -257,8 +257,8 @@ public class GroovyShell extends GroovyObjectSupport { try { Script script = InvokerHelper.newScript(scriptClass, context); return script.run(); - } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { - // ignore instantiation errors,, try to do main + } catch (InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { + // ignore instantiation errors, try to do main } } try { diff --git a/src/main/java/groovy/util/FactoryBuilderSupport.java b/src/main/java/groovy/util/FactoryBuilderSupport.java index 2c6ad9c7a9..82764bf4ba 100644 --- a/src/main/java/groovy/util/FactoryBuilderSupport.java +++ b/src/main/java/groovy/util/FactoryBuilderSupport.java @@ -22,6 +22,7 @@ import groovy.lang.Binding; import groovy.lang.Closure; import groovy.lang.DelegatingMetaClass; import groovy.lang.GroovyClassLoader; +import groovy.lang.GroovyRuntimeException; import groovy.lang.MetaClass; import groovy.lang.MissingMethodException; import groovy.lang.MissingPropertyException; @@ -659,7 +660,11 @@ public abstract class FactoryBuilderSupport extends Binding { if (checkValueIsTypeNotString(value, name, beanClass)) { return value; } else { - return beanClass.newInstance(); + try { + return beanClass.getDeclaredConstructor().newInstance(); + } catch (NoSuchMethodException | InvocationTargetException e) { + throw new GroovyRuntimeException("Failed to register bean factory", e); + } } } }); diff --git a/src/main/java/groovy/util/ObjectGraphBuilder.java b/src/main/java/groovy/util/ObjectGraphBuilder.java index 7cab1c75fe..2e82132265 100644 --- a/src/main/java/groovy/util/ObjectGraphBuilder.java +++ b/src/main/java/groovy/util/ObjectGraphBuilder.java @@ -20,10 +20,12 @@ package groovy.util; import groovy.lang.Closure; import groovy.lang.GString; +import groovy.lang.GroovyRuntimeException; import groovy.lang.MetaProperty; import groovy.lang.MissingPropertyException; import org.codehaus.groovy.runtime.InvokerHelper; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; @@ -452,7 +454,11 @@ public class ObjectGraphBuilder extends FactoryBuilderSupport { @Override public Object newInstance(Class klass, Map attributes) throws InstantiationException, IllegalAccessException { - return klass.newInstance(); + try { + return klass.getDeclaredConstructor().newInstance(); + } catch (InvocationTargetException | NoSuchMethodException e) { + throw new GroovyRuntimeException("Unable to create instance resolver", e); + } } } diff --git a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java index 42c760be89..66926668b3 100644 --- a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java +++ b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java @@ -470,14 +470,14 @@ public class InvokerHelper { return script; } - public static Script newScript(Class<?> scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException { + public static Script newScript(Class<? extends Script> scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { Script script; try { - Constructor constructor = scriptClass.getConstructor(Binding.class); - script = (Script) constructor.newInstance(context); + Constructor<? extends Script> constructor = scriptClass.getConstructor(Binding.class); + script = constructor.newInstance(context); } catch (NoSuchMethodException e) { // Fallback for non-standard "Script" classes. - script = (Script) scriptClass.newInstance(); + script = scriptClass.getDeclaredConstructor().newInstance(); script.setBinding(context); } return script;
