Skip to content

Commit

Permalink
Add custom initialization code option for constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Apr 22, 2017
1 parent f1e770a commit ab12a2e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Expand Up @@ -40,13 +40,15 @@ protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nod
for (PropertyMetaModel parameter : nodeMetaModel.getConstructorParameters()) {
constructor.addParameter(parameter.getTypeNameForSetter(), parameter.getName());
if (nodeMetaModel.getDeclaredPropertyMetaModels().contains(parameter)) {
body.addStatement(parseStatement(f("%s(%s);", parameter.getSetterMethodName(), parameter.getName())));
body.addStatement(f("%s(%s);", parameter.getSetterMethodName(), parameter.getName()));
} else {
superCall.append(parameter.getName());
}
}

body.getStatements().add(0, parseExplicitConstructorInvocationStmt(superCall.toString()));

body.addStatement("customInitialization();");

replaceWhenSameSignature(nodeCoid, constructor);
nodeCu.addImport(Range.class);
Expand Down
Expand Up @@ -150,6 +150,14 @@ public Node(Range range) {
this.range = range;
}

/**
* Called in every constructor for node specific code.
* It can't be written in the constructor itself because it will
* be overwritten during code generation.
*/
protected void customInitialization() {
}

/**
* This is a comment associated with this node.
*
Expand Down
Expand Up @@ -91,9 +91,10 @@ public VariableDeclarator(Range range, Type type, SimpleName name, Expression in
setType(type);
setName(name);
setInitializer(initializer);
customInitialization();
}

private void registerObserversForDerivedProperties() {
private void customInitialization() {
// We register an observer on the type property. When it is changed the MaximumCommonType is changes as well,
// because it is derived from the type of the variables it contains, for this reason we notify about the change
this.register(new AstObserverAdapter() {
Expand Down

0 comments on commit ab12a2e

Please sign in to comment.