Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » static field initialization using AST(static field initialization using AST)
static field initialization using AST [message #780696] Thu, 19 January 2012 05:37 Go to next message
Syam Mohan is currently offline Syam MohanFriend
Messages: 3
Registered: January 2012
Junior Member
I need to create a static String variable with a default value (e.g. public static final String COUNTRY="INDIA";) using Eclipse AST. I'm able to create such a varibale using SingleVariableDecleration or FieldDecleration. But I'm not sure how to initialize the same using AST.

This is what I could implement==> public static final String COUNTRY;

This is what I could not implement==> public static final String COUNTRY="INDIA";

Please help me to know how can I do this. Thanks in advance,

Shyam
Re: static field initialization using AST [message #780708 is a reply to message #780696] Thu, 19 January 2012 07:16 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 545
Registered: July 2009
Senior Member
Already answered here -
http://stackoverflow.com/questions/8921230/static-field-initialization-using-ast/8922106#8922106

--
Deepak Azad
http://wiki.eclipse.org/JDT/FAQ
Re: static field initialization using AST [message #780777 is a reply to message #780708] Thu, 19 January 2012 10:55 Go to previous messageGo to next message
Syam Mohan is currently offline Syam MohanFriend
Messages: 3
Registered: January 2012
Junior Member
For the iterested candidates, I'm pasting the LOC here.

VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();

StringLiteral literal = ast.newStringLiteral();
literal.setLiteralValue("INDIA");

vdf.setInitializer(literal );
vdf.setName(ast.newSimpleName("COUNTRY"));

FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(vdf);
fieldDeclaration.setType(ast.newSimpleType(ast.newSimpleName("String")));

fieldDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
fieldDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
fieldDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));

type.bodyDeclarations().add(fieldDeclaration);
icon14.gif  Re: static field initialization using AST [message #780778 is a reply to message #780708] Thu, 19 January 2012 10:56 Go to previous message
Syam Mohan is currently offline Syam MohanFriend
Messages: 3
Registered: January 2012
Junior Member
Thanks a lot Deepak...
Previous Topic:Broken Eclipse M2 Plugin
Next Topic:brakepoints in java editor plugin
Goto Forum:
  


Current Time: Fri Dec 19 08:45:54 GMT 2014

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

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