CXF
  1. CXF
  2. CXF-3613

Various problems with "java.lang.Class"

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: 2.4.1
    • Fix Version/s: 2.5.7, 2.6.4, 2.7.1
    • Component/s: Aegis Databinding
    • Labels:
      None

      Description

      I just noted that the following exception is thrown when trying to create server for some service interface that contains some reference to "java.lang.Class".

      Consider a service interface like this:

      package org.apache.cxf.aegis.type.java5;
      
      import java.io.Serializable;
      
      public class GenericsService {
      
          public <T extends Serializable> T createInstance(Class<T> type)
              throws InstantiationException, IllegalAccessException {
              return type.newInstance();
          }
      }
      


      The problem is, that during creation time it throws a NullPointerException like this:

      java.lang.NullPointerException
      	at org.apache.cxf.aegis.type.basic.BeanTypeInfo$1.compare(BeanTypeInfo.java:307)
      	at org.apache.cxf.aegis.type.basic.BeanTypeInfo$1.compare(BeanTypeInfo.java:306)
      	at java.util.Arrays.mergeSort(Arrays.java:1270)
      	at java.util.Arrays.mergeSort(Arrays.java:1282)
      	at java.util.Arrays.mergeSort(Arrays.java:1281)
      	at java.util.Arrays.mergeSort(Arrays.java:1281)
      	at java.util.Arrays.sort(Arrays.java:1210)
      	at org.apache.cxf.aegis.type.basic.BeanTypeInfo.initializeProperties(BeanTypeInfo.java:305)
      	at org.apache.cxf.aegis.type.basic.BeanTypeInfo.<init>(BeanTypeInfo.java:75)
      	at org.apache.cxf.aegis.type.java5.AnnotatedTypeInfo.<init>(AnnotatedTypeInfo.java:41)
      	at org.apache.cxf.aegis.type.java5.AnnotatedTypeInfo.<init>(AnnotatedTypeInfo.java:36)
      	at org.apache.cxf.aegis.type.java5.Java5TypeCreator.createDefaultType(Java5TypeCreator.java:208)
      	at org.apache.cxf.aegis.type.XMLTypeCreator.createDefaultType(XMLTypeCreator.java:398)
      	at org.apache.cxf.aegis.type.AbstractTypeCreator.createTypeForClass(AbstractTypeCreator.java:135)
      	at org.apache.cxf.aegis.databinding.AegisDatabinding.getParameterType(AegisDatabinding.java:574)
      	at org.apache.cxf.aegis.databinding.AegisDatabinding.initializeMessage(AegisDatabinding.java:315)
      	at org.apache.cxf.aegis.databinding.AegisDatabinding.initializeOperation(AegisDatabinding.java:273)
      	at org.apache.cxf.aegis.databinding.AegisDatabinding.initialize(AegisDatabinding.java:239)
      	at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)
      	at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:438)
      	at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:501)
      	at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:241)
      	at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:101)
      	at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:157)
      	at org.apache.cxf.dosgi.dsw.handlers.HttpServiceConfigurationTypeHandler.createServer(HttpServiceConfigurationTypeHandler.java:169)
      	at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminCore.exportService(RemoteServiceAdminCore.java:245)
      	at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance$1.run(RemoteServiceAdminInstance.java:78)
      	at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance$1.run(RemoteServiceAdminInstance.java:71)
      	at java.security.AccessController.doPrivileged(Native Method)
      	at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance.exportService(RemoteServiceAdminInstance.java:71)
      	at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance.exportService(RemoteServiceAdminInstance.java:40)
      	at org.apache.cxf.dosgi.topologymanager.TopologyManager$2.run(TopologyManager.java:259)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      	at java.lang.Thread.run(Thread.java:662)
      

      The cause for this is that the "org.springframework.beans.BeanUtils#getPropertyDescriptor" method returns "null" for the property "classLoader". However, this is not a real problem, so as a test I just modified the "org.apache.cxf.aegis.type.basic.BeanTypeInfo#initializeProperties" method to omit any PropertyDescriptor that is null.

      So far so good, but immediately afterwards, I get the next error:

      java.lang.NullPointerException
      	at java.lang.reflect.Array.newArray(Native Method)
      	at java.lang.reflect.Array.newInstance(Array.java:52)
      	at org.apache.cxf.aegis.type.TypeUtil.getTypeRelatedClass(TypeUtil.java:261)
      	at org.apache.cxf.aegis.type.AbstractTypeCreator.createTypeForClass(AbstractTypeCreator.java:108)
      	at org.apache.cxf.aegis.type.AbstractTypeCreator.createType(AbstractTypeCreator.java:395)
      	at org.apache.cxf.aegis.type.basic.BeanTypeInfo.getType(BeanTypeInfo.java:196)
      	at org.apache.cxf.aegis.type.basic.BeanType.getDependencies(BeanType.java:532)
      	at org.apache.cxf.aegis.databinding.AegisDatabinding.addDependencies(AegisDatabinding.java:394)
      	at org.apache.cxf.aegis.databinding.AegisDatabinding.initializeMessage(AegisDatabinding.java:370)
      	at org.apache.cxf.aegis.databinding.AegisDatabinding.initializeOperation(AegisDatabinding.java:273)
      	at org.apache.cxf.aegis.databinding.AegisDatabinding.initialize(AegisDatabinding.java:239)
      	at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)
      	at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:438)
      	at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:501)
      	at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:241)
      	at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:101)
      	at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:157)
      	at org.apache.cxf.aegis.AbstractAegisTest.createService(AbstractAegisTest.java:151)
      	at org.apache.cxf.aegis.type.java5.GenericsTypeTest.testCreateService(GenericsTypeTest.java:85)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
      	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
      	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
      	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
      	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
      	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
      	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
      	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
      	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
      	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
      	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
      	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
      	at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
      	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:119)
      	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:101)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
      	at $Proxy0.invoke(Unknown Source)
      	at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
      	at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
      	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
      


      This one is for the property "java.lang.Class#enumConstants" and I guess there are even more errors coming.

      What I had in ming was to substitute "java.lang.Class" with "java.lang.String" on-the-fly, so that there is an automatic default handling, e.g.:

      • public <T extends Serializable> T createInstance(Class<T> type) will become something like public Serializable createInstance(String type)
      • public Class getType(Serializable obj) will become something like public String getType(Serializable obj)

      ...so that CXF internally does something like "ClassLoaderUtils.loadClass(type, refClass);" and "type.getName()" for the other way around.

      What do you think?

        Activity

        Hide
        Ancoron Luciferis added a comment -

        For a simple test, I have added a new type "ClassAsStringType":

        Index: rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ClassAsStringType.java
        ===================================================================
        --- rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ClassAsStringType.java   (revision 1136667)
        +++ rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ClassAsStringType.java   (working copy)
        @@ -30,35 +30,48 @@
         /**
          * 
          */
        -public class CharacterAsStringType extends AegisType {
        +public class ClassAsStringType extends AegisType {
             
        -    public static final QName CHARACTER_AS_STRING_TYPE_QNAME 
        -        = new QName("http://cxf.apache.org/aegisTypes", "char");
        +    public static final QName CLASS_AS_STRING_TYPE_QNAME 
        +        = new QName("http://cxf.apache.org/aegisTypes", "class");
             
        -    private IntType intType;
        +    private StringType stringType;
             
        -    public CharacterAsStringType() {
        -        intType = new IntType();
        +    public ClassAsStringType() {
        +        stringType = new StringType();
             }
         
             /** {@inheritDoc}*/
             @Override
        -    public Object readObject(MessageReader reader, Context context) throws DatabindingException {
        -        Integer readInteger = (Integer)intType.readObject(reader, context);
        -        return new Character((char)readInteger.intValue());
        +    public Object readObject(MessageReader reader, Context context)
        +        throws DatabindingException {
        +        String className = (String) stringType.readObject(reader, context);
        +        Class cls = null;
        +        
        +        try {
        +            context.getClass().getClassLoader().loadClass(className);
        +        } catch (ClassNotFoundException x) {
        +            throw new DatabindingException("Unable to dynamically load class '"
        +                    + className + "'", x);
        +        }
        +
        +        return cls;
             }
         
             /** {@inheritDoc}*/
             @Override
             public void writeObject(Object object, MessageWriter writer, Context context) 
                 throws DatabindingException {
        -        Character charObject = (Character) object;
        -        intType.writeObject(Integer.valueOf(charObject.charValue()), writer, context);
        +        if (object == null) {
        +            stringType.writeObject(null, writer, context);
        +        } else {
        +            Class cls = (Class) object;
        +            stringType.writeObject(cls.getName(), writer, context);
        +        }
             }
         
             @Override
             public boolean usesUtilityTypes() {
                 return true;
             }
        -
         }
        


        ...then I added that one to the DefaultTypeMapping:

        Index: rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/DefaultTypeMapping.java
        ===================================================================
        --- rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/DefaultTypeMapping.java        (revision 1136667)
        +++ rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/DefaultTypeMapping.java        (working copy)
        @@ -47,6 +47,7 @@
         import org.apache.cxf.aegis.type.basic.CalendarType;
         import org.apache.cxf.aegis.type.basic.CharacterAsStringType;
         import org.apache.cxf.aegis.type.basic.CharacterType;
        +import org.apache.cxf.aegis.type.basic.ClassAsStringType;
         import org.apache.cxf.aegis.type.basic.DateTimeType;
         import org.apache.cxf.aegis.type.basic.DoubleType;
         import org.apache.cxf.aegis.type.basic.FloatType;
        @@ -373,6 +374,10 @@
                                 CharacterAsStringType.CHARACTER_AS_STRING_TYPE_QNAME,
                                 new CharacterAsStringType());
         
        +        defaultRegister(tm, defaultNillable, Class.class, 
        +                        ClassAsStringType.CLASS_AS_STRING_TYPE_QNAME,
        +                        new ClassAsStringType());
        +
                 defaultRegister(tm, defaultNillable, javax.xml.datatype.Duration.class, XMLSchemaQNames.XSD_DURATION,
                                     new org.apache.cxf.aegis.type.java5.DurationType());
                 defaultRegister(tm, defaultNillable, javax.xml.datatype.XMLGregorianCalendar.class,
        


        ...and I added the declaration in "META-INF/cxf/aegisTypes.xsd":

        Index: rt/databinding/aegis/src/main/resources/META-INF/cxf/aegisTypes.xsd
        ===================================================================
        --- rt/databinding/aegis/src/main/resources/META-INF/cxf/aegisTypes.xsd (revision 1136667)
        +++ rt/databinding/aegis/src/main/resources/META-INF/cxf/aegisTypes.xsd (working copy)
        @@ -27,4 +27,8 @@
               </xs:restriction>
             </xs:simpleType>
         
        +    <xs:simpleType name="class">
        +      <xs:restriction base="xs:string" />
        +    </xs:simpleType>
        +
         </xs:schema>
        


        However, I still have to manually map it to that type at runtime, e.g.:

        <mappings>
            <mapping>
                <method name="createInstance">
                    <parameter index="0" class="java.lang.Class" type="org.apache.cxf.aegis.type.basic.ClassAsStringType"/>
                </method>
            </mapping>
        </mappings>
        


        That would not be a problem unless the generated WSDL was consistent, but the type declaration for "class" is still missing (although being referenced from the service method request type).

        Show
        Ancoron Luciferis added a comment - For a simple test, I have added a new type "ClassAsStringType": Index: rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ClassAsStringType.java =================================================================== --- rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ClassAsStringType.java (revision 1136667) +++ rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ClassAsStringType.java (working copy) @@ -30,35 +30,48 @@ /** * */ -public class CharacterAsStringType extends AegisType { +public class ClassAsStringType extends AegisType { - public static final QName CHARACTER_AS_STRING_TYPE_QNAME - = new QName("http://cxf.apache.org/aegisTypes", "char"); + public static final QName CLASS_AS_STRING_TYPE_QNAME + = new QName("http://cxf.apache.org/aegisTypes", "class"); - private IntType intType; + private StringType stringType; - public CharacterAsStringType() { - intType = new IntType(); + public ClassAsStringType() { + stringType = new StringType(); } /** {@inheritDoc}*/ @Override - public Object readObject(MessageReader reader, Context context) throws DatabindingException { - Integer readInteger = (Integer)intType.readObject(reader, context); - return new Character((char)readInteger.intValue()); + public Object readObject(MessageReader reader, Context context) + throws DatabindingException { + String className = (String) stringType.readObject(reader, context); + Class cls = null; + + try { + context.getClass().getClassLoader().loadClass(className); + } catch (ClassNotFoundException x) { + throw new DatabindingException("Unable to dynamically load class '" + + className + "'", x); + } + + return cls; } /** {@inheritDoc}*/ @Override public void writeObject(Object object, MessageWriter writer, Context context) throws DatabindingException { - Character charObject = (Character) object; - intType.writeObject(Integer.valueOf(charObject.charValue()), writer, context); + if (object == null) { + stringType.writeObject(null, writer, context); + } else { + Class cls = (Class) object; + stringType.writeObject(cls.getName(), writer, context); + } } @Override public boolean usesUtilityTypes() { return true; } - } ...then I added that one to the DefaultTypeMapping: Index: rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/DefaultTypeMapping.java =================================================================== --- rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/DefaultTypeMapping.java (revision 1136667) +++ rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/DefaultTypeMapping.java (working copy) @@ -47,6 +47,7 @@ import org.apache.cxf.aegis.type.basic.CalendarType; import org.apache.cxf.aegis.type.basic.CharacterAsStringType; import org.apache.cxf.aegis.type.basic.CharacterType; +import org.apache.cxf.aegis.type.basic.ClassAsStringType; import org.apache.cxf.aegis.type.basic.DateTimeType; import org.apache.cxf.aegis.type.basic.DoubleType; import org.apache.cxf.aegis.type.basic.FloatType; @@ -373,6 +374,10 @@ CharacterAsStringType.CHARACTER_AS_STRING_TYPE_QNAME, new CharacterAsStringType()); + defaultRegister(tm, defaultNillable, Class.class, + ClassAsStringType.CLASS_AS_STRING_TYPE_QNAME, + new ClassAsStringType()); + defaultRegister(tm, defaultNillable, javax.xml.datatype.Duration.class, XMLSchemaQNames.XSD_DURATION, new org.apache.cxf.aegis.type.java5.DurationType()); defaultRegister(tm, defaultNillable, javax.xml.datatype.XMLGregorianCalendar.class, ...and I added the declaration in "META-INF/cxf/aegisTypes.xsd": Index: rt/databinding/aegis/src/main/resources/META-INF/cxf/aegisTypes.xsd =================================================================== --- rt/databinding/aegis/src/main/resources/META-INF/cxf/aegisTypes.xsd (revision 1136667) +++ rt/databinding/aegis/src/main/resources/META-INF/cxf/aegisTypes.xsd (working copy) @@ -27,4 +27,8 @@ </xs:restriction> </xs:simpleType> + <xs:simpleType name="class"> + <xs:restriction base="xs:string" /> + </xs:simpleType> + </xs:schema> However, I still have to manually map it to that type at runtime, e.g.: <mappings> <mapping> <method name="createInstance"> <parameter index="0" class="java.lang.Class" type="org.apache.cxf.aegis.type.basic.ClassAsStringType"/> </method> </mapping> </mappings> That would not be a problem unless the generated WSDL was consistent, but the type declaration for " class " is still missing (although being referenced from the service method request type).
        Hide
        Daniel Kulp added a comment -


        This is not something we'd expose by default as having the ability to load random classes like that is definitely a huge issue. However, I did write a system test in rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/ClassTest.java that can show how to create the custom type, register it, create the schema, etc.... It did require a minor bug fix in the aegis databinding though so it will require 2.7.1/2.6.4/2.5.7 to work.

        Show
        Daniel Kulp added a comment - This is not something we'd expose by default as having the ability to load random classes like that is definitely a huge issue. However, I did write a system test in rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/ClassTest.java that can show how to create the custom type, register it, create the schema, etc.... It did require a minor bug fix in the aegis databinding though so it will require 2.7.1/2.6.4/2.5.7 to work.

          People

          • Assignee:
            Daniel Kulp
            Reporter:
            Ancoron Luciferis
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development