Hi, I'm writing a method which converts java static blocks inside a class
declaration into a new static method. The body of this method is composed of:
(1) an if statement which checks if the method has been already called
(2) pieces of code of static blocks
I have to add a bool static attribute in order to write (1).
To add a static attribute I perform the following steps:
1. creating a FieldDeclaration using createFieldDeclaration(TypeReference, Identifier) of factory class
clazz is the ClassDeclaration related to the class where I want to add the
static attribute.
When the program executes I receive this execption when it tries to perform
step (4):
Unexpected error: java.lang.NullPointerException. End.
java.lang.NullPointerException
at recoder.service.DefaultSourceInfo.getVariable(DefaultSourceInfo.java:1656)
at recoder.service.DefaultSourceInfo.getField(DefaultSourceInfo.java:1756)
at recoder.service.DefaultSourceInfo.getVariable(DefaultSourceInfo.java:1796)
at recoder.service.DefaultCrossReferenceSourceInfo.analyzeReferences(DefaultCr
ossReferenceSourceInfo.java:599)
at recoder.service.DefaultCrossReferenceSourceInfo.reset(DefaultCrossReference
SourceInfo.java:705)
at recoder.service.DefaultCrossReferenceSourceInfo.modelChanged(DefaultCrossRe
ferenceSourceInfo.java:214)
at recoder.service.ChangeHistory.updateModel(ChangeHistory.java:500)
at ....IClassMethodFactory$StaticBlockAggregator.createMethod(IClassMethodFact
ory.java:147)
I'm doing something wrong but I don't understand what :-) Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
it looks to me that parent links are not set. All program elements need to
have a link to their parent NonTerminalProgramElement set.
Four ways of doing it:
1. call "makeParentRoleValid" on everything where you add (I recommend that)
2. call set(Parent|StatementContainer) etc. for each element that has a new parent (not recommend, tedious and methods setting the parents have different names in different classes)
3. call CompilationUnit.makeAllParentRolesValid() - this will set the parent links for the entire AST. Good for testing if that's the case, but not necessarily the most performant way.
4. use Transformation.(doAttach|doReplace) - they should take care of setting proper parent links.
Regards,
Tobias
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I added only an If and some CopyAssignment statement to method body. Every
time an If or a CopyAssignment is created the method makeAllParentRolesValid()
is called on it.
Using this code I can't still add methods to certain classes receiving an
error during updateModel call.
Thanks a lot :-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I found that the error related to the model update depends on the following
TypingException:
inner type has type arguments, but the outer does not.
which is thrown after the following statement has been analyzed:
for (Map.Entry<String, Object> e : namedList.entrySet()) { ...
namedList is correctly declared previously:
protected Map<String, Object> namedList = new LinkedHashMap<String, Object>();
The TypingException is thrown inside the checkRawTypeOk method,
recoder.service.SemanticsChecker.
if (!trType.isStatic() && rpType instanceof ErasedType) {
errorHandler.reportError(
new TypingException(
"inner type has type arguments, but the outer does not.",null));
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, I'm writing a method which converts java static blocks inside a class
declaration into a new static method. The body of this method is composed of:
(1) an if statement which checks if the method has been already called
(2) pieces of code of static blocks
I have to add a bool static attribute in order to write (1).
To add a static attribute I perform the following steps:
1. creating a FieldDeclaration using createFieldDeclaration(TypeReference, Identifier) of factory class
FieldDeclaration flag = factory.createFieldDeclaration(boolType, new
Identifier("static_initializer_flag"));
or
changeHistory.attached(flag);
changeHistory.commit();
changeHistory.updateModel();
clazz is the ClassDeclaration related to the class where I want to add the
static attribute.
When the program executes I receive this execption when it tries to perform
step (4):
I'm doing something wrong but I don't understand what :-) Thanks.
Hej,
it looks to me that parent links are not set. All program elements need to
have a link to their parent NonTerminalProgramElement set.
Four ways of doing it:
1. call "makeParentRoleValid" on everything where you add (I recommend that)
2. call set(Parent|StatementContainer) etc. for each element that has a new parent (not recommend, tedious and methods setting the parents have different names in different classes)
3. call CompilationUnit.makeAllParentRolesValid() - this will set the parent links for the entire AST. Good for testing if that's the case, but not necessarily the most performant way.
4. use Transformation.(doAttach|doReplace) - they should take care of setting proper parent links.
Regards,
Tobias
I created a subclass, XALTransformation, of recoder.kit.Transformation which
implements the following methods:
Then I wrote the following code to add a method to a class:
I added only an If and some CopyAssignment statement to method body. Every
time an If or a CopyAssignment is created the method makeAllParentRolesValid()
is called on it.
Using this code I can't still add methods to certain classes receiving an
error during updateModel call.
Thanks a lot :-)
I found that the error related to the model update depends on the following
TypingException:
which is thrown after the following statement has been analyzed:
namedList is correctly declared previously:
The TypingException is thrown inside the checkRawTypeOk method,
recoder.service.SemanticsChecker.
It's a bug. Fixed in SVN / next release.
Thanks. :-)