aspect TypeExt { syn boolean TypeDecl.isSubstitutedType() = false; eq ClassDeclSubstituted.isSubstitutedType() = true; eq InterfaceDeclSubstituted.isSubstitutedType() = true; eq GenericClassDeclSubstituted.isSubstitutedType() = true; eq GenericInterfaceDeclSubstituted.isSubstitutedType() = true; public MemberTypeDecl TypeDecl.asMemberTypeDecl() { throw new RefactoringException("cannot convert this kind of type to member type"); } public MemberClassDecl ClassDecl.asMemberTypeDecl() { return new MemberClassDecl(this); } public MemberInterfaceDecl InterfaceDecl.asMemberTypeDecl() { return new MemberInterfaceDecl(this); } // added visibility so that default contructor inherits it from the class on rewrite after creation public ClassDecl AnonymousDecl.asNamedClass(String name, int visibility) { Modifiers m = getModifiers(); m.setVisibility(visibility); lockAllNames(); ClassDecl cd = new ClassDecl(m, name, new Opt(), new List(), getBodyDecls()); cd.setVisibilityOfImplicitConstructor(visibility); if(superType().isClassDecl() && !superType().isObject()) cd.setSuperClassAccess(superType().createLockedAccess()); for(Access iface : getImplementss()) cd.addImplements(iface.type().createLockedAccess()); Collection ctors = new LinkedList(); for(BodyDecl bd : getBodyDecls()) { if(bd instanceof ConstructorDecl) { ctors.add((ConstructorDecl)bd); ((ConstructorDecl)bd).setID(name); } } // need to make initialisers explicit in constructor to ensure def-before-use for(int i=cd.getNumBodyDecl()-1;i>=0;--i) { BodyDecl bd = cd.getBodyDecl(i); // TODO: handle instance initializers if(bd instanceof FieldDeclaration) { FieldDeclaration fd = (FieldDeclaration)bd; if(!fd.hasInit()) continue; for(ConstructorDecl ctor : ctors) { if(ctor.isChained()) continue; Expr init = fd.getInit(); fd.setInit(null); ctor.getBlock().insertStmt(0, AssignExpr.asStmt(fd.createLockedAccess(), init)); } fd.setInitOpt(new Opt()); } } return cd; } public void ClassDecl.inheritVisibilityToDefaultConstructor() { setVisibilityOfImplicitConstructor(getVisibility()); } public void ClassDecl.setVisibilityOfImplicitConstructor(int visibility) { for(ConstructorDecl cd : (Collection)constructors()) if(cd.isImplicitConstructor()) cd.setVisibility(visibility); } syn lazy boolean ConstructorDecl.isImplicitConstructor() = getStart() == 0 && invokesSuperConstructor() && getBlock().getNumStmt() == 0; // the effective type of an expression is a best-effort approximation to the type of an expression syn TypeDecl Expr.effectiveType() { TypeDecl type = type(); if(type.isAnonymous()) return ((AnonymousDecl)type).superType(); else if(isInaccessibleTypeVariable(type)) return ((TypeVariable)type).uniqueUpperBound(); return type; } // whether or not instances of this type are ever constructed syn lazy boolean TypeDecl.instancesAreConstructed() = false; eq ClassDecl.instancesAreConstructed() { for(ConstructorDecl cd : (Collection)constructors()) for(Access acc : cd.uses()) if(acc instanceof ClassInstanceExpr &&!((ClassInstanceExpr)acc).hasTypeDecl()) return true; return false; } }