Sunday, February 22, 2015

I Adore Jetbrains & Intellij OpenApi


Being a Java developer feels kinda common now days, you write plenty of code, you
try to incorporate new cool technologies & frameworks, new APIs & Legacy code.
You use many tools, build servers, JEE servers, Database tools, logging tools and the list just goes
on and on.

Kinda common, like I said. but you know what's not that common, enriching your IDE so
you can add some benefits to your daily and mundane tasks that you & your colleagues do regularly.

First, If you aren't an Intellij user - move to Intellij!

If you're an Eclipse user and you must work with Eclipse, since it has many plugins that you really
can't live without, what can I tell ya...your screwed :-)
If you use NetBeans by any chance, (gush, I only worked with it for a short while, and it was only for a specific task which required the use of NetBean's swing builder, which I can admit, has no equals, but who cares about Swing these days?)

You aren't working with 1 of those three? yeah I know there are more,

Some links about the IDEs out there -
eclipse-vs-netbeans-vs-jdeveloper-vs-intellij-idea
Comparison_of_integrated_development_environments

I didn't know there are so many IDEs out there, I wonder if all are still maintained.
Let's make it easy for us all, move to Intellij!

Working with Intellij can be really fun & really productive if you know what you're doing.
  • The only thing I find Intellij a bit excessive, is the memory consumption, which is pretty preposterous. With big projects like 20+ modules with around 100 classes in each, with many plugins enabled, I get my Intellij instance reach up as 3GB (that's with defining a -Xmx=2g)
So have plenty of memory, fast SDD and a powerful computer in general and you're good to go.

Next step comes the need for a custom plugin.

And this blog post, isn't about how to develop a plugin, this blog post is only about praising JetBrains & more specifically their OpenApi.

The OpenApi is JetBrains source code for their Intellij community edition. 
I can distinctly say, that the source code is not 100% synchronized to their binary forms, but its in the high 90's and thats good enough.

I've been writing plugins & Inspections for about 5 years now, and maybe apart from Spring,
there's no other one location of code that holds so much, and written so eloquently.

Writing a plugin has a pretty steep learning curve, that's for sure. but you'll be grateful if you use your spare time to write some productive plugin that you must develop (because no body else did or will)
The Intellij plugins repo is pretty massive, and I can tell you no one knows all the useful plugins out there, and sometimes it's easier to write one then to find a plugin that does kinda what you like.

Intellij was written more then 10 years ago, and the JetBrains people wrote all the Utils you may ever want and more.
It's amazes me every time, I start writing some extension for my plugin, and after a few years of dwelling in the OpenApi, I always start with trying to find if Intellij has some Utils or some Interface/AbtractClass to be incorporated to my needs.
To my amazement they mostly do, and by most counts, you don't have to write a lot of code.

That's the silver lining, the cloudy day is you may not understand how to use their Util, interface/AbtractClass, do you need to register it for it to work, some how?, some where?  

Well, yeah, it can be a bit annoying, and the documentation is a bitch, (AKA non existing, well, that's not totally true, since they added some posts on plugins development in the last few years, but it's still really lacking, and it's mostly tutorials)

But, there are plenty of open source plugins by JetBrains , and many of the plugins in the repo are open source as well & it's a great place to learn new tricks and Utils you may never know about.

The developers forum is not bad, and you get answered to any valid question you may have. They answer pretty fast, so don't hesitate to post a question. (just look for a similar question first in the forum, if you ask a duplicated question, they take off points, I'm not kidding).

Using Intellij it's easy to search any class in the OpenApi, by search *Util* you'll get many results, try the same with *Manager* 
A few names of Utils you want to look for -

AnnotationUtil, PsiTreeUtil, PsiManager - to manipulate psi elements.
FileDocumentManager & PsiDocumentManager,FileManager - Documents, PsiFile
& VirtualFile manipulation.
ApplicationManager, ProjectManager,ModuleManager - Application/Project/Modules access
and control.

These are some base Util class they you'll find really useful and probably will
use a few of them in any plugin development.


That's all, I just had to get it off my system, thank you :-)







Sunday, November 10, 2013

NumberToEnumConverter extending Spring's factory converters

Custom Spring converters - taking it beyond the API

I use Spring regularly & quite a lot for years now, and even though I hold the highest respect for the Spring people, there are still a few needed extensions you find yourself required to write by yourself.
The Spring Converters mechanism is an awesome, huge part of Spring's core and base classes and used by their beans definitions, creation and property injections.
A nice feature which I used a long while ago, and quite some from then on is the enum conversion,
which only supports the StringToEnum conversion. (@see StringToEnumConverterFactory)
I won't get into why the Spring guys choose to make the only converter to Enum available for us simple minded, as the Enum itself is a creature whom deserves and gets a lot of respect by itself, and this post deals with the conversion API and not the subtleties of the java language.
An Extension RequirementHaven't you ever found yourself needed to use numbers to enum conversion?
(holding integers for index purposes to your enum to be used with switch-case statements...can't wait for Java 8 already to start refactoring all that redundant code :-) )
If you use spring in your code, you may find the StringToEnumConverter pretty handy, maybe you've used it without even knowing it.
Well using the StringToEnum is a given, and a nice converter infrastructure is always required in any medium to large applications. So this is the way to use Spring's power to make it simple and using Java's help (Java helping spring...weird but this time it was the case).

Firstly the convention for the ConverterFactory:
@Component(value = "NumberToEnumConverterFactory")
public class NumberToEnumConverterFactory implements ConverterFactory{
...
final Method method = number.getClass().getComponentType().getMethod("getValue");
final Object enumValue = ReflectionUtils.invokeMethod(method, instance);
...
}
see full code

Making a generic factory to be used whenever and where ever you need it.
An important thing here to do is to setup some sort of convention to your Enum classes.
I saw people use @annotations, or pass some kind of a String/Integer or even another enum to determine the conversion methods for the reflection's invocation.
I decided to make a simple convention, saying that my enum will have a value field, with a getter method to it, hence the "getValue" method invocation. Make a choice to your liking, keeping in mind the KISS factor. have-course you'll have to make changes to my ConverterFactory if you do want to take a different approach.

Secondly the PropertyEditor:
@Component
public class PriorityEnumPropertyTypeEditor extends PropertyEditorSupport
implements ApplicationContextAware

Making the above class as a @Component will assist you to register into Spring's core.
And during the Beans creation Spring will use the PropertyEditorSupport class, trying to find the proper Converter class to convert the type when used as a property.

Lastly the application context xml:
Don't forget to register the Custom PropertyEditor, this can be done with Spring's CustomEditorConfigurer, which is simply accessed in one of your application-context XMLs.

see the Context xml

Using This mechanism is a simple way to achieve nice and simple conversion while injecting your integer property values as Enums into your beans. A Maven project source code example can be downloaded from my Github, see you next time :-)

full source code

Tuesday, June 11, 2013

Intellij with 74 bugfix - 
that's great, but what's the deal with the XML/XSD memory problem???

Saturday, December 31, 2011

Imporving your Windows TCP congestion rate

So it's been a while since I lost posted, most of the time now spending at the
new job, which revolves around communication.
Being a curious guy at nature I find myself looking for information regarding the Windows TCP/IP stack
and IPv6 in Windows and in general.
Whilst browsing Microsoft's data center I encountered a nice peace of information regarding the Windows
congestion control rate and the way it uses some features.
The following command should use a nice feature if it exists in the Network your connected to increase congestion control rate performance -
netsh interface tcp set global ecncapability=enabled


link to the article:
http://technet.microsoft.com/en-au/library/bb878127.aspx

Saturday, December 18, 2010

IntelliJ Plugin Development, [Personal Log]

 Log entry 1.0.0

I've been working with IntelliJ for a few years now, actually started using it back
in college, I think I was version 4.5.
But really working with IntelliJ only comes when you work more then a few hours a day with it,
so after years of close encountering all of IntelliJ little perks and corks I came across a requirement in my work place for a plugin.

The scenario :
We created an annotation that posses a value of an id, let's call it "Id"
This Id's value corresponds to a Xml element that has an attribute named Id with the same value as the annotation's one.
(The XML file will probably have multiple Id elements, in them is some content which isn't important for the plugin's purpose)

let's illustrate it for clearance :

The XML file :
<queries>

   <query id="exampleId">
                SELECT * FROM CLIENTS
   query>

   <query id="anotherExampleId">
                SELECT * FROM CLIENTS where CLIENT_NAME=@CLIENT_NAME
   query>

</queries>

The CLASS file :
@RefId(Id = "exampleId")
public interface MockForTesting {
}

The language behind the xml & annotation is not important for the plugin development, so will not be further elaborated.

The requirement was that on mouse-click or keyboard-shortcut on the Id, on either the class
file or xml file, the cursor will jump to the corresponding Id.

So I started digging a bit in jetbrains' plugin repository and try to find a plugin that does exactly that or at least close to it.
well I found the JumpToUsage plugin which is pretty cool one, all sources are included &
it pretty straight-forward, so understanding the code wasn't hard, the thing is the IntelliJ openApi
is filled with Factories & Utilities that can do practically everything, but! how the hell you suppose to find the right Factory or utility class the meets your needs?

Well it's hard, that's the answer :)

A few tips for starting a plugin development:
  1. Try to find an open source plugin that provides a similar activity for your needs, you may have to change a lot of the code, but it will give a great start getting familiar with the provided tools.
  2. Download Jetbrains OpenApi sources, and attach them to your project, you soon find out that most of the classes are not placed in the proper packages.
    So there's a lot of work to do here if you want all the classes arranged properly, but no need, just move the classes to their right packages when you need to see their sources.
  3. After understanding the basics of the plugin development from an example project,
    create a new plugin project, construct a small activity or component and debug it,
    the first time you do this - IntelliJ will open a new instance (like after a fresh installation)
    so configure and let it run your plugin, if you have a Breakpoint placed in a proper place, you get to see if it all worked, and you're set for starting some real coding.

End of Log entry 1.0.0

Tuesday, November 16, 2010

Knowing the RIGHT languages, my objective POV

There are so many programming languages out there, one in his thirties should ask him/her self a few questions when working in this hectic industry.
Am I working with the right language, am I getting myself familiar with the languages that will take me to the place that I want to eventually be?

Well, I'm in that place in life, as a Java programmer, and former .NET programmer, I want to know that the language that I learn and specialize more & more everyday will get me the the place that I so desire to be, which I actually don't know what it is...yet!

One field in the programming realm that always excited me, is the Gaming world. I just came across another nice Open API for Game developing, Unity3D is the name, which provides
nice interface and an API that works under (or above) C#, Javascript or BOO.

So my current work place (that I develop solely on Java) doesn't help me a lot there.
My knowledge in .NET and specifically in C# is pretty good, but it's not near my Java knowledge which accumulated during the past 3 years, even though I had a few side projects in C# during the last years, they were on my own, so the requirements and guidance was lacking, and it shows :)

Is the future of us programmers to dwell in the Blogs and forums for an answer ?
well, I guess for me, for now, it is...cause I live in a country that the possibilities in game developing are so small, they practically don't exist.

And the work in a field that is not so exciting, can be a downer, maybe not at the beginning but someday, sometimes.
For now, just continue enriching your knowledge & expertise, maybe an answer will find you :)

Saturday, October 23, 2010

Party of Five - A Tale Of Java Servers

PROLOGUE

I've been working with a few JBoss servers which, or should I use the word Who instead..well who are responsible for all Distribution processes in my Company, Distribution means - sending all wanted material from the System to the out side world.

The outside world means - Printers,Fax Server, Email (Mail Server) and any kinda of a Network repository storage place you can imagine.
The servers also take part in building & constructing the data it suppose to distribute, like fetching data from the DataBase, and invoking all kinds of reports generating tools that construct documents as a result. all the data is finally converted to PDFs and then makes the it to the final stop - the Distribution final destination (Printer, Fax Server etc').

Well it's all sound really interesting doesn't ? well actually it is, and the thing that can be really interesting, is to see this family of servers servicing a roughly 1000 clients in real-time, with many of different requests and factors that influence the construction phase of the data in a myriad of ways. not to forget the fact that running in multiple threading environment that it is massive as this can be really fascinating to observe and learn from.

So after a few years of watching and encountering many scenarios and solving many problems I thought, why not put it on paper...or the Internet, which is the same thing our days, for the benefit of others, well if nobody will read this, no matter, at least I had fun writing it.

Now the Tale of the Family of servers begins, I'll start the story & end this prologue introducing the family -
  • There's the 2 Big Brothers who are responsible mostly on producing a big piece of the documents and placing them in Storage. we call them the "Storage brothers".
  • There's Printery who constructs data (PDFs documents) and zip them for delivery to an outside printery that will print & deliver the documents to the clients.
  • The Printing Server handles requests sent to local (In house) Printers, there are about 120 of them, most of them HP, some Lexmark and Xenon.
  • Last but not least, there's the Non-Printing Server, with a respect to his name handles all the other stuff, like Faxes,Emailing,Sms sending and more.
So this is the family, the party of five