Fix infinite recursion in inner classes reflection methods

from Per Velschow


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137232 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-01-19 18:49:05 +00:00
parent c2d07cc3f0
commit 984bc76cd3
5 changed files with 58 additions and 37 deletions

View File

@ -105,7 +105,7 @@
* @author Stephen Colebourne
* @author Gary Gregory
* @since 1.0
* @version $Id: CompareToBuilder.java,v 1.11 2003/01/19 17:51:42 scolebourne Exp $
* @version $Id: CompareToBuilder.java,v 1.12 2003/01/19 18:49:05 scolebourne Exp $
*/
public class CompareToBuilder {
@ -247,14 +247,15 @@ private static void reflectionAppend(Object lhs, Object rhs, Class clazz, Compar
Field.setAccessible(fields, true);
for (int i = 0; i < fields.length && builder.comparison == 0; i++) {
Field f = fields[i];
if (useTransients || !Modifier.isTransient(f.getModifiers())) {
if (!Modifier.isStatic(f.getModifiers())) {
try {
builder.append(f.get(lhs), f.get(rhs));
} catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
}
if ((f.getName().indexOf('$') == -1) &&
(useTransients || !Modifier.isTransient(f.getModifiers())) &&
(!Modifier.isStatic(f.getModifiers()))) {
try {
builder.append(f.get(lhs), f.get(rhs));
} catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError("Unexpected IllegalAccessException");
}
}
}

View File

@ -108,7 +108,7 @@
* @author Stephen Colebourne
* @author Gary Gregory
* @since 1.0
* @version $Id: EqualsBuilder.java,v 1.10 2003/01/19 17:35:21 scolebourne Exp $
* @version $Id: EqualsBuilder.java,v 1.11 2003/01/19 18:49:05 scolebourne Exp $
*/
public class EqualsBuilder {
/**
@ -258,14 +258,15 @@ private static void reflectionAppend(Object lhs, Object rhs, Class clazz, Equals
Field.setAccessible(fields, true);
for (int i = 0; i < fields.length && builder.isEquals; i++) {
Field f = fields[i];
if (useTransients || !Modifier.isTransient(f.getModifiers())) {
if (!Modifier.isStatic(f.getModifiers())) {
try {
builder.append(f.get(lhs), f.get(rhs));
} catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
}
if ((f.getName().indexOf('$') == -1) &&
(useTransients || !Modifier.isTransient(f.getModifiers())) &&
(!Modifier.isStatic(f.getModifiers()))) {
try {
builder.append(f.get(lhs), f.get(rhs));
} catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError("Unexpected IllegalAccessException");
}
}
}

View File

@ -108,7 +108,7 @@
* @author Stephen Colebourne
* @author Gary Gregory
* @since 1.0
* @version $Id: HashCodeBuilder.java,v 1.9 2003/01/19 17:35:21 scolebourne Exp $
* @version $Id: HashCodeBuilder.java,v 1.10 2003/01/19 18:49:05 scolebourne Exp $
*/
public class HashCodeBuilder {
@ -337,14 +337,15 @@ private static void reflectionAppend(Object object, Class clazz, HashCodeBuilder
Field.setAccessible(fields, true);
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
if (useTransients || !Modifier.isTransient(f.getModifiers())) {
if (!Modifier.isStatic(f.getModifiers())) {
try {
builder.append(f.get(object));
} catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
}
if ((f.getName().indexOf('$') == -1) &&
(useTransients || !Modifier.isTransient(f.getModifiers())) &&
(!Modifier.isStatic(f.getModifiers()))) {
try {
builder.append(f.get(object));
} catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError("Unexpected IllegalAccessException");
}
}
}

View File

@ -113,7 +113,7 @@
* @author Stephen Colebourne
* @author Gary Gregory
* @since 1.0
* @version $Id: ToStringBuilder.java,v 1.13 2003/01/19 17:35:21 scolebourne Exp $
* @version $Id: ToStringBuilder.java,v 1.14 2003/01/19 18:49:05 scolebourne Exp $
*/
public class ToStringBuilder {
@ -373,14 +373,15 @@ private static void reflectionAppend(Object object, Class clazz, ToStringBuilder
Field.setAccessible(fields, true);
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
if (useTransients || !Modifier.isTransient(f.getModifiers())) {
if (!Modifier.isStatic(f.getModifiers())) {
try {
builder.append(f.getName(), f.get(object));
} catch (IllegalAccessException ex) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
}
if ((f.getName().indexOf('$') == -1) &&
(useTransients || !Modifier.isTransient(f.getModifiers())) &&
(!Modifier.isStatic(f.getModifiers()))) {
try {
builder.append(f.getName(), f.get(object));
} catch (IllegalAccessException ex) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError("Unexpected IllegalAccessException");
}
}
}

View File

@ -66,7 +66,7 @@
* Unit tests {@link org.apache.commons.lang.ToStringBuilder}.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: ToStringBuilderTest.java,v 1.3 2002/12/31 20:17:53 scolebourne Exp $
* @version $Id: ToStringBuilderTest.java,v 1.4 2003/01/19 18:49:05 scolebourne Exp $
*/
public class ToStringBuilderTest extends TestCase {
@ -215,6 +215,23 @@ static class ReflectionTestFixtureB extends ReflectionTestFixtureA {
private transient char transientB='t';
}
public void testInnerClassReflection() {
Outer outer = new Outer();
assertEquals(toBaseString(outer) + "[inner=" + toBaseString(outer.inner) + "[]]", outer.toString());
}
static class Outer {
Inner inner = new Inner();
class Inner {
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
public void testAppendSuper() {
assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString());
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString());