You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2022/11/04 14:05:00 UTC

[jira] [Commented] (GROOVY-10811) Missing errors for improper enum declaration

    [ https://issues.apache.org/jira/browse/GROOVY-10811?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17629003#comment-17629003 ] 

Eric Milles commented on GROOVY-10811:
--------------------------------------

The {{super()}} call can be flagged in EnumVisitor#completeEnum like this:
{code:java}
            for (ConstructorNode ctor : enumClass.getDeclaredConstructors()) {
                if (ctor.isSyntheticPublic()) {
                    ctor.setSyntheticPublic(false);
                    ctor.setModifiers((ctor.getModifiers() | ACC_PRIVATE) & ~ACC_PUBLIC);
                } else if (!ctor.isPrivate()) {
                    addError(ctor, "Illegal modifier for the enum constructor; only private is permitted.");
                }
                // add
                if (ctor.firstStatementIsSpecialConstructorCall()) {
                    ConstructorCallExpression ctorCall = (ConstructorCallExpression) ((ExpressionStatement) ctor.getFirstStatement()).getExpression();
                    if (ctorCall.isSuperCall()) {
                        StringJoiner spec = new StringJoiner(",",enumClass.getNameWithoutPackage()+"(",")");
                        for (Parameter p : ctor.getParameters()) spec.add(p.getType().getUnresolvedName());
                        addError(ctorCall, "Cannot invoke super constructor from enum constructor " + spec);
                    }
                }
                // end
            }
{code}

> Missing errors for improper enum declaration
> --------------------------------------------
>
>                 Key: GROOVY-10811
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10811
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Eric Milles
>            Priority: Minor
>              Labels: enum
>
> Consider the following:
> {code:groovy}
> final enum E {
>   FOO;
>   E(int i) {
>     super()
>   }
> }
> {code}
> * Explicit {{abstract}} or {{final}} modifier is supposed to be an error because an enum is made abstract or final depending on what's declared: "It is a compile-time error if an enum declaration has the modifier abstract or final." https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.9
> * The enum constant declaration "FOO" does not supply an argument, which results in a runtime error.
> * The special constructor call "super()" is not allowed by Java and results in a runtime error in Groovy.  "It is a compile-time error if a constructor declaration in an enum declaration contains a superclass constructor invocation statement (ยง8.8.7.1)."



--
This message was sent by Atlassian Jira
(v8.20.10#820010)