Can I instantiate an object by the class name in pure Kotlin - reflection

What is the equivalent of the following Java code in pure Kotlin?
Class.forName(className).newInstance()
Suppose we are in a Kotlin multi-platform project.

This will just work as-is in Kotlin on the JVM, as Kotlin is compiled to JVM classes which are available via Java reflection, so you can use this line of code in Kotlin as well.
For other Kotlin platforms (Kotlin/JS, Kotlin/Native), there's currently no proper reflection support, so you can't do that when running on those platforms.

Related

What reflection mechanisms are available in C++/WinRT?

I remember C++ had some runtime type information (RTTI) added sometime after Bjarne Stroustrup's original The C++ Programming Language, but I never had call to use it.
I am familiar with some of the COM and CLR reflection APIs including ITypeInfo and System.Reflection. Would any of these work in against types declared in compiled C++/WinRT app?
This question addressed a similar question 5 years back for C++/CX, have there been changes?
C++ /WinRT doesn't add to the native reflection capabilities of C++. However, the xlang metadata reader APIs can be used to inspect Windows Runtime metadata files (.winmd) that describe WinRT types. You can see the metadata reader library here (and there are examples of usage in the various tools in this repo):
https://github.com/Microsoft/xlang/blob/master/src/library/meta_reader.h
You can use that in conjunction with the Windows function RoGetMetadataFile to locate the metadata for a type at runtime.
https://learn.microsoft.com/en-us/windows/desktop/api/rometadataresolution/nf-rometadataresolution-rogetmetadatafile
Note that C++ /WinRT itself does not use the winmd file at runtime, and as such, code built with C++ /WinRT does not require the winmd to be available at runtime. If the winmd isn't present, you won't be able to rely on it for type information.
If the metadata file is supplied for a type written in C++ /WinRT, the .NET runtime can use the winmd to reflect over the projected types in much the same way that it can reflect over types written using the .NET runtime.
C++ /WinRT does not provide any support at this time for dynamic invocation of types. This is an infrequent but recurring ask and is on our backlog.
Thanks,
Ben

sqlite-pcl vs sqlite-net xamarin component

I have installed sqlite-pcl, but there is also component for Xamarin sqlite-net.
What is the main difference between those references? And which of them has better performance?
As I know pcl doesn't support relationships (foreign key), but sqlite-net does. So I use [Indexed] attribute for better performance, does it make seance?
They are both based on the same Sqlite.Net library but the PCL project is a fork which tries to improve on the previous project:
This is a fork of the original sqlite-net library
(https://github.com/praeclarum/sqlite-net), which aims to improve the
code quality by using modern technologies such as PCL (portable class
library).
The project will avoid the use of #if-based conditional code and use
platform-specific code injection instead.
I welcome pull requests, but keep in mind that this library is in
heavy use and all changes must be:
Backwards-compatible (don't change database defaults). Well tested
(please add unit tests).
Both don't support foreign keys.
Performance-wise the PCL project supposed to be better, but that is untested as far as I know.

What is the difference between JavaFX scripting and Using Regular java Syntax while programming JavaFX Applications

I am looking for a detail explanation to the following question.
Can I use regular java syntax to develop JavaFX application? And if so why is the JavaFX scripting so important?
JavaFX Script was a language developed for JavaFX 1.x. It was dropped for JavaFX 2.x and is no longer supported or developed.
Can I use regular java syntax to develop JavaFX application?
Yes. All of the JavaFX 2.x system is accessible through regular Java language code running on a Java Virtual Machine. Here is a sample JavaFX program. You will notice it is just a Java application that provides a simple GUI.
And if so why is the JavaFX scripting so important?
Students studying the history of computer languages might be interested in the design of JavaFX Script's innovative binding features and declarative programming support. From a practical development point of view, JavaFX Script is completely obsolete and never used.
In addition to the standard Java API, many alternate languages, such as Ruby, Closure, Scala, JavaScript and Groovy are also able to access and use JavaFX. For scripting JavaFX applications, languages like JavaScript and Groovy take the place of the JavaFX Script language. An XML based markup called FXML replaces JavaFX Script for declarative definition of GUI scenes.
It's probably a good idea to review the JavaFX documentation in detail before asking further questions.

PlayN - Managing Common Code / Native Code

I am thinking about using PlayN to manage "common code" in Java and use PlayN to generate iOS, Android, and HTML native versions of the common code.
I figure I could then use the playn-generated native code and link with actual platform specific code (such as UI).
In other words,
Common Code libs in Java-> PlayN -> Native Commond Code Libs -> Link with Native App
Is the use of Play for the above workflow/pipeline appropriate? Any challenges?
Thank-you...
Firstly, you have to specify what you mean by "native" code for the different platforms.
On Android, your java files are specifically compiled/prepared for dalvik. So they are already "native" of a fashion, no work needed to be done here. If you want to get C/C++ native code for Android using the NDK, you're out of luck. PlayN doesn't do this and this is a hard problem (going from Java to C++)
If you take a look at the Maven modular layout of how PlayN is intended to be used, it isn't difficult to define a Factory interface in the common code and pass in a platform specific implementation for each module. It's no big deal to support Android specific functionality this way.
For the HTML version, you can use HTML libraries no problem using JNI, although really garnering specific functionality of the browser I'd imagined of limited value compared to what PlayN has already exposed. The one thing that is useful is text/keyboard input, although I'd recommend triplePlay https://github.com/threerings/tripleplay UI library as they've solved this, and it's an active project.
As for iOS, this might be more complicated as the iOS module is a bit of a hack where the compiled Java classes are run through an JVM runtime for .net (IKVM) and then uses the Monotouch tools to compile the whole thing to native code for iOS. See https://github.com/samskivert/ikvm-monotouch
So for iOS, you won't be able to bind the code to any form of native version, and what you have access to via the toolchain method depends very much on what Monotouch has catered for iOS (quite a lot I imagine), and also what IKVM-Monotouch has supported (I imagine the bare minimum to get PlayN working).
I'm not familiar enough with the Flash pipeline to give you an appraisal, although I think that it's quite flexible.
The above answer is written assuming your app is actually a game. If it is not and you intend to use the standard widget libraries for various platforms on mass, it should be possible. Choosing a good MVP framework would be good here, and depending on the assumptions it makes on different host environments will determine how easy the whole thing will be.
I'd recommend reading and comparing https://developers.google.com/web-toolkit/articles/mvp-architecture and perhaps look at questions like What is your favorite GWT MVP Framework?
...although a lot of these frameworks might be GWT specific and not really have catered to being reused on other platforms.

Pure java implementation of the java.lang.Math class

I just downloaded the openjdk source and came to the realization that nearly all of the java.lang.Math class was implemented in native c/c++ code. I was wondering if there were any implementations that were fully written in java.
Have a look at MicroFloat. It targets J2ME platform and according to the author it implements all methods in java.lang.Math.
Currently some pure Java improvements for methods in the java.lang.Math class are integrated into apache commons-math:
See: Elementary functions in JDK are slower than necessary and not as accurate as they could be.

Resources