⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 innerclasses.jrag

📁 JDK1.4编译器后端
💻 JRAG
📖 第 1 页 / 共 2 页
字号:
/* * The JastAdd Extensible Java Compiler (http://jastadd.org) is covered * by the modified BSD License. You should have received a copy of the * modified BSD license with this compiler. *  * Copyright (c) 2005-2008, Torbjorn Ekman * All rights reserved. */aspect InnerClasses {  // no attribute since needed in phases when the AST has been modified  public boolean TypeDecl.hasField(String name) {    if(!memberFields(name).isEmpty())      return true;    for(int i = 0; i < getNumBodyDecl(); i++) {      if(getBodyDecl(i) instanceof FieldDeclaration) {        FieldDeclaration decl = (FieldDeclaration)getBodyDecl(i);        if(decl.name().equals(name))          return true;      }    }    return false;  }  private TypeDecl VarAccess.fieldQualifierType() {    if(hasPrevExpr())      return prevExpr().type();    TypeDecl typeDecl = hostType();    while(typeDecl != null && !typeDecl.hasField(name()))      typeDecl = typeDecl.enclosingType();    if(typeDecl != null)      return typeDecl;    return decl().hostType();  }  public boolean TypeDecl.hasMethod(String id) {    if(!memberMethods(id).isEmpty()) return true;    for(int i = 0; i < getNumBodyDecl(); i++) {      if(getBodyDecl(i) instanceof MethodDecl) {        MethodDecl decl = (MethodDecl)getBodyDecl(i);        if(decl.name().equals(id))          return true;      }    }    return false;  }  private TypeDecl MethodAccess.methodQualifierType() {    if(hasPrevExpr())      return prevExpr().type();    TypeDecl typeDecl = hostType();    while(typeDecl != null && !typeDecl.hasMethod(name()))      typeDecl = typeDecl.enclosingType();    if(typeDecl != null)      return typeDecl;    return decl().hostType();  }  // Helpers for arrays  inh TypeDecl ArrayInit.expectedType();  eq Program.getCompilationUnit(int i).expectedType() = null;  eq ArrayCreationExpr.getArrayInit().expectedType() = type().componentType();  eq FieldDeclaration.getInit().expectedType() = type().componentType();  eq VariableDeclaration.getInit().expectedType() = type().componentType();  eq VariableDecl.getInit().expectedType() = null;  eq ArrayInit.getInit().expectedType() = expectedType().componentType();    syn lazy int ArrayCreationExpr.numArrays() {    int i = type().dimension();    Access a = getTypeAccess();    while(a instanceof ArrayTypeAccess && !(a instanceof ArrayTypeWithSizeAccess)) {      i--;      a = ((ArrayTypeAccess)a).getAccess();    }    return i;  }  syn TypeDecl TypeDecl.stringPromotion() = this;  eq ReferenceType.stringPromotion() = typeObject();  eq NullType.stringPromotion() = typeObject();  eq ByteType.stringPromotion() = typeInt();  eq ShortType.stringPromotion() = typeInt();  syn boolean ASTNode.isStringAdd() = false;  eq AddExpr.isStringAdd() = type().isString() && !isConstant();    syn boolean AddExpr.firstStringAddPart() = type().isString() && !getLeftOperand().isStringAdd();  syn boolean AddExpr.lastStringAddPart() = !getParent().isStringAdd();  syn MethodDecl TypeDecl.methodWithArgs(String name, TypeDecl[] args) {    for(Iterator iter = memberMethods(name).iterator(); iter.hasNext(); ) {      MethodDecl m = (MethodDecl)iter.next();      if(m.getNumParameter() == args.length) {        for(int i = 0; i < args.length; i++)          if(m.getParameter(i).type() == args[i])            return m;      }    }    return null;  }  protected TypeDecl Access.superConstructorQualifier(TypeDecl targetEnclosingType) {    TypeDecl enclosing = hostType();    while(!enclosing.instanceOf(targetEnclosingType))      enclosing = enclosing.enclosingType();    return enclosing;  }  public TypeDecl MethodAccess.superAccessorTarget() {    TypeDecl targetDecl = prevExpr().type();    TypeDecl enclosing = hostType();    do {      enclosing = enclosing.enclosingType();    } while (!enclosing.instanceOf(targetDecl));    return enclosing;  }  // The set of TypeDecls that has this TypeDecl as their directly enclosing TypeDecl.  // I.e., NestedTypes, InnerTypes, AnonymousClasses, LocalClasses.  private Collection TypeDecl.nestedTypes;  public Collection TypeDecl.nestedTypes() {    return nestedTypes != null ? nestedTypes : new HashSet();  }  public void TypeDecl.addNestedType(TypeDecl typeDecl) {    if(nestedTypes == null) nestedTypes = new HashSet();    if(typeDecl != this)      nestedTypes.add(typeDecl);  }  // The set of nested TypeDecls that are accessed in this TypeDecl  private Collection TypeDecl.usedNestedTypes;  public Collection TypeDecl.usedNestedTypes() {    return usedNestedTypes != null ? usedNestedTypes : new HashSet();  }  public void TypeDecl.addUsedNestedType(TypeDecl typeDecl) {    if(usedNestedTypes == null) usedNestedTypes = new HashSet();    usedNestedTypes.add(typeDecl);  }  // collect the set of variables used in the enclosing class(es)  syn lazy Collection TypeDecl.enclosingVariables() {    HashSet set = new HashSet();    for(TypeDecl e = this; e != null; e = e.enclosingType())      if(e.isLocalClass() || e.isAnonymous())        collectEnclosingVariables(set, e.enclosingType());    if(isClassDecl()) {      ClassDecl classDecl = (ClassDecl)this;      if(classDecl.isNestedType() && classDecl.hasSuperclass())        set.addAll(classDecl.superclass().enclosingVariables());    }    return set;  }  public void ASTNode.collectEnclosingVariables(HashSet set, TypeDecl typeDecl) {    for(int i = 0; i < getNumChild(); i++)      getChild(i).collectEnclosingVariables(set, typeDecl);  }  public void VarAccess.collectEnclosingVariables(HashSet set, TypeDecl typeDecl) {    Variable v = decl();    if(!v.isInstanceVariable() && !v.isClassVariable() && v.hostType() == typeDecl)      set.add(v);    super.collectEnclosingVariables(set, typeDecl);  }  public int TypeDecl.accessorCounter = 0;  private HashMap TypeDecl.accessorMap = null;  public ASTNode TypeDecl.getAccessor(ASTNode source, String name) {    ArrayList key = new ArrayList(2);    key.add(source);    key.add(name);    if(accessorMap == null || !accessorMap.containsKey(key)) return null;    return (ASTNode)accessorMap.get(key);  }  public void TypeDecl.addAccessor(ASTNode source, String name, ASTNode accessor) {    ArrayList key = new ArrayList(2);    key.add(source);    key.add(name);    if(accessorMap == null) accessorMap = new HashMap();    accessorMap.put(key, accessor);  }  public ASTNode TypeDecl.getAccessorSource(ASTNode accessor) {    Iterator i = accessorMap.entrySet().iterator();    while (i.hasNext()) {      Map.Entry entry = (Map.Entry) i.next();      if (entry.getValue() == accessor)        return (ASTNode) ((ArrayList) entry.getKey()).get(0);    }    return null;  }  public MethodDecl MethodDecl.createAccessor(TypeDecl methodQualifier) {    MethodDecl m = (MethodDecl)methodQualifier.getAccessor(this, "method");    if(m != null) return m;    int accessorIndex = methodQualifier.accessorCounter++;    // add synthetic flag to modifiers    Modifiers modifiers = new Modifiers(new List());    if(getModifiers().isStatic())      modifiers.addModifier(new Modifier("static"));    modifiers.addModifier(new Modifier("synthetic"));    modifiers.addModifier(new Modifier("public"));    // build accessor declaration    m = new MethodDecl(      modifiers,      type().createQualifiedAccess(),      name() + "$access$" + accessorIndex,      (List)getParameterList().fullCopy(),      (List)getExceptionList().fullCopy(),      new Opt(        new Block(          new List().add(            createAccessorStmt()          )        )      )    );    m = methodQualifier.addMemberMethod(m);    methodQualifier.addAccessor(this, "method", m);    return m;  }    private Stmt MethodDecl.createAccessorStmt() {    List argumentList = new List();    for(int i = 0; i < getNumParameter(); i++)      argumentList.add(new VarAccess(getParameter(i).name()));    Access access = new MethodAccess(name(), argumentList);    if(!isStatic())      access = new ThisAccess("this").qualifiesAccess(access);    return isVoid() ? (Stmt) new ExprStmt(access) : new ReturnStmt(new Opt(access));  }  public MethodDecl MethodDecl.createSuperAccessor(TypeDecl methodQualifier) {    MethodDecl m = (MethodDecl)methodQualifier.getAccessor(this, "method_super");    if(m != null) return m;    int accessorIndex = methodQualifier.accessorCounter++;    List parameters = new List();    List args = new List();    for(int i = 0; i < getNumParameter(); i++) {      parameters.add(getParameter(i).fullCopy());      args.add(new VarAccess(getParameter(i).name()));    }    Stmt stmt;    if(type().isVoid())      stmt = new ExprStmt(new SuperAccess("super").qualifiesAccess(new MethodAccess(name(), args)));    else       stmt = new ReturnStmt(new Opt(new SuperAccess("super").qualifiesAccess(new MethodAccess(name(), args))));    m = new MethodDecl(      new Modifiers(new List().add(new Modifier("synthetic"))),      type().createQualifiedAccess(),      name() + "$access$" + accessorIndex,      parameters,      new List(),      new Opt(        new Block(          new List().add(stmt)        )      )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -