You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by "Philippe Le Berre (JIRA)" <ji...@apache.org> on 2013/02/01 09:47:13 UTC

[jira] [Commented] (AXIS2-5481) DefaultObjectSupplier fails when the provided class is an enum

    [ https://issues.apache.org/jira/browse/AXIS2-5481?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13568584#comment-13568584 ] 

Philippe Le Berre commented on AXIS2-5481:
------------------------------------------

If someone is interested, a jar with the proposed fixed is available at http://esis.svn.sourceforge.net/viewvc/esis/main/jars/axis2-1.6.2/lib/axis2-kernel-1.6.2.jar?revision=3837 .
                
> DefaultObjectSupplier fails when the provided class is an enum
> --------------------------------------------------------------
>
>                 Key: AXIS2-5481
>                 URL: https://issues.apache.org/jira/browse/AXIS2-5481
>             Project: Axis2
>          Issue Type: Bug
>          Components: kernel
>    Affects Versions: 1.6.2
>         Environment: Mac OS X
>            Reporter: Philippe Le Berre
>              Labels: patch
>         Attachments: patch.diff
>
>
> org.apache.axis2.engine.DefaultObjectSupplier	
>      - public Object getObject(Class clazz) throws AxisFault {
> if the clazz parameter is an enum the object supplier fails as it tries to call newInstance on an enum.
> There's two way to fix this :
> 1/ Simply check that clazz is an enum (clazz.isEnum()) and then throw an exception to indicate that this is not supported. However that would be limitation which can be resolved see next.
> 2/ To instanciate an enum one needs to have a value (Enum.valueOf(<enum?>, value)) however there is no default value for an enum like for instance null. So if we make the assumption - and that's then a specific of Axis2 - that the developer can add a "NULL" value to its enum it can be tried.
> ------ Proposed fixed ------
> public Object getObject(Class clazz) throws AxisFault {
> 		try {
> 			Class parent = clazz.getDeclaringClass();
> 			Object instance = null;
> 			if (parent != null && !Modifier.isStatic(clazz.getModifiers())) {
> 				// if this is an inner class then that can be a non static inner class. 
> 				// those classes have to be instantiated in a different way than a normal initialization.
> 				instance = clazz.getConstructor(new Class[] { parent })
> 						.newInstance(new Object[] { getObject(parent) });
> //-----------------------------------------------------
> 			} else if (clazz.isEnum()) {
> 				// enum just can create a new instance, so we have to resort
> 				// to a default value, obviously many options are possible.
> 				try {
> 					instance = Enum.valueOf(clazz, "NULL");
> 				} catch (IllegalArgumentException iae) {
> 					throw AxisFault.makeFault(new Exception("Cannot create an enum object of type ("+clazz.getName()+") without a default value, please add a 'NULL' value to the enum that can be used as default."));
> 				}	
> //-----------------------------------------------------
> 			} else {
> 				instance = clazz.newInstance();
> 			}
> 			return instance;
> 		} catch (Exception e) {
> 			throw AxisFault.makeFault(e);
> 		}
> 	}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@axis.apache.org
For additional commands, e-mail: java-dev-help@axis.apache.org