/* * Copyright (C)2005-2019 Haxe Foundation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ package java; /** Platform-specific Java Library. Provides some platform-specific functions for the Java target, such as conversion from Haxe types to native types and vice-versa. **/ // we cannot use the java package for custom classes, so we're redefining it as "haxe.java.Lib" @:native('haxe.java.Lib') class Lib { /** Print the specified value on the default output. **/ inline public static function print(v:Dynamic):Void { Sys.print(v); } /** Print the specified value on the default output followed by a newline character. **/ inline public static function println(v:Dynamic):Void { Sys.println(v); } /** Returns a native array from the supplied Array. This native array is unsafe to be written on, as it may or may not be linked to the actual Array implementation. If `equalLengthRequired` is true, the result might be a copy of an array with the correct size. **/ inline public static function nativeArray(arr:Array, equalLengthRequired:Bool):NativeArray { var ret = new NativeArray(arr.length); for (i in 0...arr.length) { ret[i] = arr[i]; } return ret; } /** Gets the native `java.lang.Class` from the supplied object. Will throw an exception in case of null being passed. [deprecated] - use `getNativeType` instead **/ @:deprecated('The function `nativeType` is deprecated and will be removed in later versions. Please use `getNativeType` instead') inline public static function nativeType(obj:T):java.lang.Class { return untyped obj.getClass(); } /** Gets the native `java.lang.Class` from the supplied object. Will throw an exception in case of null being passed. **/ inline public static function getNativeType(obj:T):java.lang.Class { return untyped obj.getClass(); } /** Returns a Class<> equivalent to the native java.lang.Class type. **/ public static inline function fromNativeType(t:java.lang.Class):Class { return untyped t; } /** Returns a java.lang.Class equivalent to the Haxe Class<> type. **/ public static inline function toNativeType(cl:Class):java.lang.Class { return untyped cl; } /** Returns a java.lang.Class equivalent to the Haxe Enum<> type. **/ public static inline function toNativeEnum(cl:Enum):java.lang.Class { return untyped cl; } /** Returns a Haxe Array of a native Array. Unless `copy` is true, it won't copy the contents of the native array, so unless any operation triggers an array resize, all changes made to the Haxe array will affect the native array argument. **/ @:generic public static function array(native:java.NativeArray):Array { return untyped Array.ofNative(native); } extern inline private static function doArray(native:java.NativeArray):Array { var ret:NativeArray = new NativeArray(native.length); for (i in 0...native.length) { ret[i] = native[i]; } return untyped Array.ofNative(ret); } public static function array_Int(native:java.NativeArray):Array { return doArray(native); } public static function array_Float(native:java.NativeArray):Array { return doArray(native); } public static function array_Bool(native:java.NativeArray):Array { return doArray(native); } public static function array_java_Int8(native:java.NativeArray):Array { return doArray(native); } public static function array_java_Int16(native:java.NativeArray):Array { return doArray(native); } public static function array_java_Char16(native:java.NativeArray):Array { return doArray(native); } public static function array_Single(native:java.NativeArray):Array { return doArray(native); } public static function array_haxe_Int64(native:java.NativeArray):Array { return doArray(native); } /** Allocates a new Haxe Array with a predetermined size **/ public static function arrayAlloc(size:Int):Array { return untyped Array.alloc(size); } /** Ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread attempts to enter a locked code, it will wait, block, until the object is released. This is the equivalent to "synchronized" in java code. This method only exists at compile-time, so it can't be called via reflection. **/ extern public static inline function lock(obj:Dynamic, block:T):Void { untyped __lock__(obj, block); } }