IntelliJ debugging - Watch for any variable containing a specific value - java

Is it possible to put out a global watch or breakpoint, on no specific variable or line of code, with the aim of identifying the first instance of a value?
I'm debugging on an old java6 web app that does a million calculations between multiple databases and dozens of classes with thousands of lines of code each. I'm not exactly sure which of these dozens of classes are being called within this project containing hundreds of classes.
Let's say I'm looking for where "the dog runs" appears in the flow of a calculation: Is it possible to listen for the first appearance of that string with the intention of finding what variable will contain the value?

I do not think that is possible to monitor where a value comes into existence as i think you have asked.
However, you can set "field watchpoints" which are basically expressions like break when x = "the dog runs". When i have used these before the program will run very very slowly.
Refer here for details of how to set it: Intellij breakpoints.
Another technique is to start near the top of where you think a value is being set to what you want. Step over each method until you see where the value set to what you are looking for. Then repeat the process inside the method that you saw it change (i.e. this time step into the method where the value changed).
Within this "second level method", you will probably see another call that sets the value you are looking for. So repeat the process again until you find a system call that sets the value (e.g. a read from a database or a regex match etc).
It sounds tedious, but it is a "divide and conquer" method of the form that eliminates a huge chunk of code that doesn't set the value you are looking for into the one method call that does. Then you divide and conquer the inner workings of that method. In practice, it doesn't take long before you narrow it down.

Related

AnyLogic method exceeding the byte limit error: how these methods are actually defined?

This is a follow-up question on this; regarding the error message as below:
Description: The code of method _createPersistentElementsBP5_xjal() is exceeding the 65535 bytes limit.
It basically says that the size of the code for the method _createPersistentElementsBP5_xjal() has exceeded the maximum limit. By inspecting the Java code of the model, I see that my main agent is divided into six parts: _createPersistentElementsBP1_xjal(), _createPersistentElementsBP2_xjal(),..., _createPersistentElementsBP6_xjal().
And the one with number 5 has issues. However, I cannot find any defined logic on how these methods are separated (e.g., what elements to include in which method).
If you look at the top part of the screenshot, you will see that paths 5,6,7 belong to ...BP4_xjal(). And then starts ...BP5_xjal() (the problematic one) with intersections and a node called reversePoint4. In fact, reversePoint4 and path5/6/7 all belong to the same network.
The question is: what is the basis for such method definitions in AnyLogic (e.g., which elements will belong to which method)? Is it done arbitrarily?
You can probably only know an answer to this by asking AnyLogic support since this is the internals of its code generation. My guess would be that it just splits the initialisation of space markup elements into 'chunks' of a given number (perhaps --- and this would be rather ironic --- to try to avoid method/signature size issues).
(I'm assuming that the BP1-5 splits don't correspond to any 'functional' splits in your model; e.g., different space markup networks or similar. You seem to be implying that in your question.)
Java has an in-built limit on the amount of text that can be used to define a single method (more explanation here). In this case, this error is caused by the actual text of the method is generated by AnyLogic.
To solve this I would try to restructure the model to reduce the amount of text. This can be done by defining functions that create elements instead of defining elements directly or moving elements inside other agents that are then included in Main agent.

React to console inputs (commands) - how to handle multiple options most efficient?

Some information (don't want to confuse you with a lot of shitty code):
I've done a pretty large console programm (my largest project so far) which helps me a lot with managing some accounts / assets and more. I'm constantly adding more features but at the same time I reshape the code to work on my shitty coding style.
The console program has a lot of commands the user can type and for every command different methods get called / objects get created / manipulated and so on.
My keywords which are saved in an ArrayList<String> and my commands have this type: [keyword] [...n more Strings]
DESIGN PROBLEM 1:
I have a method cmdProcessor(String[] arguments) which handles the input (command) of the user, and the [keyword] is always the first argument arguments[0]. That means I have a large number of if-statements of this type:
if(arguments[0].equalsIgnoreCase("keyword") callMethod(argmts); where in the String[] argmts the remaining arguments[1] ... [n] are.
Is this a good way to handle this or should I go with switch-case?
Or something else (what?)? Is it better to save the keywords in a HashMap<String, Method>?
DESIGN PROBLEM 2:
The methods (see above callMethod(argmts) ), which are triggered by the entered keyword look even more chaotic. Since the same method can have different numbers and forms of arguments saved in the String[] argmts the method is full of if(argmts.length == ...) to check length, and every of these if-blocks has a bunch of switch-case options which also have a lot of ifs and so on. The last else and the default-case in switch-case I always use for error-handling (throwing error codes and and explanation why the pattern doesn't match and so on).
Is this good or are there better ways?
I thought about using lots of submethods, which would also blow up
my program and cost a lot of time but maybe improve readability / overview. Is this okay, or what is the best
option in such cases (lots of ifs and switch-case)?
Since I want to build more and more around this program maybe I should start now to fix bad design before it's too late. :)
About Design-Problem 1:
My go-to would be to register a lot of Handlers, which you can base on a common interface and then implement the specific behavior individually. This is good, because the central method handling your input is slim, and you only need to register a lot of singletons once, on initialization. Disadvantage: if you forget one, it will not work. So maybe, you can register them automatically (reflection or something thelike).
Aside from that, a map is better than a List in this case, because (I assume) you don't need a sorting. You need a mapping from key to behavior, so a map seems better (though even a very large set of keywords would probably not be very inefficient, if you stick to a list).
About Design Problem 2:
If I was you, I'd use actual Regular-Expression patterns. Take a look at the java.util.regex.Pattern-class. You can isolate groups and validate the values you receive. Though it does not spare you the exception/error-handling, it does help a lot in segmentation and interpretation efforts.

IDEA shows method as in use, when it is not

Why does IDEA show this SimpleClass.start method as in use:
Even when it is not. Changing the name of the method to something else then greys it out as not in use:
Seems to not only happen with start but also init and stop for ones I've tried. Am using version 13.1.4 on Windows.
This is a performance optimisation.
Basically IntelliJ IDEA first checks its index for occurrences of the name of the method.
Then it checks in the file of every occurrence to see if it's really a usage of that method.
If the name of the method is used in many places, many files will have to be parsed and checked. To avoid taking too much time and cpu for that, the check is skipped if there are more than a certain amount of occurrences in the index and it assumes that the method is used (Since the probability is very high).
Running the unused declaration inspection in batch mode (Analyze > Run Inspection by Name...) will still report the method as unused.

What do these numbers `10k`, `6k`, `1k`, `210` in java eclipse code completion mean?

What do 10k, 6k, 1k and 210 mean in this eclipse Luna code completion popup. It appeared when I was trying to override a method from a custom class in a custom class.
That is a certain plugin at work, either Code Recommenders or something similar.
Basically, it's (crowd sourced) information that tells you how often or likely a certain method is called or overridden.
AFAIK, The greater the number to the right of a method, the greater the probability that you'll use that method in that context, as calculated by some weird algorithm.
These numbers come from our Codetrails Connect Hippie Completion.
It shows how often developers call or override methods and suggests them in order of frequency. In this case, Object.equals() was overridden about 10.000 times, while Object.hashCode() was overridden about 6.000 times.
The data comes from users of Codetrails Connect who decided to share their code completion events with us. We then aggregate those events and provide the resulting information back to our users.
You can adjust the way these numbers are shown at Preferences > Codetrails Connect > Hippie - Relevance Indicator Settings. Instead of the default "Heatmap", you can elect to show absolute, unrounded numbers with "Simple (m/n)" or show the relative "Percentage" for proposals.

Is there a way to output variables as Java sees them so I can isolate methods for testing or asking for assistance?

I'm learning Java and find myself sending methods around while asking for help but my problem is I have many methods and the data is modified at each method. I often have to send large files when only one area is relevant(it makes my SO questions excessively long as well).
But for some of the stuff I do, I can't get the right data format to be outputted as string that I can input later. For example, if I add data to a list of Points(like this, (new Point(0, 0));) then when I output the results I get something like this(with sample data):
[java.awt.Point[x=970,y=10], java.awt.Point[x=65,y=10], java.awt.Point[x=729,y=10]
I get errors when I assign this to a variable and send it to my method I want to test/show. I basically have two goals:
If I want help on a single method(thats part of a much larger class), I want to be able to send the least amount of code to the person helping me(ideally just the method itself and the inputs..which I'm unable to capture exactly right now).
When I test my code, I would like a way to isolate a method so I don't have to run a large file when all I can about is improving one method.
I am pretty sure I'm not the first person to come across this problem, How can I approach this?
UPDATE: Here's an example,
double[] data = new double[] {.05, .02, -.03, .04, .01};
System.out.println(data); //output is: [D#13fcf0ce
If I make a new variable of this and send it to a method I get errors. I have 30 methods in a class. I want to have a friend help me with one. I'm to avoid sending 29 methods that are irrelevant to the person. So I need a way to capture the data, but printout doesn't seem to capture it in a way I can send to methods.
Java outputs variables in a way that is human-readable (although it depends on the object's toString method). The output of toString is (unsurprisingly) a String. Unless you have a parsing mechanism to turn a string back into the original object, it's a one-way operation.
There should be no need to turn it back into the original object, however. If you're trying to isolate a function and sample data, the easiest thing to do is encapsulate it in a test and some data--there are many different ways to do this and communicate it to someone else.
I'm still unclear on your usecase, however. If it's an SO question, all you should need to do is show the code in question, provide a minimal amount of data that shows the problem, and you're done. This could be done in a self-contained example where you simple create the data in code, as a unit test, or by showing the string output as you've already done.
If you're trying to communicate the issue to a tech support tier, then the best mechanism depends entirely on what they're equipped to handle--they'll tell you if you didn't do it right, believe me.
You can use Debuggers and step over your code. You can 'watch' variables so that you can get their actual value, rather than their toString representation. Debuggers are usually part and parcel with all the major IDE's such as Eclipse, Netbeans and IntelliJ.
As to your questions about isolation and testing, this is much more of a design problem. Ideally your methods should be self contained, reducing coupling. What you could do is to learn to break down your problem into smaller chunks and until it can't be broken down further. Once you do this, you start building methods which tackle each part of the problem seperately.
Once you have your method, you test it on its own (thus reducing the amount of things which can go wrong, as opposed to testing tons of code at once). If you are satisfied, you integrate the method with your code and test again. If something then goes wrong, you will know that your last module is the problem since it broke your system. You can get some more information about this here.

Categories

Resources