Использование сопоставления экземпляра с шаблонами и экспрессии переключения в том же методе ослаблено в Java?JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Использование сопоставления экземпляра с шаблонами и экспрессии переключения в том же методе ослаблено в Java?

Сообщение Anonymous »

Я запускаю этот минимальный набор кода в https://www.onlinegdb.com/online_java_compiler с Java 23.0.2+7-58 . Также попробовал с Java 21 и 24 на моей собственной машине и получил те же результаты. Пример не предназначен для того, чтобы сделать что-то полезное, за исключением продемонстрирования проблемы-мой фактический код более сложный.

Код: Выделить всё

record VehicleInput(Vehicle vehicle) {}

sealed interface Vehicle permits Car {}

record Car(String licensePlate) implements Vehicle {}

sealed interface Animal permits Cat, Dog {}

record Cat() implements Animal {}

record Dog() implements Animal {}

class VehicleProgressor {
private boolean progress(VehicleInput input, Animal animal) {
if (input.vehicle() instanceof Car(String licensePlate)) {
return true;
}

return switch (animal) {
case Cat cat -> true;
case Dog dog -> true;
};
}
}

public class Main {
public static void main(String[] args) {
System.out.println(Runtime.version());

new VehicleProgressor();
}
}
< /code>
он не удается с: < /p>
23.0.2+7-58
Exception in thread "main" java.lang.VerifyError: Inconsistent stackmap frames at branch target 96
Exception Details:
Location:
VehicleProgressor.progress(LVehicleInput;LAnimal;)Z @96: aload_3
Reason:
Type top (current frame, locals[5]) is not assignable to 'Cat' (stack map, locals[5])
Current Frame:
bci: @50
flags: { }
locals: { 'VehicleProgressor', 'VehicleInput', 'Animal', 'Animal', integer }
stack: { integer }
Stackmap Frame:
bci: @96
flags: { }
locals: { 'VehicleProgressor', 'VehicleInput', 'Animal', 'Animal', integer, 'Cat' }
stack: { }
Bytecode:
0000000: 2bb6 0007 3a05 1905 c100 0d99 0015 1905
0000010: c000 0d4e 2db6 000f 3a06 1906 3a04 04ac
0000020: 2c59 b800 1357 4e03 3604 2d15 04ba 0019
0000030: 0000 ab00 0000 001a 0000 0002 0000 0000
0000040: 0000 0024 0000 0001 0000 002e bb00 1d59
0000050: 0101 b700 1fbf 2dc0 0022 3a05 04a7 000a
0000060: 2dc0 0024 3a06 04ac 4ebb 001d 592d b600
0000070: 282d b700 1fbf
Exception Handler Table:
bci [21, 24] => handler: 104
Stackmap Table:
same_frame(@32)
append_frame(@42,Object[#50],Integer)
same_frame(@76)
same_frame(@86)
append_frame(@96,Object[#34])
full_frame(@103,{Object[#43],Object[#8],Object[#50]},{Integer})
same_locals_1_stack_item_frame(@104,Object[#38])

at Main.main(Main.java:30)
это ошибка в Java/jdk?

Код: Выделить всё

class VehicleProgressor {
private boolean progress(VehicleInput input, Animal animal) {
if (input.vehicle() instanceof Car) {
return true;
}

return switch (animal) {
case Cat cat -> true;
case Dog dog -> true;
};
}
}
< /code>
или в: < /p>
class VehicleProgressor {
private boolean progress(VehicleInput input, Animal animal) {
if (input.vehicle() instanceof Car(String licensePlate)) {
return true;
}
return true;
}
}
< /code>
или в: < /p>
class VehicleProgressor {
private boolean progress(Vehicle vehicle, Animal animal) {
if (vehicle instanceof Car(String licensePlate)) {
return true;
}

return switch (animal) {
case Cat cat -> true;
case Dog dog -> true;
};
}
}
работает нормально, просто печатая номер версии. Я также попытался иметь все в отдельных файлах в качестве общего interface s/ s, но это не имеет значения.


Подробнее здесь: https://stackoverflow.com/questions/796 ... -method-bu
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»