Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34201 > unrolled thread

type question

Started bymcheung63@gmail.com
First post2016-04-26 11:04 -0700
Last post2016-04-26 19:32 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  type question mcheung63@gmail.com - 2016-04-26 11:04 -0700
    Re: type question Arne Vajhøj <arne@vajhoej.dk> - 2016-04-26 19:32 -0400

#34201 — type question

Frommcheung63@gmail.com
Date2016-04-26 11:04 -0700
Subjecttype question
Message-ID<0b41efc4-f925-466b-ae42-76f484c50bc9@googlegroups.com>
Hi
    This line works:

new Gson().fromJson(httpResult.content, new TypeToken<ArrayList<StockBalance>>() {


Now i want to create a function with a parameter for the last one, and i failed


function1(new ArrayList<StockBalance>().getClass());

public static <T> T function1(Class<T> clazz) {
   return new Gson().fromJson(httpResult.content, new TypeToken<ArrayList<StockBalance>>() {}.getType());
}
 

!!! the last parameter is wrong, able to compile but can't work.
thanks
from Peter (cmk128@hotmail.com)

[toc] | [next] | [standalone]


#34207

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-04-26 19:32 -0400
Message-ID<nfotqh$hth$1@gioia.aioe.org>
In reply to#34201
On 4/26/2016 2:04 PM, mcheung63@gmail.com wrote:
>     This line works:
>
> new Gson().fromJson(httpResult.content, new TypeToken<ArrayList<StockBalance>>() {
>
>
> Now i want to create a function with a parameter for the last one, and i failed
>
>
> function1(new ArrayList<StockBalance>().getClass());
>
> public static <T> T function1(Class<T> clazz) {
>    return new Gson().fromJson(httpResult.content, new TypeToken<ArrayList<StockBalance>>() {}.getType());
> }
>
>
> !!! the last parameter is wrong, able to compile but can't work.

You did not specify compile error message or provide enough
code so that we can compile.

But The function is declared to return T but fromJson return
blabla.getType() which is not necessarily T.

That looks very funky.

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web