Introduce ClassUtils.isInnerClass() utility method

This commit is contained in:
Sam Brannen 2018-03-29 17:45:25 +02:00
parent b165475eb6
commit 7c28152c13
2 changed files with 10 additions and 3 deletions

View File

@ -22,7 +22,6 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
@ -38,6 +37,7 @@ import kotlin.reflect.jvm.ReflectJvmMapping;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Helper class that encapsulates the specification of a method parameter, i.e. a {@link Method}
@ -519,8 +519,7 @@ public class MethodParameter {
Annotation[][] annotationArray = this.executable.getParameterAnnotations();
int index = this.parameterIndex;
if (this.executable instanceof Constructor &&
this.executable.getDeclaringClass().isMemberClass() &&
!Modifier.isStatic(this.executable.getDeclaringClass().getModifiers()) &&
ClassUtils.isInnerClass(this.executable.getDeclaringClass()) &&
annotationArray.length == this.executable.getParameterCount() - 1) {
// Bug in javac in JDK <9: annotation array excludes enclosing instance parameter
// for inner classes, so access it with the actual parameter index lowered by 1

View File

@ -209,6 +209,14 @@ public abstract class ClassUtils {
}
}
/**
* Determine if the supplied class is an <em>inner class</em>.
* @return {@code true} if the supplied class is an inner class
*/
public static boolean isInnerClass(Class<?> clazz) {
return clazz != null && clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers());
}
/**
* Replacement for {@code Class.forName()} that also returns Class instances
* for primitives (e.g. "int") and array class names (e.g. "String[]").