File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes java Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java Generics" Watch "java Generics" New topic
Author

java Generics

Mamatha Achuthan
Greenhorn

Joined: Oct 09, 2011
Posts: 8

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

Joined: Aug 06, 2007
Posts: 32

Hi!

Take a look at getClass() method documentation. Why don't you use File[].class instead?
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
can you post the exception
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Oct 09, 2011
Posts: 8

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

Joined: Oct 27, 2005
Posts: 19790
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mamatha Achuthan
Greenhorn

Joined: Oct 09, 2011
Posts: 8

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java Generics