Tuesday, January 28, 2014

Custom Rules for ADF BC Java Files

There is a very good explanation of how to write custom rules using Jdev Extensions @ Write your own Audit Rule Extension in JDeveloper 12C by Richard Olrichs.

We will be discussing how we can write rules for ADF BC framework classes like EntityImpl and ViewObjectImpl.

The way how we identify that we are in current file for example, to know wether we are in Entity defination XML file we need below piece of code. This tells the Custom Analyzer that for all the files other than entity defination files, the Rules should be set false. This means that for all files other than entity files, rules for Entity Objects should be disabled i.e  Framework should not execute further drill down methods of the Analyzer which are exit(AuditContext, Drill Down Elements) in most of the cases.




Now this works well for XML Files. But what if we need to do this check for Java files.

Lets take an example:
I need to create a rule which checks, that there is no reference to Application Module from Entity Class. 
This rule needs to check in the code of EOImpl class that is there any call to ApplicationModule.

The point worth noting here is, when we check for XML file, we use Document object in enter method as second parameter. This is because Document is the root node of any XML document and it will be executed by the framework once for each XML document. Since this is called only once, its a good place to write the logic to enable or disable further calls to other methods.

But for the source files, Document won't work. If we use the same enter method with same signature, it will not be called by the framework for java source file.

So the root node for source file is oracle.javatools.parser.java.v2.model.SourceFile. This needs to be used as second parameter of enter method , which gives us a place to write logic for enable and disable of rules.

Now again there is one more catch. How will we identify that this class is EntityImpl class. Because we need to disable rule for all classes other than EntityObjectImpl classes. Below is the code which does this.


This checks whether  the class's super class is EntityImpl. This is because every Entity Object Impl class extends EntityImpl.

Now we are good at checking part. Now lets write a logic for checking if code has reference to ApplicationModule and if yes, send warnings as violations.

Below is the code which checks for the same :



The logic inside the IF Condition is very simple. If the text contains ApplicationModule, report violation of rule. But the catch here is which parameter should be used as second parameter of exit method which framework will call again and again for a construct.

SourceBlock (oracle.javatools.parser.java.v2.model.SourceBlock) returns text of complete block within the braces({}) of the java code. This becomes little tricky to find out which type to be used as second parameter in exit method to suite your need.

So below is the link to all the Types inside oracle.javatools.parser.java.v2.model package. This can be used as reference when working with defining custom rules for source files.

http://docs.oracle.com/cd/E26098_01/apirefs.1112/e17493/oracle/javatools/parser/java/v2/model/package-summary.html

That's it !! 
Thanks



1 comment:

  1. Custom software development specialists explore exactly what it is a company does - what the necessities of the company are on a day to day basis and what they need from their IT system in order to operate to their optimum. In order to perform efficient functionality, a company would ideally need its software to do exactly what they want it to.

    ReplyDelete