• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read/understand a generic method?

 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, I'm learning Rest with Jersey 2 impl.
I had to implement a converter that takes in a json string and converts into a domain object CustomerId.
It worked.

But if I have to read and rephase in simple word for this method getConverter, I have some headache :-D

Starting with this return

1. CustomerId.valueOf(value) return a CustomerId.
2. rawType.cast(CustomerId.valueOf(value)) and return T. How possible?

What is T?


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line:

casts the argument (the object that CustomerId.valueOf(value) refers to) to the type T (since rawType is a Class<T>). You could just as well have written:

T is a type parameter. It's replaced by whatever type you want when you use the method. See Generics in Oracle's Java Tutorials for an introduction to generics.
 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Given CustomerIdConverterProvider will be called by the framework Jersey2 component or some thing like that.

and "T is a type parameter. It's replaced by whatever type you want when you use the method."

At that moment in time, the original method (or mine) is translated into some thing like this (pseudo)

 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic