days
-4
-6
hours
0
-7
minutes
-1
-1
seconds
-3
-2
search
JAX Developers Puzzle deadline approaching: Play for a chance to win a complimentary JAX London 2018 ticket!
It makes Java development more appealing

Manifold: Alien technology

Scott McKinney
manifold
© Shutterstock / lazyllama

Manifold is out of this world: this framework allows developers to dynamically and seamlessly extend Java’s type system. In this article, Scott McKinney explores how the Manifold framework makes Java development more appealing and productive.

Incident Report

Place of Occurrence: CLASSIFIED
Date: 24/11/17
Time: 22:15Z
Programmer: CLASSIFIED
Language: Java
Tooling: IntelliJ IDEA

Details of Incident:

Witnessed subject create a JSON file, Person.json, as a Java resource in package com.abc.

Subject immediately began using the file from Java classes in the project as if the JSON file were a Java class of type com.abc.Person. As subject added and modified properties in the JSON file, the changes were immediately available elsewhere in the project using code completion etc. Subject later performed a rename refactor on the JSON file, renaming it from Person to Contact. All references to Person were immediately changed to Contact. Likewise, subject frequently performed several usage searches and refactors on JSON properties and types, all behaving as if the JSON were Java.

Importantly, there were no generated class files on disk corresponding with the JSON file after subject compiled the project — no extra build steps, no artifacts created. Subject created several tests for the project and ran them without incident. The JVM appeared to use the JSON file as a Java class file. No extra class loaders were involved, no runtime agents were used. Only a single jar file in the classpath was present: manifold.jar.

Witnessed similar events involving JavaScript files, Properties files, and several others. This technology appears to be capable of working with any type of data source. Essentially, the technology somehow extends and enhances Java’s type system at will.

More recently observed subject perform other bizarre activities, such as declare and use Extension Methods as defined in C# and use Structural Typing similar to TypeScript and Go languages. More to follow regarding these accounts.

Conclusion:

Incident demonstrates beyond-next-generation technology. Recommend further investigation.

SEE MORE: NASA’s ten coding commandments

Earthly origin?

As dreamy as the “incident report” appears, it is 100% factual. Manifold is real and available as an open source project from Manifold Systems; whether or not it is reverse-engineered alien technology is yet to be determined. In any case, you can begin using it with your new or existing Java project.

Using Manifold is easy; simply add it as a dependency to your project. Manifold is designed to work with IntelliJ IDEA, which is available free from JetBrains. Visit the Manifold Setup instructions for information about using Manifold with your project and build tools.

Manifold provides three high-level features:

  • Type-safe metaprogramming — similar in concept to F# type providers
  • Extension Method — comparable to the same feature in Kotlin and C#
  • Structural Typing — much like interfaces in TypeScript and Go

Type-safe metaprogramming

Metaprogramming, a term usually reserved for dynamic languages like JavaScript, Ruby, and Python, is a powerful feature used for dynamic type creation that exploits the lack of design-time type-safety in these languages — all the metaprogramming hocus pocus is a runtime phenomenon. While flexible, metaprogramming presents a challenge for programmers using it because there is no design-time type information available for them to readily discover and use; you just have to “know”.

The allure of Manifold is squarely centered on its ability to perform compile-time as well as runtime metaprogramming. This capability is achieved via Type Manifolds.

Bridging the worlds of information and programming, type manifolds are Java projections of schematized data sources. More specifically, a type manifold transforms a data source into a data type directly accessible in your Java code without a code generation build step or extra compilation artifacts. In essence with Manifold a data source is a data type.

To illustrate, normally you access Java properties resources like this:

java Properties myProperties = new Properties();
myProperties.load(getClass().getResourceAsStream("/abc/MyProperties.properties")); String myMessage = myProperties.getProperty("my.message");

As with any resource file, a properties file is foreign to Java’s type system – there is no direct, type-safe access to it. Instead you access it indirectly using boilerplate library code sprinkled with hard-coded strings.

By contrast, with the Properties type manifold you access a properties file directly as a type:

java String myMessage = MyProperties.my.message;

Concise and type-safe, with no generated files or other build steps to engage.

Almost any type of data source imaginable is a potential type manifold. These include resource files, schemas, query languages, database definitions, templates, spreadsheets, web services, and programming languages. Currently Manifold provides type manifolds for:

  • JSON and JSON Schema
  • JavaScript
  • Properties files
  • Image files
  • Dark Java
  • Manifold Templates
  • DDL and SQL (work in progress)

Discover more about type manifolds here 

SEE MORE: Gosu — Hey look! It’s a pragmatic language for the JVM. A simple language

Extensions

The extension manifold is a special kind of type manifold that lets you augment existing Java classes including Java’s own runtime classes such as String. You can add new methods, annotations, and interfaces to any type your project uses.

Let’s say you want to make a new method on String so you can straightforwardly echo a String to the console. Normally with Java, you might write a “Util” library like this:

java public class MyStringUtil { public static void echo(String value) { System.out.println(value); } }

And you’d use it like this:

java MyStringUtil.echo("Java");

Instead, with Manifold you create an Extension Class:

java @Extension public class MyStringExtension { public static voice echo(@This String thiz) { System.out.println(this); }} 

Here we’ve added a new echo() method to String, so we use it like this:

java "Java".echo();

Extensions eliminate a lot of intermediate code such as “Util” and “Manager” libraries as well as Factory classes. As a consequence, extensions naturally promote higher levels of object-orientation, which results in more readable and maintainable code. Perhaps the most beneficial aspect of extensions, however, relates more to your overall experience than with your development environment. For instance, code-completion conveniently presents all the extension methods available on an extended class.

Extensions provide a lot more capability, learn more here .

SEE MORE: NASA drops 2017 software catalog, bringing space technology down to Earth

Structural typing

Java is a nominally typed language – types are assignable based on the names declared in their definitions. For example:

 ```java public class Foo { public void hello() { println("hello"); } }

public interface Greeting { void hello(); }

Greeting foo = new Foo(); // error ```

This does not compile because Foo does not explicitly implement Greeting by name in its implements clause.

By contrast, a structurally typed language like TypeScript or Go has no problem with this example. Basically, structural typing requires only that the interface methods are implemented; there is no need for a class to declare that it implements a structural interface.

Manifold provides this capability via the @Structural annotation:

java @Structural public interface Greeting { void hello(); }

Adding @Structural to Greeting effective changes it to behave structurally – Java no longer requires classes to implement it by name, only its methods must be implemented.

Note: a class can still implement a structural interface nominally. Doing so helps both people and tooling comprehend your code faster. The general idea is to use an interface structurally when you otherwise can’t use it nominally or doing so over-complicates your code. Learn more about Manifold structural interfaces here .

SEE MORE: Year in review: Java in 2017

Benefits

Manifold’s core technology is a dramatic departure from conventional Java tooling. There is no code generation step in the build, no extra .class files or .java files to manage, no annotation processors, and no extra class loaders to engage at runtime.
Benefits of this approach include:

  • Zero turnaround – live, type-safe access to data; make, discover and use changes instantly
  • Lightweight – direct integration with standard Java, requires no special compilers, annotation processors, or runtime agents
  • Efficient, dynamic – Manifold only produces types as they are needed
  • Simple, open API – you can build your own Manifolds
  • No code generation build step – no generated files, no special compilers
  • IntelliJ IDEA support – all manifold types and extensions work with IntelliJ

Additionally, Manifold is just a JAR file you can drop into your existing project – you can begin using it incrementally without having to rewrite classes or conform to a new way of doing things.

SEE MORE: Java 10 is finally here! A closer look at the new features

Conclusion

Finally, with Manifold, Java bridges the gap separating it from many of the features previously granted exclusively to dynamic languages. What’s more, Manifold delivers these features with type-safety intact. Meta-programming, extension classes, and structural typing are readily available and fully integrated in IntelliJ IDEA. While this may sound impossible or farfetched, you can verify it first-hand — explore Manifold at Manifold Systems. The truth is out there!

Author

Scott McKinney

Scott McKinney is the founder and principle engineer at Manifold Systems. Previously, he was a staff engineer at Guidewire where he designed and created Gosu. He currently pounds code by the truckload while listening to way too much retro synthwave.


Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of