Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » How to create and save an AST??
How to create and save an AST?? [message #255086] Wed, 16 July 2008 11:02 Go to next message
Eclipse UserFriend
Yes I know this question was asked so often.
No there is no sufficient response for this.

I read this:
http://help.eclipse.org/ganymede/topic/org.eclipse.jdt.doc.i sv/guide/jdt_api_manip.htm

My not working example is:

this.project is a java project
this.default_package is a package identifier

NullProgressMonitor monitor = new NullProgressMonitor();
IPackageFragmentRoot root = this.project.getAllPackageFragmentRoots()[0];
IPackageFragment fragment =
root.createPackageFragment(this.default_package, true, monitor);
// create a cu
ICompilationUnit cu = fragment.createCompilationUnit("test.java", "",
true, monitor);
Document document = new Document(cu.getSource());
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(cu);
// get the ast cu
CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);
unit.recordModifications();
// get the ast
AST ast = unit.getAST();
// create a package
PackageDeclaration pack = ast.newPackageDeclaration();
pack.setName(ast.newName(this.default_package));
unit.setPackage(pack);
// create a type declaration
TypeDeclaration type = ast.newTypeDeclaration();
type.setName(type.getAST().newSimpleName("test"));
unit.types().add(type);
// get the edits
TextEdit edits = unit.rewrite(document,
cu.getJavaProject().getOptions(true));
edits.apply(document);
cu.getBuffer().setContents(document.get());
cu.makeConsistent(monitor);

if you believe in the example in the documentation, this should work.
Nevertheless the produced file is empty.

Thanks

Guenther
Re: How to create and save an AST?? [message #255168 is a reply to message #255086] Tue, 22 July 2008 08:15 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

Could I please have a statement?
What's wrong with my code please?

Is there any real clean way to produce code, or is the JDT plugin not
foreseen for use outside of eclipse or in other plugins?

Thanks for any responses

Guenther

guenther schrieb:
> Yes I know this question was asked so often.
> No there is no sufficient response for this.
>
> I read this:
> http://help.eclipse.org/ganymede/topic/org.eclipse.jdt.doc.i sv/guide/jdt_api_manip.htm
>
>
> My not working example is:
>
> this.project is a java project
> this.default_package is a package identifier
>
> NullProgressMonitor monitor = new NullProgressMonitor();
> IPackageFragmentRoot root = this.project.getAllPackageFragmentRoots()[0];
> IPackageFragment fragment =
> root.createPackageFragment(this.default_package, true, monitor);
> // create a cu
> ICompilationUnit cu = fragment.createCompilationUnit("test.java", "",
> true, monitor);
> Document document = new Document(cu.getSource());
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setSource(cu);
> // get the ast cu
> CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);
> unit.recordModifications();
> // get the ast
> AST ast = unit.getAST();
> // create a package
> PackageDeclaration pack = ast.newPackageDeclaration();
> pack.setName(ast.newName(this.default_package));
> unit.setPackage(pack);
> // create a type declaration
> TypeDeclaration type = ast.newTypeDeclaration();
> type.setName(type.getAST().newSimpleName("test"));
> unit.types().add(type);
> // get the edits
> TextEdit edits = unit.rewrite(document,
> cu.getJavaProject().getOptions(true));
> edits.apply(document);
> cu.getBuffer().setContents(document.get());
> cu.makeConsistent(monitor);
>
> if you believe in the example in the documentation, this should work.
> Nevertheless the produced file is empty.
>
> Thanks
>
> Guenther
Re: How to create and save an AST?? [message #255278 is a reply to message #255168] Fri, 25 July 2008 07:02 Go to previous message
Eclipse UserFriend
Originally posted by: asterite.esperanto.org.ar

guenther a écrit :
> Hi,
>
> Could I please have a statement?
> What's wrong with my code please?
>
> Is there any real clean way to produce code, or is the JDT plugin not
> foreseen for use outside of eclipse or in other plugins?
>
> Thanks for any responses
>
> Guenther
>
> guenther schrieb:
>> Yes I know this question was asked so often.
>> No there is no sufficient response for this.
>>
>> I read this:
>> http://help.eclipse.org/ganymede/topic/org.eclipse.jdt.doc.i sv/guide/jdt_api_manip.htm
>>
>>
>> My not working example is:
>>
>> this.project is a java project
>> this.default_package is a package identifier
>>
>> NullProgressMonitor monitor = new NullProgressMonitor();
>> IPackageFragmentRoot root = this.project.getAllPackageFragmentRoots()[0];
>> IPackageFragment fragment =
>> root.createPackageFragment(this.default_package, true, monitor);
>> // create a cu
>> ICompilationUnit cu = fragment.createCompilationUnit("test.java", "",
>> true, monitor);
>> Document document = new Document(cu.getSource());
>> ASTParser parser = ASTParser.newParser(AST.JLS3);
>> parser.setSource(cu);
>> // get the ast cu
>> CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);
>> unit.recordModifications();
>> // get the ast
>> AST ast = unit.getAST();
>> // create a package
>> PackageDeclaration pack = ast.newPackageDeclaration();
>> pack.setName(ast.newName(this.default_package));
>> unit.setPackage(pack);
>> // create a type declaration
>> TypeDeclaration type = ast.newTypeDeclaration();
>> type.setName(type.getAST().newSimpleName("test"));
>> unit.types().add(type);
>> // get the edits
>> TextEdit edits = unit.rewrite(document,
>> cu.getJavaProject().getOptions(true));
>> edits.apply(document);
>> cu.getBuffer().setContents(document.get());
>> cu.makeConsistent(monitor);
>>
>> if you believe in the example in the documentation, this should work.
>> Nevertheless the produced file is empty.
>>
>> Thanks
>>
>> Guenther

Did you debug your code step by step to see what are the TextEdit you get?
Previous Topic:Conditional breakpoint for logging
Next Topic:easiest way to get multiple incremental javabuilders
Goto Forum:
  


Current Time: Wed May 07 12:25:32 EDT 2025

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

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

Back to the top