How to do code format switching in eclipse? - java

A)
public class SomeClass
{
private SomeClass()
{
}
public String someMethod()
{
return "";
}
}
B)
public class SomeOtherClass{
private SomeOtherClass(){
}
public String someOtherMethod(){
return "";
}
}
I have joined a new team and will be working on a project which follows the A) convention. However, I have always been the B) java style person and am way more comfortable with B).
1)On the checked out code, is there a way I could convert the java code style in my eclipse to B)
2)And also ensure the project->Team->Synch with Repo ignores this style change when checking for updates ?
3)Before comitting, I want to switch the code back to the commonly followed style and check it in. I synch for changes every morning and commit changes throughout the day.
Is creating a new profile in the preferences->code style->Formatter the only way ? I also looked at http://astyle.sourceforge.net/ but I am somehow confident there is a simpler eclipse solution to this. How could I achieve this in the simplest possible way ?
I am using eclipse kepler

Work flow:
In Windows > Preferences > Java > Editors Save Actions deselect formatting on save.
Check out code.
Clean up your code(Right click on project go to Source > Clean up. Note this works on project level but not on working set, so you have to do it on each and every project) with your Formatter(B) profile enabled.
In Windows > Preferences > Java > Editors Save Actions select formatting on save and start working.
Same as step 1.
Same as 3 but with formatter profile A.
Commit the code.
These steps can be automated with Ant/Maven script(?) or by developing your own eclipse plug-in.
On sync comparator will NOT ignore style change. IMHO there is no escape. Clean up before sync is only the go.
In Git SCM there are some commit and checkout hooks but I haven't explored on this.

Related

New value added to Java Enum not available during debug

I am having the following problem:
I have an Enum that was originally declared with 5 elements.
public enum GraphFormat {
DOT,
GML,
PUML,
JSON,
NEO4J,
TEXT {
#Override
public String getFileExtension() {
return ".txt";
}
};
Now I need to add an additional element to it (NEO4J). When I run my code or try to debug it I am getting an exception because the value can't be found in the enum.
I am using IntelliJ as my IDE, and have cleaned the cache, force a rebuild, etc.. and nothing happens. When I look at the .class file created on my target folder, it also has the new element.
Any ideas on what could be causing this issue ?
I found my problem and want to share here what was causing it. My code was actually for a Maven plug-in which I was pointing to another project of mine to run it as a goal. However the pom.xml of my target test project was pointing to the original version of the plug-in instead of the one I am working on, and that version of course is outdated and does not include the new value. Thank you.

can't step into java source code. "step into" somehow act like "step over"

I'm trying to studying how a method in java String class works, so I created some customised code that calls that String class method.
As you can see, I have set a break point in my own code and I have set another break point in the java String class source code.
While I'm in debug mode and is on line 7 of my code, I pressed step into.
However, rather than stepping into the String class method indexOf, eclipse instead moved onto line 8 of my code.
Why is this happening? how can I step into the java string method source code?
public class TestingIndexOfMethod {
public static void main(String[] args) {
final String stringToBeSearchedThrough = "hello world";
final String substringToLookFor = "ll";
int a = stringToBeSearchedThrough.indexOf(substringToLookFor, 0);
System.out.println(a);
}
}
Edit 1:
I have already check to see, if "use step filter" is activated before asking this question on SO, and it is not activated. So I dont think "use step filter" is the problem here.
Edit 2:
step into works fine with methods I defined myself
Most probably there is a step filter which instructs the debugger to skip certain classes.
In the preferences dialog (menu Window -> Preferences) check the step filtering settings.
Either deactivate Use Step Fitlers which deactivates all step filters or deactivate the filter for the classes java.* only.
edit Another reason might be that your project is using a JRE instead of a JDK for the execution. Find below an example using a Java 8 JRE respective a Java 8 JDK.
project build path using a JRE (pay attention to jre1.8.0_112)
project build path using a JDK (pay attention to JavaSE-1.8)
edit 2 To determine the used Java runtime library add following statement in your code and run it in debug mode.
...
public static void main(String[] args) {
Stream.of(System.getProperty("sun.boot.class.path")
.split(File.pathSeparator))
.filter(s -> s.endsWith("rt.jar"))
.forEach(System.out::println);
...

Programmatically refreshing a file in an eclipse Java application

I have the following program which adds a method to itself when run. But I have to refresh it every time using the F5 button or the refresh option.
Is there a way I could code the refresh in the program itself so that it refreshes itself after the modification? The project I am working on is a Java application and not an eclipse plugin so as far as I know the refreshLocal() method can't be used.
public class Demo {
public static void main(String[] args) throws IOException, CoreException {
File file = new File("/home/kishan/workspace/Roast/src/Demo.java");
if (file.exists()) {
JavaClassSource javaClass = Roaster.parse(JavaClassSource.class,
file);
javaClass.addMethod().setPublic().setStatic(true)
.setName("newMethod").setReturnTypeVoid()
.setBody("System.out.println(\"newMethod created\");")
.addParameter("String[]", "stringArray");
FileWriter writer = new FileWriter(file);
writer.write(javaClass.toString());
writer.flush();
writer.close();
}
}
}
I have tried using the refreshLocal() method defined in the eclipse JDT but since my project is a Java application the ResourcePlugin.getWorkspace() method does not work giving me a "workspace closed" error. Any suggestion is appreciated.
You see, eclipse runs your Java class within its own dedicated JVM. Thus there is no direct programmatic way of enforcing a refresh within eclipse.
You could check this older question; maybe that could lead to a reasonable workarounds.
On the other hand you might step back and ask yourself why exactly you want to achieve that. Your workflow simply doesn't make much sense when looking at it; as in: when generating code that way, shouldn't that generated code better go in its own specific place?
If you intend to "generate" code frequently to then continue to use it in eclipse; well, that somehow smells like a strange idea.
Eclipse has "Refresh using native hooks or polling" which might might help.
You can find it under Window > Prefrences > General > Workspace.
See On Eclipse, what does "Preferences -> General -> Workspace -> Refresh using native hooks or polling" do?

Eclipse code formatter: Perform a single action

How can I perform a single action in Eclipse Java code formatter? For example I want to clean up every occurrence of
if (bla) {
...
}
else {
...
}
To this
if (bla) {
...
} else {
...
}
But nothing else. I need this to clean up specific findbugs issues. If I'd run the whole code formatter actions on the project, it would lead us into merge hell. So I want to handle such findbugs issues one by one and therefore It would be great to just execute such a single rule. The Version of Eclipse wouldn't matter, right now I tried with the latest Luna.
A quick way to do this would be to use the search and replace option in eclipse. Go to Search menu->File
Search for : \}\s+\n\s+else\s+\{
Replace with : } else {

How do I open a new project and perspective in SWTBot plug-in test?

For example: I want to "test" Eclipse-CDT. I can't do it using Java code so I need to import a C-project. I didn't found it in Run Configurtion therefore I:
* Create SWTBot Test Plug-in
* Record a sequence of actions via SWTBot Test Recorder:
** Import a C-project
** Change perspective
** Do stuff (run program and check registers for exapmles)
* Insert recorded actions into #Test method
* Run As > SWTBot Test
Here is a problem. SWTBot fails at picking imported project showing various changing errors (mostly "WidgetNotFoundException" at expanding package view or picking imported project from project tree and "NullPointerException" at the same point). Testing code looks like
#Test
public void test() {
bot.menu("File").menu("Import...").click();
bot.tree().expandNode("General").getNode("Existing Projects into Workspace").select();
bot.button("Next >").click();
bot.comboBox().setText("C:\\Sandbox\\JavaWorkspace\\CDTtest");
bot.button("Browse...").click();
bot.button("Finish").click();
bot.toolbarButtonWithTooltip("&Restore").click();
bot.tree().getTreeItem("CDTtest").getNode("main.c").select();
bot.menu("Project").menu("Build Project").click();
bot.menu("Run").menu("Debug").click();
bot.tree().getTreeItem("General Registers").expand();
bot.menu("Run").menu("Step Over").click();
}
Is there more appropriate way to described things? Or maybe a more appropriate tool for that?
It seems like the generated code doesn't include anything about switching to another property. It's possible that there is no recording rule existing for this action. You may need to tweak your test manually to add this missing operation.

Categories