deleting an element in debug mode in eclipse - eclipse

I'm running my code in debug mode in eclipse and in the middle of it, I want to change the size of a List,say from 9 to 6, by deleting 3 elements.
But I'm not seeing any option to do that, in fact what I'm seeing is the option to change the values present in the elements.
So how can I delete the elements itself from the List ?

Make sure you have the "Variables" tabs on eclipse "Debug" view focused on your current evaluated code.
In "Debug" view, right-click on "Value" cell inside the "Variables" table, and select "Change value".
You will have an option to write a Java expression so you can add something like:
yourList.add("newItem"); or: yourList.remove(0);
Make sure to reload the variable ("F5") once you are done and you will see the updated state.
Note that not every List implementation supports add() or remove() methods.
See this for more details if you encounter an exception.
See also:
Eclipse docs - Variables view
Eclipse docs - Change variable value

I was looking to do something similar. I had an ArrayList containing 1 element, and I wanted to remove it.
I tried #Leet-Falcon's answer, e.g. yourList.remove(0), and Eclipse replied "Unsupported operation exception".
What ended up working was : return new java.util.ArrayList<>();

Looks like the "Debug Shell" view allows this, or additionally if it's a simple enough list, the following simple Change Variable can also work:
new ArrayList<String>(java.util.Arrays.asList("1","2")) // or any other simple list
Modifying Java Collection (List) Variable in Eclipse Debugger

Related

Custom content assist for default java editor in Eclipse

I'm currently trying to develop an Eclipse Plugin to support code replacement, like what the default content assist in Eclipse do. What I want to implement is something like "insert argument names automatically on method completion with visualized box around the argument" and I can "use the Tab key to navigate between the inserted names" and "while navigating, list of optional variables for current argument can be displayed and be chosen".
In short, it comes to two questions:
How to add the visualized box around the already existed variable or even Java keywords that need replacement? And at the meanwhile I can use Tab key to switch between these boxes.
How to display a list of candidates to select from when I trigger on the box?
By now I only figure out the extension point : org.eclipse.jdt.ui.javaCompletionProposalComputer may be useful, but I have no idea where to start at? Thanks in advance.
Oh, finally I've solved it myself...
For the 'box', it should be the LinkedModeModel, this class should work with LinkedPositionGroup and LinkedPosition to add mutiple boxes. And we should use LinkedModeUI to set it up.
For the content assistant, there's no need to use the extension point. There is a ProposalPosition class which extends LinkedPosition for you to add your proposals for the 'box' in its constructor. And we can simply use the CompletionProposal to construct a ICompletionProposal array as the argument of ProposalPosition's constructor.

Find CURRENTLY selected <option> with XPath

What's the correct XPath syntax to check if an option element is currently selected, or just to get the selected option element from a select element, on an open page with which the user, and JavaScript, may have interacted? Is this even possible with XPath, or does it lack the ability to look at DOM properties?
I can't find any documentation on this, and have (speculatively) tried:
//option[#selected=true]
//option[#selected="selected"]
//option[#selected]
but none of these work; they simply don't match any elements.
(In case it matters, I've tried this both using the $x function in the Chrome developer console, and using the find_elements_by_xpath method in Selenium for Python.)
Short answer: it's not possible.
Longer answer: XPath can look at HTML attributes, but it can't look at DOM properties. Selecting an <option> element in a <select> changes the selected property of the <option> to true, and also changes the value property of its parent <select> element, but it doesn't affect the attributes of either, so it is invisible to XPath.
To find <option> elements that have the selected attribute set, which is often how a page author might determine which option is initially selected, you can use //option[#selected]. But this does not find the currently selected <option>; changes that the user makes to the selection are invisible to XPath. There's no guarantee it will even find the initially selected option, since it's possible that the page author didn't put the selected attribute on any elements and either let the browser select the first option by default or had some JavaScript select the initial option via the selected property.
The multiple other answers here claiming that a selector like //option[#selected] can detect selection changes made by the user after the page loads are simply completely wrong.
Of course, if you're able to use CSS selectors instead of XPath selectors, then option:checked will do the job.
The problem could be the " (double quotes).
//select/option[#selected='selected'] - Will match the selected option, i am using this successfully.
//select/option[#selected='selected' and #value='specific value'] - Will only match the selected option if it has a 'specific value', i'm also using this.
If you are still having trouble, it could be an entirely different problem, perhaps there is no option node. I hope this helps.
I think we can use a knowledge from #Mark's answer and account that. Let's just find a node which HAS desired attribute:
tree.xpath('//select/option[#selected]/text()')[0].strip()
I tried "//option[#selected=''] and it has worked for me.
it is able to highlight the selected option within Page objects model.
I would try //option[#selected='true']
i.e. driver.findElements(By.xpath("//option[#selected='true']")).getText();

How to display all compile errors in Netbeans as a task list?

Is there a way in Netbeans 7.2 to have all compile errors of all open projects display in some task list, just like the "Action Items" do? I just can't find any.
The reason behind this is that I make some method signature change that for example affects 20 different callers which I'd like to change successively without having to work through the project tree.
You can create a filter for this in the Action Items Window: Action
Items Filter -> New (check compile errors)
Set scope of Action Items to "all opened projects"

How to get content assist for "for loop" anywhere in Eclipse?

In Eclipse, after a line like this:
List list = new ArrayList();
Typing "for" just beneath, and followed by "ctrl-space" (by default), will bring several options that can help completing this "for loop":
But if the variable "list" is declared far from here (e.g. as a class field) which may not be directly inferred from this context, or there are many Lists declared,then the assistance doesn't work well:
## split line ---
In some cases, Eclipse can assist, but just don't work for member variable. E.g. manually type "another" and ENTER after the ":" didn't persuade Eclipse to guess about it....
(P.S. workable case:
Auto guessed
Entered wanted name, and ENTER, works great
)
Does anyone have any tip to make this assistance work under such scenarios?
I followed Ashutosh Jindal's tip and I managed to configure the template that works (tested with Kepler release). Here it is:
for (${iterable_type:elemType(iterable)} ${iterable_element:newName(iterable_type)} : ${iterable:var(java.lang.Iterable)}) {
${cursor}
}
The main point was to change localVar to var in the template definition (the Eclipse docs clearly explain this).
How to use it:
Print the template name (foreach for default template) and hit Enter. The template will be used with default collection selected by Eclipse (the latest collection declared)
Hit TAB twice to jump to collection element. You will get drop down list with all iterable collections that apply.
Use UP/DOWN arrows to select desired collection, hit Enter. Eclipse will adjust element type and name (very cool).
Click for the screenshot
This works almost as good as Intellij templates. The drawbacks are:
template does not include arrays (as opposed to default foreach template). For arrays you have to define another template.
I have not tried this myself, but take a look at the code template definition. For example, the foreach code template is defined in Preferences -> Java -> Editor -> Templates as follows :
The definition is as follows :
for (${iterable_type} ${iterable_element} : ${iterable}) {
${cursor}
}
Notice the variables being used such as iterable_type.
Now take a look at this Eclipse help page.
There is a variable there called ${id:localVar(type[,type]*)} which is described as follows :
Evaluates to a local variable or parameter visible in the current scope that is a subtype of any of the given type. If no type is specified, any non-primitive local variable matches.
${array} is a shortcut for ${array:localVar(java.lang.Object[])}, but also matches arrays of primitive types.
${collection} is a shortcut for ${collection:localVar(java.util.Collection)}.
${iterable} is a shortcut for ${iterable:localVar(java.lang.Iterable)}, but also matches arrays.
A screenshot of the same :
I believe that if you wanted to increase the scope from which the foreach template infers it's variables, you may have to edit the template definition with the appropriate variable.
Let me know if this helps. Unfortunately, I have not delved into editing the code templates before so shall not be able to give a concrete example.
What I usually do to solve the content assist with for loop is the following:
create a local variable by typing collection variable that is declared far above and a semicolon:
list;
press Ctrl+2 L
Eclipse generates a new local variable declaration which looks like this:
List list2 = list;
type my foreach and autocomplete with Ctrl+space getting the following:
List list2 = list;
for (Object object : list2) {
}
place cursor on list2 in the for loop declaration and press Alt+Shift+I which stands for inline variable.
this results in what you want to achieve. The effort is minimal after some practicing:
for (Object object : list) {
}
Years later, if you enter the name of the field/variable you can press Quick Fix (⌘+1) and in the quick fix menu you can choose different types of for-loops for the collection or array.

how to debug JSF/EL

How to debug EL in the JSF page? I'd like to watch variable values, function calls an so on. The best solution would be an eclipse plugin, but any other possibility is better than guessing "Why this expression failed to render correctly?".
Closest what you can get in JSF/Facelets is placing an <ui:debug /> somewhere in the view:
<ui:debug />
Pressing CtrlShiftD should then show a popup window with debug information about the component tree and all available request parameters and request/view/flash/session/application scoped variables. It's basically a representation of the content of all those maps.
The hotkey is by the way configureable by hotkey attribute so that you can choose another whenever it clashes with browser default hotkeys, as it would do in Firefox; CtrlShiftD would by default show the Add bookmarks dialogue. Here's how you could make it to listen on CtrlShiftX instead:
<ui:debug hotkey="x" />
You'd usually also like to hide it in non-development stage, so add a rendered condition like that:
<ui:debug hotkey="x" rendered="#{facesContext.application.projectStage == 'Development'}" />
In the shown debug information, the information provided about scoped variables isn't that great as you would expect. It only shows the Object#toString() outcome of all scoped variables which defaults to com.example.Bean#hashcode. You can't explore their properties and the values of their properties directly like as you could do in debug view of Eclipse's debugger. You'd need to implement toString() on the class accordingly so that as much as possible relevant information is returned (if necessary, you can even let Eclipse autogenerate it by rightclick source code > Source > Generate toString()):
#Override
public String toString() {
return String.format("Bean[prop1=%s,prop2=%s,prop3=%s]", prop1, prop2, prop3);
}
As to method calls, just put a breakpoint on the Java source code the usual way. Eclipse will kick in there as well when EL calls the method. If it's a managed bean, you'll also just see its properties in the Eclipse debugger.
If you are really having problems then if you can get the source for the EL implementation (easy enough for the RI) then you can use Eclipse to set breakpoints in the EL implementation methods. You need to get an understanding of how the EL code works, but it isn't that complicated. Not for the very faint hearted though.
Another possibility would be to create and evaluate the EL programatically. There are examples of how to do this around. You can then use the debugger to fiddle around with what the expression is and what the result is until you've worked out where your problem lies.