From 4b9e3a9211b9c81148b2c2ebd54091a1fa037483 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 28 Mar 2018 17:42:55 +0200 Subject: [PATCH] Introduce failing test for SPR-16652 This commit introduces tests for looking up annotations on parameters in constructors for nested and inner classes via Spring's MethodParameter abstraction. The test for an inner class is currently disabled since it fails on JDK 8. See JIRA issue for details. Issue: SPR-16652 --- .../core/MethodParameterTests.java | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) 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 6d5dc18f27..2a1815b526 100644 --- a/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java +++ b/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 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. @@ -16,9 +16,15 @@ package org.springframework.core; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +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.*; @@ -26,6 +32,7 @@ import static org.junit.Assert.*; /** * @author Arjen Poutsma * @author Juergen Hoeller + * @author Sam Brannen */ public class MethodParameterTests { @@ -98,9 +105,48 @@ public class MethodParameterTests { new MethodParameter(method, 2); } + @Test + public void annotatedConstructorParameterInStaticNestedClass() throws Exception { + Constructor constructor = NestedClass.class.getDeclaredConstructor(String.class); + MethodParameter methodParameter = MethodParameter.forExecutable(constructor, 0); + assertEquals(String.class, methodParameter.getParameterType()); + assertNotNull("Failed to find @Param annotation", methodParameter.getParameterAnnotation(Param.class)); + assertNotNull(methodParameter.getParameterAnnotation(Param.class)); + } + + @Test + @Ignore("Disabled until SPR-16652 is resolved") + public void annotatedConstructorParameterInInnerClass() throws Exception { + Constructor constructor = InnerClass.class.getDeclaredConstructor(getClass(), String.class); + MethodParameter 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)); + } + public int method(String p1, long p2) { return 42; } + @SuppressWarnings("unused") + private static class NestedClass { + + NestedClass(@Param String s) { + } + } + + @SuppressWarnings("unused") + private class InnerClass { + + InnerClass(@Param String s) { + } + } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.PARAMETER) + private @interface Param { + } + }