Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: bug in comment association and pretty-printing #2482

Closed
tempuser1909 opened this issue Sep 19, 2018 · 15 comments
Closed

bug: bug in comment association and pretty-printing #2482

tempuser1909 opened this issue Sep 19, 2018 · 15 comments
Labels

Comments

@tempuser1909
Copy link
Contributor

Hi, I am not sure if this is a bug for printing the source code with the comments.

Original:

public final class test{

    
// ------------------------------------ //
// test / test test     //
// ------------------------------------ //

    /**
     * test2
     *
     */   
    public interface testInterface {
        /**
         * test3
         * 
         */
        public void mytest(short a, short b);
    }
    private static class myInterface implements testInterface {
        public void mytest(short a, short b){
            
        }
    }
    
}//end class test

Pretty-printed by Spoon Launcher:



public final class test {
    // ------------------------------------ //
    public interface testInterface {
        // test / test test     //
        // ------------------------------------ //
        /**
         * test2
         */
        /**
         * test3
         */
        public void mytest(short a, short b);
    }

    private static class myInterface implements test.testInterface {
        public void mytest(short a, short b) {
        }
    }
}


So the problem is:

  1. the test comment is printed inside of the interface class instead of being outside
  2. the comment that marks the end of the class is gone
@pvojtechovsky
Copy link
Collaborator

It is a bug in JDTCommentBuilder which assigns comments and AST nodes together. it looks like it assigns correctly only first single line comment. Next are assigned wrong.

@tempuser1909
Copy link
Contributor Author

I checked via the GUI mode too.

It seems that the "end class" comment is detected but not printed.

And the two comments that were printed wrongly inside the interface class were detected wrongly as part of the method that is within the interface class.

image

@monperrus monperrus changed the title Comment displayed weirdly bug: bug in comment association and pretty-printing Sep 19, 2018
@monperrus monperrus added the bug label Sep 19, 2018
@pvojtechovsky
Copy link
Collaborator

@tempuser1909 could you make a pull request with failing test? It is then much easier/faster to fix the problem.

@tempuser1909
Copy link
Contributor Author

tempuser1909 commented Sep 20, 2018

@pvojtechovsky, sorry, I do not understand what do you mean by a pull request with failing test.

Do you mean to include a unit test for this with a pull request?

Is it under src/test/java/spoon/test/comment/CommentTest.java

@pvojtechovsky
Copy link
Collaborator

Do you mean to include a unit test for this with a pull request?

yes

Is it under src/test/java/spoon/test/comment/CommentTest.java

yes 👍

@tempuser1909
Copy link
Contributor Author

Sorry for the late pull request. This is my first time writing a unit test.

Please let me know if writing a more specific unit test is required. I just compared the original file with the prettyprint-ed file.

@monperrus
Copy link
Collaborator

closed by #2636

Thanks a lot @tempuser1909 for the bug report and the failing test case!

@rihou
Copy link

rihou commented Nov 8, 2018

The comments in the lambda expression are reported to have errors.

An example code:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CommentInLambdaExpressionTest {

public static void main(String[] args) {
	CommentInLambdaExpressionTest test = new CommentInLambdaExpressionTest();
	List<Integer> lst = test.setup();
	System.out.println(lst);
}

public List<Integer> setup() {
	List<Integer> messages = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
	return messages.stream().filter( num -> 
	// comment in lambda expression
	num > 5
	).collect(Collectors.toList());
}

}
The code triggers the error:
2018-11-08 10:11:15,847 ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "// comment in lambda expression" cannot be added into the AST, with parent class spoon.support.reflect.code.CtLambdaImplplease report the bug by posting on #2482

@shoeoffhead
Copy link

I just got this error messing about ;)

01:45:53.266 [main] ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "/* &0xff /" cannot be added into the AST, with parent class spoon.support.reflect.code.CtVariableReadImpl at (/mnt/data/src/com/jcraft/jzlib/Deflate.java:569), please report the bug by posting on #2482
01:45:53.269 [main] ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "/
&0xff */" cannot be added into the AST, with parent class spoon.support.reflect.code.CtVariableReadImpl at (/mnt/data/src/com/jcraft/jzlib/Deflate.java:574), please report the bug by posting on #2482

@monperrus
Copy link
Collaborator

monperrus commented Jan 27, 2020 via email

@skhokhlov
Copy link

On 8.0.0

2020-02-12 12:05:54,399 ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "/* (int) */" cannot be added into the AST, with parent class spoon.support.reflect.code.CtLiteralImpl at (/guava/target/jdk-sources/sun/invoke/util/Wrapper.java:35), please report the bug by posting on https://github.com/INRIA/spoon/issues/2482
2020-02-12 12:16:14,284 ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "/* ... */" cannot be added into the AST, with parent class spoon.support.reflect.code.CtVariableReadImpl at (/guava/target/jdk-sources/java/lang/invoke/LambdaFormEditor.java:615), please report the bug by posting on https://github.com/INRIA/spoon/issues/2482
2020-02-12 12:24:15,055 ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "// || (Axis.PRECEDINGSIBLING == axis)" cannot be added into the AST, with parent class spoon.support.reflect.code.CtBinaryOperatorImpl at (/guava/target/jdk-sources/com/sun/org/apache/xpath/internal/axes/WalkerFactory.java:787), please report the bug by posting on https://github.com/INRIA/spoon/issues/2482

@ontologiae
Copy link

ontologiae commented Feb 27, 2020

ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "// " cannot be added into the AST, with parent class spoon.support.reflect.declaration.CtAnnotationImpl

Le code

@Interceptor(I_M_HU_PI_Item_Product.class)
     34 public class M_HU_PI_Item_Product
     35 {
     36         public static final transient M_HU_PI_Item_Product instance = new M_HU_PI_Item_Product();
     37
     38         private M_HU_PI_Item_Product()
     39         {
     40         }
     41
     42         @ModelChange(timings = ModelValidator.TYPE_AFTER_CHANGE
     43         //
     44         , ifColumnsChanged = { I_M_HU_PI_Item_Product.COLUMNNAME_M_Product_ID, I_M_HU_PI_Item_Product.COLUMNNAME_Description, I_M_HU_PI_Item_Product.COLUMNNAME_IsActive, I_M_HU_PI_Item_Product.COLUMNNAME_C_BPartner_ID }
     45         //
     46         )
     47         public void updatePMM_Product(final I_M_HU_PI_Item_Product piip)
     48         {
     49                 Services.get(IPMMProductBL.class).updateByHUPIItemProduct(piip);
     50         }
     51 }

`

@QifanWang
Copy link

On 8.3.0

[ERROR] "/* inner */" cannot be added into the AST, with parent class spoon.support.reflect.declaration.CtTypeParameterImpl at (/home/ezio/testSpoon/testExample/src/cstar/testExample/CellClient2bak.java:2), please report the bug by posting on #2482

Analyzed code

package cstar.testExample;
public class CellClient2bak<S extends /*inner*/ CellClient2bak.Day> {

    public static class Cell<T> {
	public T val;
	public T getVal() { return val; }
	public void setVal(T val) { this.val = val; }
    }

    public static class Day {
        public int day;
        public Day() { }
        public Day(int d) { this.day = d; }
        public int getDay() { return day; }
        public void setDay(int d) { this.day = d; }
    }

    private Cell<S> f;

    public CellClient2bak(S t) {
        f = new Cell<S>();
        f.setVal(t);
    }

    public Cell<S> getF() {
        return f;
    }

    public void foo() {
        Cell<S> tmp = getF();
        Day d = tmp.getVal();
        d.setDay(2);
    }

    public void setDay(S t) {
        f.setVal(t);
    }
}


@Zuplyx
Copy link
Contributor

Zuplyx commented Oct 17, 2022

On Spoon 10.2.0-beta-20 analyzing the following code gives the error messages:

ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "// comment 1" cannot be added into the AST, with parent class spoon.support.reflect.code.CtBinaryOperatorImpl at (Comments.java:5), please report the bug by posting on #2482

ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "// comment 2" cannot be added into the AST, with parent class spoon.support.reflect.code.CtArrayWriteImpl at (Comments.java:15), please report the bug by posting on #2482

Example code:

public class Comments
{
  public void foo(int bar)
  {
    if (!(bar < 0 || bar == 10
    // comment 1
    ))
    {

    }
  }

  public void bar(int[] foo)
  {
    foo// comment 2
    [1] = 0;
  }
}

@lzaoral
Copy link

lzaoral commented Nov 2, 2022

Spoon 10.3.0-beta-1

[main] ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "// comment" cannot be added into the AST, with parent class spoon.support.reflect.code.CtBinaryOperatorImpl at (/tmp/spoon/spoon.java:11), please report the bug by posting on https://github.com/INRIA/spoon/issues/2482
[main] ERROR spoon.support.compiler.jdt.JDTCommentBuilder - "// comment" cannot be added into the AST, with parent class spoon.support.reflect.code.CtBinaryOperatorImpl at (/tmp/spoon/spoon.java:11), please report the bug by posting on https://github.com/INRIA/spoon/issues/2482
class Test {
    Test() {
        byte b =
            // comment

            // comment  
            (byte)
            // comment

            // comment
            (10
            // comment

            // comment
            | 20
            // comment

            // comment
            );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants