How can I add a FieldDeclaration to a ClassType? I looked at the ProgramFactory, but the createFieldDeclaration doesn't seem to take an argument for where to attach it to. In my case, I have a field f, which I want to replace with n other fields.
And is there a way to write the transformed source code out even if the model check fails? Say I created some situation where the types in one statement do not match, can I somehow still write that code to disk?
Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1) First, create the FieldDeclaration (e.g., using the program factory). Then use one of the recoder.kit.Transformation.doAttach() methods, in your case doAttach(MemberDeclaration, TypeDeclaration, int). The "int" is the index where to add the member, with "0" it will be the first one in the type declarations' members.
NOTE: you have to report all changes to the ChangeHistory:
serviceConfiguration.getChangeHistory().attached(member, type);
Otherwise the model will get corrupted on a model update. You also need to do this when you delete something:
getChangeHistory().detached(ProgramElement pe, int position) where you must set "position" to the detached element's former "childPositionCode". Prior to detaching, call:
pe.getASTParent().getChildPositionCode(pe);
If you don't do this (and set position to -1), the Undo-function will not be available. Therefore, not recommended ;-)
Ok, if you want it a little bit easier, look at recoder.kit.transformation.AppendMember ;-) The class is marked as "deprecated", but don't worry. It works just fine, unless you try to declare, e.g., duplicate fields (fields with the same name). Call it like this:
new AppendMember(serviceConfiguration, true, newMember, newMemberParent).execute();
2) Should be possible. Write your own ErrorHandler, by extending DefaultErrorHandler. Overwrite "modelUpdated()" with an empty method body (e.g., not bailing out), and set the errorTheshold to 9999 or so. Then, use it by calling serviceConfiguration.getProjectSettings().setErrorHandler()
/Tobias
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I have accumulated more questions :-)
How can I add a FieldDeclaration to a ClassType? I looked at the ProgramFactory, but the createFieldDeclaration doesn't seem to take an argument for where to attach it to. In my case, I have a field f, which I want to replace with n other fields.
And is there a way to write the transformed source code out even if the model check fails? Say I created some situation where the types in one statement do not match, can I somehow still write that code to disk?
Thanks!
Hej!
1) First, create the FieldDeclaration (e.g., using the program factory). Then use one of the recoder.kit.Transformation.doAttach() methods, in your case doAttach(MemberDeclaration, TypeDeclaration, int). The "int" is the index where to add the member, with "0" it will be the first one in the type declarations' members.
NOTE: you have to report all changes to the ChangeHistory:
serviceConfiguration.getChangeHistory().attached(member, type);
Otherwise the model will get corrupted on a model update. You also need to do this when you delete something:
getChangeHistory().detached(ProgramElement pe, int position) where you must set "position" to the detached element's former "childPositionCode". Prior to detaching, call:
pe.getASTParent().getChildPositionCode(pe);
If you don't do this (and set position to -1), the Undo-function will not be available. Therefore, not recommended ;-)
Ok, if you want it a little bit easier, look at recoder.kit.transformation.AppendMember ;-) The class is marked as "deprecated", but don't worry. It works just fine, unless you try to declare, e.g., duplicate fields (fields with the same name). Call it like this:
new AppendMember(serviceConfiguration, true, newMember, newMemberParent).execute();
2) Should be possible. Write your own ErrorHandler, by extending DefaultErrorHandler. Overwrite "modelUpdated()" with an empty method body (e.g., not bailing out), and set the errorTheshold to 9999 or so. Then, use it by calling serviceConfiguration.getProjectSettings().setErrorHandler()
/Tobias
Just a quick update, both hints worked like a charm.
I ended up just implementing the ErrorHandler interface instead of extending the DefaultErrorHandler, and it writes the file just as I expected.
Thanks again Tobias!
~David