How to create your own Live Template, using the current line (Intellij) - java

I am trying to create my own live template which uses the current line that my cursor is on, but I cannot find the variable for it.
To explain what I mean, IntelliJ has a built-in .sout template which SOUTs the current statement.
I am trying to find a way to add my own .[extention] that wraps the current code on that line, into a different statement.
So something like this:
Will turn into this:
Any ideas on how to get that done properly?

You need to create postfix completion. Rather than Live template, postfix completion surrounds an expression you’ve just typed.
See https://www.jetbrains.com/help/idea/settings-postfix-completion.html

Related

infinite loop detected, partial ../foo.mustache was previously loaded

Hi I have two handlebars templates which is as below.
foo.mustache
{{#if hasProperties}}
{{>bar}}
{{/if}}{{^hasProperties}}{{propsName}}{{/hasProperties}}
bar.mustache
{{#propertyObject}}{{>foo}}{{/propertyObject}}
When I compile this and run, I get the following error.
infinite loop detected, partial '/templates/foo.mustache' was
previously loaded
What is the proper way to fix this?
I've found the solution for this issue. It's not something related to the template itself. The mustache files were in correct order but there is a property in the mustache java implementation where default value is set to false.
public void setInfiniteLoops(boolean infiniteLoops) link
this method will actually set to accept infinite loops which will be needed if you are running a recursive call inside the mustache template itself.
I hope this will help anyone looking to solve a similar kind of error.

How to run a java class only once in background, in a feature file

I have a statement in the background of a feature file as,
def token = Java.type("Calling a static method from a java class")
I have 4 scenarios in my feature file and this java class is called 4 times. How to call this java class only once?
According to karate docs you have to use callonce
Variables set using def in the Background will be re-set before every Scenario. If you are looking for a way to do something only once
per Feature, take a look at callonce. On the other hand, if you are
expecting a variable in the Background to be modified by one Scenario
so that later ones can see the updated value - that is not how you
should think of them, and you should combine your 'flow' into one
scenario. Keep in mind that you should be able to comment-out a
Scenario or skip some via tags without impacting any others. Note that
the parallel runner will run Scenario-s in parallel, which means they
can run in any order.
Please check
https://github.com/intuit/karate#script-structure
https://github.com/intuit/karate#callonce
https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/callonce/call-once.feature
Background:
karate.callSingle('called.feature#scenarioToBeCalledOnceInBackground');

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.

How to access Java API within netbeans and another question

I just have two specific questions that over the years I have never been able to figure out in netbeans.
How do I access the Java API documentation without having to use my web browser. It would be nice if I could have a window in netbeans of the Java API. Is this not possible? How do you normally access the API document? Note that I don't mean just access for the documentation for the current code (cntrl-space, I think)... I just mean for browsing.
Code completion will automatically generate right brackets (which I like). When this occurs, the cursor is still within the brackets. How do you jump out of the brackets without mousing? I know a command to move to the end of line, but sometimes the brackets aren't at the end of the line... it seems like there should be a way to just jump to the end of the autocompletion.
For 2) I honestly just use the right arrow.
re 1) I guess you are looking for: Windows -> Others -> Javadoc
Additionally if you use Shift-F1 (instead of Alt-F1) on e.g. a classname it will show the documentation in the internal browser as well.
re 2) I don't think there is a shortcut for this, but you might be able to do this with a (recorded) keyboard macro (by searching for a closing bracket).
You can open the JavaDoc window
with Window > Other > JavaDoc
Typing the closing bracket or
parenthesis just puts you outside of
the generated brackets

Can I automatically refactor an entire java project and rename uppercase method parameters to lowercase?

I'm working in a java project where a big part of the code was written with a formatting style that I don't like (and is also non standard), namely all method parameters are in uppercase (and also all local variables).
On IntellJ I am able to use "Analyze -> Inspect Code" and actually find all occurrences of uppercase method parameters (over 1000).
To fix one occurrence I can do "refactor > rename parameter" and it works fine (let's assume there is no overlapping).
Is there a way to automagically doing this refactor (e.g: rename method parameter starting with uppercase to same name starting with lowercase)?
Use a Source Parser
I think what you need to do is use a source code parser like javaparser to do this.
For every java source file, parse it to a CompilationUnit, create a Visitor, probably using ModifierVisitorAdapter as base class, and override (at least) visit(MethodDeclaration, arg). Then write the changed CompilationUnit to a new File and do a diff afterwards.
I would advise against changing the original source file, but creating a shadow file tree may me a good idea (e.g. old file: src/main/java/com/mycompany/MyClass.java, new file src/main/refactored/com/mycompany/MyClass.java, that way you can diff the entire directories).
I'd advise that you think about a few things before you do anything:
If this is a team effort, inform your team.
If this is for an employer, inform your boss.
If this is checked into a version control system, realize that you'll have diffs coming out the wazoo.
If it's not checked into a version control system, check it in.
Take a backup before you make any changes.
See if you have some tests to check before & after behavior hasn't changed.
This is a dangerous refactoring. Be careful.
I am not aware of any direct support for such refactoring out of the box in IDEs. As most IDEs would support name refactoring (which is regularly used). You may need to write some IDE plugin that could browse through source code (AST) and invoke rename refactoring behind the scene for such parameter names matching such format.
I have done a lot of such refactorings on a rather large scale of files, using TextPad or WildPad, and a bunch of reg-ex replace-all. Always worked for me!
I'm confident that if the code is first formatted using an IDE like Eclipse (if it is not properly formatted), and then a reg-ex involving the methods' signature (scope, return-type, name, bracket, arg list, bracket) can be devised, your job will be done in seconds with these tools. You might need more than one replace-all sets of reg-ex.
The only time-taking activity would be to come up with such a set of reg-ex.
Hope this helps!

Categories

Resources