Bug 536612 - Save action "remove redundant modifiers" does not handle correctly anonymous implementations of interfaces
Summary: Save action "remove redundant modifiers" does not handle correctly anonymous ...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.8   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.9 M2   Edit
Assignee: Karsten Thoms CLA
QA Contact:
URL:
Whiteboard:
Keywords: triaged
: 536556 (view as bug list)
Depends on: 531441
Blocks:
  Show dependency tree
 
Reported: 2018-07-03 02:34 EDT by Lyor Goldstein CLA
Modified: 2018-08-01 02:48 EDT (History)
5 users (show)

See Also:
kalyan_prasad: review+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lyor Goldstein CLA 2018-07-03 02:34:49 EDT
Let's assume the following definition:


@FunctionalInterface
public interface IOFunction<T, R> {
    R apply(T t) throws IOException;
}

I am writing the following anonymous implementation in some Java file:

return new IOFunction<Object, O>() {
    @Override
    public O apply(Object t) throws IOException {
          ... return something ....
    }

    @Override
    public String toString() {
        return ... some string ...
    }
};

When I save the file it removes the 'public' modifier - which is an error, because the code cannot be compiled without them.
Comment 1 Noopur Gupta CLA 2018-07-11 04:47:51 EDT
Karsten, can you please have a look?
Comment 2 Noopur Gupta CLA 2018-07-11 04:49:22 EDT
*** Bug 536556 has been marked as a duplicate of this bug. ***
Comment 3 Karsten Thoms CLA 2018-07-11 06:50:34 EDT
I have created this class which contains your snippet. The redundant modifier clean up does not remove 'public' anywhere. But it would e.g. remove 'final' when adding 'final' to the 'foo' method. So the clean up action is triggered, but does not cause the failure you are describing.

What am I missing?

================================
package test;

import java.io.IOException;

public final class IOFunctionUsage<O> {
	public IOFunction foo() {
		return new IOFunction<Object, O>() {
			@Override
			public O apply(Object t) throws IOException {
				return null;
			}

			@Override
			public String toString() {
				return "";
			}
		};

	} 
}
================================
Comment 4 Karsten Thoms CLA 2018-07-12 05:53:39 EDT
Please reopen when the reproduction steps are more precise. Ideally attach a small demo project.
Comment 5 Michael Wiles CLA 2018-07-12 14:52:05 EDT
Thanks for taking a look...

I logged the duplicate (https://bugs.eclipse.org/bugs/show_bug.cgi?id=536556) bug but now realise that strictly my bug is subtly different...

The issue only happens when the anonymous class is created in the interface itself. As you say, it does not surface if you create the anonymous outside of the interface.

This is how it works:

Create the following interface:

public interface X {

    void B();
    
    void A();
    default  X y() {
        return new X() {

            @Override
            public void B() {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void A() {
                // TODO Auto-generated method stub
                
            }
        };
    }
}

When you save this code with remove redundant modifiers on it will remove the public from methods A and B in the anonymous implementation of X.
Comment 6 Karsten Thoms CLA 2018-07-12 16:53:44 EDT
Thanks, this one reproduces the issue. I will investigate this.
Comment 7 Eclipse Genie CLA 2018-07-14 16:42:21 EDT
New Gerrit change created: https://git.eclipse.org/r/126070
Comment 9 Kalyan Prasad Tatavarthi CLA 2018-08-01 02:48:25 EDT
Verified in the build I20180731-2000