[Jastadd] Beaver + fullCopy()

From: Jesper Mattsson <jesper.mattsson_at_modelon.com>
Date: Wed, 22 Aug 2012 15:16:33 +0000


I have found a problem when using the beaver flag to JastAdd. The beaver.Symbol class has a final field value, that in the default constructor is set to this. When using copy() or fullCopy(), then the node is copied using clone(), and thus the value field is set to a reference to the original node. This leads to strange memory leaks, where old nodes are retained because of this field.

We have had huge problems in Eclipse with memory leaks, and changing ASTNode.clone() to set value to this solves those problems. Doing this requires using reflection (since value is final). Here is one way to do that:

  public ASTNode<T> clone() throws CloneNotSupportedException {
    ASTNode node = (ASTNode)super.clone();
    try {
           Field f = beaver.Symbol.class.getField("value");
           new ClearFinalFieldAction().perform(f, node);
    } catch (Exception e) {
    return node;

  private static class ClearFinalFieldAction implements PrivilegedExceptionAction {
           private Field field;

           public void perform(Field f, Object o) throws Exception {
                      field = f;
                      f.set(o, o);

           public Object run() throws Exception {
                      return null;


Software Developer & IT Administrator

Phone direct: +46 73 324 5909
Email: jesper.mattsson_at_modelon.com<mailto:jesper.mattsson_at_modelon.com>

[Description: Description: Description: Modelon_2011_Gradient_RGB_400]
Modelon AB
Ideon Science Park
SE-223 70 Lund, Sweden

Phone: +46 46 286 2200
Fax: +46 46 286 2201

Web: http://www.modelon.com<http://www.modelon.com/>

This email and any attachments are intended solely for the use of the individual or entity to whom it is addressed and may be confidential and/or privileged. If you are not one of the named recipients or have received this email in error, (i) you should not read, disclose, or copy it, (ii) please notify sender of your receipt by reply email and delete this email and all attachments, (iii) Modelon does not accept or assume any liability or responsibility for any use of or reliance on this email.

(image/png attachment: image001.png)

Received on Wed Aug 22 2012 - 17:17:02 CEST

This archive was generated by hypermail 2.3.0 : Wed Apr 16 2014 - 17:19:06 CEST