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.
Related
I am trying to write a standalone Java application in IntelliJ using edu.stanford.nlp.trees.GrammaticalStructure. Therefore, I have imported the module:
import edu.stanford.nlp.trees.GrammaticalStructure;
Currently, Intellij doesn't recognize this and many others of the imported external libraries (cannot resolve the symbols) and is also not able to automatically download/import them.
Is there a way to use the GrammaticalStructure class without having to download the entire Stanford CoreNLP .jar and adding it to the project as a library? This question applies to other dependencies as well, since I want to use other external libraries but avoid including their .jar files as much as possible (to minimize the size of the final application, given that it will be standalone). Unfortunately, all the solutions I have found proposed exactly that.
Apologies if I have overlooked some basic setting or setup steps, it has been a while since I have worked with Java.
Any help is greatly appreciated.
If you want to use it means you want to execute the code in them. How is the runtime supposed to execute code that is does not have? How is the compiler supposed to know how the code is defined (e.g. what the classes look like)? This is simply impossible. If you want to use the code you have to provide it to the compiler as well as the runtime.
If you just dont want to include all of that code into your application, you need either access to the sources and just pick the class you need or you need some kind of JAR minimizer as #CrazyCoder suggested.
I'm doing a project which in essence is a way for a user to create macros for very long/repetitive code so they can then put these macros into a their code to increase readability and encourage re-usability.
The project is split up into two areas: an RCP application where the user constructs their macros and saves them to file, and a plugin-in in eclipse where a user can insert a macro.
The RCP application has come along nicely, but now I'm getting to the real bit where I want java to look at a piece of code and at compile time go 'Oh, that piece of code means this'.
For example a user could have created a macro with an identifier macro1 that represents the code System.out.println("Hello World");. They want to use it in their class foo which will look like:
class foo{
//lots of java....
macro1
//lots more java
}
I want the user to run their class, and when Java compiles it replace macro1 with it's value.
I have been looking into this for a while and read up a little bit on JavaCC and looked into Ants Replace Task but I feel I want to go down the road of creating a custom annotation to inform Java that this string below the annotation should be transformed into/replaced by a piece of java code located in a specific text file. Hopefully ending up with something like:
#ExpandMacro
macro1
To me it seems like it could be possible but I don't know if I'm just being too optimistic! :)
Also one of my concerns is how deployable would it be as a plugin? If I create a custom annotation how would I make it available to the user to use?
Any advice/insights would be very helpful!
Edit: I have also looked into Project Lombok which is looks extremely insteresting. But again I'm concerned about deployability because of the amount of setup required for a lombok project.
The best way for doing this is Annotation Processor (something like Lombokg). With JSR 269 Pluggable Annotation Processing API configuration is easy, because jar with processor is autodiscovered.
Look at: http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java page 34
Look at this differently. I think better for you is to use dynamic language (groovy for example). You can write code in Java and run it dynamically with Groovy (without compilation) - because java code is compatible with groovy. So you can simple replace macro1 with what you need and run this code. With groovy you can
write code than looks like java
replace macros with real code
run code without compilation
Links to read:
http://groovy.codehaus.org/Embedding+Groovy
http://groovy.codehaus.org/Differences+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?
I need to create a Java command line to that will be invoked remotely on a server. It will need to:
Read "lines" of text from the user.
Recognise if the user presses the "tab" key to facilitate auto-complete.
Recognise if the user presses the "up/down" keys for history.
Before I go off and roll my own, Is anyone aware of a Java library that might facilitate all or part of this?
i.e. From the command line in ssh it might look like this:
bob> java -jar MyTool.jar
MyTool Started.
Please enter command:
> server1 startup
server starting...
server started
> server2 load accounts
Done
> server3 shutdown
Complete
>quit
Check out JReadline and jline2.
Update: picocli-shell-jline2 and picocli-shell-jline3 combine the strength of JLine 2 and 3 with picocli.
Picocli allows you to write commands (and subcommands) with options and positional parameters using very little code, and the library will generate JLine 2 and 3 command completers for all commands.
It uses ANSI colors and styles in the usage help message, and has many other unique features like negatable options, repeatable nested argument groups, variable interpolation and more.
Disclaimer: I am the author and therefore biased.
it seems like you're trying to ask 3 different questions at once and don't know what you really want an answer to.
accepting user input and providing auto-complete is trivial and i highly doubt you will find a generalized library for such a task
parsing complex bash-like statements sounds like something cool to have and a library may exist to do that, but i don't think it would give you much headroom to create your own set of bash-like instructions. (especially considering you say it needs to be more sophisticated than anything you could do as a bash script - which is a tall order)
parsing simple user input as if it was a command-line input or command is also rather trivial, and if this is what you are looking for, you should look at this possible duplicate: How to parse command line arguments in Java?
i recommend restructuring your question to be more specific in exactly what you are looking for and to avoid putting emphasis on the trivial task of "auto-complete" and simply accepting the users input in a text box.
Have you taken a look at the BeanShell? It doesn't act like a shell proper (like bash or csh) but it'll let you type java commands like an interpreter and you can use tab to autocomplete.
I've only used the 1.X versions of bean shell but they always open a window for you so it's not something you can run inside an existing shell.
I assume you mean something similar to the python interpreter. The reason there is not equivalent in Java is because Java needs to be compiled to bytecode before it can be executed.
If you are looking for something with good auto-complete capabilities. I would recommend eclipse or netbeans. They also compile your application automatically, allowing you to quickly run your code once you are done writing it.
Hope that helps.
Bit late to the party on this one, but I'd add Crash and Cliche to the mix.
Apache Karaf has a command shell which can be used as a library to build a command shell for other applications. You create classes to represent commands, and use annotations to specify the options to the commands. The Karaf library provides tab completion, history and line editing, and the ability to run interactively or read in files or command line arguments to execute as a script.
I found out about it here and have used it in my own project; it works quite well. I can't compare it to any of the others as I haven't used them.
Are there any open source tools that automate the functionality of finding the number of usages of a Java API? I can figure out this information for one class at a time in my IDE. I want to use this information to create a rudimentary report on the speed of adoption of a particular library. I would create a daily report on the number of usages of dozens of classes, and I would report on several code bases.
I'd go with one of those tools for analyzing dependencies in Java code. Let it work on your source tree, a package or a single class and see if you can export the results to XML or something like that. I've used Dependency Finder in a project about two years ago and I think it should do what you want. Not sure about the export to XML, though.
In Eclipse you can right click on a method name or class and go to the References menu and from there you can choose the scope of where you want to search for classes that reference that item.
Is that what you need?
I'm going to try Macker. Its style is to report references to configured classes as errors, but that's fine. It can be run from an automated build. Thanks Robert.