eclipse - search variable name by it's value in debugger - java

I'm debugging web app, which produces very complex bean as result of form, and I'd like to know if value that I entered on some form field is present somewhere or not.
Is there any way to find if some of my variables (on debug list) has given value?
PS:
There isn't any way to search thou all variables (I have hundreds of them...) shown in debug list? Problem is that this bean is made of tens of hashmaps and lists, on different levels. And real problem is that I don't really know which variable holds this value or if it wasn't saved to any variable. And I can't write such big expression cover all of variables, structure is too complex.

Add a breakpoint at a location where you can access the variables you want, then add a watch for each variable and you can see the list of the values of the variables in the watch list
PS: In a watch you can write whatever Java code you want such as x.equals("value") and it will output true if equals or false if otherwise. Or you can just visualize the variable's value directly

Related

How to assign a value to a variable in Android studio

I'm trying to do a simple text game where the player has a weapon.
I want that, when clicking a certain option, the value that holds the weapon name "knife" gets updated to "sword".
I made a resources string that holds the "knife" value but can't seem to find a syntax that will update it.i already read that maybe you can't change resource values. Is this true?
If it is. How do I solve this?
Thanks.
It seems like you really need to go back and start with the basics of Android and Internationalization. It is not possible to update a resource at runtime, as you have correctly discovered, so it is your approach that needs to change.
Here is an example:
TextView text;
String value = getString(R.string.knife);
text.setText(value);
It seems that onClick you try to do something like:
setString(R.string.knife, "sword");
Which is impossible. Instead, you need to have the two strings as separate resources and then switch your value to the new resource when needed. So simply:
value = getString(R.string.sword);
text.setText(value); //and reset the display
For reference, if anyone stumbles upon the same problems, I found a good solution with Shared Preferences. It allows saving variable values in the device's memory.

The reference of the object changes in every debug

Doubtlessly, this question is asked already (may be many times) but I could not find the correct keywords to find them.
Basically, my question is about the object references. What I know is that the object references points the objects physical location on the memory. However, when I debug my code and every time when I debug, I get a difference object reference for the same object.
For example, when I firstly debugged my code and the reference of a button looks like
INFO [sysout] [AWT-EventQueue-0]
[Ljava.awt.event.ComponentListener;#28be012c
at the second time, it is
INFO [sysout] [AWT-EventQueue-0]
[Ljava.awt.event.ComponentListener;#31a056d8
My related questions are;
1.Is the part after (#) symbol (a.k.a #28be012c) reference to the object, if yes, it is something like ip address, which changes continiously?
2.Is there a way to obtain an address, which does not change over time (like a Mac-address)
Any answer or link related to these questions will be highly appreciated.
Edit
I am debugging in this scenario. There is a button and everytime when this button is clicked, the debugger stops at this point. That is to say, the program is not started from the beginning.
Is the part after (#) symbol (a.k.a #28be012c) reference to the object, if yes, it is something like ip address, which changes
continiously?
The part after the # is Integer.toHexString(hashCode());. The hashCodemethod is not designed to return the same value every time it is invoked for different runs (even if the object being created has the same value). It is also not mandatory that the returned value is related to the memory. JVM spec specifies that a unique value should be returned, but it doesn't specify "how".
Is there a way to obtain an address, which does not change over time
(like a Mac-adress)
No. Each run of the JVM will almost always give different hashcodes (unless you override the hashCode method to return something else.

How can I stop Eclipse's auto-completion feature from suggesting made up variable names?

I've set up Content Assist to trigger on aAbBcCdDeEfFgGhHjIiJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ.() rather than only on .( (Under Window/Preferences/Java/Editor/Content Assist->Auto activation triggers for Java:) so that I can quickly select classes without pressing Ctrl+Space.
This was all good until I went back to creating variable names rather than using them, and it started suggesting a camel case variant of the name I just type, which doesn't yet exist, suffixed with the type! And of course pressing space afterwards uses this suggestion.
i.e. I type only the keys needed for
private static String HELLO = "hello";
I'll end up with the line
private static String hELLOString = "hello";
This comes under Java Proposals (Task-Focused), but turning this off under Advanced options obviously removes all the useful suggestions.
My aim is to allow auto-completion using known objects, not make them up, is there a way of doing this, and if so, How?
It looks like you're asking for code completion at a point (variable declaration) where there are no correct answers since only you know what name you want to enter. Eclipse does try to suggest a possible name, as you point out, from the already-entered type, but it can only guess.
Once you've declared the variable, Eclipse will be able to offer up that correct variable name later on in the code.
You can reduce the likelihood of accidentally selecting suggested variable names by increasing the 'Auto activation delay (ms)', and making sure that when you type a variable name you press [space] or [;] immediately afterwards before thinking about what to type next (if you haven't already).
Unfortunately there is no way (at least in the current Kepler SR1 release) of turning off variable name suggestion without also losing declared variable name suggestion, which is too useful to lose.

Is there a way to search the variables available at the current place in the stack during remote debugging?

I'm remote debugging a Java application and (not for the first time) I find myself looking for a value without knowing what variable might hold it (if any at all). This is especially hard to find since I'm stepping through library code rather than my own code, so I was wondering; since eclipse can display the variables currently available on the stack, along with all contained values, is there any way I can search these? Or at the very least dump it out as text somewhere and grep it or something.
I usually do an export to JSON using Jackson's ObjectMapper whenever I find myself into the situation of having to search among a bunch of values caught while debugging. On breakpoint hit, let's say I want to search some string inside a text representation of myObj, which could be some messy POJO deep with nested objects. Just evaluate the following:
org.codehaus.jackson.map.ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper();
mapper.writeValue(new java.io.File("/tmp/myObj.json"), myObj);
and then go grep your value inside the file you just created.
YMMV: if you have no idea where to start the search you'll have to iterate through what's available on the stack. Also the JSON representation might not be suitable for every kind of search.
I'm not sure about the feature you are asking for but there is another approach you could take. Assuming you know the general area AND the object you are looking for isn't too common, eclipse supports conditional breakpoints so you could set breakpoints on the end of methods chechking the method variables and object state.
You could try evars. I haven't tried the search function, but it allows expanding and exporting all the variables on the stack to a file, which you can then grep for your value. I installed the latest version into Eclipse manually, i.e. putting the jar in the dropins/plugins directory. Worked for me on Eclipse 3.6.1.

JVMTI - how to get the value of a method parameter from callback

I am recording all method entries from my Java app thanks to a JVMTI Agent. For now, I am able to get the name of each method, but I'd want to be able to get the value of the parameters that method received.
This problem has already been discussed in an older topic (see How to get parameter values in a MethodEntry callback); it fits perfectly what I'm looking for, so I know I have to use GetLocalObject function, but I can't figure out how to (the example given in the topic is broken).
Can anyone help me finding out how to do this? Thanks.
I think you want to access arbitrary method parameters without foreknowledge of their content, if not could you clarify your question?
See the JVMTI docs on local variables.
First, you need to ensure you have enabled local variable access in your capabilities list. Then, find out what parameters are available using GetLocalVariableTable. The returned table will contain a description of each local variable in the method, including the parameters. Don't forget to Deallocate it when you're done.
You'll need to work out which variables are parameters. You can do that by finding the current jlocation and eliminating local variables which are not yet available. This won't tell you the parameter order, but it'll tell you which locals are parameters. You can probably assume that the slot number is the correct order.
Find the current jlocation using GetFrameLocation, iterate over the local variable table, and for each local variable whose start_location is less than or equal to your current location, add the slot number and type to your list of parameters.
For each parameter, call the appropriate GetLocal{X} method based on its type. You'll need the depth of your current frame, which you already have from GetFrameLocation.
That should get you your parameters, but it'll be slow and tricky to implement. You'd be far better off following the guide's recommendation of avoiding MethodEntry callbacks and use bytecode instrumentation (BCI) instead.

Categories

Resources