Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2015-09-18 00:04:50 +0000
committerJoakim Erdfelt2015-09-18 00:05:27 +0000
commitd39677a6359e9e768b35cff7957227ea14b8bb7a (patch)
tree29bdd3135baaa941ca32ce3316d7355384f6a5a1 /jetty-util/src/main
parent014d0216187724183735e378a4a72caa4df65954 (diff)
downloadorg.eclipse.jetty.project-d39677a6359e9e768b35cff7957227ea14b8bb7a.tar.gz
org.eclipse.jetty.project-d39677a6359e9e768b35cff7957227ea14b8bb7a.tar.xz
org.eclipse.jetty.project-d39677a6359e9e768b35cff7957227ea14b8bb7a.zip
477757 - Null args in TypeUtil .call & .construct result in confusing exceptions
+ Not allowing null class references + Allowing null argument lists
Diffstat (limited to 'jetty-util/src/main')
-rw-r--r--jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java40
1 files changed, 35 insertions, 5 deletions
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java
index 6eedd3e6fe..7e703c04ec 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java
@@ -29,6 +29,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import org.eclipse.jetty.util.annotation.Name;
import org.eclipse.jetty.util.log.Log;
@@ -498,6 +499,13 @@ public class TypeUtil
public static Object call(Class<?> oClass, String methodName, Object obj, Object[] arg)
throws InvocationTargetException, NoSuchMethodException
{
+ Objects.requireNonNull(oClass,"Class cannot be null");
+ Objects.requireNonNull(methodName,"Method name cannot be null");
+ if (StringUtil.isBlank(methodName))
+ {
+ throw new IllegalArgumentException("Method name cannot be blank");
+ }
+
// Lets just try all methods for now
for (Method method : oClass.getMethods())
{
@@ -554,9 +562,17 @@ public class TypeUtil
public static Object construct(Class<?> klass, Object[] arguments) throws InvocationTargetException, NoSuchMethodException
{
+ Objects.requireNonNull(klass,"Class cannot be null");
+
for (Constructor<?> constructor : klass.getConstructors())
{
- if (constructor.getParameterTypes().length != arguments.length)
+ if (arguments == null)
+ {
+ // null arguments in .newInstance() is allowed
+ if (constructor.getParameterTypes().length != 0)
+ continue;
+ }
+ else if (constructor.getParameterTypes().length != arguments.length)
continue;
try
@@ -573,20 +589,34 @@ public class TypeUtil
public static Object construct(Class<?> klass, Object[] arguments, Map<String, Object> namedArgMap) throws InvocationTargetException, NoSuchMethodException
{
+ Objects.requireNonNull(klass,"Class cannot be null");
+ Objects.requireNonNull(namedArgMap,"Named Argument Map cannot be null");
+
for (Constructor<?> constructor : klass.getConstructors())
{
- if (constructor.getParameterTypes().length != arguments.length)
+ if (arguments == null)
+ {
+ // null arguments in .newInstance() is allowed
+ if (constructor.getParameterTypes().length != 0)
+ continue;
+ }
+ else if (constructor.getParameterTypes().length != arguments.length)
continue;
try
{
Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
- // target has no annotations
- if ( parameterAnnotations == null || parameterAnnotations.length == 0 )
+ if (arguments == null || arguments.length == 0)
+ {
+ if (LOG.isDebugEnabled())
+ LOG.debug("Constructor has no arguments");
+ return constructor.newInstance(arguments);
+ }
+ else if (parameterAnnotations == null || parameterAnnotations.length == 0)
{
if (LOG.isDebugEnabled())
- LOG.debug("Target has no parameter annotations");
+ LOG.debug("Constructor has no parameter annotations");
return constructor.newInstance(arguments);
}
else

Back to the top