/* * some extensions to ease testing */ aspect Testing { public Program Frontend.getProgram() { return program; } // sort compilation units alphabetically before printing; this helps when comparing results refine PrettyPrint public void Program.toString(StringBuffer s) { ArrayList cus = new ArrayList(); for(Iterator iter = compilationUnitIterator(); iter.hasNext();) { CompilationUnit cu = iter.next(); if(cu.fromSource()) cus.add(cu); } Collections.sort(cus, new Comparator() { public int compare(CompilationUnit cu1, CompilationUnit cu2) { String id1 = cu1.getPackageDecl() + "." + cu1.getID(); String id2 = cu2.getPackageDecl() + "." + cu2.getID(); return id1.compareTo(id2); } }); for(CompilationUnit cu : cus) cu.toString(s); } syn String BodyDecl.ppID() = ""; eq ConstructorDecl.ppID() = name(); eq FieldDeclaration.ppID() = name(); eq MemberTypeDecl.ppID() = typeDecl().name(); eq MethodDecl.ppID() = name(); refine PrettyPrint protected void TypeDecl.ppBodyDecls(StringBuffer s) { ArrayList bds = new ArrayList(); for(int i=0;i fields = new ArrayList(); java.util.List constrs = new ArrayList(); java.util.List methods = new ArrayList(); java.util.List other = new ArrayList(); // classes and interfaces for (BodyDecl bd : bds) { if (bd instanceof FieldDecl || bd instanceof FieldDeclaration || bd instanceof InstanceInitializer || bd instanceof StaticInitializer) fields.add(bd); else if (bd instanceof ConstructorDecl) constrs.add(bd); else if (bd instanceof MethodDecl) methods.add(bd); else other.add(bd); } for (BodyDecl bd : fields) bd.toString(s); Collections.sort(constrs, new Comparator() { //@Override public int compare(BodyDecl o1, BodyDecl o2) { return ((ConstructorDecl)o1).signature().compareTo(((ConstructorDecl)o2).signature()); } }); for (BodyDecl bd : constrs) bd.toString(s); Collections.sort(methods, new Comparator() { //@Override public int compare(BodyDecl o1, BodyDecl o2) { MethodDecl p1 = (MethodDecl)o1; MethodDecl p2 = (MethodDecl)o2; return (p1.getTypeAccess() +"/"+ p1.signature()).compareTo(p2.getTypeAccess() +"/"+ p2.signature()); } }); for (BodyDecl bd : methods) bd.toString(s); Collections.sort(other, new Comparator() { public int compare(BodyDecl o1, BodyDecl o2) { String name1, name2; if (o1 instanceof MemberClassDecl) name1 = ((MemberClassDecl)o1).getClassDecl().name(); else name1 = ((MemberInterfaceDecl)o1).getInterfaceDecl().name(); if (o2 instanceof MemberClassDecl) name2 = ((MemberClassDecl)o2).getClassDecl().name(); else name2 = ((MemberInterfaceDecl)o2).getInterfaceDecl().name(); return name1.compareTo(name2); } }); for (BodyDecl bd : other) bd.toString(s); s.append(indent() + "}"); } refine PrettyPrint public void Modifiers.toString(StringBuffer s) { ArrayList mods = new ArrayList(); for(Modifier mod : getModifiers()) mods.add(mod); Collections.sort(mods, new Comparator() { public int compare(Modifier mod1, Modifier mod2) { return mod1.getID().compareTo(mod2.getID()); } }); for(Modifier mod : mods) { mod.toString(s); s.append(" "); } } refine GenericsPrettyPrint private void GenericClassDecl.ppTypeParameters(StringBuffer s) { ArrayList typeparms = new ArrayList(); for(TypeVariable tv : getTypeParameters()) typeparms.add(tv); Collections.sort(typeparms, new Comparator() { public int compare(TypeVariable tv1, TypeVariable tv2) { return tv1.name().compareTo(tv2.name()); } }); s.append('<'); if(typeparms.size() > 0) { typeparms.get(0).toString(s); for (int i=1; i < typeparms.size(); i++) { s.append(", "); typeparms.get(i).toString(s); } } s.append('>'); } refine GenericMethods private void GenericMethodDecl.ppTypeParameters(StringBuffer s) { ArrayList typeparms = new ArrayList(); for(TypeVariable tv : original().getTypeParameters()) typeparms.add(tv); Collections.sort(typeparms, new Comparator() { public int compare(TypeVariable tv1, TypeVariable tv2) { return tv1.name().compareTo(tv2.name()); } }); s.append(" <"); for(int i = 0; i < typeparms.size(); i++) { if(i != 0) s.append(", "); typeparms.get(i).toString(s); } s.append("> "); } // output names of compilation units; this helps when comparing results public static boolean CompilationUnit.printCUNames = false; refine PrettyPrint public void CompilationUnit.toString(StringBuffer s) { if(printCUNames) s.append("compilation unit "+getID()+"\n"); try { if(!getPackageDecl().equals("")) { s.append("package " + getPackageDecl() + ";\n"); } for(int i = 0; i < getNumImportDecl(); i++) { getImportDecl(i).toString(s); } ArrayList tds = new ArrayList(); for(int i=0;i() { public int compare(TypeDecl td1, TypeDecl td2) { return td1.name().compareTo(td2.name()); } }); for(TypeDecl td : tds) { td.toString(s); s.append("\n"); } } catch (NullPointerException e) { System.out.print("Error in compilation unit hosting " + getTypeDecl(0).typeName()); throw e; } } // find a doubly-parenthesised expression public Expr ASTNode.findDoublyParenthesised() { for(int i=0;i=0;--i) { ASTNode res = getChild(i).findNodeBefore(clazz, line, column); if(res != null) return res; } if(clazz.isAssignableFrom(this.getClass())) { int endLine = getLine(getEnd()), endColumn = getColumn(getEnd()); if(endLine != 0 && endLine < line || endLine == line && endColumn <= column) return this; } return null; } public ASTNode Program.findNodeBetween(Class clazz, String startComment, String endComment) { for(CompilationUnit cu : getCompilationUnits()) { FileRange fr1 = findComment(startComment), fr2 = findComment(endComment); if(fr1 != null && fr2 != null) return findNodeBetween(clazz, fr1.el, fr1.ec, fr2.sl, fr2.sc); } return null; } public ASTNode ASTNode.findNodeBetween(Class clazz, int line1, int column1, int line2, int column2) { for(int i=0;i printed = new HashSet(); public static String prefix = ""; static void print(String s) { s = prefix + " :: " + s; if(!printed.contains(s) && !s.endsWith(" same\n") && !s.matches(".*_computed : boolean\\s.*\\s*") && !s.matches(".*_visited : int\\s.*\\s*")) { printed.add(s); System.out.print(s); } } } public void List.toString(StringBuffer s) { s.append("[ "); for (int i = 0; i < getNumChild(); i++) { s.append(getChild(i).toString()); if (i+1