Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AJDT » Aspect Rewrite
Aspect Rewrite [message #71366] Tue, 18 March 2008 23:15 Go to next message
Eclipse UserFriend
Originally posted by: cartona.tcd.ie

Hi,

While using AJDT - is it possible to change the AST of an AspectJ file
and then rewrite it back to a file. The JDT has a "recordModification"
section that shows you how to do this - but the AJCompilationUnit
doesn't support this? I see in there is a AjASTRewrite that does
something like this in the source file. I wonder if anyone has any
success or knows how to do this.

Also, you can't use a visitor on the AJCompilationUnit unlike the
CompilationUnit - which is a bit distressing.

Thanks,
Andrew.
Re: Aspect Rewrite [message #71386 is a reply to message #71366] Wed, 19 March 2008 02:51 Go to previous messageGo to next message
Martin Görg is currently offline Martin GörgFriend
Messages: 10
Registered: July 2009
Junior Member
On Tue, 18 Mar 2008 23:15:06 +0000, Andrew Carton wrote:
> While using AJDT - is it possible to change the AST of an AspectJ file
> and then rewrite it back to a file. The JDT has a "recordModification"
> section that shows you how to do this - but the AJCompilationUnit
> doesn't support this? I see in there is a AjASTRewrite that does
> something like this in the source file. I wonder if anyone has any
> success or knows how to do this.

I just asked a related question on the aspectj-dev mailinglist. You can
view the thread here:
http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02330.ht ml

There is also some information included in the docs module of the AspectJ
project (docs/developer/compiler-weaver/index.html). Let me know, if you
find something useful.

--
Cheers
Martin
Re: Aspect Rewrite [message #71405 is a reply to message #71386] Wed, 19 March 2008 12:14 Go to previous messageGo to next message
Andrew Carton is currently offline Andrew CartonFriend
Messages: 104
Registered: July 2009
Senior Member
Hi Martin + any other people reading this,

Yes - I wish to rewrite a small modification of an aspect from it's AST
back to it's original file.

I have had partial success with visitors etc but the "writing back"
still doesn't work.

Here is my example code below:

Just a note on the code - The ChangeSomethingVisitor extends the
AjASTVisitor and modifies something in the AST (specific to the AspectJ
class from the AJCompilationUnit).

To make sure the modification is changed I use the AjNaiveASTFlattener
visitor which just makes a kind of debug information about the aspect.
The change is successfully implemented.

However, when I attempt to use the AjASTRewrite to modify the tree and
get the TextEdits - it appears to be 0. I don't understand why!

So I'm stumped really - I read a lot and did a lot to get to this stage
- it really is a black art (The documentation/API is really poor and
there are not many posts about how to do this kind of thing!) - is it
something I did wrong or is it just the libraries aren't fully
implemented? I'd really appreciate some help on this!

Thanks,
Andrew.

currentCompUnit is an AJCompilationUnit

if (currentCompUnit == null)
return;


currentCompUnit.requestOriginalContentMode();

String contents = "";

try {
contents = currentCompUnit.getBuffer().getContents();

} catch (org.eclipse.jdt.core.JavaModelException e)
{
e.printStackTrace();

}
currentCompUnit.discardOriginalContentMode();

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setCompilerOptions(new HashMap());
parser.setSource(contents.toCharArray());
Document doc = new Document(contents);
CompilationUnit c = (CompilationUnit) parser.createAST(null);
c.recordModifications();
AjASTRewrite ajrewrite = AjASTRewrite.create(c.getAST());
ChangeSomethingVisitor visit = new ChangeSomethingVisitor();

AjNaiveASTFlattener visit2 = new AjNaiveASTFlattener();
c.accept(visit);
c.accept(visit2);
System.err.println(visit2.getResult());
TextEdit edits =
ajrewrite.rewriteAST(doc,currentCompUnit.getJavaProject().ge tOptions(true));
System.err.println(edits.getChildrenSize());
edits.apply(doc);
System.out.println(doc.get());

Ar 19/03/2008 02:51, Scríobh Martin Görg:
> On Tue, 18 Mar 2008 23:15:06 +0000, Andrew Carton wrote:
>> While using AJDT - is it possible to change the AST of an AspectJ file
>> and then rewrite it back to a file. The JDT has a "recordModification"
>> section that shows you how to do this - but the AJCompilationUnit
>> doesn't support this? I see in there is a AjASTRewrite that does
>> something like this in the source file. I wonder if anyone has any
>> success or knows how to do this.
>
> I just asked a related question on the aspectj-dev mailinglist. You can
> view the thread here:
> http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02330.ht ml
>
> There is also some information included in the docs module of the AspectJ
> project (docs/developer/compiler-weaver/index.html). Let me know, if you
> find something useful.
>
Re: Aspect Rewrite [message #71424 is a reply to message #71405] Wed, 19 March 2008 12:43 Go to previous messageGo to next message
Andrew Carton is currently offline Andrew CartonFriend
Messages: 104
Registered: July 2009
Senior Member
Ok I'm reading this
http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse. jdt.doc.isv/guide/jdt_api_manip.htm

and I think I'm incorrect in using the AjRewriter

However, just going on the CompilerUnit recordModifications / rewrite alone

When I attempt record the modifications and then change the AST and
attempt a rewrite - I get an error message in the console saying

"Warning: AspectJ type declaration factory class not found on classpath"

I had a peek in the source and this is the code:

public final class AjASTRewriteAnalyzer extends AjASTVisitor {

private static final String AJ_ASTREWRITEANALYZER_FACTORY
" org.eclipse.ajdt.core.dom.rewrite.AjASTRewriteAnalyzerFactor y ";

private static IASTRewriteAnalyzerFactory astRewriteAnalyzerFactory;

static {
try{
astRewriteAnalyzerFactory = (IASTRewriteAnalyzerFactory)
Class.forName(AJ_ASTREWRITEANALYZER_FACTORY).newInstance();
} catch (InstantiationException ex) {
throw new ExceptionInInitializerError(ex.getMessage());
} catch (IllegalAccessException ex) {
throw new ExceptionInInitializerError(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.err.println("Warning: AspectJ type declaration factory
class not found on classpath"); //$NON-NLS-1$
//throw new ExceptionInInitializerError(ex.getMessage());
}

I've added all the necessary jars in the extension file of my plugin and
I'm not sure why it isn't picking up this - because it is picking up
other classes in this jar file.

Thanks,
Andrew.


Ar 19/03/2008 12:14, Scríobh Andrew Carton:
> Hi Martin + any other people reading this,
>
> Yes - I wish to rewrite a small modification of an aspect from it's AST
> back to it's original file.
>
> I have had partial success with visitors etc but the "writing back"
> still doesn't work.
>
> Here is my example code below:
>
> Just a note on the code - The ChangeSomethingVisitor extends the
> AjASTVisitor and modifies something in the AST (specific to the AspectJ
> class from the AJCompilationUnit).
>
> To make sure the modification is changed I use the AjNaiveASTFlattener
> visitor which just makes a kind of debug information about the aspect.
> The change is successfully implemented.
>
> However, when I attempt to use the AjASTRewrite to modify the tree and
> get the TextEdits - it appears to be 0. I don't understand why!
>
> So I'm stumped really - I read a lot and did a lot to get to this stage
> - it really is a black art (The documentation/API is really poor and
> there are not many posts about how to do this kind of thing!) - is it
> something I did wrong or is it just the libraries aren't fully
> implemented? I'd really appreciate some help on this!
>
> Thanks,
> Andrew.
>
> currentCompUnit is an AJCompilationUnit
>
> if (currentCompUnit == null)
> return;
>
>
> currentCompUnit.requestOriginalContentMode();
>
> String contents = "";
>
> try {
> contents = currentCompUnit.getBuffer().getContents();
>
> } catch (org.eclipse.jdt.core.JavaModelException e)
> {
> e.printStackTrace();
>
> }
> currentCompUnit.discardOriginalContentMode();
>
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setCompilerOptions(new HashMap());
> parser.setSource(contents.toCharArray());
> Document doc = new Document(contents);
> CompilationUnit c = (CompilationUnit) parser.createAST(null);
> c.recordModifications();
> AjASTRewrite ajrewrite = AjASTRewrite.create(c.getAST());
> ChangeSomethingVisitor visit = new ChangeSomethingVisitor();
>
> AjNaiveASTFlattener visit2 = new AjNaiveASTFlattener();
> c.accept(visit);
> c.accept(visit2);
> System.err.println(visit2.getResult());
> TextEdit edits =
> ajrewrite.rewriteAST(doc,currentCompUnit.getJavaProject().ge tOptions(true));
>
> System.err.println(edits.getChildrenSize());
> edits.apply(doc);
> System.out.println(doc.get());
>
> Ar 19/03/2008 02:51, Scríobh Martin Görg:
>> On Tue, 18 Mar 2008 23:15:06 +0000, Andrew Carton wrote:
>>> While using AJDT - is it possible to change the AST of an AspectJ
>>> file and then rewrite it back to a file. The JDT has a
>>> "recordModification" section that shows you how to do this - but the
>>> AJCompilationUnit doesn't support this? I see in there is a
>>> AjASTRewrite that does something like this in the source file. I
>>> wonder if anyone has any success or knows how to do this.
>>
>> I just asked a related question on the aspectj-dev mailinglist. You can
>> view the thread here:
>> http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02330.ht ml
>>
>> There is also some information included in the docs module of the AspectJ
>> project (docs/developer/compiler-weaver/index.html). Let me know, if you
>> find something useful.
>>
Re: Aspect Rewrite [message #71443 is a reply to message #71424] Thu, 20 March 2008 16:28 Go to previous messageGo to next message
Andrew Carton is currently offline Andrew CartonFriend
Messages: 104
Registered: July 2009
Senior Member
Ok I have had limited success with rewriting. However, I can only ever
get it working with SimpleTypes.

For example when I try duplicating a PointcutDeclaration and rewriting
it with the AjASTRewriter it just comes out as empty. I have tested the
copy of the PointcutDeclaration and the settings are not empty...

AjASTRewrite ajrewrite =....

AspectDeclaration ad = ...

PointcutDeclaration[] pointcuts = ad.getPointcuts();
PointcutDeclaration old = pointcuts[0];
PointcutDeclaration newd = (PointcutDeclartion)
PointcutDeclaration.copySubtree(ast, old);


ajrewrite.replace(old, newd, null);

There is 1 rewrite but the subtree is not copied in the TextEdit when I
save the file.

It is just ";" - an empty semicolon.

Does anyone have any ideas - is the implementation unfinished?

Thanks,
andrew.

Ar 19/03/2008 12:43, Scríobh Andrew Carton:
> Ok I'm reading this
> http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse. jdt.doc.isv/guide/jdt_api_manip.htm
>
>
> and I think I'm incorrect in using the AjRewriter
>
> However, just going on the CompilerUnit recordModifications / rewrite alone
>
> When I attempt record the modifications and then change the AST and
> attempt a rewrite - I get an error message in the console saying
>
> "Warning: AspectJ type declaration factory class not found on classpath"
>
> I had a peek in the source and this is the code:
>
> public final class AjASTRewriteAnalyzer extends AjASTVisitor {
>
> private static final String AJ_ASTREWRITEANALYZER_FACTORY
> " org.eclipse.ajdt.core.dom.rewrite.AjASTRewriteAnalyzerFactor y ";
>
> private static IASTRewriteAnalyzerFactory astRewriteAnalyzerFactory;
>
> static {
> try{
> astRewriteAnalyzerFactory = (IASTRewriteAnalyzerFactory)
> Class.forName(AJ_ASTREWRITEANALYZER_FACTORY).newInstance();
> } catch (InstantiationException ex) {
> throw new ExceptionInInitializerError(ex.getMessage());
> } catch (IllegalAccessException ex) {
> throw new ExceptionInInitializerError(ex.getMessage());
> } catch (ClassNotFoundException ex) {
> System.err.println("Warning: AspectJ type declaration factory
> class not found on classpath"); //$NON-NLS-1$
> //throw new ExceptionInInitializerError(ex.getMessage());
> }
>
> I've added all the necessary jars in the extension file of my plugin and
> I'm not sure why it isn't picking up this - because it is picking up
> other classes in this jar file.
>
> Thanks,
> Andrew.
>
>
> Ar 19/03/2008 12:14, Scríobh Andrew Carton:
>> Hi Martin + any other people reading this,
>>
>> Yes - I wish to rewrite a small modification of an aspect from it's
>> AST back to it's original file.
>>
>> I have had partial success with visitors etc but the "writing back"
>> still doesn't work.
>>
>> Here is my example code below:
>>
>> Just a note on the code - The ChangeSomethingVisitor extends the
>> AjASTVisitor and modifies something in the AST (specific to the
>> AspectJ class from the AJCompilationUnit).
>>
>> To make sure the modification is changed I use the AjNaiveASTFlattener
>> visitor which just makes a kind of debug information about the aspect.
>> The change is successfully implemented.
>>
>> However, when I attempt to use the AjASTRewrite to modify the tree and
>> get the TextEdits - it appears to be 0. I don't understand why!
>>
>> So I'm stumped really - I read a lot and did a lot to get to this
>> stage - it really is a black art (The documentation/API is really poor
>> and there are not many posts about how to do this kind of thing!) - is
>> it something I did wrong or is it just the libraries aren't fully
>> implemented? I'd really appreciate some help on this!
>>
>> Thanks,
>> Andrew.
>>
>> currentCompUnit is an AJCompilationUnit
>>
>> if (currentCompUnit == null)
>> return;
>>
>> currentCompUnit.requestOriginalContentMode();
>>
>> String contents = "";
>>
>> try {
>> contents = currentCompUnit.getBuffer().getContents();
>>
>> } catch (org.eclipse.jdt.core.JavaModelException e)
>> {
>> e.printStackTrace();
>>
>> }
>> currentCompUnit.discardOriginalContentMode();
>>
>> ASTParser parser = ASTParser.newParser(AST.JLS3);
>> parser.setCompilerOptions(new HashMap());
>> parser.setSource(contents.toCharArray());
>> Document doc = new Document(contents);
>> CompilationUnit c = (CompilationUnit) parser.createAST(null);
>> c.recordModifications();
>> AjASTRewrite ajrewrite = AjASTRewrite.create(c.getAST());
>> ChangeSomethingVisitor visit = new ChangeSomethingVisitor();
>>
>> AjNaiveASTFlattener visit2 = new AjNaiveASTFlattener();
>> c.accept(visit);
>> c.accept(visit2);
>> System.err.println(visit2.getResult());
>> TextEdit edits =
>> ajrewrite.rewriteAST(doc,currentCompUnit.getJavaProject().ge tOptions(true));
>>
>> System.err.println(edits.getChildrenSize());
>> edits.apply(doc);
>> System.out.println(doc.get());
>>
>> Ar 19/03/2008 02:51, Scríobh Martin Görg:
>>> On Tue, 18 Mar 2008 23:15:06 +0000, Andrew Carton wrote:
>>>> While using AJDT - is it possible to change the AST of an AspectJ
>>>> file and then rewrite it back to a file. The JDT has a
>>>> "recordModification" section that shows you how to do this - but the
>>>> AJCompilationUnit doesn't support this? I see in there is a
>>>> AjASTRewrite that does something like this in the source file. I
>>>> wonder if anyone has any success or knows how to do this.
>>>
>>> I just asked a related question on the aspectj-dev mailinglist. You can
>>> view the thread here:
>>> http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02330.ht ml
>>>
>>> There is also some information included in the docs module of the
>>> AspectJ
>>> project (docs/developer/compiler-weaver/index.html). Let me know, if you
>>> find something useful.
>>>
Re: Aspect Rewrite [message #71773 is a reply to message #71443] Thu, 12 June 2008 16:04 Go to previous message
Andrew Clement is currently offline Andrew ClementFriend
Messages: 162
Registered: July 2009
Senior Member
Just going back over some older posts now I've remembered this newsgroup
is here.

Yes, the AST support is incomplete. It has been an ongoing effort,
currently we have two bugs open to track changes to it:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=168714 ('AST needs more
work')
https://bugs.eclipse.org/bugs/show_bug.cgi?id=146528 ('AST support for
resolving type bindings')

In the past we have integrated changes from the community:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=228633
https://bugs.eclipse.org/bugs/show_bug.cgi?id=110465

My hands are full ensuring the compiler is as good as it can be, so the
AST does get neglected I am afraid, but once functionality is added (with
associated tests), I will maintain it.

Andy.
Re: Aspect Rewrite [message #596773 is a reply to message #71366] Wed, 19 March 2008 02:51 Go to previous message
Martin Görg is currently offline Martin GörgFriend
Messages: 10
Registered: July 2009
Junior Member
On Tue, 18 Mar 2008 23:15:06 +0000, Andrew Carton wrote:
> While using AJDT - is it possible to change the AST of an AspectJ file
> and then rewrite it back to a file. The JDT has a "recordModification"
> section that shows you how to do this - but the AJCompilationUnit
> doesn't support this? I see in there is a AjASTRewrite that does
> something like this in the source file. I wonder if anyone has any
> success or knows how to do this.

I just asked a related question on the aspectj-dev mailinglist. You can
view the thread here:
http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02330.ht ml

There is also some information included in the docs module of the AspectJ
project (docs/developer/compiler-weaver/index.html). Let me know, if you
find something useful.

--
Cheers
Martin
Re: Aspect Rewrite [message #596780 is a reply to message #71386] Wed, 19 March 2008 12:14 Go to previous message
Andrew Carton is currently offline Andrew CartonFriend
Messages: 104
Registered: July 2009
Senior Member
Hi Martin + any other people reading this,

Yes - I wish to rewrite a small modification of an aspect from it's AST
back to it's original file.

I have had partial success with visitors etc but the "writing back"
still doesn't work.

Here is my example code below:

Just a note on the code - The ChangeSomethingVisitor extends the
AjASTVisitor and modifies something in the AST (specific to the AspectJ
class from the AJCompilationUnit).

To make sure the modification is changed I use the AjNaiveASTFlattener
visitor which just makes a kind of debug information about the aspect.
The change is successfully implemented.

However, when I attempt to use the AjASTRewrite to modify the tree and
get the TextEdits - it appears to be 0. I don't understand why!

So I'm stumped really - I read a lot and did a lot to get to this stage
- it really is a black art (The documentation/API is really poor and
there are not many posts about how to do this kind of thing!) - is it
something I did wrong or is it just the libraries aren't fully
implemented? I'd really appreciate some help on this!

Thanks,
Andrew.

currentCompUnit is an AJCompilationUnit

if (currentCompUnit == null)
return;


currentCompUnit.requestOriginalContentMode();

String contents = "";

try {
contents = currentCompUnit.getBuffer().getContents();

} catch (org.eclipse.jdt.core.JavaModelException e)
{
e.printStackTrace();

}
currentCompUnit.discardOriginalContentMode();

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setCompilerOptions(new HashMap());
parser.setSource(contents.toCharArray());
Document doc = new Document(contents);
CompilationUnit c = (CompilationUnit) parser.createAST(null);
c.recordModifications();
AjASTRewrite ajrewrite = AjASTRewrite.create(c.getAST());
ChangeSomethingVisitor visit = new ChangeSomethingVisitor();

AjNaiveASTFlattener visit2 = new AjNaiveASTFlattener();
c.accept(visit);
c.accept(visit2);
System.err.println(visit2.getResult());
TextEdit edits =
ajrewrite.rewriteAST(doc,currentCompUnit.getJavaProject().ge tOptions(true));
System.err.println(edits.getChildrenSize());
edits.apply(doc);
System.out.println(doc.get());

Ar 19/03/2008 02:51, Scríobh Martin Görg:
> On Tue, 18 Mar 2008 23:15:06 +0000, Andrew Carton wrote:
>> While using AJDT - is it possible to change the AST of an AspectJ file
>> and then rewrite it back to a file. The JDT has a "recordModification"
>> section that shows you how to do this - but the AJCompilationUnit
>> doesn't support this? I see in there is a AjASTRewrite that does
>> something like this in the source file. I wonder if anyone has any
>> success or knows how to do this.
>
> I just asked a related question on the aspectj-dev mailinglist. You can
> view the thread here:
> http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02330.ht ml
>
> There is also some information included in the docs module of the AspectJ
> project (docs/developer/compiler-weaver/index.html). Let me know, if you
> find something useful.
>
Re: Aspect Rewrite [message #596786 is a reply to message #71405] Wed, 19 March 2008 12:43 Go to previous message
Andrew Carton is currently offline Andrew CartonFriend
Messages: 104
Registered: July 2009
Senior Member
Ok I'm reading this
http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse. jdt.doc.isv/guide/jdt_api_manip.htm

and I think I'm incorrect in using the AjRewriter

However, just going on the CompilerUnit recordModifications / rewrite alone

When I attempt record the modifications and then change the AST and
attempt a rewrite - I get an error message in the console saying

"Warning: AspectJ type declaration factory class not found on classpath"

I had a peek in the source and this is the code:

public final class AjASTRewriteAnalyzer extends AjASTVisitor {

private static final String AJ_ASTREWRITEANALYZER_FACTORY
" org.eclipse.ajdt.core.dom.rewrite.AjASTRewriteAnalyzerFactor y ";

private static IASTRewriteAnalyzerFactory astRewriteAnalyzerFactory;

static {
try{
astRewriteAnalyzerFactory = (IASTRewriteAnalyzerFactory)
Class.forName(AJ_ASTREWRITEANALYZER_FACTORY).newInstance();
} catch (InstantiationException ex) {
throw new ExceptionInInitializerError(ex.getMessage());
} catch (IllegalAccessException ex) {
throw new ExceptionInInitializerError(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.err.println("Warning: AspectJ type declaration factory
class not found on classpath"); //$NON-NLS-1$
//throw new ExceptionInInitializerError(ex.getMessage());
}

I've added all the necessary jars in the extension file of my plugin and
I'm not sure why it isn't picking up this - because it is picking up
other classes in this jar file.

Thanks,
Andrew.


Ar 19/03/2008 12:14, Scríobh Andrew Carton:
> Hi Martin + any other people reading this,
>
> Yes - I wish to rewrite a small modification of an aspect from it's AST
> back to it's original file.
>
> I have had partial success with visitors etc but the "writing back"
> still doesn't work.
>
> Here is my example code below:
>
> Just a note on the code - The ChangeSomethingVisitor extends the
> AjASTVisitor and modifies something in the AST (specific to the AspectJ
> class from the AJCompilationUnit).
>
> To make sure the modification is changed I use the AjNaiveASTFlattener
> visitor which just makes a kind of debug information about the aspect.
> The change is successfully implemented.
>
> However, when I attempt to use the AjASTRewrite to modify the tree and
> get the TextEdits - it appears to be 0. I don't understand why!
>
> So I'm stumped really - I read a lot and did a lot to get to this stage
> - it really is a black art (The documentation/API is really poor and
> there are not many posts about how to do this kind of thing!) - is it
> something I did wrong or is it just the libraries aren't fully
> implemented? I'd really appreciate some help on this!
>
> Thanks,
> Andrew.
>
> currentCompUnit is an AJCompilationUnit
>
> if (currentCompUnit == null)
> return;
>
>
> currentCompUnit.requestOriginalContentMode();
>
> String contents = "";
>
> try {
> contents = currentCompUnit.getBuffer().getContents();
>
> } catch (org.eclipse.jdt.core.JavaModelException e)
> {
> e.printStackTrace();
>
> }
> currentCompUnit.discardOriginalContentMode();
>
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setCompilerOptions(new HashMap());
> parser.setSource(contents.toCharArray());
> Document doc = new Document(contents);
> CompilationUnit c = (CompilationUnit) parser.createAST(null);
> c.recordModifications();
> AjASTRewrite ajrewrite = AjASTRewrite.create(c.getAST());
> ChangeSomethingVisitor visit = new ChangeSomethingVisitor();
>
> AjNaiveASTFlattener visit2 = new AjNaiveASTFlattener();
> c.accept(visit);
> c.accept(visit2);
> System.err.println(visit2.getResult());
> TextEdit edits =
> ajrewrite.rewriteAST(doc,currentCompUnit.getJavaProject().ge tOptions(true));
>
> System.err.println(edits.getChildrenSize());
> edits.apply(doc);
> System.out.println(doc.get());
>
> Ar 19/03/2008 02:51, Scríobh Martin Görg:
>> On Tue, 18 Mar 2008 23:15:06 +0000, Andrew Carton wrote:
>>> While using AJDT - is it possible to change the AST of an AspectJ
>>> file and then rewrite it back to a file. The JDT has a
>>> "recordModification" section that shows you how to do this - but the
>>> AJCompilationUnit doesn't support this? I see in there is a
>>> AjASTRewrite that does something like this in the source file. I
>>> wonder if anyone has any success or knows how to do this.
>>
>> I just asked a related question on the aspectj-dev mailinglist. You can
>> view the thread here:
>> http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02330.ht ml
>>
>> There is also some information included in the docs module of the AspectJ
>> project (docs/developer/compiler-weaver/index.html). Let me know, if you
>> find something useful.
>>
Re: Aspect Rewrite [message #596794 is a reply to message #71424] Thu, 20 March 2008 16:28 Go to previous message
Andrew Carton is currently offline Andrew CartonFriend
Messages: 104
Registered: July 2009
Senior Member
Ok I have had limited success with rewriting. However, I can only ever
get it working with SimpleTypes.

For example when I try duplicating a PointcutDeclaration and rewriting
it with the AjASTRewriter it just comes out as empty. I have tested the
copy of the PointcutDeclaration and the settings are not empty...

AjASTRewrite ajrewrite =....

AspectDeclaration ad = ...

PointcutDeclaration[] pointcuts = ad.getPointcuts();
PointcutDeclaration old = pointcuts[0];
PointcutDeclaration newd = (PointcutDeclartion)
PointcutDeclaration.copySubtree(ast, old);


ajrewrite.replace(old, newd, null);

There is 1 rewrite but the subtree is not copied in the TextEdit when I
save the file.

It is just ";" - an empty semicolon.

Does anyone have any ideas - is the implementation unfinished?

Thanks,
andrew.

Ar 19/03/2008 12:43, Scríobh Andrew Carton:
> Ok I'm reading this
> http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse. jdt.doc.isv/guide/jdt_api_manip.htm
>
>
> and I think I'm incorrect in using the AjRewriter
>
> However, just going on the CompilerUnit recordModifications / rewrite alone
>
> When I attempt record the modifications and then change the AST and
> attempt a rewrite - I get an error message in the console saying
>
> "Warning: AspectJ type declaration factory class not found on classpath"
>
> I had a peek in the source and this is the code:
>
> public final class AjASTRewriteAnalyzer extends AjASTVisitor {
>
> private static final String AJ_ASTREWRITEANALYZER_FACTORY
> " org.eclipse.ajdt.core.dom.rewrite.AjASTRewriteAnalyzerFactor y ";
>
> private static IASTRewriteAnalyzerFactory astRewriteAnalyzerFactory;
>
> static {
> try{
> astRewriteAnalyzerFactory = (IASTRewriteAnalyzerFactory)
> Class.forName(AJ_ASTREWRITEANALYZER_FACTORY).newInstance();
> } catch (InstantiationException ex) {
> throw new ExceptionInInitializerError(ex.getMessage());
> } catch (IllegalAccessException ex) {
> throw new ExceptionInInitializerError(ex.getMessage());
> } catch (ClassNotFoundException ex) {
> System.err.println("Warning: AspectJ type declaration factory
> class not found on classpath"); //$NON-NLS-1$
> //throw new ExceptionInInitializerError(ex.getMessage());
> }
>
> I've added all the necessary jars in the extension file of my plugin and
> I'm not sure why it isn't picking up this - because it is picking up
> other classes in this jar file.
>
> Thanks,
> Andrew.
>
>
> Ar 19/03/2008 12:14, Scríobh Andrew Carton:
>> Hi Martin + any other people reading this,
>>
>> Yes - I wish to rewrite a small modification of an aspect from it's
>> AST back to it's original file.
>>
>> I have had partial success with visitors etc but the "writing back"
>> still doesn't work.
>>
>> Here is my example code below:
>>
>> Just a note on the code - The ChangeSomethingVisitor extends the
>> AjASTVisitor and modifies something in the AST (specific to the
>> AspectJ class from the AJCompilationUnit).
>>
>> To make sure the modification is changed I use the AjNaiveASTFlattener
>> visitor which just makes a kind of debug information about the aspect.
>> The change is successfully implemented.
>>
>> However, when I attempt to use the AjASTRewrite to modify the tree and
>> get the TextEdits - it appears to be 0. I don't understand why!
>>
>> So I'm stumped really - I read a lot and did a lot to get to this
>> stage - it really is a black art (The documentation/API is really poor
>> and there are not many posts about how to do this kind of thing!) - is
>> it something I did wrong or is it just the libraries aren't fully
>> implemented? I'd really appreciate some help on this!
>>
>> Thanks,
>> Andrew.
>>
>> currentCompUnit is an AJCompilationUnit
>>
>> if (currentCompUnit == null)
>> return;
>>
>> currentCompUnit.requestOriginalContentMode();
>>
>> String contents = "";
>>
>> try {
>> contents = currentCompUnit.getBuffer().getContents();
>>
>> } catch (org.eclipse.jdt.core.JavaModelException e)
>> {
>> e.printStackTrace();
>>
>> }
>> currentCompUnit.discardOriginalContentMode();
>>
>> ASTParser parser = ASTParser.newParser(AST.JLS3);
>> parser.setCompilerOptions(new HashMap());
>> parser.setSource(contents.toCharArray());
>> Document doc = new Document(contents);
>> CompilationUnit c = (CompilationUnit) parser.createAST(null);
>> c.recordModifications();
>> AjASTRewrite ajrewrite = AjASTRewrite.create(c.getAST());
>> ChangeSomethingVisitor visit = new ChangeSomethingVisitor();
>>
>> AjNaiveASTFlattener visit2 = new AjNaiveASTFlattener();
>> c.accept(visit);
>> c.accept(visit2);
>> System.err.println(visit2.getResult());
>> TextEdit edits =
>> ajrewrite.rewriteAST(doc,currentCompUnit.getJavaProject().ge tOptions(true));
>>
>> System.err.println(edits.getChildrenSize());
>> edits.apply(doc);
>> System.out.println(doc.get());
>>
>> Ar 19/03/2008 02:51, Scríobh Martin Görg:
>>> On Tue, 18 Mar 2008 23:15:06 +0000, Andrew Carton wrote:
>>>> While using AJDT - is it possible to change the AST of an AspectJ
>>>> file and then rewrite it back to a file. The JDT has a
>>>> "recordModification" section that shows you how to do this - but the
>>>> AJCompilationUnit doesn't support this? I see in there is a
>>>> AjASTRewrite that does something like this in the source file. I
>>>> wonder if anyone has any success or knows how to do this.
>>>
>>> I just asked a related question on the aspectj-dev mailinglist. You can
>>> view the thread here:
>>> http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02330.ht ml
>>>
>>> There is also some information included in the docs module of the
>>> AspectJ
>>> project (docs/developer/compiler-weaver/index.html). Let me know, if you
>>> find something useful.
>>>
Re: Aspect Rewrite [message #596992 is a reply to message #71443] Thu, 12 June 2008 16:04 Go to previous message
Andrew Clement is currently offline Andrew ClementFriend
Messages: 162
Registered: July 2009
Senior Member
Just going back over some older posts now I've remembered this newsgroup
is here.

Yes, the AST support is incomplete. It has been an ongoing effort,
currently we have two bugs open to track changes to it:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=168714 ('AST needs more
work')
https://bugs.eclipse.org/bugs/show_bug.cgi?id=146528 ('AST support for
resolving type bindings')

In the past we have integrated changes from the community:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=228633
https://bugs.eclipse.org/bugs/show_bug.cgi?id=110465

My hands are full ensuring the compiler is as good as it can be, so the
AST does get neglected I am afraid, but once functionality is added (with
associated tests), I will maintain it.

Andy.
Previous Topic:AJDT source checkout and errors in modules
Next Topic:Error opening AspectJ source file...
Goto Forum:
  


Current Time: Sun Nov 23 10:14:15 GMT 2014

Powered by FUDForum. Page generated in 0.04703 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software