@@ -785,18 +785,28 @@ void java_object_factoryt::gen_nondet_pointer_init(
785785 // (This can currently happen for some cases of #exception_value)
786786 bool must_be_null = subtype == empty_typet ();
787787
788- // If we may be about to initialize a non-null object , always run the
788+ // If we may be about to initialize a non-null enum type , always run the
789789 // clinit_wrapper of its class first.
790+ // TODO: TG-4689 we may want to do this for all types, not just enums, as
791+ // described in the Java language specification:
792+ // https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.7
793+ // https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4.1
794+ // But we would have to put this behavior behind an option as it would have an
795+ // impact on running times.
790796 // Note that it would be more consistent with the behaviour of the JVM to only
791797 // run clinit_wrapper if we are about to initialize an object of which we know
792798 // for sure that it is not null on any following branch. However, adding this
793- // case in gen_nondet_struct_init would slow symex down too much.
794- if (!must_be_null)
799+ // case in gen_nondet_struct_init would slow symex down too much, so if we
800+ // decide to do this for all types, we should do it here.
801+ if (can_cast_type<java_class_typet>(subtype))
795802 {
796- const java_class_typet &class_type = to_java_class_type (subtype);
797- const irep_idt &class_name = class_type.get_name ();
798- const irep_idt class_clinit = clinit_wrapper_name (class_name);
799- gen_method_call_if_present (assignments, expr, class_clinit);
803+ const java_class_typet &class_type=to_java_class_type (subtype);
804+ if (class_type.get_base (" java::java.lang.Enum" ) && !must_be_null)
805+ {
806+ const irep_idt &class_name=class_type.get_name ();
807+ const irep_idt class_clinit=clinit_wrapper_name (class_name);
808+ gen_method_call_if_present (assignments, expr, class_clinit);
809+ }
800810 }
801811
802812 code_blockt new_object_assignments;
0 commit comments