Inside Paulo Abrantes' head
[ start | index | login or register ]
start > 2006-07-13 > 1

Java Programming: Refactoring, more reflection and something else

Created by pabrantes. Last edited by pabrantes, 2 years and 130 days ago. Viewed 974 times. #3
[diff] [history] [edit] [rdf]
labels
attachments

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.

Please login to www.pabrantes.net.
Who am I?
paulo-roca2My name is Paulo Abrantes AKA pabrantes and I'm a software developer. I'm currently employed at >>CIIST working as a Java developer in >>FenixEDU.

This blog is mostly about Java programming, domain driven design and snipsnap bliki developing. Everything written in this blog is my personal opinion and it may not reflect the opinions of my employer and co-workers.


Blog subscription
subscribe by rss subscribe by email

Links
>> Home
>> Paulo's Profile
>> Post History
>> Add to Technorati Favorites
>> Paulo's Photo Gallery
>> WishList
>> Posting without Login

Search Blog
Fellow Bloggers

Recent Posts

Java Programming: Bytecode Injection
Intermission: Sorry For Downtime
Software Developing: Studying The Bliki Domain Model
SnipSnap Developing: Trying to settle a roadmap
System Administration: Load Balancing with Apache
Blogging: Two years have passed
Software Developing: The SnipSnap Saga
Java Programming: Getting your code spicy with Groovy
Software Developing: Fluent Interfaces
Software Developing: Implementing a ShoutBox on SnipsSnip
Software Developing: SnipSnap, SnipIt and SnipSnip
Java Programming: Proxies and Access Control
Java Programming: Proxies and References
Java Programming: References' Package
YALM: Yet Another Layout Modification

For older posts, please refer to post-history for a complete Post History

Logged in Users: (0)
… and a Guest.
This is a modified version of snipsnap.org created by >>Paulo Abrantes