Unable to subclass GlyphIcon

Issue #20 resolved
zarnikov created an issue

It is not possible to create deep class hierarchy of GlyphIcon.

public class FontAwesomeBug extends Application {

    public static void main(String[] args) {
        Font.loadFont(FontAwesomeBug.class.getResource("/de/jensd/fx/glyphs/fontawesome/fontawesome-webfont.ttf").toExternalForm(), Font.getDefault().getSize());
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

//         doesn't work
        FontAwesomeGlyphIcon icon = new ExtendedAwesomeGlyphIcon();

//        this works
//        FontAwesomeGlyphIcon icon = new FontAwesomeGlyphIcon();

        primaryStage.setScene(new Scene(new BorderPane(icon)));
        primaryStage.show();
    }

    private static class FontAwesomeGlyphIcon extends GlyphIcon<FontAwesomeIcon> {

        public FontAwesomeGlyphIcon() {
            setFont(new Font("FontAwesome", DEFAULT_ICON_SIZE));
        }

        @Override
        public FontAwesomeIcon getDefaultGlyph() {
            return FontAwesomeIcon.ADJUST;
        }
    }

    private static class ExtendedAwesomeGlyphIcon extends FontAwesomeGlyphIcon {

    }
}

The cause is the constructor of GlyphIcon:

    public GlyphIcon() {
        this.typeOfT = (Class<T>) ((ParameterizedType) getClass()
                .getGenericSuperclass())
                .getActualTypeArguments()[0];
        // remainder omitted.

    }

This is a very bad way of determining the parameter type because it doesn't work in many cases (like the example above). It would be much cleaner, easier and less buggy to simply pass the class as parameter to the constructor:

    public GlyphIcon(Class<T> iconType) {
        this.typeOfT = iconType;
        // remainder omitted.

    }

Comments (2)

  1. Log in to comment