/* * Copyright 2008 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package t2ext.weaver.utils; /** * @author Maaike Gerritsen */ public class Template { public static String getPackage() { return "package t2ext.weaver;\n\n"; } public static String getImports() { StringBuilder imports = new StringBuilder(); imports.append("import java.util.List;\n"); imports.append("import t2ext.weaver.utils.WeaverMethods;\n\n"); return imports.toString(); } public static String getClassOpener() { return "public aspect Weaver {\n"; } public static String getCloseBracket() { return "}\n\n"; } public static String getFieldAuxMethods() { return "\t private WeaverMethods _auxMethods;\n"; } public static String getInitialisationAdvice() { StringBuilder builder = new StringBuilder(); builder.append("\t before() : initialisation(){\n"); builder.append("\t \t if (_auxMethods == null) { \n"); builder.append("\t \t \t _auxMethods = new WeaverMethods();\n"); builder.append("\t \t " + getCloseBracket()); builder.append("\t " + getCloseBracket()); return builder.toString(); } public static String getInitialisationPointCut(String className) { StringBuilder builder = new StringBuilder(); builder.append("\t pointcut initialisation() : initialization("); builder.append(className); builder.append(".new(..)); \n \n"); return builder.toString(); } public static String getPointCut(String methodName, int pointCutID) { StringBuilder builder = new StringBuilder(); builder.append("\t pointcut weave"); builder.append(pointCutID); builder.append("() : execution(* "); builder.append(methodName); builder.append("); \n \n"); return builder.toString(); } public static String getAroundPointcut(String methodName, int id) { StringBuilder builder = new StringBuilder(); builder.append("\t pointcut aroundMethod"); builder.append(id); builder.append("() : withincode(* "); builder.append(methodName); builder.append("); \n \n"); return builder.toString(); } public static String getAroundAdvice(String methodName, int id) { StringBuilder builder = new StringBuilder(); builder.append("\t Object around() : aroundMethod"); builder.append(id); builder.append("() { \n"); builder.append("\t \t Object obj = thisJoinPoint.getTarget(); \n"); builder.append("\t \t if(_auxMethods.findPath(\"" + methodName + "\")){\n"); builder.append("\t \t \t obj = proceed();\n"); builder.append("\t \t } \n"); builder.append("\t \t return obj; \n"); builder.append("\t " + getCloseBracket()); return builder.toString(); } public static String getBeforeAdvice(String methodName, int pointCutID, boolean endMethod) { StringBuilder builder = new StringBuilder(); builder.append("\t before() : weave"); builder.append(pointCutID); builder.append("() { \n"); builder.append("\t \t if (_auxMethods == null) \n"); builder.append("\t \t \t _auxMethods = new WeaverMethods(); \n"); builder.append("\t \t _auxMethods.startPathList(\""); builder.append(methodName); builder.append("\"); \n"); if (endMethod) { builder.append("\t \t _auxMethods.getPathToFindFromXML("); builder.append("thisJoinPoint.toLongString()"); builder.append("); \n"); } builder.append("\t " + getCloseBracket()); return builder.toString(); } public static String getAdvice(String parentName, String methodName, int id, boolean endMethod) { StringBuilder builder = new StringBuilder(); builder.append("\t before() : weave"); builder.append(id); builder.append("() { \n"); builder.append("\t \t int id = WeaverMethods.getIdFromMethodName(thisJoinPoint.toLongString()); \n"); builder.append("\t \t if(id!=(-1)){ \n"); builder.append("\t \t \t _auxMethods.addToList(\""); builder.append(methodName); builder.append("\",id); \n"); if (endMethod) { builder.append("\t \t \t _auxMethods.checkList(\""); builder.append(methodName + "\","); builder.append("\"" + parentName + "\""); builder.append(");\n"); } builder.append("\t \t } \n"); builder.append("\t " + getCloseBracket()); return builder.toString(); } public static String getAfterAdvice(String methodName, int id, boolean endMethod) { StringBuilder builder = new StringBuilder(); builder.append("\t after() : weave" + id + "() { \n"); if (endMethod) builder.append("\t \t if(_auxMethods.findPath(thisJoinPoint.toLongString())){\n"); builder.append("\t \t \t List list = _auxMethods.getCompletePathList(\""); builder.append(methodName); builder.append("\");\n"); builder.append("\t \t \t if(list!=null){ \n"); builder.append("\t \t \t \t WeaverMethods.findPath(thisJoinPoint.toLongString(),list); \n"); builder.append("\t \t \t } \n"); if (endMethod) builder.append("\t \t } \n"); builder.append("\t " + getCloseBracket()); return builder.toString(); } }