Eclipse Plugin for custom Strg+Click hook - java

Eclipse for Java has the convenient feature of jumping to the definition of a class or method by clicking on a usage while holding the Ctrl-key.
I want to implement a similar functionality for the following usecase:
We have an annotation taking the path of a yaml-file as a parameter like this:
#MyAnnotation("myYamlFile.yaml")
Clicking on the filename while holding Ctrl (or some other key or combination) should open the file in an editor (the path is relative to a specified root path, which is on the classpath). Obviously selecting the filename and performing Ctrl+Shift+R (for "Open Resource") would work too, but since it'll be a very common usecase to jump between the Java and the yaml file, I want to look into making it even simpler.
My plan is to write a plugin for this. Before I start, I wanted to ask, if there's a simpler or better solution, maybe an existing plugin, which can be configured to be used for my purpose. Does anyone have experience with something similar, who can point me in the right direction?

It turned out quite easy to write an Eclipse plugin for it.
All I needed to do was implementing org.eclipse.jface.text.hyperlink.IHyperlink and org.eclipse.jface.text.hyperlink.IHyperlinkDetector.
http://codeandme.blogspot.co.at/2014/06/adding-hyperlink-detectors-to-editors.html

Related

Is there a way to leave notes in library code in eclipse?

due to reasons I am working with undocumented java library code that I cannot alter in any way or write into. Im using eclipse 2020-06 and I would like to leaves some notes for myself to make things easier. Is there a way to do that? or maybe an eclipse extension?
You can use bookmarks. They work somewhat similarly to breakpoints, without pausing execution when you're debugging.
You can add them via the context menu.
If I recall, you're a bit limited by the amount of information you can add. It's basically just a single text box.
See the help section on bookmarks
Maybe there's some plugins which extend the functionality (e.g. this one for adding keyboard shortcuts)

File structure in netbeans

I'm trying to understand the file structure in Netbeans.
For example: I want to create a new class. I do so by right clicking the navigation bar, and get prompted to name my new class. A warning appears with the words "It is highly recommended that you do not place Java classes in the default package"
So,
What does go in the "default package"?
What goes in the Test Packages and Test Libraries folders?
If I have some text files or some such thing for my program to read, where should they go?
I'm taking some online courses on Java, but these sort of nuances aren't covered in the classes. I want to start doing it right, right now, so I don't have to untangle all of my files later on down the road.
You should refresh your understanding by reading the tutorial.
The default package is a package that gets created for your project. It's OK to organize your files under the default package, but if your work is somewhat serious, you're going to want to place them under a named (and therefore non-default) package, like com.myorganization.myproject as per the tutorial. NetBeans will also allow you to refactor (rename) an existing package. Left click, hit F2 and supply a new name.
Test packages and libraries used to test your project go in the Test Packages and Test Libraries directories. It sounds like you're a ways off from testing with frameworks like JUnit, however, so you needn't concern yourself with those things just yet.
If you have arbitrary data files, you can really put them wherever you can find them later. If you're working with Android, for example, you'd have a /res directory just under your project root with resources like images, icons, data files, etc. You can create your own resources directory, or you can be lazy and dump them directly in the project root. Wherever you put them, you have to make sure you call them correctly using absolute or relative paths.
If you're using online courses, especially if you're paying for them, use the resources they provide, like live tutors or their forums. This kind of fundamental/tutorial help is a bit below the threshold for Stack Overflow.

Library to access Javadoc from Java

I want to find a library that I can use from my Java application that will allow me to access specific Javadoc in the scope of my project (I specify where Javadocs are located). Just like in Netbeans, I want to potentially access the Javadoc from html files locally and remotely, and from source.
I expect that I could use code from Netbeans to achieve this, but I don't know how, and I can't easily digest their documentation.
Today I started thinking about the same thing.
From CI point of view, I could use #author annotation to send e-mail to someone, who wrote a test that is failing with error, not with a failure.
Google didn't help me (or I didn't google deep enough), so I started wondering how to do it on my own.
First thing that came to my mind is writing a little tool that will check all *.java files specified in a directory, bound file name to annotations and allow user to perform some actions on them.
Is that reasonable?

Realize modular architecture / simple plugin system

I wrote a simple program taking some commands from a XML file, executing it, checkings for errors of the commands, reporting back by mail etc. An entry for a command looks e.g. like this:
<command>C:\Program Files\Test\test.exe /xyz</command>
Now I want to introduce some kind of plugins:
<command myPlugin1="abc,1" myPlugin2="def">C:\Program Files\Test\test.exe /xyz</command>
I thought about writing the plugins as a Java class implementing some interface or subclassing some Plugin class, then having a method like
pluginExec(List<String> parameters, String command)
that gets passed the parameters (e.g. abc,1) and the command (C:\Program Files\Test\test.exe /xyz).
Then maybe take all attribute names of the command tag, therefore myPlugin1 and myPlugin2, then search for a class with the same name as the attribute on myapp.plugins namespace and call pluginExec(...).
Any better ideas on this? I guess with my idea I need some kind of reflection, is there a way without it? The application is a private tool, no need to be able to add plugins without recompiling the software. I just don't want to change the code everytime, I just want to plug in my new plugin class.
If your plugin based program you develop will not grow a lot in the next future then your design is fine.
However, if you expect it to grow and you expect to add multiple features to it than I suggest you try Java standards like OSGi.

Override existing Eclipse plugin extension

I have an existing Eclipse plugin that provides an extension point. The plugin uses standard Eclipse mechanism to find the extensions. In this plugin's code, following code is used to get the extension.
IConfigurationElement[] config = Platform.getExtensionRegistry()
.getConfigurationElementsFor(extensionPoint);
if (config.length > 0) {
return config[0];
}
As you can see in the code, only the first found extension is used. This plugin already provides an extension and this extension is used in the default case.
Now I need to override the behavior of the default extension, so I created a new plugin and extends the same extension point. But it turns out that the default extension is always the first one in the IConfigurationElement array, so it's always picked up.
How can I make my own plugin appear first in the found IConfigurationElement array, then my own plugin is used instead of the default one?
The existing plugin is written by others and I don't want to make changes to it until it's absolutely necessary.
I'd say this is a bad way to get extensions from an extension point, either way. If they just want the use the pluginsystem to load a specific extension they have created, they could use the getConfigurationElementsFor(String namespace, String extensionPointName, String extensionId) method instead and close off the possibility for others to use the extension point. As of now there is no sure way of knowing which extension they will get. Chances are, there are instances in the code later on that assumes they will get their extension and when they don't get the extension they expect, Mr ClassCastException comes knocking on the door. (Had a bug like this in a system once)
Of course the best way is to change the code to handle many extensions!
But to your question; I dont know how the ExtensionRegistry fills the array, the API doesnt say. Perhaps there is a way to perhaps set a specific version of your extension that will allow it to be placed first in the array. You would have to look in the code of the ExtensionRegistry to know exactly how the extensions are found. I think it may be in alphabetical order, but im not sure.
Another way is to overload the existing plugin with your plugin and replace functionality. A very dirty approach, but in some cases it is doable. See one of my questions regarding this

Categories