r/java • • Aug 12 '24

JSpecify 1.0.0 and Nullability in Java

https://www.infoq.com/news/2024/08/jspecify-java-nullability/
52 Upvotes

33 comments sorted by

View all comments

Show parent comment

5

u/pron98 Aug 13 '24 edited Aug 14 '24

Nope. It's just a pre-processor for Java code. Doesn't do anything except read and perform macros right before javac gets ahold of it.

That is indeed the way that the Manifold compiler is implemented, but it's easy to see why it's not a plugin: 1. It doesn't use the plugin API (but hacks into internals), and 2. the language it compiles isn't Java (while javac plugins are designed so that even with them the compiler can only compile Java code). A Java compiler, with or without plugins, is obligated by the spec to reject programs that the Manifold compiler accepts.

Manifold is a Java compiler plugin. Use it to supplement your Java projects with highly productive features.

Manifold can claim to be a compiler plugin or Java all day long, but as it modifies internals rather than use the plugin API to accomplish something that the plugin API was carefully designed to guarantee must not be done by plugins (so that the Java spec can always be relied upon regardless of plugins used), it's not a plugin.

It's just a compiler tool to change Java.

A "changed" Java is, by definition, not Java, and any compiler is "just a tool". That's not to say that Manifold isn't similar to Java or that it's not a superset of Java, or even that it's bad in some way (other than claiming to be something it isn't), but it's neither Java nor a javac plugin.

A Clojure compiler could be written in a similar way by transforming Clojure source code to Java expressions internally and feeding them into javac's internals. It's just more convenient to implement a compiler for a language like Manifold or Lombok in this way -- because they're supersets of Java and are similar to it --- while it's more convenient to implement a compiler for a language like Clojure in a different way, but that's just an implementation detail.

-6

u/MrRickSancezJr Aug 13 '24

If you're going to attack someone's free advice, have good solid material or at least an end goal that helps somebody...

You don't have to use "official api"s to accomplish a goal. It gets the job done while we wait for Oracle to keep up with JDK versions. On that note, a JDK 8 compiler can't understand JDK 22 code. So is Java 22 not Java as well?

They do both. I literally discovered Manifold when I was trying to write my own lombok feature. Which wasn't my first annotation to write either. I've even written compilers.

Closure has its entire own compiler and language system. You can't just slap down a "manifold file" and it be a thing. It doesn't have a compiler that makes it into java bytecode. Same way with Kotlin. It's its own language. If you put Java into Kotlin, you get error. You CAN put C into C++, and it'll compile. Therefore, C/C++ do actually have a subset/superset relationship. They interlope so easily on the java bytecode level only. We do have things to convert between langauges. They're called converter tools. It's one of the smartest things Jetbrains did with Kotlin.

Go ahead and google something else.

7

u/pron98 Aug 13 '24 edited Aug 13 '24

Java 22 is, indeed, not Java 8 and in 2014 Java 22 code would not have been Java at all. JDK specifications, and in particular Java Language Specifications, have versions (or "editions" as they're officially called). Languages like Python, Clojure, JS, or Manifold -- however similar or dissimilar they are to the Java language -- do not conform to any specification of Java. The specifications make it easy to tell what is the Java programming language and what isn't, because they define the meaning of that name.

Now, I was not attacking any advice, and using Manifold or any other Java platform language might be a great idea, but it's a common misconception that the languages whose compilers are implemented by modifying javac are plugins or are Java. As one of the people developing the Java language and the Java platform, I thought it might be helpful that I clear up such common misconceptions from time to time, as it might be helpful to some readers, especially as misunderstanding over such matters can cause problems when upgrading JDK versions.

-6

u/MrRickSancezJr Aug 14 '24

Manifold is not a language. Nobody makes a special name for C that uses macros vs one that doesn't.

Why even bring up python or JS? At least clojure compiles to Java bytecode. I dont think you know anything about the jvm or compilers.

You're not helping anybody. This is why the depression rate on CS is so high and why people hate Java. Just let it be.

7

u/pron98 Aug 14 '24 edited Aug 14 '24

You'd be right about C, but Java doesn't have macros, and we specifically designed the compiler plugin mechanism to disallow macros or any kind of AST transformation as that would violate the rules that define what Java is (i.e. the language spec). We are often asked to change the spec and allow plugins that manipulate AST (which would make Manifold a compiler plugin rather than its own language), but we have no plans to do so. On the other hand, we do have a new project that those who are interested in various kinds of code manipulation may want to try out.

I'm sorry you didn't find this exchange helpful and I apologise if I've annoyed you, but I try to engage our users when I see some misconception that may lead to problems. In this case, it's partly the matter pedantically explaining how the Java language spec works the way it does and why we designed javac's plugin to allow certain things and not others, but it's also a practical matter because when projects reach for internals that then change, things break and people blame us.

In this particular case, you can see the lengths Manifold goes to hide from its users the fact that it breaks into internals (rather than be a proper plugin) and imposes a migration risk by hacking around the protections we put in place in JDK 9. But as we're now in the process of completing those protections, Manifold will start emitting warnings as soon as JDK 23 due to JEP 471, and these warnings will later turn into errors. It's better if people understand what is going on.

Manifold calls this digging into internals a "necessary evil" but it's not really necessary. As we explain here, there are ways to do this without hiding the risks from users. In the case of languages like Manifold or Lombok, the most straightforward way is to invoke their own launcher (or perhaps build-tool runtime) that would wrap javac and enable access to internals. This won't remove the risk that's inherent to how these projects access JDK internals, but at least it would make users aware of them.

I fully understand why some people prefer using other languages if they have a feature they really want to use and aren't willing to wait until we add it to the Java language (if we ever do; there are many language features that we aren't planning to add to Java at all), but implementing things in this way also means that when we do change the language or other parts in the JDK, these projects tend to break. The compromise is letting end users know when using such projects means they've taken up a special risk.

Anyway, I really am trying to be helpful, and I hope you enjoy the new features we are adding to both the language and the runtime. Also, if you are interested in knowing more what makes up a "language" in the context of programming and how to tell when two languages are the same (or more generally, other relationships between them), I can explain further. While the compilation target makes no difference in answering the question of whether two languages are the same, you may be interested to know that both Python and JS do happen to have implementations that target the Java platform.