Skip to content

Commit

Permalink
Fixed issue #16, package private constructor access.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Nov 1, 2013
1 parent 39f9f3d commit 900b4aa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/com/esotericsoftware/reflectasm/ConstructorAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ static public <T> ConstructorAccess<T> get (Class<T> 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());
Expand Down
35 changes: 35 additions & 0 deletions test/com/esotericsoftware/reflectasm/ConstructorAccessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,41 @@ public void testNewInstance () {
assertEquals(someObject, access.newInstance());
}

public void testPackagePrivateNewInstance () {
ConstructorAccess<PackagePrivateClass> 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;
Expand Down

0 comments on commit 900b4aa

Please sign in to comment.