Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-dev] AspectJ AST Manibulation - advice and pointcut

It's hard to say exactly what is going on here.  This particular API
is not used very often and not very well tested.

I would recommend that you have a look at the PullOutRefactoring and
the PushInRefactoring that is currently available in AJDT.  Check out
the AJDT source code (or install the AJDT source plugins), and have a
look at org.eclipse.ajdt.internal.ui.refactoring.pullout.PullOutRefactoring
and org.eclipse.ajdt.internal.ui.refactoring.PushInRefactoring.  This
may give you some ideas on what you need to do.

On Tue, Jul 19, 2011 at 8:54 PM, Fatima <faa359@xxxxxxxxxxxxx> wrote:
> Hi there,
> 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.
> Fatima,
>
>
> _______________________________________________
> aspectj-dev mailing list
> aspectj-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-dev
>
>


Back to the top