I am a grad student at the
University of Saskatchewan working under the supervision of professor Christopher Dutchyn. My research project is to build a plugin to refactor Java into AspectJ.
I have been trying to create an aspect by manipulating the ast that I got by parsing a string and then get the compilation unit from the parser. This is my code down
String doc = "package " +
pkgFragment.getElementName() +
";" +
"\n\n";
parser =
org.aspectj.org.eclipse.jdt.core.dom.ASTParser.newParser(org.aspectj.org.eclipse.jdt.core.dom.AST.JLS3); parser.setKind(org.aspectj.org.eclipse.jdt.core.dom.ASTParser.K_COMPILATION_UNIT);
parser.setSource(helloWorld.toCharArray());
parser.setCompilerOptions(new HashMap<String,String>());
parser.setUnitName(aspectName);
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
unit = ((org.aspectj.org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null));
ast = (AjAST) unit.getAST();
document = new Document(aspectCode);
AjTypeDeclaration ajtype = ast.newAjTypeDeclaration();
ajtype.setName(ast.newSimpleName(aspectName));
ajtype.modifiers().add(ast.newModifier(org.aspectj.org.eclipse.jdt.core.dom.Modifier.ModifierKeyword.PUBLIC_KEYWORD));
ajtype.setAspect(true);
org.aspectj.org.eclipse.jdt.core.dom.MethodDeclaration method = ast.newMethodDeclaration();
method.modifiers().add(ast.newModifier(org.aspectj.org.eclipse.jdt.core.dom.Modifier.ModifierKeyword.PUBLIC_KEYWORD));
method.setName(ast.newSimpleName(name));
method.setReturnType2(null);
method.setBody(ast.newBlock());
PointcutDeclaration pointcut = ast.newPointcutDeclaration();
pointcut.setName(ast.newSimpleName(pointcutName)); pointcut.modifiers().add(ast.newModifier(org.aspectj.org.eclipse.jdt.core.dom.Modifier.ModifierKeyword.ABSTRACT_KEYWORD));
//add method to ajtype body
ajtype.bodyDeclarations().add(methodOne);
//add pointcut to ajtype body
System.out.println("adding pcut: " + ajtype.bodyDeclarations().add(pointcut));
org.aspectj.org.eclipse.jdt.core.dom.rewrite.ASTRewrite rewriter = ASTRewrite.create(ast);
org.aspectj.org.eclipse.jdt.core.dom.rewrite.ListRewrite lrw = rewriter.getListRewrite((org.aspectj.org.eclipse.jdt.core.dom.ASTNode) unit,
org.aspectj.org.eclipse.jdt.core.dom.CompilationUnit.TYPES_PROPERTY);
lrw.insertLast(ajtype, null);
TextEdit edits = rewriter.rewriteAST(document, new HashMap<String,String>());
try {
edits.apply(document);
} catch (MalformedTreeException e) {
e.printStackTrace();
}
printing the content of the document after editing results in this:
package observer;
public class AspectOfsetX { public void methodOne(){
}
}
without the pointcut being applied to the document but it is actually added to the AST because System.out.println(ajtype.bodyDeclarations().add(pointcut)) prints true. Also, I am expexting the result of this should be an aspect not a class.
The parser gives me this warning
Warning: AspectJ type declaration factory class not found on classpath
I have tried to add a pointcut and an advice to the body declaration of the ajTypeDeclaration and also I tried with AspectDeclaration but get no luck with both, only method declaration get added. what is it that I am doing wrong? Please point me to the correct way of doing this.