• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java Generics

 
Mamatha Achuthan
Greenhorn
Posts: 8
Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In some part of my code I need to invoke a third party function which has a signature like this

public static <T> T[] functionToProcess(T[][] a_rrs, Class<T[]> a_destType)

From the signature I make out that the function accepts a generic 2-dimension array and a Class object referring to single dimension array of the same generic type as parameters.

I tried to invoke the function like this





and received an error.

Any idea where is has gone wrong?

Thanks,

Mamatha
 
Marcin Kwiatkowski
Ranch Hand
Posts: 32
Eclipse IDE Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Take a look at getClass() method documentation. Why don't you use File[].class instead?
 
Nomaan Butt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you post the exception
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you're calling theoutput.getClass() before you've defined theoutput, so that's never going to work. Marcin's suggestion should fix that - you might as well use a class literal if you can.

One further point - casting the return value to File[] is unnecessary - the return type is T[], so it will already be File[] in that call.
 
Mamatha Achuthan
Greenhorn
Posts: 8
Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The File[].class works! Thanks.


The Netbeans IDE was showing the following error message when I used the getClass function

Method cannot be applied to given types
found : java.io.File[][],java.lang.Class<capture #1 of ? extends java.io.File[]>
required : T[][],java.lang.Class(T[])

Thanks again,

Mamatha
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because getClass() returns a Class<?>, without any generic information. The main way to get a Class object with generic information is to use a class literal. Class.getSuperclass and Class.asSubclass return a Class object with some generic information but it still has a wildcard type.
 
Mamatha Achuthan
Greenhorn
Posts: 8
Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the information you all have shared. Thanks to you all, I took a look the 'Class' class and leaned new things.



Thanks again,

Mamatha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic