Announcement Announcement Module
Collapse
No announcement yet.
primitive arrays and ClassUtil Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • primitive arrays and ClassUtil

    I'm trying to inject a byte[].class via IOC into a custom class. However it it looks like ClassUtils does not handle primitive arrays. It does work with primitives, just not primitive arrays like byte[].

    I am using Spring 1.2.1

    Is this a bug? Or am I missing something obvious?

    Below is a snippet from from my application context.

    Thanks,
    Tyler


    Code:
    	<bean id="blobRegister" class="org.blahblah.core.bind.CustomEditorRegister">
     		 <constructor-arg>
    			  <value>byte&#91;&#93;</value>
    		 </constructor-arg>
    		 <constructor-arg>
    			  <value>null</value>
    		 </constructor-arg>
    		 <constructor-arg>
    			 <ref local="byteArrayMultipartFileEditor" />
    		 </constructor-arg>
     	</bean>

  • #2
    Get the latests version (1.2.3) and do a simple unit test to see if it works. Check the sources - if this feature is not present issue an enhancement on JIRA.

    Comment


    • #3
      Looks like it is a feature that needs to be added.

      Here is the Array of primitives classes. as you can see there are no entries for byte[].class. boolean[].class etc

      Code:
      private static Class&#91;&#93; PRIMITIVE_CLASSES = &#123;
      		boolean.class, byte.class, char.class, short.class, int.class, long.class, float.class, double.class&#125;;

      This method runs through the primitives first, then if they are not found it tries it as a array. However Class.forName does not work with primitives so it errors out. Which makes sense.




      Code:
      public static Class forName&#40;String name, ClassLoader classLoader&#41; throws ClassNotFoundException &#123;
      		Class clazz = resolvePrimitiveClassName&#40;name&#41;;
      		if &#40;clazz != null&#41; &#123;
      			return clazz;
      		&#125;
      		if &#40;name.endsWith&#40;ARRAY_SUFFIX&#41;&#41; &#123;
      			// special handling for array class names
      			String elementClassName = name.substring&#40;0, name.length&#40;&#41; - ARRAY_SUFFIX.length&#40;&#41;&#41;;
      			Class elementClass = Class.forName&#40;elementClassName, true, classLoader&#41;;
      			return Array.newInstance&#40;elementClass, 0&#41;.getClass&#40;&#41;;
      		&#125;
      		return Class.forName&#40;name, true, classLoader&#41;;
      	&#125;

      So it looks like the fix would be to add the primitive arrays (byte[].class boolean[].class, etc) to Class[] PRIMITIVE_CLASSES. Let me know if you think there is better way to handle this.

      I will add a issue and include a fix for the issue also.

      Thanks,

      Tyler

      Comment


      • #4
        Ok I'm a little slow today
        byte[].class, etc work a little different than byte.class

        Rather than passing it in as the string byte[] you need to pass it in as the string [B
        which is the class name for a byte[].class.

        That seemed to fix the problem.


        Code:
        <bean id="blobRegister" class="org.blahblahblah.core.bind.CustomEditorRegister">
         		<constructor-arg>
        			<value>&#91;B</value>
        		</constructor-arg>
        		<constructor-arg>
        			<value>null</value>
        		</constructor-arg>
        		<constructor-arg>
        			<ref local="byteArrayMultipartFileEditor" />
        		</constructor-arg>
         	</bean>
        --Tyler

        Comment


        • #5
          FYI, I have added support for primitive array names such as "byte[]", "int[]", etc to our ClassUtils.forName() method: available for constructor type hints and the like. This will be part of 1.2.4.

          Juergen

          Comment

          Working...
          X