//[Personalities/Java - (c) Luis Blando.] //[Personalities/Java behavior file #1 under construction, by Diego Rodrigo.] //[thesis advisor : Daniel Gandara.] //this file contains traversal definitions & visitors. //********************************************************************* //traversals definition. //********************************************************************* CompilationUnit{ //get to classes avoiding NestedClasses. traversal toClasses(PJVisitor) { via ClassDeclaration to UnmodifiedClassDeclaration ; } traversal toPersonalities(PJVisitor) { to PersonalityDeclaration ; } } ImportDeclarations{ traversal toImportDeclaration(PJVisitor) { to ImportDeclaration; } } PersonalityDeclaration{ traversal toEveryField(PJVisitor) { to {PersonalityDownStreamDecl ,PersonalityMethodDecl ,PersonalityInitializerDecl ,PersonalityConstructorDecl ,PersonalityNestedClassDecl ,PersonalityFieldDecl}; } } UnmodifiedClassDeclaration{ traversal toEveryField(PJVisitor) { to { Initializer ,CBD_NestedClassDeclaration ,CBD_NestedInterfaceDeclaration ,ConstructorDeclaration ,CBD_MethodDeclaration ,CBD_FieldDeclaration}; } } /* deprecated!!! PersonalityDownStreamDecl{ traversal downStreams(PJVisitor) { via PersonalityDownStreamDecl to MethodDeclarator; } } */ /* deprecated!!! BlockStatements{ traversal toBlockStatement(PJVisitor) {to BlockStatement;} } */ FormalParameters{ traversal toParameters(PJVisitor) {to FormalParameter;} } NameList{ traversal toNames(PJVisitor) {to Name;} } Name{ traversal toIdentifiers(PJVisitor) {to Identifier;} } AnyBlock{ traversal toPrimaryExpressions(PJVisitor) { to PrimaryExpression; } } //********************************************************************* //useful methods defined below. //********************************************************************* //overriding toString()... Identifier{ (@ public String toString() { Ident a=get_ident(); return a.toString(); } @) } //overriding toString()... Name{ (@ public String toString() { NamesVisitor nv=new NamesVisitor(); this.toIdentifiers(nv); String s=nv.getName(); return s; } @) } //method to obain the name of a 'di' PersonalityDownStreamDecl{ (@ public String getDiName() { PMethodDeclarator PMD = this.get_pmethoddeclarator(); MethodDeclarator MD = PMD.get_methoddeclarator(); Identifier I = MD.get_identifier(); return I.toString(); } @) } //some functions to obtain a list of personified personalities & extended class //[both getExtended are *very* stupid !!!] UnmodifiedClassDeclaration{ (@ public Vector getPersonified() { NameList nl; Vector PersonalityList=new Vector(); try{ nl = get_personalities(); if (nl!=null) { NameListVisitor nlv=new NameListVisitor(); nl.toNames(nlv); PersonalityList = nlv.getList(); } } catch(Exception e){ ; } return PersonalityList; } public Vector getImplemented() { NameList nl; Vector InterfaceList=new Vector(); try{ nl = get_interfaces(); if (nl!=null) { NameListVisitor nlv=new NameListVisitor(); nl.toNames(nlv); InterfaceList = nlv.getList(); } } catch(Exception e){ ; } return InterfaceList; } public String getExtended() { Name a; String s; try{ a=get_extended(); if (a!=null) s=a.toString(); else s=""; } catch (Exception e){ return ""; } return s; } @) } PersonalityDeclaration{ (@ public String getExtended() { Name a; String s; try{ a=get_extended(); if (a!=null) s=a.toString(); else s=""; } catch (Exception e){ return ""; } return s; } @) } //********************************************************************* //visitor classes definition. //********************************************************************* //this is the superclass for all visitors. PJVisitor{ //according to Luis, this is necessary (empty bodies for superclass visitor) //for demjava v0.6.3 . it is also true in v0.7 :-( before { ImportDeclarations, ImportDeclaration ,CompilationUnit ,ClassDeclaration ,UnmodifiedClassDeclaration ,PersonalityDeclaration ,Initializer ,CBD_NestedClassDeclaration ,CBD_NestedInterfaceDeclaration ,ConstructorDeclaration ,CBD_MethodDeclaration ,CBD_FieldDeclaration ,PersonalityDownStreamDecl ,PersonalityMethodDecl ,PersonalityInitializerDecl ,PersonalityConstructorDecl ,PersonalityNestedClassDecl ,PersonalityFieldDecl ,FormalParameters ,FormalParameter /*,BlockStatements ,BlockStatement*/ ,PrimaryExpression ,NameList , Name , Identifier} (@ @) after {CompilationUnit, ClassDeclaration ,UnmodifiedClassDeclaration ,PersonalityDeclaration ,Initializer ,CBD_NestedClassDeclaration ,CBD_NestedInterfaceDeclaration ,ConstructorDeclaration ,CBD_MethodDeclaration ,CBD_FieldDeclaration ,PersonalityDownStreamDecl ,PersonalityMethodDecl ,PersonalityInitializerDecl ,PersonalityConstructorDecl ,PersonalityNestedClassDecl ,PersonalityFieldDecl ,FormalParameters ,FormalParameter /*,BlockStatements ,BlockStatement*/ ,PrimaryExpression ,NameList , Name , Identifier} (@ @) } ImportVisitor{ (@ Vector impList; public Vector getImpList() { return impList; } @) init (@ impList = new Vector(); @) before ImportDeclarations (@ impList = new Vector(); @) before ImportDeclaration (@ impList.addElement(host); @) } //this visitor is used to obtain a class or personality from a //compilation unit. ClassesVisitor{ (@ boolean verbose = false; DepLeaf df; String pack; Vector imp; //function to get a class or personality after parsing... public DepLeaf getObject() { return df; } //a constructor for ClassesVisitor that enables being verbose. public ClassesVisitor(boolean b) { //maybe if we put "this();" here ... verbose = b; df=new DepLeaf("noname","noname","noname"); //pack = ""; //imp = new Vector(); } @) init (@ df=new DepLeaf("noname","noname","noname"); //pack = ""; //imp = new Vector(); @) //make sure we have an empty depleaf before the visit starts. before CompilationUnit (@ df=new DepLeaf("noname","noname","noname"); PackageDeclaration pd = host.get_packagedeclaration(); if (pd != null) pack = pd.get_name().toString(); else pack = ""; ImportDeclarations ids = host.get_importdeclarations(); ImportVisitor IV = new ImportVisitor(); ids.toImportDeclaration(IV); Vector v = IV.getImpList(); imp = new Vector(); for(int i=0; i extends " + ext ); } per = host.getPersonified(); int length = per.size(); if (length > 0) { System.out.println( " -> personifies"); for (int i=0; i extends " + ext ); } } @) after PersonalityDeclaration (@ if (verbose) System.out.println("leaving personality: "+host.get_ident() ); @) } //this visitor loads vectors with the fields of a personality, //and has some methods to obtain the vectors. PersonalityFieldsVisitor{ //vectors & getters for the 6 kinds of fields of a personality: //downstream, method, initializer, nested class, constructor, field. //[method = (ui + private + protected)] (@ Vector diVec; Vector meVec; Vector inVec; Vector ncVec; Vector coVec; Vector fiVec; public Vector getDiList() { return diVec; } public Vector getMeList() { return meVec; } public Vector getInList() { return inVec; } public Vector getNcList() { return ncVec; } public Vector getCoList() { return coVec; } public Vector getFiList() { return fiVec; } @) //initalize vectors (make sure they're empty at the begining). before PersonalityDeclaration (@ diVec = new Vector(); meVec = new Vector(); inVec = new Vector(); ncVec = new Vector(); coVec = new Vector(); fiVec = new Vector(); @) before PersonalityDownStreamDecl (@ diVec.addElement(host); @) before PersonalityMethodDecl (@ meVec.addElement(host); @) before PersonalityInitializerDecl (@ inVec.addElement(host); @) before PersonalityNestedClassDecl (@ ncVec.addElement(host); @) before PersonalityConstructorDecl (@ coVec.addElement(host); @) before PersonalityFieldDecl (@ fiVec.addElement(host); @) } //the same applied to classes (vectors & getters for every field). ClassFieldsVisitor{ (@ Vector inVec; Vector coVec; Vector ncVec; Vector niVec; Vector meVec; Vector fiVec; public Vector getInList(){ return inVec; } public Vector getCoList(){ return coVec; } public Vector getNcList(){ return ncVec; } public Vector getNiList(){ return niVec; } public Vector getMeList(){ return meVec; } public Vector getFiList(){ return fiVec; } @) //initalize vectors (make sure they're empty at the begining). before UnmodifiedClassDeclaration (@ inVec = new Vector(); coVec = new Vector(); ncVec = new Vector(); niVec = new Vector(); meVec = new Vector(); fiVec = new Vector(); @) before Initializer (@ inVec.addElement(host); @) before CBD_NestedClassDeclaration (@ ncVec.addElement(host); @) before CBD_NestedInterfaceDeclaration (@ niVec.addElement(host); @) before ConstructorDeclaration (@ coVec.addElement(host); @) before CBD_MethodDeclaration (@ meVec.addElement(host); @) before CBD_FieldDeclaration (@ fiVec.addElement(host); @) } //this visitor gets a vector of parameters from a list of formal parameters. ParameterVisitor{ (@ Vector list; public Vector getList() { return list; } @) init (@ list = new Vector(); @) //empty the list before traversal. before FormalParameters (@ list = new Vector(); @) before FormalParameter (@ list.addElement(host); @) } /* deprecated!!! BlockStatementsVisitor{ (@ Vector list; public Vector getBSList(){ return list; } @) before BlockStatements (@ list = new Vector(); @) before BlockStatement (@ list.addElement(host); @) } */ //this visitor builds a vector of strings with the names of a namelist. //a namelist is comma-separated list of names. NameListVisitor{ (@ Vector list; public Vector getList() { return list; } NamesVisitor nv; @) init (@ nv = new NamesVisitor(); list = new Vector(); @) //empty the list before the visit starts. before NameList (@ list = new Vector(); @) before Name (@ String s; host.toIdentifiers(nv); s = nv.getName(); list.addElement(s); @) } //this visitor fills a string with the name it's visiting. //a name is a dot-separated list of identifiers. NamesVisitor{ (@ String buffer; public String getName() { return buffer; } @) //empty the buffer before the visit starts. before Name (@ buffer=""; @) before Identifier (@ Ident i=host.get_ident(); //System.out.println(" name: "+i.toString() ); if (buffer=="") buffer=buffer+i.toString(); else buffer=buffer+"."+i.toString(); @) } PrimaryVisitor{ (@ Vector v; Vector getPrimaries() { return v; } @) init (@ v = new Vector(); @) before PrimaryExpression (@ v.addElement(host); @) } //this is a rare visitor : it modifies the original code. //it adds "host." before any method which name is contained in the list of DIs. HostDotVisitor{ (@ Vector diList; //boolean debug = false; public HostDotVisitor(Vector v) { diList = v; } //public HostDotVisitor(Vector v, boolean b) { diList = v; debug = b;} @) before PrimaryExpression (@ PrimaryPrefix PP = host.get_primaryprefix(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter( sw ); PrintVisitor pv = new PrintVisitor( pw ); PP.universal_trv0(pv); String tmp = "" + sw; //System.out.println(tmp); PersonalityDownStreamDecl di; String diName; int i, length = diList.size(); for(i=0; i