From 0eb2d55da2ba1c41f784f9d86655433f538f1f12 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 3 Dec 2013 16:30:25 -0600 Subject: [PATCH] Class#getDeclaredClasses(): exclude inner classes of inner classes Inner classes can have inner classes, but getDeclaredClasses() is supposed to list *only* the immediate inner classes. Example: if class Reflection contains a class Hello that contains a class World, Reflection.class.getDeclaredClasses() must not include World in its result. Signed-off-by: Johannes Schindelin --- classpath/java/lang/Class.java | 2 +- test/Reflection.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/classpath/java/lang/Class.java b/classpath/java/lang/Class.java index 70bd4dc81e..2f9c9a72ee 100644 --- a/classpath/java/lang/Class.java +++ b/classpath/java/lang/Class.java @@ -415,7 +415,7 @@ public final class Class implements Type, AnnotatedElement { byte[] inner = table[i].inner; if (inner != null && inner.length > 1) { String name = new String(inner, 0, inner.length - 1); - if (name.startsWith(prefix)) { + if (name.startsWith(prefix) && name.indexOf('$', prefix.length()) < 0) { Class innerClass = getClassLoader().loadClass(name); result[counter++] = innerClass; } diff --git a/test/Reflection.java b/test/Reflection.java index 8d889837b4..fc9ccfed97 100644 --- a/test/Reflection.java +++ b/test/Reflection.java @@ -38,7 +38,9 @@ public class Reflection { if (! v) throw new RuntimeException(); } - private static class Hello { } + private static class Hello { + private class World { } + } private static void innerClasses() throws Exception { Class c = Reflection.class;