001    /*
002     * The JastAdd Extensible Java Compiler (http://jastadd.org) is covered
003     * by the modified BSD License. You should have received a copy of the
004     * modified BSD license with this compiler.
005     * 
006     * Copyright (c) 2005-2008, Torbjorn Ekman
007     * All rights reserved.
008     */
009    
010    aspect EnclosingMethodAttribute {
011      // 4.8.6
012      refine GenericsCodegen eq TypeDecl.attributes() {
013        Collection c = refined();
014        if(isLocalClass() || isAnonymous()) {
015          c.add(new EnclosingMethod(constantPool(), this));
016        }
017        return c;
018      }
019    
020      class EnclosingMethod extends Attribute {
021        public EnclosingMethod(ConstantPool cp, TypeDecl typeDecl) {
022          super(cp, "EnclosingMethod");
023          u2(cp.addClass(typeDecl.enclosingType().constantPoolName()));
024          BodyDecl b = typeDecl.enclosingBodyDecl();
025          if(b instanceof MethodDecl) {
026            MethodDecl m = (MethodDecl)b;
027            u2(cp.addNameAndType(m.name(), m.descName()));
028          }
029          else if(b instanceof ConstructorDecl) {
030            ConstructorDecl m = (ConstructorDecl)b;
031            u2(cp.addNameAndType(m.name(), m.descName()));
032          }
033          else {
034            u2(0);
035          }
036        }
037      }
038    }