Преглед изворни кода

[jvm] fix Type.getEnumConstructs on native enums

closes #10508
Simon Krajewski пре 3 година
родитељ
комит
15849c9818
4 измењених фајлова са 38 додато и 3 уклоњено
  1. 12 0
      src/generators/genjvm.ml
  2. 6 1
      std/haxe/EnumTools.hx
  3. 10 2
      std/jvm/_std/Type.hx
  4. 10 0
      tests/unit/src/unit/issues/Issue10508.hx

+ 12 - 0
src/generators/genjvm.ml

@@ -1541,6 +1541,18 @@ class texpr_to_jvm gctx (jc : JvmClass.builder) (jm : JvmMethod.builder) (return
 			| _ ->
 				Error.typing_error (Printf.sprintf "Bad __array__ type: %s" (s_type (print_context()) tr)) e1.epos;
 			end
+		| TField(_,FStatic({cl_path = (["haxe"],"EnumTools")}, {cf_name = "values"})) ->
+			begin match el with
+			| [e1] ->
+				let jsig_ret = array_sig (object_path_sig object_path) in
+				let meth = gctx.typed_functions#register_signature [] (Some jsig_ret) in
+				let jsig_meth = (method_sig meth.dargs meth.dret) in
+				self#read (fun () -> jm#cast jsig_meth) e1 (FDynamic "values");
+				jm#invokevirtual haxe_function_path meth.name jsig_meth;
+				Some jsig_ret
+			| _ ->
+				die "" __LOC__
+			end
 		| TField(e1,FStatic(c,({cf_kind = Method (MethNormal | MethInline)} as cf))) ->
 			let tl,tr = self#call_arguments cf.cf_type el in
 			jm#invokestatic c.cl_path cf.cf_name (method_sig tl tr);

+ 6 - 1
std/haxe/EnumTools.hx

@@ -37,7 +37,7 @@ extern class EnumTools {
 
 		If `e` is inside a package, the package structure is returned dot-
 		separated, with another dot separating the enum name:
-		
+
 			pack1.pack2.(...).packN.EnumName
 
 		If `e` is a sub-type of a Haxe module, that module is not part of the
@@ -110,6 +110,11 @@ extern class EnumTools {
 	static inline function getConstructors<T>(e:Enum<T>):Array<String> {
 		return Type.getEnumConstructs(e);
 	}
+
+	#if (java && jvm)
+	@:noCompletion
+	extern static function values<T>(en:Enum<T>):java.NativeArray<java.lang.Enum<T>>;
+	#end
 }
 
 /**

+ 10 - 2
std/jvm/_std/Type.hx

@@ -234,7 +234,15 @@ class Type {
 	public static function getEnumConstructs(e:Enum<Dynamic>):Array<String> {
 		var clInfo:java.lang.Class<EnumReflectionInformation> = cast EnumReflectionInformation;
 		var annotation = e.native().getAnnotation(clInfo);
-		return @:privateAccess Array.ofNative(annotation.constructorNames());
+		if (annotation != null) {
+			return @:privateAccess Array.ofNative(annotation.constructorNames());
+		}
+		var vals = e.values();
+		var ret = [];
+		for (i in 0...vals.length) {
+			ret[i] = vals[i].name();
+		}
+		return ret;
 	}
 
 	public static function typeof(v:Dynamic):ValueType {
@@ -296,7 +304,7 @@ class Type {
 		var ret = [];
 		for (name in all) {
 			var v = Jvm.readField(e, name);
-			if (Jvm.instanceof(v, jvm.Enum)) {
+			if (Jvm.instanceof(v, java.lang.Enum)) {
 				ret.push(v);
 			}
 		}

+ 10 - 0
tests/unit/src/unit/issues/Issue10508.hx

@@ -0,0 +1,10 @@
+package unit.issues;
+
+class Issue10508 extends Test {
+	#if java
+	function test() {
+		t(java.math.RoundingMode.getConstructors().length > 0);
+		t(java.math.RoundingMode.createAll().length > 0);
+	}
+	#end
+}