From 900b4aab708e106bc8849055e93f42265c37e2de Mon Sep 17 00:00:00 2001 From: Nathan Sweet Date: Fri, 1 Nov 2013 16:15:35 +0000 Subject: [PATCH] Fixed issue #16, package private constructor access. --- .../reflectasm/ConstructorAccess.java | 4 +-- .../reflectasm/ConstructorAccessTest.java | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/com/esotericsoftware/reflectasm/ConstructorAccess.java b/src/com/esotericsoftware/reflectasm/ConstructorAccess.java index 5fff31a..e639923 100644 --- a/src/com/esotericsoftware/reflectasm/ConstructorAccess.java +++ b/src/com/esotericsoftware/reflectasm/ConstructorAccess.java @@ -49,14 +49,14 @@ static public ConstructorAccess get (Class type) { if (!isNonStaticMemberClass) { enclosingClassNameInternal = null; try { - type.getConstructor((Class[])null); + type.getDeclaredConstructor((Class[])null); } catch (Exception ex) { throw new RuntimeException("Class cannot be created (missing no-arg constructor): " + type.getName()); } } else { enclosingClassNameInternal = enclosingType.getName().replace('.', '/'); try { - type.getConstructor(enclosingType); // Inner classes should have this. + type.getDeclaredConstructor(enclosingType); // Inner classes should have this. } catch (Exception ex) { throw new RuntimeException("Non-static member class cannot be created (missing enclosing class constructor): " + type.getName()); diff --git a/test/com/esotericsoftware/reflectasm/ConstructorAccessTest.java b/test/com/esotericsoftware/reflectasm/ConstructorAccessTest.java index 9456c17..7c55694 100644 --- a/test/com/esotericsoftware/reflectasm/ConstructorAccessTest.java +++ b/test/com/esotericsoftware/reflectasm/ConstructorAccessTest.java @@ -12,6 +12,41 @@ public void testNewInstance () { assertEquals(someObject, access.newInstance()); } + public void testPackagePrivateNewInstance () { + ConstructorAccess access = ConstructorAccess.get(PackagePrivateClass.class); + PackagePrivateClass someObject = new PackagePrivateClass(); + assertEquals(someObject, access.newInstance()); + assertEquals(someObject, access.newInstance()); + assertEquals(someObject, access.newInstance()); + } + + static class PackagePrivateClass { + public String name; + public int intValue; + protected float test1; + Float test2; + private String test3; + + public boolean equals (Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + PackagePrivateClass other = (PackagePrivateClass)obj; + if (intValue != other.intValue) return false; + if (name == null) { + if (other.name != null) return false; + } else if (!name.equals(other.name)) return false; + if (Float.floatToIntBits(test1) != Float.floatToIntBits(other.test1)) return false; + if (test2 == null) { + if (other.test2 != null) return false; + } else if (!test2.equals(other.test2)) return false; + if (test3 == null) { + if (other.test3 != null) return false; + } else if (!test3.equals(other.test3)) return false; + return true; + } + } + static public class SomeClass { public String name; public int intValue;