From 935214c2456b268a99e789804486ce5c9f602538 Mon Sep 17 00:00:00 2001 From: Arthur Hirata Date: Sat, 3 Jan 2015 14:50:26 -0200 Subject: [PATCH] Adjust constructor parameter names of inner classes As of jdk8, the debug info of inner classes compiled with javac contains the "this$n" that references the enclosing instance. --- .../fixturefactory/util/ReflectionUtils.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/br/com/six2six/fixturefactory/util/ReflectionUtils.java b/src/main/java/br/com/six2six/fixturefactory/util/ReflectionUtils.java index b192ef1..18992a7 100755 --- a/src/main/java/br/com/six2six/fixturefactory/util/ReflectionUtils.java +++ b/src/main/java/br/com/six2six/fixturefactory/util/ReflectionUtils.java @@ -118,7 +118,8 @@ public static List filterConstructorParameters(Class target, Coll Paranamer paranamer = new AdaptiveParanamer(); for (Constructor constructor : new Mirror().on(target).reflectAll().constructors()) { - List constructorParameterNames = Arrays.asList(paranamer.lookupParameterNames(constructor, false)); + List constructorParameterNames = lookupParameterNames(paranamer, constructor); + if (result.size() < constructorParameterNames.size()) { if (names.containsAll(constructorParameterNames) && constructorParameterTypesMatch(target, constructorParameterNames, Arrays.asList(constructor.getParameterTypes()))) @@ -127,6 +128,17 @@ && constructorParameterTypesMatch(target, constructorParameterNames, Arrays.asLi } return result; } + + private static List lookupParameterNames(Paranamer paranamer, Constructor ctor) { + String[] paramNames = paranamer.lookupParameterNames(ctor, false); + + // as of jdk8, javac adds the "this$n" in the debug info. + if (isInnerClass(ctor.getDeclaringClass()) && paramNames[0].startsWith("this$")) { + paramNames = Arrays.copyOfRange(paramNames, 1, paramNames.length); + } + + return Arrays.asList(paramNames); + } private static boolean constructorParameterTypesMatch(Class target, List parameterNames, List> parameterTypes) { for (int idx = 0; idx < parameterNames.size(); idx++) {