Uploaded image for project: 'Forge'
  1. Forge
  2. FORGE-1096

JavaParser fails to save a newly added constructor when Java 1.5+ features are used in the body

    Details

      Description

      The following exception stack trace or something similar is obtained when saving the JavaSource instance, after a constructor is added to it:

      java.lang.ArrayIndexOutOfBoundsException: 96
      	at org.eclipse.jdt.internal.formatter.CodeFormatterVisitor.visit(CodeFormatterVisitor.java:3467)
      	at org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration.traverse(ConstructorDeclaration.java:508)
      	at org.eclipse.jdt.internal.formatter.CodeFormatterVisitor.format(CodeFormatterVisitor.java:593)
      	at org.eclipse.jdt.internal.formatter.CodeFormatterVisitor.formatClassBodyDeclarations(CodeFormatterVisitor.java:1587)
      	at org.eclipse.jdt.internal.formatter.CodeFormatterVisitor.format(CodeFormatterVisitor.java:830)
      	at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.internalFormatClassBodyDeclarations(DefaultCodeFormatter.java:364)
      	at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.formatClassBodyDeclarations(DefaultCodeFormatter.java:186)
      	at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.format(DefaultCodeFormatter.java:161)
      	at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.format(DefaultCodeFormatter.java:146)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteFormatter.formatString(ASTRewriteFormatter.java:245)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteFormatter.formatNode(ASTRewriteFormatter.java:370)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteFormatter.getFormattedResult(ASTRewriteFormatter.java:186)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.doTextInsert(ASTRewriteAnalyzer.java:1287)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer$ListRewriter.rewriteList(ASTRewriteAnalyzer.java:583)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer$ListRewriter.rewriteList(ASTRewriteAnalyzer.java:736)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.rewriteParagraphList(ASTRewriteAnalyzer.java:1109)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.visit(ASTRewriteAnalyzer.java:1727)
      	at org.eclipse.jdt.core.dom.TypeDeclaration.accept0(TypeDeclaration.java:467)
      	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2514)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.doVisit(ASTRewriteAnalyzer.java:357)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.voidVisitList(ASTRewriteAnalyzer.java:395)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.voidVisit(ASTRewriteAnalyzer.java:389)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.doVisitUnchangedChildren(ASTRewriteAnalyzer.java:402)
      	at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.visit(ASTRewriteAnalyzer.java:1589)
      	at org.eclipse.jdt.core.dom.CompilationUnit.accept0(CompilationUnit.java:214)
      	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2514)
      	at org.eclipse.jdt.core.dom.InternalASTRewrite.rewriteAST(InternalASTRewrite.java:99)
      	at org.eclipse.jdt.core.dom.AST.rewrite(AST.java:2867)
      	at org.eclipse.jdt.core.dom.CompilationUnit.rewrite(CompilationUnit.java:925)
      	at org.jboss.forge.parser.java.impl.AbstractJavaSource.toString(AbstractJavaSource.java:632)
      	... 24 more
      

      An example test that triggers this scenario is as follows:

         @Test
         public void testSupportsGenericsSourceFromAddedConstructor() throws Exception {
            JavaClass source = JavaParser.parse(JavaClass.class, "public class Test{}");
            // Add a new method to get JDT to recognize the new ASTs
            source.addMethod().setConstructor(true).setBody("java.util.List<String> s = new java.util.ArrayList<String>(); for (String item : s){}");
            // Forces a rewrite to happen via AbstractJavaSource
            source.toString();
            Assert.assertFalse(source.hasSyntaxErrors());
         }
      

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                vineet.reynolds Vineet Reynolds
                Reporter:
                vineet.reynolds Vineet Reynolds
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: