Unable to Overload toString for Object

O

Ojesh Dugar

Code:

package XMLRPC;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;

public class Search {


@Override
public String toString() {
//System.err.println ("Ojesh");
return String.format("oje"+"abx");
}

public static void main(String args[])
throws MalformedURLException, XmlRpcException {


HttpClient httpClient = new HttpClient();
XmlRpcClient rpcClient = new XmlRpcClient();
XmlRpcCommonsTransportFactory factory = new XmlRpcCommonsTransportFactory(rpcClient);
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

factory.setHttpClient(httpClient);
rpcClient.setTransportFactory(factory);
config.setServerURL(new URL("http://abc2/bugzilla/xmlrpc.cgi"));
rpcClient.setConfig(config);

//map of the login data
Map loginMap = new HashMap();
loginMap.put("login", "abc@bag");
loginMap.put("password", "***");
loginMap.put("rememberlogin", "Bugzilla_remember");




// login to bugzilla
Object loginResult = rpcClient.execute("User.login", new Object[]{loginMap});
System.err.println ("loginResult=" + loginResult);

// map of the bug data ok
Map bugMap = new HashMap();

bugMap.put("id", "350");

//bugMap.put("status", "NEW");

// create bug
Object createResult = rpcClient.execute("Bug.search", new Object[]{bugMap});
//createResult.toString();

System.err.println("createResult =" + createResult.toString());


}


}



Its not returnig OJEABX as expected. Instead bugs=[Ljava.lang.Object;@2ee5e48a being diplayed.
Where am i going wrong.??
 
S

Stefan Ram

Ojesh Dugar said:
public class Search {
public String toString() {
Object createResult = rpcClient.execute("Bug.search", new Object[]{bugMap});
System.err.println("createResult =" + createResult.toString());
Its not returnig OJEABX as expected. Instead bugs=[Ljava.lang.Object;@2ee5e48a being diplayed.
Where am i going wrong.??

You attempt to tackle a programming task without having
learned the language fundamentals first. Otherwise, the
error would be obvious to you.

You are overwriting the toString() method for objects
of the class »Search«, but the object referred to by
»createResult« is not an instance of the class »Search«.

In Java, one cannot modify the toString() method for
the standard class »java.lang.Object« as in JavaScript:

Object . prototype . toString = function(){ return "b" }
var test = new Object();
test.toString()
"b"
return String.format("oje"+"abx");

You cannot expect »OJEABX« when you return »ojeabx«.
 
O

Ojesh Dugar

Ojesh Dugar said:
public class Search {
public String toString() {
Object createResult = rpcClient.execute("Bug.search", new Object[]{bugMap});
System.err.println("createResult =" + createResult.toString());
Its not returnig OJEABX as expected. Instead bugs=[Ljava.lang.Object;@2ee5e48a being diplayed.
Where am i going wrong.??



You attempt to tackle a programming task without having

learned the language fundamentals first. Otherwise, the

error would be obvious to you.



You are overwriting the toString() method for objects

of the class »Search«, but the object referred to by

»createResult« is not an instance of the class »Search«.



In Java, one cannot modify the toString() method for

the standard class »java.lang.Object« as in JavaScript:



Object . prototype . toString = function(){ return "b" }

var test = new Object();

test.toString()

"b"


return String.format("oje"+"abx");



You cannot expect »OJEABX« when you return »ojeabx«.

Yes now i see the problem. Thanks a lot.
But my problem still remains how do i ACCESS the content of »createResult« which is not instance of my class??
Thanks.
 
S

Stefan Ram

Ojesh Dugar said:
But my problem still remains how do i ACCESS the content of »createResult« which is not instance of my class??

In Java and OOP, one prefers to hide the content. This is
called »information hiding« or »encapsulation«. Objects
instead have »behavior«.

What comes most close to »access the content of an object«,
would be to read the documentation of the class of the object.

When »o« is an object, you can get its class with »o.getClass()«.
When the class is being defined in a library or program, someone
should have written a documentation for it, and you should read
that documentation to learn how to get information from the object
(using your point of view, although we prefer not to think of
objects as information containers but rather of entities with behavior).
 
O

Ojesh Dugar

In Java and OOP, one prefers to hide the content. This is

called »information hiding« or »encapsulation«. Objects
Thanks a lot. I'll work on it.
instead have »behavior«.



What comes most close to »access the content of an object«,

would be to read the documentation of the class of the object.



When »o« is an object, you can get its class with »o.getClass()«.

When the class is being defined in a library or program, someone

should have written a documentation for it, and you should read

that documentation to learn how to get information from the object

(using your point of view, although we prefer not to think of

objects as information containers but rather of entities with behavior)..
 
L

Lew

Ojesh said:
But now how can I access content of a object from some other class??

(You only need one question mark at the end to indicate an interrogative sentence.)

You access content from an instance of a class by using what methods it provides to reveal what it considers "content".

What is the type whose content you are trying to reveal, and what is its API?
 
O

Ojesh Dugar

(You only need one question mark at the end to indicate an interrogative sentence.)



You access content from an instance of a class by using what methods it provides to reveal what it considers "content".



What is the type whose content you are trying to reveal, and what is its API?

Basically, I am able to access Bugzilla Webservice API through java code(xmlrpc) and in return i get a object of <<class java.util.HashMap>>.
According to Bugzilla documentation two items are returned, bugs and faults, both are array of hashes and I want to access these.
Thanks.
 
M

markspace

Basically, I am able to access Bugzilla Webservice API through java
code(xmlrpc) and in return i get a object of <<class
java.util.HashMap>>. According to Bugzilla documentation two items
are returned, bugs and faults, both are array of hashes and I want to
access these. Thanks.


Cast to a HashMap and iterate over all of its keys.

(not tested):

// create bug
Object createResult = rpcClient.execute("Bug.search", new
Object[]{bugMap});
//createResult.toString();

HashMap bugs = (HashMap)createResult;
for( Object key : bugs.keySet() ) {
System.out.println( "Key "+key.getClass().getName()
+ "= "+key
+ ", value "+ bugs.get( key ).getClass().getName()
+ "= "+ bugs.get( key )
);
}

More info here:

http://www.mkyong.com/java/how-to-loop-a-map-in-java/
 
E

Eric Sosman

Basically, I am able to access Bugzilla Webservice API through java
code(xmlrpc) and in return i get a object of <<class
java.util.HashMap>>. According to Bugzilla documentation two items
are returned, bugs and faults, both are array of hashes and I want to
access these. Thanks.


Cast to a HashMap and iterate over all of its keys.

(not tested):

// create bug
Object createResult = rpcClient.execute("Bug.search", new
Object[]{bugMap});
//createResult.toString();

HashMap bugs = (HashMap)createResult;
for( Object key : bugs.keySet() ) {
System.out.println( "Key "+key.getClass().getName()
+ "= "+key
+ ", value "+ bugs.get( key ).getClass().getName()
+ "= "+ bugs.get( key )
);
}

More info here:

http://www.mkyong.com/java/how-to-loop-a-map-in-java/

This probably won't work as written. The original post said
createResult.toString() produced "[Ljava.lang.Object;@2ee5e48a",
indicating that rpcClient.execute() returns an Object[], that
is, an array of Object references. (To those surprised that an
array of Objects is itself an Object: Someday it will all make
sense, I promise.) So what he needs to do is cast the returned
value to an Object[]:

Object[] array = (Object[]) createResult;

.... then iterate over the contents of the array:

for (Object obj : array) {

.... and then (assuming the Objects in the array are HashMaps as
advertised, or in any case *some* kind of Maps):

Map bugs = (Map) obj;

.... and *then* he can iterate over the entries, either in the
manner markspace shows or by using the key/value pairs directly:

for (Map.Entry ent : bugs.entrySet()) {
Object key = ent.getKey();
Object val = ent.getValue();
...
 
O

Ojesh Dugar

On 2/12/2014 12:38 AM, Ojesh Dugar wrote:


Cast to a HashMap and iterate over all of its keys.

(not tested):

// create bug
Object createResult = rpcClient.execute("Bug.search", new
Object[]{bugMap});
//createResult.toString();

HashMap bugs = (HashMap)createResult;
for( Object key : bugs.keySet() ) {
System.out.println( "Key "+key.getClass().getName()
+ "= "+key
+ ", value "+ bugs.get( key ).getClass().getName()



This probably won't work as written. The original post said

createResult.toString() produced "[Ljava.lang.Object;@2ee5e48a",

indicating that rpcClient.execute() returns an Object[], that

is, an array of Object references. (To those surprised that an

array of Objects is itself an Object: Someday it will all make

sense, I promise.) So what he needs to do is cast the returned

value to an Object[]:



Object[] array = (Object[]) createResult;



... then iterate over the contents of the array:



for (Object obj : array) {



... and then (assuming the Objects in the array are HashMaps as

advertised, or in any case *some* kind of Maps):



Map bugs = (Map) obj;



... and *then* he can iterate over the entries, either in the

manner markspace shows or by using the key/value pairs directly:



for (Map.Entry ent : bugs.entrySet()) {

Object key = ent.getKey();

Object val = ent.getValue();

...



--

Eric Sosman

(e-mail address removed)

Object[] array = (Object[]) createResult;
this line itself gives an error.
Exception in thread "main" java.lang.ClassCastException: java.util.HashMap cannot be cast to [Ljava.lang.Object;
at XMLRPC.Testing.main(Testing.java:64)
 
O

Ojesh Dugar

Basically, I am able to access Bugzilla Webservice API through java
code(xmlrpc) and in return i get a object of <<class
java.util.HashMap>>. According to Bugzilla documentation two items
are returned, bugs and faults, both are array of hashes and I want to
access these. Thanks.





Cast to a HashMap and iterate over all of its keys.



(not tested):



// create bug

Object createResult = rpcClient.execute("Bug.search", new

Object[]{bugMap});

//createResult.toString();



HashMap bugs = (HashMap)createResult;

for( Object key : bugs.keySet() ) {

System.out.println( "Key "+key.getClass().getName()

+ "= "+key

+ ", value "+ bugs.get( key ).getClass().getName()

+ "= "+ bugs.get( key )

);

}



More info here:



http://www.mkyong.com/java/how-to-loop-a-map-in-java/

The Code you have given gives output as:

<< Key java.lang.String= bugs, value [Ljava.lang.Object;= [Ljava.lang.Object;@78812862 >>

Which means that object is a hashmap Having Key as "bugs" and value itself is another object.(As per my understanding)

According to documentation,
bugs- is an array of hashes that contains information about the bugs with the valid ids. Each hash contains the following items
1.component
string, The name of the current component of this bug.

2.creation_time
dateTime, When the bug was created.

3.creator
string, The login name of the person who filed this bug (the reporter).

4.id
int, The unique numeric id of this bug.

Now i cant undersatnd how do i proceed to get these informations.

Thanks.
 
M

markspace

According to documentation, bugs- is an array of hashes that contains
information about the bugs with the valid ids. Each hash contains the
following items

It sounds to me like the first property, with a key "bugs" is an array,
probably of HashMap (or possibly just Map), which contains a the actual
bug information.

The chance that I'm going to mess something up here is increasing
because I'm not compiling this, but try this.

// create bug
Object createResult = rpcClient.execute("Bug.search", new
Object[]{bugMap});
//createResult.toString();

HashMap bugs = (HashMap)createResult;
for( Object key : bugs.keySet() ) {
System.out.println( "Key "+key.getClass().getName()
+ "= "+key
+ ", value "+ bugs.get( key ).getClass().getName()
+ "= "+ bugs.get( key )
);
Object value = bugs.get( key );
if( value instanceof Object[] ) {
Object[] valueArray = (Object[])value;
for( Object v : valueArray ) {
System.out.println( "-- "+v.getClass().getName() );
}
} else System.out.println( "not an array!" );
}


You should be able to run this and determine what to replace the
v.getClass().getName() part with; probably a cast to a Map. I don't
recall if collections normally override their to string -- they might,
though the result normally isn't very pretty.
 
O

Ojesh Dugar

According to documentation, bugs- is an array of hashes that contains
information about the bugs with the valid ids. Each hash contains the
following items



It sounds to me like the first property, with a key "bugs" is an array,

probably of HashMap (or possibly just Map), which contains a the actual

bug information.



The chance that I'm going to mess something up here is increasing

because I'm not compiling this, but try this.



// create bug

Object createResult = rpcClient.execute("Bug.search", new

Object[]{bugMap});

//createResult.toString();



HashMap bugs = (HashMap)createResult;

for( Object key : bugs.keySet() ) {

System.out.println( "Key "+key.getClass().getName()

+ "= "+key

+ ", value "+ bugs.get( key ).getClass().getName()

+ "= "+ bugs.get( key )

);

Object value = bugs.get( key );

if( value instanceof Object[] ) {

Object[] valueArray = (Object[])value;

for( Object v : valueArray ) {

System.out.println( "-- "+v.getClass().getName() );

}

} else System.out.println( "not an array!" );

}





You should be able to run this and determine what to replace the

v.getClass().getName() part with; probably a cast to a Map. I don't

recall if collections normally override their to string -- they might,

though the result normally isn't very pretty.

Yeah, This worked out for me.
That was a great help. Thanks a lot.
 
E

Eric Sosman

[... lots of ugly double-spaced googlegroup grunge ...]

Object[] array = (Object[]) createResult;
this line itself gives an error.
Exception in thread "main" java.lang.ClassCastException: java.util.HashMap cannot be cast to [Ljava.lang.Object;
at XMLRPC.Testing.main(Testing.java:64)

Your original post said the code's output was

bugs=[Ljava.lang.Object;@2ee5e48a

.... which is what you'd get if it output "bugs=" and the string
representation of an Object[] array. On closer inspection, though,
I see that there's nothing in the code that could have produced
the "bugs=" part. Conclusion: You misreported the output or you
reported the output of some slightly different piece of code, and
I was wrong to believe that "[Ljava.lang.Object;@2ee5e48a" was the
string representation of `createResult'. Sorry about that -- but
in the future, please make sure your code and your reported output
belong together.

If `createResult' refers to a HashMap (as the exception text
indicates), you can proceed as markspace showed. If it's an array
(in a follow-up you wrote that "two items are returned [...] both
are array of hashes") you'll need to access its individual elements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top