Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JVM language interoperability

Recently I've been writing a compiler for a JVM programming language and I've realised a problem.

I would like to access a Java method from my programming language and also allow a Java method to access a method in my language. The problem is that I need to know the Java methods signature to call it in the bytecode I generate and vice versa.

I've been trying to think of any methods for how Scala does this. Here are my thoughts.

  1. Scala accesses the .java files on the class path and parses them, extracting the method signatures from there.
  2. .java files are compiled to .class files. The Java ASM library is then used to access the .class files and get the method signatures. The problem with this method is that the .java files must be compiled first.
  3. .java files are loaded dynamically using reflection. The problem with this is I believe that the JVM doesn't allow for loading classes that are outside of the compilers class path.

Looking into Scala it works well with other JVM languages but I can't find information on exactly how it does it.

How does Scala get method signatures of other JVM language methods?

like image 842
Michael Avatar asked Jul 12 '18 13:07

Michael


People also ask

Which language is used for JVM?

The JVM was initially designed to support only the programming language Java. However, as time passed, even more languages were adapted or designed to run on the Java platform.

Can JVM run any language?

Any language can be compiled into bytecode which the JVM understands - all you need for this is a compiler emitting bytecode. From then on, there is no difference from the JVM's point of view. So much so that you can take a compiled Scala, Clojure, Jython etc.

What is an alternative JVM language?

The languages we'll look at in this first category are Scala, Groovy, Xtend, Ceylon, Kotlin, and Fantom. The second category is existing languages that were ported to the JVM. Many languages, such as Python and Ruby, can interact with Java APIs and are popular for scripting and quick prototyping.

Is Golang a JVM language?

Java vs Go: A quick glance Java is the older and more widely used programming language. It is object-oriented, has a larger community—thus library, and relies on the Java virtual machine (JVM). Go, or Golang, is newer, supports concurrency, is more readable, and is not object-oriented.


1 Answers

I think you are confusing class path and source path: there are no .java or .scala files on the class path, there are .class files (possibly inside .jars). So for dependencies (on the class path), you don't need to do anything special. They can have their own dependencies on your language, including previous versions of your project, but they are already compiled by definition.

Now, for mixed projects, where you have Java and your language on the source path, scalac does parse Java with its own parser (i.e. your option 1).

The problem with option 3 is not that "the JVM doesn't allow for loading classes that are outside of the compilers class path", but that reflection also only works on classes, not on source files.

like image 186
Alexey Romanov Avatar answered Oct 18 '22 09:10

Alexey Romanov