o Fixed cycle detection

git-svn-id: https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk@714129 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2008-11-14 20:32:14 +00:00
parent 70e9ce9de8
commit 9fd4684d9f
2 changed files with 17 additions and 7 deletions

View File

@ -68,8 +68,9 @@ class PropertyUtil
*/ */
private static void store( Properties props, String key, Object obj, Collection visited ) private static void store( Properties props, String key, Object obj, Collection visited )
{ {
if ( obj != null ) if ( obj != null && !visited.contains( obj ) )
{ {
visited.add( obj );
if ( ( obj instanceof String ) || ( obj instanceof Number ) || ( obj instanceof Boolean ) if ( ( obj instanceof String ) || ( obj instanceof Number ) || ( obj instanceof Boolean )
|| ( obj instanceof File ) ) || ( obj instanceof File ) )
{ {
@ -83,7 +84,7 @@ class PropertyUtil
for ( Iterator it = coll.iterator(); it.hasNext(); index++ ) for ( Iterator it = coll.iterator(); it.hasNext(); index++ )
{ {
Object elem = it.next(); Object elem = it.next();
store( props, key + "." + index, elem ); store( props, key + "." + index, elem, visited );
} }
} }
else if ( obj instanceof Map ) else if ( obj instanceof Map )
@ -94,7 +95,7 @@ class PropertyUtil
for ( Iterator it = map.entrySet().iterator(); it.hasNext(); index++ ) for ( Iterator it = map.entrySet().iterator(); it.hasNext(); index++ )
{ {
Map.Entry entry = (Map.Entry) it.next(); Map.Entry entry = (Map.Entry) it.next();
store( props, key + "." + entry.getKey(), entry.getValue() ); store( props, key + "." + entry.getKey(), entry.getValue(), visited );
} }
} }
else if ( obj.getClass().isArray() ) else if ( obj.getClass().isArray() )
@ -104,12 +105,11 @@ class PropertyUtil
for ( int index = 0; index < length; index++ ) for ( int index = 0; index < length; index++ )
{ {
Object elem = Array.get( obj, index ); Object elem = Array.get( obj, index );
store( props, key + "." + index, elem ); store( props, key + "." + index, elem, visited );
} }
} }
else if ( obj != null && !visited.contains( obj ) ) else if ( obj != null )
{ {
visited.add( obj );
Class type = obj.getClass(); Class type = obj.getClass();
Method[] methods = type.getMethods(); Method[] methods = type.getMethods();
for ( int i = 0; i < methods.length; i++ ) for ( int i = 0; i < methods.length; i++ )
@ -133,8 +133,8 @@ class PropertyUtil
// just ignore // just ignore
} }
} }
visited.remove( obj );
} }
visited.remove( obj );
} }
} }

View File

@ -93,6 +93,16 @@ public class PropertyUtilTest
assertEquals( 2, props.size() ); assertEquals( 2, props.size() );
} }
public void testStoreCycle()
{
Object[] arr = { null };
arr[0] = Collections.singleton( Collections.singletonMap( "key", arr ) );
Properties props = new Properties();
PropertyUtil.store( props, "cycle", arr );
assertTrue( "Should not die because of stack overflow", true );
}
public void testGetPropertyName() public void testGetPropertyName()
{ {
assertEquals( "name", PropertyUtil.getPropertyName( "getName" ) ); assertEquals( "name", PropertyUtil.getPropertyName( "getName" ) );