How can I add a custom warning to Java files in Eclipse? - java

I'd like to add a custom warning message (i.e. add an entry to the Warnings section of the Problems tab) for .java files in Eclipse.
While I've found information (here and elsewhere) on creating plugins and using markers, I haven't yet found anything which talks about extending the "problem-finding" process. Most of the articles I've found don't discuss when to create markers, only how, and those that do assume that I'm interested in attaching them to a custom build step or custom editor, neither of which seems appropriate for what I'm trying to do.
How can I create a warning which functions like those available via Window > Preferences > Java > Compiler > Errors/Warnings?

I think that a builder might be appropriate for what you want to do. There's a template in the new plugin wizard that creates everything you need for a builder (ex. business logic). Since the builder runs every time the file is modified, you can clear the old warning (code generated for you) and add a new warning (code generated for you) if applicable. This also applies when the file is first created.
You could even look through the code and find hard-coded strings and flag them with warnings if you want.
As for the comments marking translation keys in the code today, those are there to guide NLS tools (you probably already knew that). The idea was to write the code first and then go back later and translate. If a change is made to the code later, the tools can run again and only act on new strings added by the delta.

Related

Annotation processor-generated Errors/warnings not showing in Eclipse editor or Problems view

I have written a customer annotation processor to generate various source files, wrapped in an Eclipse plugin. As part of this process it also logs various errors and warnings using the usual call ProcessingEnvironment#getMessager().printMesssage(Kind, String, Element).
I have been testing the processor by debugging the plugin in Eclipse. In the launched sub-instance of Eclipse the processor all works as expected - source files are generated, picked up and interpreted by the compiler as desired. Any compiler (i.e. non-custom) errors in the generated and non-generated appear in the editor, Problems view etc. as expected.
However I am seeing a lot of inconsistencies in terms of how custom errors and warnings appear. The behaviour I see is as follows:
If no Element is specified all messages appear in the Error Log under type Info, regardless of the Kind specified when logging the error.
If a message is of Kind NOTE it always appears in the Error Log under type Info, regardless of whether or not an Element is specified.
Otherwise, if an Element is specified errors and warnings intermittently appear in the Problems view and in the editor; sometimes they don't appear anywhere. They never appear in the Error Log, whether the Kind is ERROR or WARNING
As per the emphasis above, the real issue is item 3 - that under certain situations I simply cannot get errors to appear in the editor despite being logged on valid elements. In fact, I have managed to reliably make errors appear and not appear by simply changing the name of a particular generated source file.
Of course the issue is not the filename itself, but it is certainly the case that generating the class with a name that matches references already in code causes errors to get hidden, whilst generating it with a different name (or not at all) causes errors to show (as well as all the regular compiler errors caused by a missing class). The strangest thing is that there is nothing fundamentally different about this generated class compared to any of the others (of which there are many), although it is unique in its structure and how it is referenced. It also reasonably long (around 400 methods), but artificially shortening it did not make any difference. Other generated classes also have existing references in the code and don't suppress errors.
Unfortunately I have also not yet had the time to test if this issue occurs when the Eclipse plugin is deployed (i.e. running in a 'real' instance of Eclipse), or indeed if the issue occurs when calling javac explicitly or invoking a Maven build.
Without posting the full code of the plugin I don't expect anyone to be able to help directly, but I am very open to any suggestions or advice if anybody has experience issues with annotation processor-generated errors. It seems to me like a bug in Eclipse, but I have not been able to find any reference to it online. I also cannot find any errors in the .metadata/.log files of either the underlying Eclipse instance or the launched sub-instance of Eclipse. Finally I have ensured that there are no Exceptions suppressed or reported in the annotation processor code.
Eclipse version details:
Version: Luna Service Release 1a (4.4.1)
Build id: 20150109-0600
Any help appreciated and many thanks in advance :)
If a problem/error has to be shown in the Eclipse Problems view, you need to create a Marker on the particular resource (file/folder/project). Please see the following links on how to create the Markers in an Eclipse Plugin:
https://wiki.eclipse.org/FAQ_How_do_I_create_problem_markers_for_my_compiler%3F
https://www.eclipse.org/articles/Article-Mark%20My%20Words/mark-my-words.html
In terms of how the custom errors/warnings appear, the Problems view (or the generic MarkersView) is completely flexible to show/hide certain elements. Have a look at the "Configure Contents..." menu in the Problems view to get more idea.

Debugging using Java source code

Is there a way to include whole Java source code into an eclipse project so the program is easier to debbug (e.g. by inserting println in methods you otherwise couldn't insert anything)?
I have a bug in my code. But to better understand why the bug in my code appears, I'd like to see what intermediate results in some system method (on which use the bug occurs) are.
For example, I'd like to know what JViewPort.scrollRectToVisible() exactly does and how my input behaves in it by printing out some intermediate results that occur in the method itself.
EDIT:
Instead of using JRE System Library X, I want to add the source code from JDK as if I had written the code myself. I want to be able to edit any System class just as I am able to edit any class I created myself. I want editable .java files, not packed .jars...
You would need to add the 3rd-party library to your Eclipse workspace as an project. (How you would do that depends on the code you are dealing with.) Once you have done that, you can hack your copy of the library to add trace prints etcetera.
A better alternative is to simply attach the source code for the 3rd-party library so that the debugger can show you source as you step through the code, set breakpoints,. Then use "advanced breakpoint" techniques instead of trace prints; e.g. http://www.vogella.com/articles/EclipseDebugging/article.html#advanced
You cannot change the library code, but you can view it by using de-compiler. The max you can do is this. Now if you change any code in the libraries which you reached via the de-compiler, you would find an error stating "the integrity of the .jar package has changed which is not allowed"
Eclipse have built-in support for what you wanna do.
All you have to do is set breakpoint and execute application in debugging mode.
You can use these icons in Eclipse debugging perspective.
Follow along Eclipse Debugging Tutorial for details.

Eclipse Plugin Dev - Creating and updating custom markers as files are edited

I am working on an Eclipse plugin which uses custom textmarker annotations. There are plenty of examples out there which explain how to create and delete such markers and I have this all working fine from my own event handlers (when the user clicks a 'refresh' menu item).
However, I cannot work out how to hook the Java editor such that I can do this automatically. I would like all open Java editors to call my code at the same time that it invokes the Java compiler / spell checker / etc. so that I can add and update my annotations then.
How do I do this, and can it be done without creating my own builder? I'd prefer that the user does not have to add a new builder themselves.
Thanks very much
-Ian
The easiest way to this is probably to add a new JDT Compilation Participant - see the extension point org.eclipse.jdt.core.compilationParticipant.

AutoIndent in Eclipse possible?

I have been wracking my brain trying to figure this out. For the first time I used jEdit the other day and I was pleasantly surprised that it auto indented my code (meaning that I'd put in the following code:
int method () {
_ //<-- and it put me here automatically
I've tried to get the same thing working with eclipse but with no success. I got into the code formatter but I don't see how to make that happen.
Is it possible to do this? Also while I'm here, is there a such thing as a eclipse plugin that will allow you to search the methods and classes of the standard java library?
Thanks
Personally all I use for this is the format options Window->preferences under Java->Code Style ->Formatter.
I once took the time to tweek how I like my code to look like when I work and exported the whole thing. After that I just code without too much bother on what it looks like. When I find the code looks messy by pressing the combination ctrl+shift+f and the whole class becomes pretty again, comments and all.
After a while it pretty much became a reflex...
code code code
ctrl-s, ctrl-b (cause I disable auto build sometimes), ctrl-shift-f
code some more etc...
Once I got used to this I never really cared how it presented the code as i was typing because I knew it would look all pretty as soon as the loop/if/switch/method etc is finished
My clean eclipse install does this by default.
Have you changed any options? Make sure the file you are editing has the .java file extension. The preference options that control the typing automations are under Java -> Editor -> Typing in the Window -> Preferences menu.
Also, I find that the auto-indenting, and most of the other auto-complete functions of eclipse do not function well if the file I am editing has errors in it which prevent compilation. Make sure that your curly-braces are matched correctly, this is the main one that I've noticed blocks auto-indent.
Regarding searching through the standard Java libraries, use the Search -> Java.. menu option, and check the JRE libraries checkbox, then search away. You can also use the Hierarchy view to see how the classes relate. Also, in the Package and Project views you can expand the JRE System Library, and then expand rt.jar which holds pretty much all the standard Java pacakges.
Eclipse has always done this for me by default.
One really cool thing about eclipse is that you can search preference pages. Just right click and go to prefrences. Go to the "Window" menu, and click "Prefrences". Then at the top of the tree view there's a text box that says "type filter text". Replace that with "indent" and it should bring up the page where the indent option is.
Make sure that eclipse recognizes your file as a java file, that you're using the Java distribution, the latest version, etc.
Iv been trying to work around the eclipse indenting and other supposed features for years, and it seems that the bottom line is this ...
It only works for the programming style of the authors, so to use it you need to modify your style to comply.
This would be OK except that the authors of eclipse have some very strange ideas about common shortcut keys.
One horrid example is the search features, eg when did Ctrl+K become "Find Next occurrence" and why doesnt F3 or n work?
That all being said I use eclipse because if you have the time to wait around while it starts up - or never close it - and you can modify everything youve learned about using an editor - why why why - then it will certainly increase your efficiency.
Please note that there is a preference setting for indenting, it can be set for a project, a workspace, or globally, but no matter how you set it eclipse will still chuck tab characters in where you dont want them.
In fact its indent crazy, like it wants to indent everything, even if its already indented.
Like I said Iv been using it for years and it STILL drives me nuts with its random behavior.
Follow these steps for Eclipse:
Select all text: ctrl+A
Correct indentation: ctrl+I
You should check:
Hidden features/tricks for Eclipse?
What is your favorite hot-key in Eclipse?

Questions about IntelliJ to Eclipse transition

I just started using eclipse for some personal projects and am finding the transition from IntelliJ (what I use at work) kind of annoying. I hope it's kosher to ask a few different questions in the same thread. Here goes:
1) How do I get "views" (I'm not sure if this is the term. I mean windows such as Project Explorer, Servers, Console, etc) to stay expanded and on top even after I've clicked back on the editor or another view. I'm pretty sure that right now all of these tabs are "quick views" that I have minimized and then docked, so I may not be doing this right to begin with. In IntelliJ, I would simply just pin the tab.
2) How can I open a file (for instance, an ant build.xml) without having to make it part of an eclipse project? I want the syntax highlighting and Ctrl-click ability that the IDE will give me (not to mention being able to use eclipse's built-in ant), but I don't need to associate the file with any others and so don't see the point of having to make it a part of a project.
3) Is it just me (wouldn't be surprised) or does eclipse have a bug with parsing empty html tags within the body of html tags of the same type. I've only tested this in a JSP, and it doesn't happen with JSF tags. For example: <div id="foo"><div id="bar"/></div>. Eclipse will give a warning saying the first div tag has no end tag. This is with the most recent version of eclipse for Java EE, no plugins have been installed.
4) Finally, a general question: Any best practices or resources to look at for organizing the eclipse interface and perspectives/views? What about workspaces/projects? Is there some tutorial out there that would be really informative that I could read through in less than an hour?
I appreciate any answers and tips/tricks.
First of all, please acknowledge that there are different people in the world and there are people who don't work the "Eclipse way". Even if I was paid for it (and I am), I couldn't work with IDEA. So if Eclipse rubs you the wrong way, it may not be for you. That out of the way, your answers:
In Eclipse, you open a view and let it stay where it is. In IDEA, the view changes all the time, things pop up and go away. Eclipse is static unless you specifically move things around. There are two ways to move things: You can minimize a part (a part is something which contains tabbed views). This moves the part into the closest border. Or you can maximize the current part (Ctrl-M). This pushes all other parts out of the way. Another Ctrl-M will restore the view.
This is a good place to show the difference between IDEA and Eclipse. IDEA tries to anticipate what you're doing and to be helpful. For me, this means it always gets in my way. It will start to format source as I type, things move, etc. That freaks me out. Eclipse is like a toolbox. Everything is there but you have to pick it up. A toolbox doesn't move on its own accord and it doesn't try to be smart.
Eclipse is based on the idea of a workspace. The workspace is the universe and nothing outside exists. If you need to go outside, you must first create a file or folder. In the "New File/Folder" wizard, you can open the advanced options (at the bottom) and link this resource to a real file/folder in the file system. May sound like a lot of effort but it allows Eclipse to display virtually anything in the explorer since it just shows "resources" in there, not actually files.
Smells like a bug. Please report it at https://bugs.eclipse.org/bugs/
I'm not aware of anything.
[EDIT] 3. As cletus pointed out, is not valid HTML. So that might cause the warning.
An Eclipse Perspective is a collection Views and their position. You can customize or create new perspectives, but the existing ones are good enough for a start (Java, J2EE, Java Browsing etc.). I recommend to stick with the default layout for a while until you've managed to use the quick view feature (which, personally, i find quite annoying). On small screens, i simply like to use Ctrl-M to switch the Editor to fullscreen mode and back, without the need of minimizing single views or move them around.
Yes, you can run external build scripts as well and it's called External Tool in Eclipse. Go to Run > External Tools > External Tools Configurations. Create either a new Ant-based config or a native executable (Program). The location of the build script or executable can either be workspace-relative (Browse Workspace) or absolute on the file system (Browse File System)

Categories