The Source for Java Technology Collaboration
User: Password:



Ahmed Hashim's Blog

Ahmed Hashim Ahmed Hashim has been working with Java technologies since 2002, he is currently working as software engineer in Enterprise Application Integration team in Etisalat. He has experience with EJB, Hibernate, Spring, Struts, JSF & AJAX, ESB & BPEL. He is SCJP and SCWCD. Ahmed Hashim is the initiative, founder and leader of the Egyptian Java User group, he is a frequent speaker in local event in Egypt organized by Egyptian JUG and other communities. Ahmed is a Sun Java Champion since December 2007. Ahmed graduated from Faculty of Computers and Information, Computer Science Department, Cairo University, after doing MSc thesis 'Design Level Assertion and Architecture quality'.



AspectJ: Please don't use System.out.println

Posted by ahashim on July 24, 2008 at 01:39 PM | Permalink | Comments (0)

Usually if you are working in a "big" project and doing a code review; if you have a vendor who develop some application for your company; if you have a fresh graduate developer ... you will find a lot of bad-practices in the code being written! I don't like to see System.out.println() in the code .. because simply, it doesn't appears in the log files!!!

As a solution, you can use Aspects to figure out the pointcuts in the code which uses "SYSO" and the advice will be to log the message yourself.

Let's have a look into the code and how this will work:-

1- the idiot code will be like that

aop1.PNG

as you see, the method add() will print some info the default output stream which is the console. This method is called from the main method. If you are an eclipse user, you will notice that there is a small orange arrow on the left side of the code editor referring to the line of code uses "SYSO"; we will back to this arrow after while ;).

2- We will now create a new aspect to solve this problem, the BPAspect will looks like the following:-

aop3.PNG

to define a pointcut to SYSO, the pointcut definition will be

pointcut syso():call(* java.io.PrintStream.print*(..));

which means: define a pointcut named syso and get all places which call a method print,println as a members in the PrintStream class, take any argument and return any value.

then, mark the called for this method as warning in your code and display this message "Don't use SYSO"

declare warning:syso():"Don't use SYSO";

then, how to log the message? the advice will come to do this job

after():syso(){logger.info(thisJoinPoint.getArgs()[0]);}

3- What about the orange arrow? it indicates that there is an pointcut which this line matches with.

There is a panel also shows the aspects which reference the code in this class

aop2.PNG

on the other side, if you open the aspect code, the panel will show the code referenced by the aspects.

aop4.PNG

you will see also that there is a warning message in this line

aop5.PNG

4- Now, we can run the application to see how it will work
It will print the to the console

Add 1 to 2
2008-07-24 14:28:22,828 [main] INFO BPAspect - Add 1 to 2

and prints to the log file
2008-07-24 14:28:22,828 [main] INFO BPAspect - Add 1 to 2



Beside Java ... Flex

Posted by ahashim on June 10, 2008 at 01:42 PM | Permalink | Comments (5)

Few months ago I did my Hello World Flex, I used Adobe Flex Builder and founded it interesting to produce RIA. After than I tried a simple application for file download and discovered some features in Flex. Now, I came with a new project and decided to use Flex to do the job.

What is my project? It is a small and smart product catalog with search facilities and customer reviews.

Why I decided to make this site with Flex? Java is not suitable because of the hosting issue, finally I want to upload my application in my cheap host which supports PHP only. Why not PHP? because I plan add this small project to a current projects, some of them developed with PHP and other with ASP. I thought about Flex to make it easy to plug to any website

The first problem I got is the DB connectivity, there is a way to connect to DB through PHP code or Java, but I didn't like the idea; it will be the same problem again. I asked one of my friends, he is a Flex fan and he told me about a new project which add the feature of DB connection from Flex. The Action Script SQL it is a very new project and tried it yesterday .. it works well as early beta version.



July 2008
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    


Search this blog:
  

Categories
Business
Community: Java Patterns
Community: Java User Groups
J2EE
J2SE
Open Source
Programming
Web Applications
Archives

July 2008
June 2008
April 2008
February 2008
January 2008
December 2007
July 2007
March 2007
January 2007
November 2006

Recent Entries

AspectJ: Please don't use System.out.println

Beside Java ... Flex

Book Review: High performance web sites



Powered by
Movable Type 3.01D


 Feed java.net RSS Feeds