Skip to content

Commit

Permalink
Adjust constructor parameter names of inner classes
Browse files Browse the repository at this point in the history
As of jdk8, the debug info of inner classes compiled with javac contains the
"this$n" that references the enclosing instance.
  • Loading branch information
ahirata committed Jan 3, 2015
1 parent c53e753 commit 935214c
Showing 1 changed file with 13 additions and 1 deletion.
Expand Up @@ -118,7 +118,8 @@ public static <T> List<String> filterConstructorParameters(Class<T> target, Coll
Paranamer paranamer = new AdaptiveParanamer();

for (Constructor<T> constructor : new Mirror().on(target).reflectAll().constructors()) {
List<String> constructorParameterNames = Arrays.asList(paranamer.lookupParameterNames(constructor, false));
List<String> constructorParameterNames = lookupParameterNames(paranamer, constructor);

if (result.size() < constructorParameterNames.size()) {
if (names.containsAll(constructorParameterNames)
&& constructorParameterTypesMatch(target, constructorParameterNames, Arrays.asList(constructor.getParameterTypes())))
Expand All @@ -127,6 +128,17 @@ && constructorParameterTypesMatch(target, constructorParameterNames, Arrays.asLi
}
return result;
}

private static <T> List<String> lookupParameterNames(Paranamer paranamer, Constructor<T> 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<String> parameterNames, List<Class<?>> parameterTypes) {
for (int idx = 0; idx < parameterNames.size(); idx++) {
Expand Down

0 comments on commit 935214c

Please sign in to comment.