Hi,
I've just implemented the initInstance() method, as requested by Courtney.
You can now do
JFieldVar myField = c.field(JMod.PRIVATE, String.class, "myField");
c.instanceInit().assign(JExpr._this().ref(myField),
JExpr.lit("myValue"));
To get:
class MyClass {
private String myField;
{
this.myField = "myValue";
}
}
There is one important improvement: I've used the JavaParser in the
unit test to check that the initializer is indeed generated. I think
it is a good approch to test the code generation an hopefully it can
be adopted in furuter fixes/improvements on the CodeModel as well.
Here's the testing code:
CompilationUnit compilationUnit = JavaParser.parse(bis, encoding);
TypeDeclaration typeDeclaration = compilationUnit.getTypes().get(0);
ClassOrInterfaceDeclaration classDeclaration =
(ClassOrInterfaceDeclaration) typeDeclaration;
final InitializerDeclaration initializerDeclaration =
(InitializerDeclaration) classDeclaration
.getMembers().get(1);
assertNotNull(initializerDeclaration);
Just parse and check if expected things are on their places.
Bye,
/lexi
On Sat, May 12, 2012 at 4:30 PM, Courtney Falk <courtney.falk_at_gmail.com> wrote:
> There should be a new issue:
>
> Instance Variable Initialization
> --------------------------------
>
> Key: CODEMODEL-10
> URL:http://java.net/jira/browse/CODEMODEL-10
> Project: codemodel
> Issue Type: New Feature
> Components: codemodel
> Affects Versions: 2.5
> Reporter: InfiniteAutomata
>
>
>
>
> On 05/12/2012 01:53 AM, Aleksei Valikov wrote:
>>
>> Hi,
>>
>>> Certainly. I've never filed an issue before, but I'd be glad to to
>>> advance the code base. Do you have a URL to the issue submission page
>>> perhaps?
>>
>> It's just java.net JIRA:
>>
>> http://java.net/jira/browse/CODEMODEL/
>>
>> Please let me know the issue id then.
>>
>> You can use the instanceInit() method.
>>
>> Bye,
>> /lexi
>
>