Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » anonymous java class generation using jdt and lombok
anonymous java class generation using jdt and lombok [message #1769763] Thu, 03 August 2017 08:20
Beno Beko is currently offline Beno BekoFriend
Messages: 1
Registered: August 2017
Junior Member
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.
Previous Topic:Running JShell as an interactive external tool?
Next Topic:How to implement a self defined LaunchDelegate to bebug java code?
Goto Forum:
  


Current Time: Wed Sep 20 20:16:52 GMT 2017

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

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