Framework: AbstractComponent cannot implement focus()

Created on 4 Aug 2019  路  6Comments  路  Source: vaadin/framework

Vaadin framework: 8.8.5

focus() in com.vaadin.ui.AbstractComponent cannot implement focus() in com.vaadin.ui.Component.Focusable
attempting to assign weaker access privileges; was public

Method focus in AbstractComponent should be public.

question

Most helpful comment

Hello. I am having a similar problem.

I have the following class:
public class LocalGeodesyLayout extends DrawableVerticalLayout<PlaceTO> implements SourceChangeConsumer<PlaceTO>, CrudEnabled<PlaceTO> { ... }

where DrawableVerticalLayout is an indirect subclass of AbstractComponent:
public abstract class DrawableVerticalLayout<T> extends VerticalLayout implements DrawableComponent<T> { ... }

None of the classes or interfaces implement com.vaadin.ui.Component.Focusable. All compiles in OpenJDK8 or AdoptOpenJDK8, but with OpenJDK 9, or 11 or AdoptOpenJDK11 I have the following error:

Compilation failure /Users/my.name/Progetti/bbox/geodesy/src/main/java/it/imc/vaadin/ui/geodesy/LocalGeodesyLayout.java: focus() in com.vaadin.ui.AbstractComponent cannot implement focus() in com.vaadin.ui.Component.Focusable attempting to assign weaker access privileges; was public

LocalGeodesyLayout does not implement focus(), nor Focusable.

Compiling on different machines, as the Maven multiproject build changes the order of the modules, the same error occurs on other AbstractComponent subclasses.

All 6 comments

In Vaadin not all components are focusable by default. So you plan to create custom component by extending AbstractComponentand want it to be focusable, you need to also implement Focusable interface. Focusableinterface defines focus() as public. This also mandates you to implement setTabIndexand getTabIndexmethods.

import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Component.Focusable;

public class MyComponent extends AbstractComponent implements Focusable {
    ...     
    @Override
    public void focus() {
        super.focus();      
    }

    @Override
    public int getTabIndex() {
        return 0;
    }

    @Override
    public void setTabIndex(int tabIndex) {
        ... 
    }
}

Hello. I am having a similar problem.

I have the following class:
public class LocalGeodesyLayout extends DrawableVerticalLayout<PlaceTO> implements SourceChangeConsumer<PlaceTO>, CrudEnabled<PlaceTO> { ... }

where DrawableVerticalLayout is an indirect subclass of AbstractComponent:
public abstract class DrawableVerticalLayout<T> extends VerticalLayout implements DrawableComponent<T> { ... }

None of the classes or interfaces implement com.vaadin.ui.Component.Focusable. All compiles in OpenJDK8 or AdoptOpenJDK8, but with OpenJDK 9, or 11 or AdoptOpenJDK11 I have the following error:

Compilation failure /Users/my.name/Progetti/bbox/geodesy/src/main/java/it/imc/vaadin/ui/geodesy/LocalGeodesyLayout.java: focus() in com.vaadin.ui.AbstractComponent cannot implement focus() in com.vaadin.ui.Component.Focusable attempting to assign weaker access privileges; was public

LocalGeodesyLayout does not implement focus(), nor Focusable.

Compiling on different machines, as the Maven multiproject build changes the order of the modules, the same error occurs on other AbstractComponent subclasses.

I came across the same compiler error and in my case, it ended up being related to a lambda expression.

The minimal way I found to reproduce this issue is as follows:

final TextField tf = new TextField();
final ComboBox<String> cb = new ComboBox<>();
Stream.of(tf,cb).forEach(c -> c.markAsDirty());

Gives me the following compiling error:

focus() in com.vaadin.ui.AbstractComponent cannot implement focus() in com.vaadin.ui.Component.Focusable
attempting to assign weaker access privileges; was public

The amazing thing is that any of the following version compiles just fine:

Stream.<Component>of(tf,cb).forEach(c -> c.markAsDirty());
Stream.<AbstractComponent>of(tf,cb).forEach(c -> c.markAsDirty());
Stream.of(tf,cb).forEach((Component c) -> c.markAsDirty());
Stream.of(tf,cb).forEach((AbstractComponent c) -> c.markAsDirty());
Stream.of(tf,cb).forEach(Component::markAsDirty);
Stream.of(tf,cb).forEach(AbstractComponent::markAsDirty);

As mentioned above, the Java 8 compiler does not generate this error. If I use the Java 11 compiler, the first example will not compile.
I cannot quite figure out what the compiler is doing for this to happen...

Same issue here with mixing component types in a stream. In this scenario the compiler joins the common interfaces into a large generic that includes Focusable. Specifying Stream.>of(...) fixes it.

Sorry, byt why this bug was closed?

This issue was only ever open for one day, presumably because the original explanation for the access level was deemed satisfactory. Everything after that has been commented on an already closed issue, which is generally not a great option for getting anyone to notice them. On a quick glance it seems that the previous commenters already found the underlying reason and several workarounds for the presented Java9+ compilation issues, though.

Was this page helpful?
0 / 5 - 0 ratings