I'm trying to generate a field or local variable with anonymous class initialization in eclipse using lombok.
If I run the code everything works fine but the problem is that eclipse keeps throwing exceptions in error log like this:
java.lang.IllegalArgumentException: startPos = -1 and length is 2.
This breaks the rule that length must be 0 if startPosition is negative. Affected Node:
{
}
at lombok.eclipse.agent.PatchDiagnostics.setSourceRangeCheck(PatchDiagnostics.java:38)(this is the same as the next basically just a wrapper)
at org.eclipse.jdt.core.dom.ASTNode.setSourceRange(ASTNode.java)
at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:2516)
I have generated a fair bit of code but until this point there was no need for me to set positions. So my guess is that I'm missing something that is not related to source or body end/start pos.
Basically I want to generate this code for example:
java.lang.Runnable generated = new java.lang.Runnable() {
public void run() {
}
};
Here is my code:
void handle( Annotation ast, EclipseNode annotationNode) {
EclipseNode parentClass = annotationNode.up();
LocalDeclaration runnableField = new LocalDeclaration("generated".toCharArray(), 0, 0);
runnableField.modifiers = 0;
runnableField.type = createQualifiedTypeReference("java.lang.Runnable");
runnableField.initialization = createNewRunnableImplementation(parentClass,ast);
//.. here I create a method and add this local decl to its statement,
// I don't think the problem is here so I do not include it
}
private Expression createNewRunnableImplementation(EclipseNode classNode, Annotation source) {
MethodDeclaration runMethod = new MethodDeclaration(((TypeDeclaration)(classNode.get())).compilationResult);
runMethod.modifiers = Modifier.PUBLIC;
runMethod.returnType = new SingleTypeReference(TypeConstants.VOID, 0);
runMethod.selector = "run".toCharArray();
TypeDeclaration newRunnableType = new TypeDeclaration(((TypeDeclaration)(classNode.get())).compilationResult);
newRunnableType.modifiers = 0;
newRunnableType.name = CharOperation.NO_CHAR;
newRunnableType.methods = new AbstractMethodDeclaration[] {runMethod};
newRunnableType.bits = ASTNode.IsAnonymousType;
QualifiedAllocationExpression runnableInit = new QualifiedAllocationExpression(newRunnableType);
runnableInit.type = createQualifiedTypeReference("java.lang.Runnable");
return runnableInit;
}
A working example would be great but at this point I would be happy if someone could give me some direction on where to look for some information.