How do I acquire AST of an .aj source file? [message #67311] |
Wed, 18 October 2006 18:08  |
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 #67339 is a reply to message #67324] |
Thu, 19 October 2006 18:08  |
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 #595108 is a reply to message #67324] |
Thu, 19 October 2006 18:08  |
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
|
|
|
Powered by
FUDForum. Page generated in 0.24203 seconds