Groovy enum: оператор switch выдает ошибку перед запуском, относящуюся к статическому - PullRequest
1 голос
/ 29 мая 2019

Следующий код может прекрасно скомпилироваться в Intellij, но при попытке использовать Groovy в качестве скрипта выдает ошибку даже перед запуском. Я не могу найти трещину, так как на самом деле все статично?

public enum OutputType {
        ABC,
        DEF,
        GHI
    }
    //Just initializing here
        public static OutputType output=OutputType.ABC;



    public static void run() {

        switch (output){
            case ABC:
                runABC();
                break;

            case DEF:
                runDEF();
                break;

            case GHI:
                runGHI();
                break;

            default:
                break;
        }
    }

Ошибка:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 50: Apparent variable 'ABC' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'ABC' but left out brackets in a place not allowed by the grammar.
 @ line 50, column 18.
               case ABC:
                    ^

Script1.groovy: 54: Apparent variable 'DEF' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'DEF' but left out brackets in a place not allowed by the grammar.
 @ line 54, column 18.
               case DEF:
                    ^

Script1.groovy: 58: Apparent variable 'GHI' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'GHI' but left out brackets in a place not allowed by the grammar.
 @ line 58, column 18.
               case GHI:

Ответы [ 2 ]

1 голос
/ 31 мая 2019

Итак, я повторил попытку удаления всех статических операторов.Получил новую ошибку сейчас.К сожалению, мне пришлось стереть имя класса:

groovy.lang.MissingPropertyException: No such property: OutputType for class: XXXXXXXXXXX.tempscripts.Script1

Так как это еще одна проблема, я пометил ответ выше как правильный.Спасибо!

1 голос
/ 30 мая 2019

Вы пытались удалить статический материал из вашего скрипта и добавить имя класса enum в ваших случаях?

    switch (output){
        case OutputType.ABC:
            runABC();
            break;

        case OutputType.DEF:
            runDEF();
            break;

        case OutputType.GHI:
            runGHI();
            break;

        default:
            break;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...