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.
Related
I'm trying to use the Increment function to keep track of how many alerts of a specific container are stored on Firestore. To do that, on each API call that contains an alert I'm updating this counter by doing this:
db.collection("container")
.document(entryDataDto.getCollectionId())
.update("alarmCount", FieldValue.increment(1));
I have also tried like this:
Map<String, Object> update = new HashMap<>();
update.put("alarmCount", FieldValue.increment(1));
db.collection("container")
.document(entryDataDto.getCollectionId())
.set(update, SetOptions.merge());
These two snippets of code works seemingly the same but they run into the same problem, they're quite unpredictable.
From what I've tested 4 things may occur:
If the field don't already exist it will be created with value 1;
If the field already exists, it can be replaced with the same field with value 1 (by replaced I literally mean the old field is deleted and a new one take it's place);
If the field already exists, it can be incremented but the field is deleted right after the value is incremented;
And finally the rarest of them, if the field already exists, it is incremented by 1.
These 4 behaviors are quite random and I couldn't figure out a pattern among them. I've only seen number 4 happens one or two times.
Some pictures of the Firestore data that may be helpful to visualize the issue (sorry I wasn't able to screenshot the events):
Behavior 2
Behavior 3
Behavior 4
Posting this as a Community Wiki, as the issue has already been fixed by the OP himself.
Since you already know that this field is going to be used anyway, initializing the document with 'alarmCount' = 0 is a good practice, that way you can make checks with a value instead of a check for null values and this might be a good way to mitigate these unexpected behaviors.
That being said, most of the behaviours you experienced are indeed a bit strange, and you could open a case in Google's Issue Tracker to figure out why this is happening.
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.
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
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.
This question already has answers here:
Is there a way to get a reference address? [duplicate]
(5 answers)
Closed 8 years ago.
Is there a way to get address of a Java object?
Where the question comes from?:
At First, I read properties file and all the data from file was placed into table. Properties file can update. So, I want to listen that file. I listen an object using PropertyChangeSupport and PropertyChangeListener.
updatedStatus = new basit.data.MyString();
updatedStatus.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
//After changes "i", we inform the table model about new value
public void propertyChange(PropertyChangeEvent evt) {
Object objec=evt.getNewValue();
tableModel.setValueAt(objec.toString(), 0, 5);
}
});
If updatedStatus changes then i update table. MyString class have private String "Value". I want to listen properties file. So, it should make updatedStatus.value and String of Properties File equal at the same address. If i can do it, so i don't need to listen properties file.
updatedStatus.setValue(resourceMap.getString("HDI.Device.1.Name"));
I tried to use StringBuffer, but i couldn't achieve it. That's why, I asked the question.
Firstly - no, you can't get the address of an object in Java; at least, not pure Java with no debugging agent etc. The address can move over time, for one thing. You don't need it.
Secondly, it's slightly hard to follow your explanation but you certainly won't be able to get away without listening for changes to the file itself. Once you've loaded the file into a Properties object, any later changes to the file on disk won't be visible in that object unless you specifically reload it.
Basically you should listen for changes to the file (or poll it) and reload the file (either into a new Properties or overwriting the existing one) at that point. Quite whether you also need to listen for updates on the string container will depend on your application.
System.identityHashCode(obj) delivers the next-best thing: a number unique for each object. It corresponds to the default Object.hashCode() implementation.
To quote the API: "As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)".
we can get address of an object in memory. Well how? it is like that;
using sun.misc.Unsafe class in java.
create new Unsafe object and use the getAddress(Object); method and it will return a long value that is address.
and also there are many methods for this class.
you can change the values in this address using putInt(Object,long offset, int value) or like this method.(getting some value getnt(Object)).
Note: this class is really UNSAFE . if you make wrong things on your project, JVM will be stopped.
Look into Apache Commons Configuration. This library has support for dynamic reloading of (for example) property files. See here.
The best way to observe if some file changes is IMHO to make a hash value with sha1 or mda5 and save the value in a cache. And you make a Thread that every minutes, seconds, depends how often you watch file changes, and make hash value over the file. So you can compare this two values and if the values are not equivalent so you can reload the new file.
Java not like C/C++. in C++, you will often work with address (that C++ programmer has a concept call pointer). But, I afraid that not in Java. Java is very safe that prevent you to touch its address.
But, there other ways maybe same with your idea is use HashCode. HashCode of an object base on their address on HEAP.