MPS 2019.1 Help

MPS Java compatibility

Configuration

The Java Compiler configuration tab in the preferences window only holds a single setting - Project bytecode version.

Screen Shot 2015 01 30 at 17 20 40

This setting defines the bytecode version of all Java classes compiled by MPS. These classes include classes generated from languages aspects, classes of the runtime solutions, classes of the sandbox solutions, and so on.

By default, the bytecode version is set to JDK Default. This means that the version of the compiled classes will be equal to the version of Java, which MPS is running under. For example, if you run MPS under JDK 1.8 and JDK Default is selected, the bytecode version will be 1.8.

The other options for project bytecode version are 1.6, 1.7 and 1.8.

Build scripts

Also, dont forget to set java compliance level in the build scripts of your project. It should be the same as the project bytecode version.

Screen Shot 2015 01 29 at 18 08 28

Using java classes compiled with JDK 1.8

In the MPS modules pool you can find the JDK solution, which holds the classes of the running Java. So when you start MPS under JDK 1.8, the latest Java Platform classes will be available in the JDK solution.

You can also use any external Java classes, compiled under JDK 1.8 by adding them as Java stubs.

Since version 1.8, Java interfaces can contain default and static methods. At present, MPS does not support creating them in your BaseLanguage code, but you can call static and default methods defined in external Java classes, for example, classes of the Java Platform.

Static interface method call

Screen Shot 2015 01 22 at 02 52 51

In the example, we sort a list with the Comparator.reverseOrder()Comparator is an interface from java.util, and reverseOrder() is its static method, which was introduced in Java 1.8.

Default interface methods

Java 8 introduced also default methods. These are methods implemented directly in the interface. You can read about default methods here: http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

These methods can be called just like the usual instance methods. Sometimes, however, you need to call the default method directly from an interface that your class is implementing. For example, in case of multiple inheritance when a class implements several interfaces, each containing a default method with the same signature.

public interface A {   public default void foo() {     System.out.println("A");    } } public interface B {   public default void foo() {     System.out.println("B");    } }

In that case foo() can be called explicitly on one of the interfaces via a SuperInterfaceMethodCall construction, which is newly located in the jetbrains.mps.baseLanguage.jdk8 language.

Screen Shot 2015 01 29 at 20 39 29

The jetbrains.mps.baseLanguage.jdk8 language allows users to create the 'default' methods in BaseLanguage interfaces.

comx1

The 'default' keyword is implemented by the DefaultModifier concept which extends the Modifier concept in the BaseLanguage.

The transformation/substitute menu + some cosy intentions are also supplied.

comx2
comx3

Using Java platform API

Java 8 introduced lambda expressions, of which you can learn more here: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

MPS doesnt yet have a language that would be generated into lambda-expressions. Instead, it has its own closure language, which is compatible with the new Java API!

Heres the example of an interaction with the new JDK 8 Collections API:

Screen Shot 2015 01 29 at 18 03 43

The forEach() method is the new default method of java.lang.Iterable. It takes a Consumer interface as a parameter. Consumer is a functional interface as it only has one method. In Java 8 it would be possible to pass a lambda expression to forEach(). In MPS you can pass the MPS closure. A closure knows the type of the parameter taken by forEach() while generating and it will be generated exactly to the correct instance of the Consumer.

Last modified: 5 July 2019