Author: britter Date: Sat Feb 2 17:18:11 2013 New Revision: 1441774 URL: http://svn.apache.org/viewvc?rev=1441774&view=rev Log: [SANDBOX-433] - Setting properties or calling methods very often results in a NullPointerException
Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/issues/ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/issues/JiraSandbox433TestCase.java (with props) Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java?rev=1441774&r1=1441773&r2=1441774&view=diff ============================================================================== --- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java (original) +++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java Sat Feb 2 17:18:11 2013 @@ -27,8 +27,6 @@ import static org.apache.commons.beanuti import static org.apache.commons.beanutils2.TypeUtils.getPrimitiveWrapper; import static org.apache.commons.beanutils2.TypeUtils.isAssignmentCompatible; -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Constructor; import java.lang.reflect.Member; @@ -69,8 +67,7 @@ abstract class AccessibleObjectsRegistry return METHODS_REGISTRY; } - private final Map<AccessibleObjectDescriptor, WeakReference<AO>> cache = - new WeakHashMap<AccessibleObjectDescriptor, WeakReference<AO>>(); + private final Map<AccessibleObjectDescriptor, AO> cache = new WeakHashMap<AccessibleObjectDescriptor, AO>(); private AccessibleObjectsRegistry() { @@ -90,17 +87,17 @@ abstract class AccessibleObjectsRegistry lock.lock(); try { - Reference<AO> accessibleObjectReference = cache.get( key ); - if ( accessibleObjectReference != null ) + AO accessibleObject = cache.get( key ); + if ( accessibleObject != null ) { - return accessibleObjectReference.get(); + return accessibleObject; } // see if we can find the accessible object directly // most of the time this works and it's much faster try { - AO accessibleObject = resolveDirectly( type, name, parameterTypes ); + accessibleObject = resolveDirectly( type, name, parameterTypes ); if ( exact || accessibleObject != null ) { return makeAccessible( accessibleObject ); @@ -156,7 +153,7 @@ abstract class AccessibleObjectsRegistry { bestMatch = makeAccessible( bestMatch ); } - cache.put( key, new WeakReference<AO>( bestMatch ) ); + cache.put( key, bestMatch ); return bestMatch; } finally Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java?rev=1441774&r1=1441773&r2=1441774&view=diff ============================================================================== --- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java (original) +++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PropertyDescriptorsRegistry.java Sat Feb 2 17:18:11 2013 @@ -25,8 +25,6 @@ import java.beans.BeanInfo; import java.beans.IndexedPropertyDescriptor; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; @@ -49,8 +47,8 @@ final class PropertyDescriptorsRegistry private final AccessibleObjectsRegistry<Method> methodsRegistry = AccessibleObjectsRegistry.getMethodsRegistry(); - private final Map<Class<?>, WeakReference<Map<String, PropertyDescriptor>>> cache = - new WeakHashMap<Class<?>, WeakReference<Map<String, PropertyDescriptor>>>(); + private final Map<Class<?>, Map<String, PropertyDescriptor>> cache = + new WeakHashMap<Class<?>, Map<String, PropertyDescriptor>>(); /** * This class can not be instantiated. @@ -67,13 +65,13 @@ final class PropertyDescriptorsRegistry lock.lock(); try { - Reference<Map<String, PropertyDescriptor>> methodReference = cache.get( beanType ); - if ( methodReference != null ) + Map<String, PropertyDescriptor> propertiesIndex = cache.get( beanType ); + if ( propertiesIndex != null ) { - return methodReference.get(); + return propertiesIndex; } - final Map<String, PropertyDescriptor> propertiesIndex = new HashMap<String, PropertyDescriptor>(); + propertiesIndex = new HashMap<String, PropertyDescriptor>(); BeanInfo beanInfo = getBeanInfo( beanType ); for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() ) @@ -82,7 +80,7 @@ final class PropertyDescriptorsRegistry propertiesIndex.put( propertyDescriptor.getName(), propertyDescriptor ); } - cache.put( beanType, new WeakReference<Map<String, PropertyDescriptor>>( propertiesIndex ) ); + cache.put( beanType, propertiesIndex ); return propertiesIndex; } @@ -140,7 +138,8 @@ final class PropertyDescriptorsRegistry public PropertyDescriptor getPropertyDescriptor( Class<?> beanType, String propertyName ) throws IntrospectionException { - return getPropertiesIndex( beanType ).get( propertyName ); + Map<String, PropertyDescriptor> propertiesIndex = getPropertiesIndex( beanType ); + return propertiesIndex.get( propertyName ); } } Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/issues/JiraSandbox433TestCase.java URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/issues/JiraSandbox433TestCase.java?rev=1441774&view=auto ============================================================================== --- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/issues/JiraSandbox433TestCase.java (added) +++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/issues/JiraSandbox433TestCase.java Sat Feb 2 17:18:11 2013 @@ -0,0 +1,87 @@ +package org.apache.commons.beanutils2.issues; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import static org.apache.commons.beanutils2.Argument.argument; +import static org.apache.commons.beanutils2.BeanUtils.on; + +import org.apache.commons.beanutils2.TestBean; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Setting properties or invoking methods to often results in a NullPointerException + */ +public class JiraSandbox433TestCase +{ + + private final static int COUNT = 50000; + + private TestBean testBean; + + @Before + public void setUp() + { + testBean = new TestBean(); + } + + @After + public void tearDown() + { + testBean = null; + } + + @Test + public void setPropertyVeryOften() + { + for ( int i = 0; i < COUNT; i++ ) + { + on( testBean ).set( "intProperty" ).with( i ); + } + } + + @Test + public void getPropertyVeryOften() + { + for ( int i = 0; i < COUNT; i++ ) + { + on( testBean ).get( "intProperty" ); + } + } + + @Test + public void callMethodVeryOften() + { + for ( int i = 0; i < COUNT; i++ ) + { + on( testBean ).invoke( "setIntProperty" ).with( argument( i ) ); + } + } + + @Test + public void callConstructorVeryOften() + { + for ( int i = 0; i < COUNT; i++ ) + { + on( TestBean.class ).newInstance(); + } + } +} \ No newline at end of file Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/issues/JiraSandbox433TestCase.java ------------------------------------------------------------------------------ svn:eol-style = native