Académique Documents
Professionnel Documents
Culture Documents
InclassactivityforaspectorientedprogrammingByAkhilaT.
1. CreateanaspectprojectnamedDemo1
2. CreateaclassnamedJavaDemo.java
3. Createafunctiontoaddtwointegersasfollows:
4. Createafunctiontoperformsquareofanumberasfollows:
5. Inthemainmethod,calltheabovetwomethodsasfollows:
6. Nowexecutethejavacode.Theoutputshouldlooklike:
7. CreateanaspectnamedAspectDemo.aj
8. Createapointcutthatprintsamessageaftertheexecutionofanypublicmethodasfollows:
Fromtheabovesnippet,youcanseethatintheexecutefunction,wehavetheaccess
specifier(inthiscasepublic)tochecktheexecutionofpublicmethod.
Thefirstasterisk(*)isthereturntypeandtheseconddenotesthenameofthemethod.
Thedotsintheparenthesisdenotethenumberofparameters.
Thus,weareprintingamessageaftertheexecutionofanypublicmethod,irrespective
ofthereturntype,nameandparametersofthemethod.
9. Afterthepointcutiscreated,executethejavacode.Theoutputshouldlooklikethefollowing:
Fromtheoutput,youcanseethattheaspectcodeisbeingcalledaftertheexecutionofadd()
methodandsquare()methods,whicharepublicmethods.Thelaststatementcorrespondsto
thecalltothemain()method
10. Now,toobservethedifference,createaprivatemethodintheJavaDemo.javafileasfollows:
11. Calltheabovefunctioninthemainmethod:
12. Observethedifferenceinoutput.
13. Changetheaccessspecifiertoprivateinthepointcutandexecutethecode.
14. Theoutputshouldlooklike:
15. CreateanewprotectedmethodtoconcatenatetwostringsinJavaDemo.java
16. CalltheabovefunctioninthemainmethodofJavaDemo.java
17. CreateapointcutinAspectDemo.ajthatprintsamessageaftertheexecutionofprotected
methodasfollows:
18. Runthecodeandverifytheresults
Wildcardswithfunctionparameters
19. CreateapointcutinAspectDemo.ajthatprintsamessagebeforetheexecutionofanymethod
withoneparameterasfollows:
20. Theoutputshouldlikefollows:
Intheabovescreenshot,thefirstlinehasthestatementExecutingamethodwithone
parameter.Thisisbecauseofthemain()methodthatisexecutedfirstandhasoneargument.
21. Thefollowingcodeprintsamessagebeforeaprotectedmethodwithtwoparametersis
executed.AddthefollowingcodetoAspectDemo.ajfile:
22. Theoutputisasfollows:
Inoutjavafile,thereisonlyoneprotectedmethodthattakestwoarguments,i.e,addStrings().
HencethemessageisdisplayedonlybeforetheexecutionofaddStrings()method.
Usingwildcardswithmethodnames
23. NowletsprintamessageaftertheexecutionofamethodthathasthewordString.Inour
code,weonlyhaveonefunctionaddStrings().Henceamessageisdisplayedaftertheexecution
oftheaddStrings()Method.AddthefollowingpointcutcodetotheAspectDemo.ajfile
Noticethe*String*.ThisstatementchecksforthemethodsthathasthewordStringatany
positionintheprotectedmethodswithtwoparameters.
24. Theoutputisasfollows:
Note:Commentedpreviouspointcutsforreadabilitypurpose
25. Toprintamessageaftertheexecutionofmethodsthatstartswiththewordaddandhastwo
arguments,irrespectiveofthereturntypeandaccessspecifierofthemethod,dothefollowing:
26. Sincetherearetwomethodsthatstartwithwordaddandhastwoparameters,theoutput
lookslikefollows: