From 53d01392d70daef1d198e6563fd8f96d0d0166a0 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 29 Mar 2018 15:45:02 +0200 Subject: [PATCH] Workaround for inner class constructor parameter annotation bug in javac Issue: SPR-16652 --- .../springframework/core/MethodParameter.java | 20 +++++++++++++---- .../core/MethodParameterTests.java | 22 ++++++++++++------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/MethodParameter.java b/spring-core/src/main/java/org/springframework/core/MethodParameter.java index 1a6dda592d..b8116804ea 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ 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; @@ -57,6 +58,8 @@ import org.springframework.util.Assert; */ public class MethodParameter { + private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0]; + private final Executable executable; private final int parameterIndex; @@ -514,11 +517,20 @@ public class MethodParameter { Annotation[] paramAnns = this.parameterAnnotations; if (paramAnns == null) { Annotation[][] annotationArray = this.executable.getParameterAnnotations(); - if (this.parameterIndex >= 0 && this.parameterIndex < annotationArray.length) { - paramAnns = adaptAnnotationArray(annotationArray[this.parameterIndex]); + int index = this.parameterIndex; + if (this.executable instanceof Constructor && + this.executable.getDeclaringClass().isMemberClass() && + !Modifier.isStatic(this.executable.getDeclaringClass().getModifiers()) && + 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 + index = this.parameterIndex - 1; + } + if (index >= 0 && index < annotationArray.length) { + paramAnns = adaptAnnotationArray(annotationArray[index]); } else { - paramAnns = new Annotation[0]; + paramAnns = EMPTY_ANNOTATION_ARRAY; } this.parameterAnnotations = paramAnns; } diff --git a/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java b/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java index 2a1815b526..738e630c12 100644 --- a/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java +++ b/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java @@ -24,7 +24,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.*; @@ -46,7 +45,7 @@ public class MethodParameterTests { @Before - public void setUp() throws NoSuchMethodException { + public void setup() throws NoSuchMethodException { method = getClass().getMethod("method", String.class, Long.TYPE); stringParameter = new MethodParameter(method, 0); longParameter = new MethodParameter(method, 1); @@ -114,15 +113,22 @@ public class MethodParameterTests { assertNotNull(methodParameter.getParameterAnnotation(Param.class)); } - @Test - @Ignore("Disabled until SPR-16652 is resolved") + @Test // SPR-16652 public void annotatedConstructorParameterInInnerClass() throws Exception { - Constructor constructor = InnerClass.class.getDeclaredConstructor(getClass(), String.class); - MethodParameter methodParameter = MethodParameter.forExecutable(constructor, 1); + Constructor constructor = InnerClass.class.getConstructor(getClass(), String.class, Integer.class); + + MethodParameter methodParameter = MethodParameter.forExecutable(constructor, 0); + assertEquals(getClass(), methodParameter.getParameterType()); + assertNull(methodParameter.getParameterAnnotation(Param.class)); + + methodParameter = MethodParameter.forExecutable(constructor, 1); assertEquals(String.class, methodParameter.getParameterType()); - assertNull(methodParameter.getParameterAnnotation(Override.class)); // The following assertion currently fails if this test class is compiled using JDK 8. assertNotNull("Failed to find @Param annotation", methodParameter.getParameterAnnotation(Param.class)); + + methodParameter = MethodParameter.forExecutable(constructor, 2); + assertEquals(Integer.class, methodParameter.getParameterType()); + assertNull(methodParameter.getParameterAnnotation(Param.class)); } @@ -140,7 +146,7 @@ public class MethodParameterTests { @SuppressWarnings("unused") private class InnerClass { - InnerClass(@Param String s) { + public InnerClass(@Param String s, Integer i) { } }