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


Groups > comp.lang.java.programmer > #29271

Re: generics array type check

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: generics array type check
Date Thu, 24 Jul 2014 09:13:00 -0400
Organization A noiseless patient Spider
Lines 33
Message-ID <lqr0p1$ost$1@dont-email.me> (permalink)
References <lqqekm$mtq$1@ariadne.rz.tu-clausthal.de>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Thu, 24 Jul 2014 13:13:05 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="25501"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GJiBdkKTk+hYBTWKOhyI2"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <lqqekm$mtq$1@ariadne.rz.tu-clausthal.de>
Cancel-Lock sha1:AThoIrvjYBURQBesw5895ZcLbSU=
Xref csiph.com comp.lang.java.programmer:29271

Show key headers only | View raw


On 7/24/2014 4:03 AM, Philipp Kraus wrote:
> Hello,
>
> I have got this signature of a method private static <T> long[] getSize(
> T p_data )
> Depend on the type T the return value is changes e.g. on a non-array type
>
> getSize( 5 ) => new long[1]{1}
> getSize( "test" ) => new long[1]{1}
>
> on an array it should return
>
> double[] p_array
> getSize( p_array ) => new long[1]{ p_data.length }
>
> and on 2-dimensional array it should be
> double[][] p_array
> getSize( p_array ) => new long[2]{ p_array.length, p_array[0].length }
>
> How can I check the generic data type in a correct way to detect the
> array type?

     You can't, as far as I'm aware.

     However, you can use p_data.getClass().getComponentType() to
query the reference's actual (not generic) identity.  If it returns
null the reference is not to an array; if it's non-null you apply
getComponentType() recursively to descend through additional levels.

     If p_data is null I think you're out of luck.

-- 
esosman@comcast-dot-net.invalid

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

generics array type check Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-07-24 10:03 +0200
  Re: generics array type check Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-07-24 09:13 -0400
  Re: generics array type check markspace <markspace@nospam.nospam> - 2014-07-24 08:24 -0700
    Re: generics array type check Arne Vajhøj <arne@vajhoej.dk> - 2014-07-25 21:14 -0400
  Re: generics array type check Mike Amling <mamling@chaff.us> - 2014-07-25 12:15 -0500
    Re: generics array type check Mike Amling <mamling@chaff.us> - 2014-07-25 13:21 -0500
  Re: generics array type check Arne Vajhøj <arne@vajhoej.dk> - 2014-07-25 21:12 -0400

csiph-web