It's been a slow month for blogging, due to the release of a new World of Warcraft expansion, crazy year end software releases at work and the general retardation of the human collective consciousness that happens this time of year.
With all of that I did however have some fun writing the following bit of code. We have a "generic" getter and setter test utility used to ensure a couple extra %'s of code coverage with minimal effort. That utility did however requires that you call it for every class you want to test, being lazy I wanted to just give it a class from a specific package and have that check all the classes.
I initially thought there would have to be just a simple way to do it with the Java reflection API. Unfortunately I didn't find any, it seems a "Package" does not keep track of it's classes, so after a bit of digging on the net and feverish typing (ignoring the actual project I am currently working on for a little) here is what I came up with. The tip is actually the just methods from the java.lang.Class object: "getProtectionDomain().getCodeSource().getLocation().toURI()", giving you the base to work from and I found that somewhere on Stackoverflow.
There are 2 public methods: findAllClassesInSamePackage and findAllInstantiableClassesInSamePackage. (For my purposes of code coverage I just needed the instantiable classes.)
Usage:
PackageClassInformationFinder:
Subscribe to:
Post Comments (Atom)
Popular Posts
-
I have recently been slacking on content on my blog, between long stressful hours at work and to the wonderful toy that is an iPhone, I have...
-
I make no claim to be a "computer scientist" or a software "engineer", those titles alone can spark some debate, I regar...
-
I saw an article (well more of a rant) the other day, by Rob Williams Brain Drain in enterprise Dev . I have to say, I do agree with some o...
-
This series of posts will be about me getting to grips with JBoss Drools . The reasoning behind it is: SAP bought out my company's curre...
-
Update: Check out my updated re-certification on the new 2019 exam... here Let me start by saying, for this certification I studied and...
I have used a custom doclet for a similar task. I think doclet is cleaner way to do this.
ReplyDeleteI have never really used doclets. I'll have a look if I have some time in the next couple days, but I am quite sure there are a couple of ways of doing this. :)
ReplyDeleteWell you describe it, you are absolutely right.
ReplyDeleteĐặt vé máy bay tại Aivivu, tham khảo
ReplyDeleteVe may bay di My
máy bay từ mỹ về việt nam
chuyến bay từ đức về hà nội hôm nay
đặt vé máy bay từ nga về việt nam
vé máy bay từ anh về việt nam
mua vé máy bay giá rẻ từ pháp về việt nam
Chi phi cho chuyen gia nuoc ngoai
It is the best deal for those Who wants to get classes. They can get all classes in same package. Likely, I will provide them a package of cabinet refinishing manassas. They can changed their cabinets with new designs and according to his desire.
ReplyDelete