3

I was asked to edit a java file so that it would retrieve a value. The value name is not important, but it was SiteCategory.

With the help of Eclipse I was able to find the method that looked like what I was looking for.

I then created these seven lines to see if one of them returned what I was looking for.

System.out.println("1) " + p_objRecord.getSiteCategoryList().toString());
System.out.println("2) " + p_objRecord.getSiteCategoryList().getClass());
System.out.println("3) " + p_objRecord.getSiteCategoryList().getSiteCategory().toString());
System.out.println("4) " + p_objRecord.getSiteCategoryList().getSiteCategory(0).getCategoryDescription());
System.out.println("5) " + p_objRecord.getSiteCategoryList().getSiteCategory(0).toString());
System.out.println("6) " + p_objRecord.getSiteCategoryList().getSiteCategory(0).getCategory().getName());
System.out.println("7) " + p_objRecord.getSiteCategoryList().getSiteCategory(0).getCategory().toString());

This was the output

1) com.webservices.lists.farming.SiteCategoryList@47c7b1f5

2) class com.webservices.lists.farming.SiteCategoryList

3) [Lcom.webservices.lists.farming.SiteCategory;@105e924

4) >P align=left>

5) com.webservices.lists.farming.SiteCategory@ca143fc6

6) Products : Miscellaneous : Bottles

7) com.webservices.platform.core.RecordRef@78f8dcf1

Number 6 was exactly what I was looking for. It took me some time to finally get to this point and figure out the correct syntax. If you were asked to do this with an unfamiliar API, what would you have done to figure out the correct syntax?

3

3 Answers 3

7

I would look for the same thing that you were looking for. However, I would typically not use Sys-Out. Instead, I would add a breakpoint there, run to the point with the Debugger. Then, use "inspect" to see what it coming back.

3

I would look at the code that imports a SiteCategory and then see if I could find javadoc documentation on some.package.name.SiteCategory.

Really, without some sort of javadoc or the actual source code, you're pretty much constrained to hoping the method names are descriptive "enough".

So in the file, you should either see a class or interface definition for SiteCategory or an "import" block of the above class, which looks like

 import org.myorg.site.SiteCategory;

and if you don't, then it's actually in the same package as the package declaration (the first non-comment, non-blank line in the file). So

 package com.mypackage.here;

would mean a com.mypackage.here.SiteCategory failing an import statement or a definition within the '.java' file.

Then depending on the source code availability, I'd probably open up the source code, or if it isn't available, look for some published javadoc documentation.

1

Set a breakpoint at a line that has a reference to the parent object, start your app in debug mode, then when the program halts at the breakpoint, inspect the object. You would quickly figure out how to get the value you want.

All IDEs allow you to inspect objects and their fields (getters or not).

Don't write code to debug: Use the built in debugging capabilities of java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.