Java: How to find object address while debugging? - java

While debugging I want to make sure that the Object in use (passed/modified in methods) by multiple threads is the same Object.
As in Java we can not find an object address, how to check if it is the same object? Is there a special way to find that in Netbeans or any other IDE?

When using NetBeans then you can show at the Value in your variables window.
As long as the value is not a primitive datatype the number shows you some type of instance number for the object. As long as the number is the same the objcts are also the same.
E.g.
If the size or something else is shown then you can configure your view by pressing the red marked configuration button.
Create a new Formatter as shown in the next picture (leave the Value code snippet blank) and deactivate the Default Formatter:

Related

How/where can I "stash"/keep data in my application?

I display dates to the user in a layout that are let's say are textual.
When the user presses a button I want to get the information in those fields that represent these "dates" but if I get the text in them is not of value to me.
I would need to store somewhere the original dates that created these "textual" elements and fetch them from there.
Is there a specific construct in android that one can use as a stash area or should I just use a static class with variable to hold them?
In your case, you should use SharedPreferences to store the data by converting it into a String (text) or int/long first.
This will allow you to easily write and retrieve data, and you should use this.
You can also use the file system to save almost any Java object using serializable, on Internal Storage.
Either way, the data will stay there even if your app is closed or the device is turned off.

Displaying a string, from an array within an array with JasperReports

I currently use iReport 5.1.0, Tomcat 7, Java, Spring MVC amongs other things.
I believe my current problem lies within the iReport itself, although it may be more complex.
I currently have an Object (Guide),
My Guide has a list of Objects, (itemsGuide)
My itemsGuide has a list of Objects, (itemsTeam)
My itemsTeam has a list of Objects, (professional)
My professional has a name.
My jasper file, receives as parameter the Guide, in the iReport software I must assign the fields and the current variables, although I must print, in a single line, parameters from the itemsTeam object (a couple of strings), and in this very specific case I will always have only one professional, although the variable lies within a list, therefore I must access the first object in this array to then display its attribute.
I keep getting the most unusual errors as I try to do this rather simple process.
The iReport either doesn't find the variables in itemsTeam or the ones in professional, when, for some unknown reason it "works" my preview of the PDF (it says "The document has no pages", to avoid the error msg I change the attribute in my report proprety, but now, it simple shows the iReport file without any field/variable, only the graphical elements).
I am utterly confused, would anyone be so kind as to lend a hand to this poor newcomer?
the JRXML file has over 37k characters and I can't post it as an edit, I uploaded as a txt file insted.
Thanks in advance.
No more need for the link to the xml ~
-- CATURDAY EDIT --
Allright, I seem to be getting closer to my solution.
At this point I've tried quite a few different approaches to my problem.
Although I couldn't completely solve it, I managed to get closer, or so I believe.
Now, all I have to find out (it seems) is:
How to access an array within an array.
I managed my way around the guide and the itemsGuide, I can't access the array within itemsGuide, I need to access a variable named "team" within that array (itemsGuide) and then print the value of a few strings in this array (which is of a specific class, itemsTeam) and a string in an object (professional).
All this, without subreport.
Anyone ?
Allright, what I spent about 4 days trying to find out, my co-worker with more experience than me found out in about 20 minutes.
The issue wasn't within iReports itself, it was necessary to create methods in the class in order to return the desired object/list, something I did not even consider given that I know very little of how iReports (and java honestly) works.
But according to what I understood from what has been done it has something to do with reflection and a couple of methods in the java class.
Thanks guys. :>

User-defined strings for cross-program copy / paste

I'm trying to build a program that supplements and replaces Windows shortcuts with those defined by the user. Currently, I'm working on building a copy/paste function, but can't figure out how to move the copied object into a program other than mine. I tried using the Robot class to simulate keypresses depending on the value at a specific index in a string, but that won't allow me to pass variables other than the VKEY constants.
What would be needed in a method to copy and paste text between programs?
If you would consider using JavaFX: There is a Clipboard class, which claims to get the system clipboard, but I haven't tried if it works accross (non-JavaFX) applications:
http://docs.oracle.com/javafx/2/api/javafx/scene/input/Clipboard.html
Edit:
There is also an AWT-based Clipboard: http://docs.oracle.com/javase/7/docs/api/java/awt/datatransfer/Clipboard.html

Alter order of content assist eclipse for android development

I am working on an Android app, using Eclipse 4.2. I have content assist set up almost as I want it. However, when I, for example, start typing int to create a Java int variable, the Android.R.Integer entry in content assist is the primary entry.
I have content assist set up by relevance. This is just an annoyance because every time I want to make an int variable, I have to either hit Esc or hit enter and delete "eger" from the end of the type.
Thanks in advance.
None of the solutions posted here really worked, so what I did was to go to
Java -> Appearance -> Type Filters
and add *int* (yes, it's an asterisk followed by int and then another asterisk).
There's a checkbox next to the filter you just added, make sure it is checked.
Problem solved (finally!)
You can use Preferences->Java->Appearance->Type Filters to filter out the suggestions you want to ignore.
I'm wondering why you really need content assist to create an int variable, it would be easier to simply type it. But if you really want to, you can type the integer value and then use the Ctrl-2-L shortcut to create the local variable.
In
Java->Appearance>Type Filters
click add
type 'int*' -> click ok.
Then when you type in int , the first is int instead of integer .
Cren I am in the same boat. I have the content assist set to auto activate with every letter because I find it extremely useful that way, but have the same int(eger) problem that you do.
I have come up with a few ways to deal with it and thought I would share them in case you find one of them useful.
You could use a Type Filter as ralph suggested works just fine, you just don't get the assist when you actually want Integer, which isn't that big of a deal.
You could delay the content assist long enough so that you can type "int" and enter before it pops up, giving you a delay always. (In Preferences->Java->Editor->Content Assist there is an Auto activation delay setting)
Or remove 'i', 'n', and 't' from the Auto activation triggers (again in Preferences->Java->Editor->Content Assist). This is the method I am currently trying out, and it seems to work with everything, even Integer, saving me from having to type "ger" :).

IDEA: "Assign statement to new local variable"?

As a long time Eclipse user, I'm playing around a little bit with IntelliJ IDEA 10. I can't seem to find out how to perform an "Assign statement to new local variable" code completion.
Feature explanation:
I type something like
new BufferedOutputStream(out)
and then hit Cmd (or Ctrl)+1 and enter, and Eclipse changes the line into:
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
At the same time, I can type over "bufferedOutputStream" immediately to rename it (or select from the options "bufferedOutputStream", "outputStream" and "stream" from a dropdown).
I use this feature for absolutely every assignment, and it's an enormous time saver - this must be available in IDEA, too - but where is it hidden?
Refactor | Introduce Variable (Ctrl+Alt+V on Windows). Note that you don't need to select the text if it's the only text in the current line. Then you can change the variable name in-line just like you've described and press Enter to complete editing.
Another way is to use the Postfix Completion:
Type .var (or just .v to select it from the list) and confirm it with Enter.
As CrazyCoder mentions you can use Ctrl+Alt+V. Also instead of selecting the expression, clicking into somewhere in your expression and using Ctrl+W to expand scope is very useful while using introduce refactorings. Extract refactorings are:
Extract variable: Ctrl+Alt+V
Extract field: Ctrl+Alt+F
Extract method: Ctrl+Alt+M
Extract parameter: Ctrl+Alt+P
Extract constant: Ctrl+Alt+C
Also, Idea is a polygot editor so you can use these extract refactorings for other file types like js or html also (not all refactorings work in all file types but Ctrl+W works mostly).
There are more extract refactorings which do not have shortcuts which you can access from Refactor|Extract menu (both menu bar and context menu). To quick access all refactorings you can use Ctrl+Alt+Shift+T for a popup menu.
As a last word, I highly encourage you using "Tip of the Day" (Help|Tip of the Day). It is a fast way to learn many helpful features of Idea.
It's not as nice as Eclipse, but you can try the following:
new BufferedOutputStream(out)
Select the expression above, either with your mouse (or by using Ctrl+W).
Then hit Ctrl+Alt+V to Introduce a Variable or (Ctrl+Alt+F to Introduce a Field)
Easiest is, hit Alt+Enter, you will be offered with a list of options, and just select "Introduce local Variable".

Categories