Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BTC-China getTrades() ClassCastException #517

Closed
zholmes1 opened this issue Jul 11, 2014 · 25 comments
Closed

BTC-China getTrades() ClassCastException #517

zholmes1 opened this issue Jul 11, 2014 · 25 comments
Labels

Comments

@zholmes1
Copy link
Contributor

I have the same issue with JustcoinExchange

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.xeiam.xchange.btcchina.dto.marketdata.BTCChinaTrade
at com.xeiam.xchange.btcchina.BTCChinaAdapters.adaptTrades(BTCChinaAdapters.java:125)

Then happens when the getTrades(CurrencyPair) method is called.

@timmolter
Copy link
Member

Can you please try the latest snapshot jars and report back?

@zholmes1
Copy link
Contributor Author

Yes, this occurs with the most recent snapshot JARS.
On Jul 12, 2014 5:38 AM, "Tim Molter" notifications@github.com wrote:

Can you please try the latest snapshot jars and report back?


Reply to this email directly or view it on GitHub
#517 (comment).

@sutra
Copy link
Contributor

sutra commented Jul 13, 2014

I cannot reproduce this problem either, by running com.xeiam.xchange.examples.btcchina.marketdata.BTCChinaTradesDemo.generic() on version f766bac

@zholmes1
Copy link
Contributor Author

What version of Java are you using?
On Jul 13, 2014 10:19 AM, "Sutra Zhou" notifications@github.com wrote:

I cannot reproduce this problem either, by running
com.xeiam.xchange.examples.btcchina.marketdata.BTCChinaTradesDemo.generic()
on version f766bac
f766bac


Reply to this email directly or view it on GitHub
#517 (comment).

@sutra
Copy link
Contributor

sutra commented Jul 13, 2014

java version "1.6.0_65"
the same as specified in pom.xml

@zholmes1
Copy link
Contributor Author

I had the same issue for Justcoin, and another user was able to get code
working on Java 8 that I wasn't able to with Java 7. Unfortunately
developing for Android doesn't give me any choices between Java versions.
On Jul 13, 2014 10:23 AM, "Sutra Zhou" notifications@github.com wrote:

java version "1.6.0_65"
the same as specified in pom.xml


Reply to this email directly or view it on GitHub
#517 (comment).

@sutra
Copy link
Contributor

sutra commented Jul 13, 2014

Cannot reproduced in Java 1.7 either.

@jpe42
Copy link
Contributor

jpe42 commented Jul 13, 2014

@zholmes1 Can you try out writing a custom deserializer for BTCChinaTrade that you can get working in your environment as well as a non-andriod vm? If you search the codebase for 'JsonDeserializer' you will find plenty of examples. This isn't a preferred solution as it is just more code to maintain. Otherwise this seems like it should be an issue with whatever version of Jackson you are using.

@zholmes1
Copy link
Contributor Author

I didn't consider that it could be Jackson. I will add the correct version
when I get to my computer and see if that works.
On Jul 13, 2014 10:57 AM, "James Edwards" notifications@github.com wrote:

@zholmes1 https://github.com/zholmes1 Can you try out writing a custom
deserializer for BTCChinaTrade that you can get working in your environment
as well as a non-andriod vm? If you search the codebase for
'JsonDeserializer' you will find plenty of examples. This isn't a preferred
solution as it is just more code to maintain. Otherwise this seems like it
should be an issue with whatever version of Jackson you are using.


Reply to this email directly or view it on GitHub
#517 (comment).

@zholmes1
Copy link
Contributor Author

Sorry for the delay. I updated Jackson to 2.3 and I am still having this issue.

@sutra
Copy link
Contributor

sutra commented Jul 24, 2014

I guess if we change the return type of com.xeiam.xchange.btcchina.BTCChina.getTrades(String, long, int) from List<BTCChinaTrade> to BTCChinaTrade[] will resolve this problem. That is because of the type erasure of Java's generic implementation. So in byte code level, the List<BTCChinaTrade> maybe(here maybe means different JVMs may have different implementations, even bugs, I am not sure about this in JVM spec) will be erased to List, then the type BTCChinaTrade lost, it will fail to LinkedHashMap in reflection.

  @GET
  @Path("data/historydata")
  public List<BTCChinaTrade> getTrades(
      @QueryParam("market") String market,
      @QueryParam("since") long since,
      @QueryParam("limit") int limit) throws IOException;

@timmolter
Copy link
Member

can you post the exact line of code in BTCChinaAdapters that is throwing the error? The line number.

@timmolter
Copy link
Member

Does running BTCChinaTradesDemo allow you to reproduce the error or is it only a call to the adapter method when run on Android that causes the error?

@timmolter
Copy link
Member

I believe the issue is that in rescu (@mmazi) when the json is mapped to the return type defined in the rest interface method, it's not being correctly mapped to a List. In the case on for a certain Android JVM, it's getting mapped to a LinkedHashMap for some reason.

In RestMethodMetadata in rescu the return type is determined with this call:

method.getGenericReturnType()

There's some more possibly useful info here that could be used perhaps in rescu to get a more accurate return type: http://tutorials.jenkov.com/java-reflection/generics.html. If possible a solution like this in rescu would be more robust.

However to get this working now, you can do what @sutra suggested and change the return type to BTCChinaTrade[] or also possibly ArrayList<BTCChinaTrade>.

If you look in Justcoin, the rest interface methods also return Lists. In implementations where an array is returned you're not seeing this issue.

@sutra
Copy link
Contributor

sutra commented Jul 26, 2014

Found it was using array before 31cf69a, how about changing back to use array, and avoid using generic type for return type of getTicker too. @jamespedwards42

@timmolter
Copy link
Member

It'd be easier to try ArrayList first. i bet that would work.

@sutra
Copy link
Contributor

sutra commented Jul 26, 2014

I don't think List or ArrayList is a good choice, but array, as it should be readonly, we do not need to add/remove elements.

@zholmes1
Copy link
Contributor Author

Hey, I'm working on a Poloniex implementation and I was getting this error when I was parsing the returned JSON as a List<PoloniexPublicTrade>.

A simple switch to PoloniexPublicTrade[] solved the problem completely.

@jpe42
Copy link
Contributor

jpe42 commented Jul 30, 2014

I would be willing to help out with migrating all of the exchanges to use primitive arrays. I think we could just throw up a wiki page and start claiming each exchange one by one.

@timmolter
Copy link
Member

That's excellent! As a committer, can you create/edit Wiki pages?

@jpe42
Copy link
Contributor

jpe42 commented Jul 30, 2014

@mmazi
Copy link
Contributor

mmazi commented Aug 11, 2014

I've opened an issue in rescu for @timmolter 's suggestion.

@timmolter
Copy link
Member

@zholmes1 This should now be fixed in the latest snapshot version. Can you please test and close this issue if it's OK?

@timmolter timmolter added the Bug label Aug 11, 2014
@zholmes1
Copy link
Contributor Author

Yes I will test it out tonight.
On Aug 11, 2014 9:31 AM, "Tim Molter" notifications@github.com wrote:

@zholmes1 https://github.com/zholmes1 This should now be fixed in the
latest snapshot version. Can you please test and close this issue if it's
OK?


Reply to this email directly or view it on GitHub
#517 (comment).

@zholmes1
Copy link
Contributor Author

Everything is working perfectly.

badgerwithagun added a commit to badgerwithagun/XChange that referenced this issue May 5, 2020
…ockito-mockito-core-3.3.0

Bump mockito-core from 3.2.4 to 3.3.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants