Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Is clone a member function for array types?

Thread view: 
tam@milkyway.gsfc.nasa.gov - 03 Dec 2007 16:02 GMT
According to the JLS clone is a public method for all array types:

6.4.5 Members of an Array Type

...
The public method clone, which overrides the method of the same name
in class Object and throws no checked exceptions. The return type of
the clone method of an array type T[] is T[].
...

However if I run the following program (JDK 1.5.0_04-b05 on Linux):

 import java.lang.reflect.*;
 public class Test {

   public static void main(String[] args) {
    int[] x = {1,2,3};

    System.out.println("Class:"+x.getClass());
    Class cls = x.getClass();
    Method[] methods= cls.getMethods();
    for(Method meth: methods) {
       System.out.println("Method:"+meth);
    }
   }
 }

I get the output

 Class:class [I
 Method:public native int java.lang.Object.hashCode()
 Method:public final native java.lang.Class
java.lang.Object.getClass()
 Method:public final native void java.lang.Object.wait(long) throws
java.lang.InterruptedException
 Method:public final void java.lang.Object.wait(long,int) throws
java.lang.InterruptedException
 Method:public fina  lean java.lang.Object.equals(java.lang.Object)
 Method:public final native void java.lang.Object.notify()
 Method:public final native void java.lang.Object.notifyAll()
 Method:public java.lang.String java.lang.Object.toString()

The clone method is missing.

The JavaDocs for the Class.getMethods is:

 Returns an array containing Method objects reflecting all the public
member methods of the class or interface represented by this Class
object, including those declared by the class or interface and those
inherited from superclasses and superinterfaces.

So I should have gotten all the public methods, and clone is supposed
to be public, but it's
not showing up....  Does anyone have any idea what I'm missing?

  Regards,
  Tom McGlynn
Daniel Pitts - 03 Dec 2007 17:06 GMT
> According to the JLS clone is a public method for all array types:
>
[quoted text clipped - 53 lines]
>    Regards,
>    Tom McGlynn

hmm... Try using getDeclaredMembers to see.  It could be that array
cloning is a special case, look at the VM spec to see if there are any
hints there.  Array types *are* a special case in the JVM.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

tam@milkyway.gsfc.nasa.gov - 03 Dec 2007 17:31 GMT
On Dec 3, 12:06 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> t...@milkyway.gsfc.nasa.gov wrote:
> > According to the JLS clone is a public method for all array types:
[quoted text clipped - 35 lines]
> > java.lang.InterruptedException
> >   Method:public fina  lean java.lang.Object.equals(java.lang.Object)

Seem to have accidentally done some editing here.
This should be 'public boolean"...

> >   Method:public final native void java.lang.Object.notify()
> >   Method:public final native void java.lang.Object.notifyAll()
[quoted text clipped - 19 lines]
> cloning is a special case, look at the VM spec to see if there are any
> hints there.  Array types *are* a special case in the JVM.

Do you mean getDeclaredMethods?  I tried that but it returns an empty
array.  That's OK in principle since the method might be declared in
some superclass of [I but getMethods should find it regardless of
where it's declared.

Although arrays are special, it seems very peculiar for there to be
objects of any type with methods not visible by reflection that may
still be invoked non-reflectively.

Regards,
Tom McGlynn
tam@milkyway.gsfc.nasa.gov - 03 Dec 2007 17:31 GMT
On Dec 3, 12:06 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> t...@milkyway.gsfc.nasa.gov wrote:
> > According to the JLS clone is a public method for all array types:
[quoted text clipped - 35 lines]
> > java.lang.InterruptedException
> >   Method:public fina  lean java.lang.Object.equals(java.lang.Object)

Seem to have accidentally done some editing here.
This should be 'public boolean"...

> >   Method:public final native void java.lang.Object.notify()
> >   Method:public final native void java.lang.Object.notifyAll()
[quoted text clipped - 19 lines]
> cloning is a special case, look at the VM spec to see if there are any
> hints there.  Array types *are* a special case in the JVM.

Do you mean getDeclaredMethods?  I tried that but it returns an empty
array.  That's OK in principle since the method might be declared in
some superclass of [I but getMethods should find it regardless of
where it's declared.

Although arrays are special, it seems very peculiar for there to be
objects of any type with methods not visible by reflection that may
still be invoked non-reflectively.

Regards,
Tom McGlynn
Roedy Green - 03 Dec 2007 17:22 GMT
> Does anyone have any idea what I'm missing?

My guess is arrays are handled as a special case.  For example you can
iterate over them with for:each even though they don't implement
Iterable.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

tam@milkyway.gsfc.nasa.gov - 03 Dec 2007 23:26 GMT
On Dec 3, 12:22 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Mon, 3 Dec 2007 08:02:51 -0800 (PST), t...@milkyway.gsfc.nasa.gov
> wrote, quoted or indirectly quoted someone who said :
[quoted text clipped - 7 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

The JLS explicitly mentions that arrays can be iterated over in the
enhanced for loop.  Unless there is something I'm missing here [which
is quite likely but I'd like to know what it is], there seems to be a
clear incompatibility between the JLS specification of array members
and Class.getMethods results for arrays.  There's nothing I've found
in the documentation that suggests arrays should be handled specially
here.

   Regards,
   Tom McGlynn
Mike  Schilling - 04 Dec 2007 00:30 GMT
> On Dec 3, 12:22 pm, Roedy Green <see_webs...@mindprod.com.invalid>
> wrote:
[quoted text clipped - 6 lines]
>> iterate over them with for:each even though they don't implement
>> Iterable.

> The JLS explicitly mentions that arrays can be iterated over in the
> enhanced for loop.  Unless there is something I'm missing here [which
[quoted text clipped - 3 lines]
> in the documentation that suggests arrays should be handled specially
> here.

Of course arrays can be used in enhanced for loops; that doesn't imply that
they implement Iterable.  The compiler knows that it's an array, and can
emit code accordingly.  E.g.

       for (int entry : i)
       {
           System.out.println(entry);
       }

generates

       int arr$[] = i;
       int len$ = arr$.length;
       for(int i$ = 0; i$ < len$; i$++)
       {
           int entry = arr$[i$];
           System.out.println(entry);
       }
Daniel Pitts - 04 Dec 2007 00:41 GMT
> On Dec 3, 12:22 pm, Roedy Green <see_webs...@mindprod.com.invalid>
> wrote:
[quoted text clipped - 19 lines]
>     Regards,
>     Tom McGlynn

array.length is actually a special JVM instruction, not a field access.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Patricia Shanahan - 04 Dec 2007 01:00 GMT
>> On Dec 3, 12:22 pm, Roedy Green <see_webs...@mindprod.com.invalid>
>> wrote:
[quoted text clipped - 18 lines]
>>
> array.length is actually a special JVM instruction, not a field access.

At the Java source language level, according to the JLS, length is a
public final field member of an array type, so it *should* show up in
reflection. However, according to the API documentation for
java.lang.Class, "Every array also belongs to a class that is reflected
as a Class object that is shared by all arrays with the same element
type and number of dimensions."

Patricia
Stefan Ram - 04 Dec 2007 01:11 GMT
>However, according to the API documentation for
>java.lang.Class, "Every array also belongs to a class that is
>reflected as a Class object that is shared by all arrays with
>the same element type and number of dimensions."

 It is always on my mind, whether an array is an instance
 of a class. What you quote sounds as if this is so.

 On the other hand, the JLS3 says:

     »An object (§4.3.1) is a dynamically created instance
     of a class type or a dynamically created array.«

   JLS3, 4

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html

     »An object is a class instance or an array.«

   JLS3, 4.3.1

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.1

 If an array is an instance of a class, then why does the JLS
 adds »... or an array« in the quotations? It would be
 sufficient to state »An object is a class instance.«, because
 this would include arrays.
tam@milkyway.gsfc.nasa.gov - 04 Dec 2007 14:34 GMT
On Dec 3, 12:22 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Mon, 3 Dec 2007 08:02:51 -0800 (PST), t...@milkyway.gsfc.nasa.gov
> wrote, quoted or indirectly quoted someone who said :
[quoted text clipped - 7 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

You're right!  I've looked more carefully at the JavaDocs and I see
that you're right and the description for getMethods explicitly
mentions that only the
methods inherited from Object are returned:
"Array classes return all the (public) member methods inherited from
the Object class."

Not sure how I missed that in my first read through, but I should have
been more careful when posting.  It's still a bit of a gotcha, but my
apologies for misleading anyone that it is not consistent with the
documentation.

I see that the discussion of getFields similarly talks in detail about
not handling the length attribute that Patricia talks about elsewhere
in this thread.

This came up in a routine that was trying to do a generic clone.  I
was  checking that an object implemented Cloneable and had a public
clone method reflectively, It looks like I need to check for arrays
explicitly also.

  Regards,
  Tom McGlynn
Daniel Pitts - 04 Dec 2007 17:27 GMT
> On Dec 3, 12:22 pm, Roedy Green <see_webs...@mindprod.com.invalid>
> wrote:
[quoted text clipped - 32 lines]
>    Regards,
>    Tom McGlynn
Not to mention Arrays are a special case anyway.
I believe that if you clone a multi-dimensional array, you in fact only
clone the left-most dimension, and therefor have shared elements.

Not to mention that performance wise, .clone on arrays(in currently
implementations) tends to be slower than creating a new array and using
System.arrayCopy.  Also, in Java 6 java.util.Arrays has some very nice
methods for copying and resizing arrays.  No deep copy though :-(

Why, may I ask, are you trying to create a generic clone? What problem
will be solved by it?  Its been my experience that using reflection
should be a last resort, or used only in framework libraries/APIs.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

tam@milkyway.gsfc.nasa.gov - 04 Dec 2007 21:00 GMT
On Dec 4, 12:27 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> t...@milkyway.gsfc.nasa.gov wrote:
...
> Not to mention Arrays are a special case anyway.
> I believe that if you clone a multi-dimensional array, you in fact only
> clone the left-most dimension, and therefor have shared elements.

Actually I think that a deep clone of arrays would have been the
special case, were that to have been Java's behavior.  Java does not
have multidimensional arrays.  It only has arrays of arrays.  The
default behavior of shallow cloning seems perfectly in keeping with
the shallow cloning of objects generally.

> Not to mention that performance wise, .clone on arrays(in currently
> implementations) tends to be slower than creating a new array and using
> System.arrayCopy.  Also, in Java 6 java.util.Arrays has some very nice
> methods for copying and resizing arrays.  No deep copy though :-(

The library that this came up in dates back all of the way to Java 1.0
in its origins, though clearly it's been updated a bit since I don't
think getMethods was available till at least 1.1 (Maybe 1.2?).  A deep
copy of arrays is another of the functions it provides.

> Why, may I ask, are you trying to create a generic clone? What problem
> will be solved by it?  Its been my experience that using reflection
> should be a last resort, or used only in framework libraries/APIs.

The method is used in a library which reads a data format (FITS) which
can
contain a variety of objects including arrays of any primitive type,
and tables
with columns of heterogeneous data.  It's often necessary to make
duplicates
of a structure and having a single method to do so is very
convenient.  At one
time using clone seemed simplest since it allows me to use a common
method for both my custom classes, primitive arrays and the a few
standard Java classes that may be included in the structures
conveniently. In fact this method is not used much -- the deep array
copy I mentioned above is used in most of the cases where I
anticipated this method might be used.  That uses reflection too but
not the classes of java.lang.reflect.  It's not liable to the bug that
lay hidden in the generic clone for so long.

When one is dynamically constructing objects using reflection can be
the natural approach and that's one of the things that the library is
supporting -- that might be what you have in mind as a 'framework/
API'.

  Regards,
  Tom McGlynn


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.