Tickaroo / tikxml

Modern XML Parser for Android
Apache License 2.0
423 stars 44 forks source link

error: incompatible types: Object cannot be converted to ... #123

Open svedie opened 5 years ago

svedie commented 5 years ago

Using Kotlin.

I have defined in my root XML data class an element an annotated it with @Element

...
  @Element
  var synchronization: Synchronization?,
...

also I have defined a data class Synchronization

@Xml
data class Synchronization(
  @Attribute
  var type: String?,

  @Attribute
  var autosync: Int?,

  @Attribute
  var revision: String?,

  @PropertyElement
  var url: String?,

  @PropertyElement
  var download: String?,

  @PropertyElement
  var upload: String
)

When Android Studio now compiles everything to send it to the device I get following error:

error: incompatible types: Object cannot be converted to Synchronization Incompatible types. Required: xxx.Synchronization Found: java.lang.Object

In the TypeAdapter of the main XML file there is following code generated and it looks like there is no cast to Synchronization object like in a list.

    childElementBinders.put("synchronization", new ChildElementBinder<ValueHolder>() {
      @Override
      public void fromXml(XmlReader reader, TikXmlConfig config, ValueHolder value) throws
          IOException {
        value.synchronization = config.getTypeAdapter(Synchronization.class).fromXml(reader, config);
      }
    });

For example a list has the needed cast to an account class:

    childElementBinders.put("account", new ChildElementBinder<ValueHolder>() {
      @Override
      public void fromXml(XmlReader reader, TikXmlConfig config, ValueHolder value) throws
          IOException {
        if (value.accounts == null) {
          value.accounts = new ArrayList<Account>();
        }
        value.accounts.add((Account) config.getTypeAdapter(Account.class).fromXml(reader, config) );
      }
    });

Is this a bug or did I miss something?

sockeqwe commented 5 years ago

Is there no line number and filename attached to the error

Incompatible types. Required: xxx.Synchronization Found: java.lang.Object

The compiler should print the file and line number where the error happens. Can you please check that and provide that information?

svedie notifications@github.com schrieb am Sa., 19. Jan. 2019, 22:04:

Using Kotlin.

I have defined in my root XML data class an element an annotated it with @element https://github.com/element

... @Element var synchronization: Synchronization?, ...

also I have defined a data class Synchronization

@Xml data class Synchronization( @Attribute var type: String?,

@Attribute var autosync: Int?,

@Attribute var revision: String?,

@PropertyElement var url: String?,

@PropertyElement var download: String?,

@PropertyElement var upload: String )

When Android Studio now compiles everything to send it to the device I get following error:

error: incompatible types: Object cannot be converted to Synchronization Incompatible types. Required: xxx.Synchronization Found: java.lang.Object

In the TypeAdapter of the main XML file there is following code generated and it looks like there is no cast to Synchronization object like in a list.

childElementBinders.put("synchronization", new ChildElementBinder<ValueHolder>() {
  @Override
  public void fromXml(XmlReader reader, TikXmlConfig config, ValueHolder value) throws
      IOException {
    value.synchronization = config.getTypeAdapter(Synchronization.class).fromXml(reader, config);
  }
});

For example a list has the needed cast to an account class:

childElementBinders.put("account", new ChildElementBinder<ValueHolder>() {
  @Override
  public void fromXml(XmlReader reader, TikXmlConfig config, ValueHolder value) throws
      IOException {
    if (value.accounts == null) {
      value.accounts = new ArrayList<Account>();
    }
    value.accounts.add((Account) config.getTypeAdapter(Account.class).fromXml(reader, config) );
  }
});

Is this a bug or did I miss something?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Tickaroo/tikxml/issues/123, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjnrhYWBOeDr9bnR-dQfFjOZXSfNBk5ks5vE4hYgaJpZM4aJVQD .

svedie commented 5 years ago

Hi Hannes,

the error is on these lines:

    childElementBinders.put("synchronization", new ChildElementBinder<ValueHolder>() {
      @Override
      public void fromXml(XmlReader reader, TikXmlConfig config, ValueHolder value) throws
          IOException {
--->
        value.synchronization = config.getTypeAdapter(Synchronization.class).fromXml(reader, config);
<---
      }
    });

There is no other information as

error: incompatible types: Object cannot be converted to Synchronization

If I scroll down in the generated TypeConverter to the List Element there is a cast on the object:

    childElementBinders.put("account", new ChildElementBinder<ValueHolder>() {
      @Override
      public void fromXml(XmlReader reader, TikXmlConfig config, ValueHolder value) throws
          IOException {
        if (value.accounts == null) {
          value.accounts = new ArrayList<Account>();
        }
<---
        value.accounts.add((Account) config.getTypeAdapter(Account.class).fromXml(reader, config) );
--->
      }
    });
sockeqwe commented 5 years ago

Hm, interesting. You are right that a cast to Synchronization.class should fix the problem, I am just wondering why the compiler isn't doing that automatically (or isn't smart enough to do that on his own). What version of javac do you run, which jdk and which operation system? Which version of TikXml do you use?

svedie commented 5 years ago

I use Kotlin: org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.11 apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt'

implementation 'com.tickaroo.tikxml:annotation:0.8.13' implementation 'com.tickaroo.tikxml:core:0.8.15' implementation 'com.tickaroo.tikxml:converters:0.8.13' kapt 'com.tickaroo.tikxml:processor:0.8.13'

Win10 and Android Studio 3.3.

If I edit the generated file and let the Android Studio add the cast, the problem is "solved" (the file will be generated next build, so it is just a temporary change).

sockeqwe commented 5 years ago

can you please print the output of the following command in windows command line: javac -version

svedie commented 5 years ago

I use Java SDK 1.8.181: javac 1.8.0_181

svedie commented 5 years ago

Any news on this issue?

ZsoltSzabo88 commented 5 years ago

@svedie I've also faced your mentioned issue and it has been driving my crazy since Tickaro has seemed to me to be an ideal solution for xml parsing. I've managed to solve the issue which was caused in my case by the different versioning of dependencies. I've set the core version to 0.8.15 and the others (annotation, processor) to 0.8.13 because

  1. the newer version of annotation package was missing from the maven repo (0.8.15)
  2. the processor (0.8.15) has a dependency on the missing annotation package. That was a big mistake as it unrevaled. So the solution is simple, just use version 0.8.13 for every dependency.
svedie commented 5 years ago

Thank you. I will try it.