Lab3 part2 (CreateDecorator.java) created by: Adrian Pop (adrpo@ida.liu.se) date : 2004-10-04 Ideea: - create an empty member list and add to it: + the PRIVATE delegate field + the public constructor + the public list of methods with calls to the delegate field - create the class declaration using the created member list use attach the class to the first position in the cu like this: this.astTransformation.attach(decorator, createdCu, 0); NOTE: this is the ONLY attach in the program The same thing broken in steps: NOTES: + use ProgramFactory pf almost everywhere when creating stuff!!! + exceptions: - new ModifierArrayList(...); // needed to set public/private for members - new StatementArrayList(...); // needed when creating a StatementBlock - new ParameterDeclarationArrayList(...); // needed when creating parameters for constructor - new ExpressionArrayList(...); // needed to add variables for method calls + DO NOT use deepClone() for methods! create new ones + use deepClone() only for: - method.getParameters().deepClone(); // used for new method declaration - method.getTypeReference().deepClone(); // used for new method declaration Step1: Create the empty member list MemberDeclarationMutableList members = new MemberDeclarationArrayList(); Step2: Create the delegate field and append it to the members FieldDeclaration delegateField = pf.createFieldDeclaration(...); members.add(delegateField); Step3: Create and add the constructor to the members list - create copy assignment and add it to the constructor body - create the parameter declaration - create the constructor constructorDecl = pf.createConstructorDeclaration(...); - members.add(constructorDecl); Step4: Create the new methods of the decorator and add them to the member list - for each method in the component create a new method and add it to the method list for (int i = 0; i < component.getMethods().size(); i++) { MethodDeclaration method = (MethodDeclaration)component.getMethods().getMethod(i); // create new method here using the information from the method // before you can actually create the new method you need to build: // 1. method reference arguments (take them from method.getParameters()) methodArguments // NOTE!!!: one parameter can have several variables, use param.getVariables() // 2. create the method reference // MethodReference methodRef = pf.createMethodReference(..., methodArguments); // 3. now look at the return type of the method (it tells you if you have to create a return statement or not) // if (method.getTypeReference().getName().equals(....)) // make call to the delegate // else // create return statement with call to delegate // 4. create the new method declaration // MethodDeclaration newMethod = pf.createMethodDeclaration(...); // 5. add the method to the members // members.add(newMethod); } Step5: Create the decorator class and attach it to the createdCu TypeReference componentReference = pf.createTypeReference(...); ClassDeclaration decorator = pf.createClassDeclaration(..., pf.createImplements(componentReference), members); // attach the class to the first position in the cu. this.astTransformation.attach(decorator, createdCu, 0); // the only attach in this transformation Well that's all folks! Regular mistakes are to use new XXX instead of pf.createXXX() Have fun! Regards, Adrian Pop/