Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Howto use AST to declare a local variable
Howto use AST to declare a local variable [message #251679] Fri, 29 February 2008 00:57 Go to next message
Eclipse UserFriend
Originally posted by: rahulrevo.gmail.com

Hi

I am trying to create Java source programatically using the dom.AST
classes. I was unable to get the AST code required for a local variable
declaration. Like ..

double bar = 1.1;

VariableDeclarationFragment allows me to set the name to "bar" and using
an Assignment expression I can create the rest of the expression. I was
not able to get how to set the type of double to the variable and needed
help on this to get the complete expression.

Thanks

Rahul
Re: Howto use AST to declare a local variable [message #251688 is a reply to message #251679] Fri, 29 February 2008 02:01 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: topgun.in.gmail.com

Rahul,

> not able to get how to set the type of double to the variable and needed

Use SingleVaraibleDeclaration to create the variable.
It allows you to set the variable type through the method
setType(ast.newPrimitveType(PrimitiveType.DOUBLE));
where 'ast' is your AST.

Thanks,
Pushkar
Re: Howto use AST to declare a local variable [message #251707 is a reply to message #251679] Fri, 29 February 2008 10:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

Rahul Revo wrote:
> Hi
>
> I am trying to create Java source programatically using the dom.AST
> classes. I was unable to get the AST code required for a local variable
> declaration. Like ..
>
> double bar = 1.1;
>
> VariableDeclarationFragment allows me to set the name to "bar" and using
> an Assignment expression I can create the rest of the expression. I was
> not able to get how to set the type of double to the variable and needed
> help on this to get the complete expression.
>
> Thanks
>
> Rahul

You can create a VariableDeclarationStatement, passing your current
fragment into the constructor. The type can then be set for the
VariableDeclarationStatement.

Remember that Java allows you to have a declaration of the form
double foo=1.1, bar=2.2;

which is the reason for the separation of the fragment -- there might be
multiple fragments within a declaration statement.

Mike
Re: Howto use AST to declare a local variable [message #251731 is a reply to message #251707] Fri, 29 February 2008 13:54 Go to previous message
Eclipse UserFriend
Originally posted by: rahul.gmail.com

Mike Yawn wrote:
> Rahul Revo wrote:
>> double bar = 1.1;
>>
>> VariableDeclarationFragment allows me to set the name to "bar" and
>> using an Assignment expression I can create the rest of the
>> expression. I was not able to get how to set the type of double to the
>> variable and needed help on this to get the complete expression.
>>
> You can create a VariableDeclarationStatement, passing your current
> fragment into the constructor. The type can then be set for the
> VariableDeclarationStatement.
>
> Remember that Java allows you to have a declaration of the form
> double foo=1.1, bar=2.2;
>
> which is the reason for the separation of the fragment -- there might be
> multiple fragments within a declaration statement.

Thanks. That was what I was looking for.

Rahul
Previous Topic:project setup for a client program which use two jar files
Next Topic:pointing to javadoc using classpath variables
Goto Forum:
  


Current Time: Sat Apr 19 14:12:58 EDT 2025

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

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

Back to the top