Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AJDT » How do I acquire AST of an .aj source file?
How do I acquire AST of an .aj source file? [message #67311] Wed, 18 October 2006 22:08 Go to next message
Eclipse User
Originally posted by: Rafal.Krzewski.caltha.pl

Hello,

I have a question about AJDT (or perhaps AJDE) API usage. I need to get
a use it to collect information about binary that get compiled from a
source file, and also source information for a binary type sufficient to
open the relevant source in an editor.

Here is a method that I use for parsing Java sources:

public void parseSource( final IFile sourceFile, final Map
sourceToBinary, final Map binaryToSource, IProgressMonitor monitor ) {
ASTParser parser = ASTParser.newParser( AST.JLS3 );
ICompilationUnit source = ( ICompilationUnit ) JavaCore.create(
sourceFile );
parser.setSource( source );
parser.setResolveBindings( true );
ASTNode ast = parser.createAST( monitor );
ast.accept( new ASTVisitor() {
public boolean visit( TypeDeclaration node ) {
registerType( node.resolveBinding() );
return true;
}

public boolean visit( AnonymousClassDeclaration node ) {
registerType( node.resolveBinding() );
return true;
}

private void registerType( ITypeBinding binding ) {
Set binarySet = ( Set ) sourceToBinary.get(
sourceFile.getFullPath() );
if ( binarySet == null ) {
binarySet = new HashSet();
sourceToBinary.put( sourceFile.getFullPath(), binarySet );
}
binarySet.add( binding.getBinaryName() );
binaryToSource.put( binding.getBinaryName(),
binding.getJavaElement().getHandleIdentifier() );
}
} );
}

I was trying to do the same for AspectJ sources but without success. My
first guess was flipping the imports from org.eclipse.ajdt to
org.aspectj.org.eclipse.ajdt but it didn't work - I got a
ClassDefNotFound error without detail message inside JavaCore.create(..)
call, presumably during JavaModelManager.<clinit>. Weird, isn't it?

My second attempt involved AJCompilationUnitManager:

AJCompilationUnit source =
AJCompilationUnitManager.INSTANCE.getAJCompilationUnit( sourceFile );
parser.setCompilerOptions( source.getJavaProject().getOptions( true ) );
parser.setSource( source.getContents() );

But it's no good because the bindings don't get resolved. According to
my limited knowledge of JDT it is probably because
parser.setSource(ICompilationUnit) nor parser.setProject(IJavaProject)
were not called. I couldn't to that since AJCompilationUnit implements
ICompilationUnit from org.eclipse.jdt.core and not from
org.aspectj.org.eclipse.jdt.core

I'll be happy to do my own source digging, but please show me the
direction :)

Thanks in advance,
RafaƂ
Re: How do I acquire AST of an .aj source file? [message #67324 is a reply to message #67311] Thu, 19 October 2006 08:10 Go to previous messageGo to next message
Eclipse User
Originally posted by: hawkinsh.uk.ibm.com

Hi Rafa,

You want to do something like:

ASTParser parser = ASTParser.newParser(AST.JLS3);
ICompilationUnit source = ( ICompilationUnit )
org.eclipse.jdt.core.JavaCore.create(
sourceFile);
parser.setSource(source);
CompilationUnit cu = (CompilationUnit) parser.createAST(monitor);
cu.accept(new AjASTVisitor() {...});

The AjASTVisitor and ASTParser are in the
org.aspectj.org.eclipse.jdt.core.dom package. Also note that the AST that
is returned from cu.getAST() is an AjAST (which is also in the
org.aspectj.org.eclipse.jdt.core.dom package).

There have been a couple of bugs/enhancements opened regarding the AspectJ
AST which may have some other examples:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=88861 and
https://bugs.eclipse.org/bugs/show_bug.cgi?id=110465.

Hope this helps,

Helen

PS. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=119017 for info
about org.aspectj.org.eclipse.jdt.core.JavaCore.create(..)
Re: How do I acquire AST of an .aj source file? [message #67339 is a reply to message #67324] Thu, 19 October 2006 22:08 Go to previous message
Eclipse User
Originally posted by: Rafal.Krzewski.caltha.pl

Helen,

thanks for the tips. Unfortunately I weren't able to go much further:

> ASTParser parser = ASTParser.newParser(AST.JLS3);
> ICompilationUnit source = ( ICompilationUnit )
> org.eclipse.jdt.core.JavaCore.create(sourceFile);
> parser.setSource(source);

There's a compilation error on the last included statement -
ICompilationUnit type of source variable is from JDT, but
ICompilationUnit argument of setSource method is from AJDE compiler shadow.

The examples in the bugs you mentioned use
parser.setCompilerOptions(new HashMap());
parser.setSource(source.getSource().toCharArray());
And with this kind of setup the bindings are not getting resolved.

It turns out that there is a bug reported on the issue of bindings
resolution: https://bugs.eclipse.org/bugs/show_bug.cgi?id=146528
that seems to depend on
https://bugs.eclipse.org/bugs/show_bug.cgi?id=119017
(JavaCore.create(..)) bug. The latter seems to depend on 3.2 compiler
integration (which is a hard nut to crack, as I read elsewhere) so
things are not looking very optimistic.

Is there any other approach on the task of determining .aj <-> .class
relationships within an AJDT enabled project that I could try?

thanks,
Rafal
Re: How do I acquire AST of an .aj source file? [message #595101 is a reply to message #67311] Thu, 19 October 2006 08:10 Go to previous message
Eclipse User
Originally posted by: hawkinsh.uk.ibm.com

Hi Rafa,

You want to do something like:

ASTParser parser = ASTParser.newParser(AST.JLS3);
ICompilationUnit source = ( ICompilationUnit )
org.eclipse.jdt.core.JavaCore.create(
sourceFile);
parser.setSource(source);
CompilationUnit cu = (CompilationUnit) parser.createAST(monitor);
cu.accept(new AjASTVisitor() {...});

The AjASTVisitor and ASTParser are in the
org.aspectj.org.eclipse.jdt.core.dom package. Also note that the AST that
is returned from cu.getAST() is an AjAST (which is also in the
org.aspectj.org.eclipse.jdt.core.dom package).

There have been a couple of bugs/enhancements opened regarding the AspectJ
AST which may have some other examples:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=88861 and
https://bugs.eclipse.org/bugs/show_bug.cgi?id=110465

Hope this helps,

Helen

PS. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=119017 for info
about org.aspectj.org.eclipse.jdt.core.JavaCore.create(..)
Re: How do I acquire AST of an .aj source file? [message #595108 is a reply to message #67324] Thu, 19 October 2006 22:08 Go to previous message
Eclipse User
Originally posted by: Rafal.Krzewski.caltha.pl

Helen,

thanks for the tips. Unfortunately I weren't able to go much further:

> ASTParser parser = ASTParser.newParser(AST.JLS3);
> ICompilationUnit source = ( ICompilationUnit )
> org.eclipse.jdt.core.JavaCore.create(sourceFile);
> parser.setSource(source);

There's a compilation error on the last included statement -
ICompilationUnit type of source variable is from JDT, but
ICompilationUnit argument of setSource method is from AJDE compiler shadow.

The examples in the bugs you mentioned use
parser.setCompilerOptions(new HashMap());
parser.setSource(source.getSource().toCharArray());
And with this kind of setup the bindings are not getting resolved.

It turns out that there is a bug reported on the issue of bindings
resolution: https://bugs.eclipse.org/bugs/show_bug.cgi?id=146528
that seems to depend on
https://bugs.eclipse.org/bugs/show_bug.cgi?id=119017
(JavaCore.create(..)) bug. The latter seems to depend on 3.2 compiler
integration (which is a hard nut to crack, as I read elsewhere) so
things are not looking very optimistic.

Is there any other approach on the task of determining .aj <-> .class
relationships within an AJDT enabled project that I could try?

thanks,
Rafal
Previous Topic:How do I acquire AST of an .aj source file?
Next Topic:AspectJ and RMI problem
Goto Forum:
  


Current Time: Sun Sep 21 12:27:12 GMT 2014

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

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