Следующий код может быть скомпилирован в 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:
Должны ли ваши переключатели быть OutputType.DEF
и так далее (а не просто case(DEF)
?
Ну, для меня нет, но я делаю это для того, кто на этом настаивает. К сожалению.
Вы пытались удалить статический материал из своего скрипта и добавить имя класса перечисления в своих случаях?
switch (output){
case OutputType.ABC:
runABC();
break;
case OutputType.DEF:
runDEF();
break;
case OutputType.GHI:
runGHI();
break;
default:
break;
}
Поэтому я повторил попытку, удалив все статические операторы раньше. Сейчас появилась новая ошибка. К сожалению, мне пришлось стереть имя класса:
groovy.lang.MissingPropertyException: No such property: OutputType for class: XXXXXXXXXXX.tempscripts.Script1
Поскольку это еще одна проблема, я отметил ответ выше как правильный. Спасибо!
Также мне пришлось импортировать Type перед классом. Странно, но нашел решение в книге: books.google.de/…
в groovy сценарий вы не можете определять статические переменные. однако вы могли бы сделать это в классе