If you have a generic model and you want to supply it with custom code you have a problem to solve. Here is the solution for this project.

You have a generic model - in the case of this project a generated abstract syntax tree and you have a custom input model as an XML structure. What you want is to add custom methods to the AST based on your data.
The way to do this has two steps:

  1. Adding methods to the AST from a reference implementation
  2. Customising these methods.

How do you add a method to the AST? That is quite simple. Since you have a reference implementation of the method you can parse the abstract syntax tree for this method. This tree can be added as a subtree in the AST.
The customizing part is the tricky part. The methods (or at least some of them) you want to add needs customizing. Imagine you want to print out your hello world program instead of "Hello, world!", "Hello, !" whereas comes from the input file:

Before you finally insert the parsed tree you have to swap the part with 'Arthur'. The replacement of variables or part thereof is simple: instead of the you place a place holder method, that you define in the same class.
This poses another problem you cannot 'copy' every method from your reference implementation to your AST. You have to mark the fields and methods. This is done with annotations. Therefore the implementation class of hello world can look like this:

class HelloWorldImpl{

@ReferenceImplementation
public void printHelloWorld{
System.out.println("Hello, "+getName()+"!");
}

@ValueProvider
private String getName(){
...
}

This is done by stepping through the parsed method declaration and for each method (defined on the same class) call found, that calls a method annoted with ValueProvider, replaces the method invocation with the result of the method invocation.
I wonder if there is a case where the part you want to replace is not a part of a variable or a simple type. Time will tell.
For fully compliance between the reference implementation and the resulting class the inheritance structur must be maintained.

Copyright © 2017 Sahits's Homepage. All Rights Reserved.
Joomla! is Free Software released under the GNU General Public License.
Joomla Design By Sahits