Java Programming: Refactoring, more reflection and something else 
I'm gonna approach two subjects, actually three - but the 3rd one is non it - in this post, they are:
- Code refactoring
- Refactoring the method on the post Java Programming: Unit Testing + security manager + reflection api
- Talking with the readers
Even if they are three subjects, this is gonna be a brief post.
I chose to talk a bit about refactoring, because I had to refactor the idea of the previous post. But before presenting it to you, I felt like introducing the concept of refactoring.
Have you ever written an application and you said "the code is so sloppy!", or even better...Have you ever written some sort of program that a few months after you looked at it and you had to take a timeout to understand what was being done? Well...Refactoring, tried to fix that situations… Refactoring code, means that you are not fixing bugs, nor adding new functionalities but rather improving the understandability of the code you develop.
This means that refactoring, tries to end with repeated code creating a method, deleting dead code, improving design and structure of the code,
without ever modifying it's external behavior. That means that refactoring is self containing.
I just felt like sharing this concept with you because talking with other people I've realized that sometimes it's still not a common practice, and believe me it can help you a lot in maintaining code!
Lately I've been refactoring some code, and it's amazing how things can sometimes be reduced. You just have to stop for a while and think, don't be a
code monkey. Otherwise you risk to start showing up on
The daily WTF.
Anyway, let's move on!
Remember the private test interface on the previous post? Well after writing it, I start thinking that it wasn't generic. So if I had 3 types of objects to test, I had to had 3 methods, one for each object, now if we are working on a large system has you can imagine this becomes unbearable. So I sit, thought for some minutes and wrote the following method:
public Object objectFactory(
Class objectClass,
Class constructorArguments[],
Object parameters[]) {
try {
Constructor constructor =
objectClass.getDeclaredConstructor(constructorArguments);
constructor.setAccessible(
true);
Object generatedObject = constructor.newInstance(parameters);
constructor.setAccessible(
false);
return generatedObject;
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
Now with this method (you might want to put it static somewhere, or on a testclass that every other test class will extend) for you to generate an object for type MyClass, that receives two Strings and you want to put it in the state of string1 with value "ok", string2 with value "also ok" you only need to write the following code:
Class classes[] = { String.class , String.Class};
Object objects[] = { "ok", "also ok" };
MyClass myClass = (MyClass) objectFactory(MyClass.class, classes, objects);
And there you go… You have the object you want! Hope it helps!
Finally the part I talk with the readers, what I want to say is that I've been noticing that I'm receiving regular and almost daily visits from readers that are from, Canada and Australia (among other places). Now I found those two places interesting because I don't know anyone in Canada nor Australia, so I would like to know how you found my blog, also if you are liking the topics (you might, otherwise you wouldn't be coming back I guess) and if you want to suggest anything.
If you don't want to register you can always use the
anonymous login, that's why I created it. It's anonymous without a password. I'm waiting for your replies.