Skip to content

The auxiliary layer to use AST library

Oren Afek edited this page May 31, 2017 · 6 revisions

#iz.java This is part of a huge layer built above the ASTNode library to make it's use more fluent, easy and intuitive. the class contains a large number of mostly simple getters which allows the user to investigate easily the kind of block the plugin is currently checking. For example, if you're looking at a variable deceleration statement and you want to know whether this variable is set to be final, you can use:

static boolean final¢(final VariableDeclarationStatement ¢) {
    return ¢ != null && (Modifier.FINAL & ¢.getModifiers()) != 0;
  }

instead of writing the check yourself. The library offers you a lot of very useful services, helping you investigating all kinds of blocks, if's, loops, infix exp. , and any other kind of block you can find in a java code, some of them, we already saw and experienced during the last assignment.

Using it in InteliJ: the class is a long but its a simple java code that depends on itself (calling inner methods) and constants of ASTNode such as STATIC,PRIVATE,IF_STATEMENT etc, this means that if we create our own AST library or we use a different library then the one used in Eclipse, it will need adjustments, but only simple ones such as changing names of constants.

#az.java Basically, this class is supposed to help you with any kind of cast you are trying to make. The first part of the class is handling numbers (positive and negative). It consists many methods that accept an expression as a parameter, and has the ability to determine whether its a number, and what type, and if so, the methods return this number in the right type. The second part of this class is DownCasts. There is a large number of methods that takes a parameter such as Expression, ASTNode etc, and check if a downcast to a desired type is available. For example, I guess it is very common to check whether an ASTNode is a MethodDecleration. For this, you can use:

public static MethodDeclaration methodDeclaration(final ASTNode $) {
   return $ == null ? null : eval(() -> ((MethodDeclaration) $)).when($ instanceof MethodDeclaration);
 }

Using this class we can easily determine the type of node we're currently investigate and it simplifies the code.

Using it in InteliJ: same as iz.java, the class depends on itself and the ASTNode library, it will need the small adjustments similar to those I described above.

#step.java This class is more about extracting parts of the code with a purpose or a title, I'll try to explain myself. Using this class you can extract conditions from loops and if statements, you can extract an operand from an infix Expression or any other similar thing. It also gives you the option to get Lists of field's names, or method parameter's names. The difference of this class than the other 2 already described above is, that this class can hep you get inner parts of the code, as long as you know the specific type of code you are currently working on.For example, you are able to get a condition out of the for loop, but you have to use a ForStatement type to do that:

public static Expression condition(final ForStatement ¢) {
    return ¢.getExpression();
  }

You can also get the loop body, expression and any other part of a loop structure.

Using it in InteliJ: same as the others, the class depends on itself and the ASTNode library, it will need the small adjustments similar to those I described above.


We can see that the ast library created by @yossigil above the Eclipse ASTNode Library is separated into 3 packages:

  1. Safety
  2. Navigate
  3. Factory

az.java and iz.java belong to the safety package. As described above, their services allows you to make casts, check for modifiers, handle numbers and other attributes that can be associated with safety part of analyzing code. It makes sure you're not using nulls and makes the use of the nodes more safe and of course comfortable.

step.java as expected belongs to the Navigate package. Together with many other .java files such as, wizzard.java, have.java, haz.java etc., this package is responsible for navigating through the code services. It allows you to get ant information about the code, conditions, names, structures, types, methods, classes, types of expressions and more. Using the package you can analyse dipper parts of the code and navigate through every part of it, even the smallest parts.

The factory is a very important package which includes a couple of .java files that gives us services that makes the spartanizer's code changing abilities simpler to work on. For example the main class is subject.java gives us the ability to create new Nodes from existing ones, doing it by pairing expressions together, pairing statements together, adding operators to existing expressions, etc. It can create different kinds of statements out of existing expressions. In order to make things a little bit clearer I'll give an example. Say, you have this line on code: a=a+b; After extracting 'a' and 'b' as Nodes, subject.java class will help you making a new node, 'a+=b' by two methods:

subject.pair(e("a"), e("b")).to(Assignment.Operator.ASSIGN)

Factory of course has other classes such as make that allows you to make different kinds of nodes, such as creating an if without else, creating a null literal node, a string literal etc.

To sum up, this Layer is very easy and comfortable to use, it makes the usage of ASTNode much simpler faster and easier. It will be a good idea to adjust it to the new plugin, and in my opinion its mostly an easy and technical work, as long as we have the ASTNode library, or a similar one for InteliJ.

By @amirsagiv83.

Clone this wiki locally