Frgaal - 2022-04-21 15:44:19

Summary: /* Can I use Frgaal via {{JDK|javax/tools|ToolProvider}} API? */


I love [http://frgaal.org Frgaal] and you'll love it too: Wouldn't it be great to have '''record'''s and other latest [[Java]] language features available on your old [[JDK]]s? As old as [[JDK]]8? [[Frgaal]] allows you to do it. There is a [[Maven]] plugin, integration with [[Gradle]], a way to use it from command line... read more at http://frgaal.org


=== Can I use [[Frgaal]] via {{JDK|javax/tools|ToolProvider}} [[API]]? ===

Let's imagine we have a following code invoking the {{JDK|javax/tool|JavaCompiler}} via {{JDK|javax/tool|ToolProvider}} for a custom ''CODE'' snippet defining {{JDK|java/lang|Record}} with main method:

<source lang="java">
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;

class ModernJavaCompiler {
private static final String CODE = ""
+ "record R (String name) {\n"
+ " public static void main(String... args) {\n"
+ " System.out.println(new R(\"Hello World!\"));\n"
+ " }\n"
+ "}\n";

public static void main(String... args) throws Exception {
JavaCompiler compiler = javax.tools.ToolProvider.getSystemJavaCompiler();
List<String> arguments = Arrays.asList(args);
List<JavaFileObject> sources = Collections.singletonList(new Src(CODE));
JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, arguments, null, sources);
Boolean res = task.call();
if (!Boolean.TRUE.equals(res)) {
throw new IllegalStateException("Compilation failed: " + res);
} else {
System.out.println("Compiled OK");
}
}

static class Src extends SimpleJavaFileObject {
private final String code;

public Src(String code) throws URISyntaxException {
super(new URI("memory://Code.java"), Kind.SOURCE);
this.code = code;
}

@Override
public String getName() {
return "Code.java";
}


@Override
public CharSequence getCharContent(boolean bln) throws IOException {
return code;
}
}
}
</source>

This is a regular Java source file that can be compiled using standard [[Javac]] compiler:

<source lang="bash">
$ javac -source 8 -target 8 ModernJavaCompiler.java
</source>

The ''ModernJavaCompiler'' source code then uses {{JDK|javax/tool|JavaCompiler}} on its own to generate class ''R''.
It can be executed [[JDK]]-17 as

<source lang="bash">
$ /jdk-17/bin/java ModernJavaCompiler
Compiled OK
$ /jdk-17/bin/java R
R[name=Hello World!]
</source>

However executing the same steps with [[JDK]]-11 is going to fail as the [[Javac]] from [[JDK]]-11 doesn't support '''record''' keyword:

<source lang="bash">
$ /jdk11/bin/java ModernJavaCompiler
Code.java:1: error: class, interface, or enum expected
record R (String name) {
^
</source>

Can we fix that? Sure...

The idea would be to replace the standard "compiler tool" with the one coming from the [[frgaal]] project. Let's try to disable the regular compiler first. There is an option '''--limit-modules''' in [[JDK]]-11+ and the idea is to use it to disable standard [[JavaC]]:

<source lang="bash">
$ /jdk11/bin/java --limit-modules java.base ModernJavaCompiler
Exception in thread "main" java.lang.NoClassDefFoundError: javax/tools/ToolProvider
</source>

Great! Disabling works. The "compiler tool" is completely missing. Now there is a time to inject [[Frgaal]] instead. Following script shows how to get it all working:

<source lang="bash">
$ mkdir -p META-INF/services
$ echo com.sun.tools.javac.api.JavacTool > META-INF/services/javax.tools.JavaCompiler
$ curl -o compiler.jar https://repo1.maven.org/maven2/org/frgaal/compiler/18.0.0/compiler-18.0.0.jar
$ /jdk-11/bin/java --limit-modules java.base,jdk.zipfs -cp compiler.jar:. ModernJavaCompiler
warning: [options] system modules path not set in conjunction with -source 17
warning: No output directory specified, cannot perform multi-release compilation
2 warnings
Compiled OK
/jdk-11/bin/java R
R[name=Hello World!]
</source>

Hey, using '''record''' on JDK-11!

Having '''record''' (and other [[JDK]]-18 language features) available on old JDKs is great, isn't it? All that is necessary is to provide extra {{JDK|java/util|ServiceLoader}} registration in ''META-INF/services'' path on the class path. Then the [[Frgaal]] compiler gets picked up and is used for compilation even on older [[JDK]]s.


Embed [[Frgaal]] into your own programs and use the latest [[Java]] features on any [[JDK]]!

Rust - 2022-03-26 05:59:56

Summary: New page: [[wikipedia:Rust_(programming_language)|Rust]] is a modern successor of [[C]] - a low level ''system'' programming language without [[Garbage Collection]], but with memory safety! That saf...


[[wikipedia:Rust_(programming_language)|Rust]] is a modern successor of [[C]] - a low level ''system'' programming language without [[Garbage Collection]], but with memory safety! That safety comes from a strict ''linear'' type system - the strongest, but also hardest to use thing in [[Rust]]. [[I]] am just trying to rewrite my [[Sieve of Eratosthenes]] to [[Rust]] and it is almost impossible without reading [https://rust-unofficial.github.io/too-many-lists/ Too Many Lists] book. Will spending days of studying to implement something as trivial pay off?

Garbage Collection - 2022-03-26 05:43:44

Summary:


Every modern language has [[wikipedia:garbage collection (computer science)|garbage collection]]! In a [[language]] like [[C]] one had to manually deallocate all the allocated memory - without such clean up the running program would continually eat more and more memory. [[Garbage Collection]] offers a mechanism to do such cleanup automatically. It comes with some overhead, but at the end of last century [[Java]] demonstrated, it can be implemented quite effectively. Since then every modern language uses it - be it a dynamic language ([[Python]], [[JavaScript]], [[Ruby]], etc.), [[Java]]-like [[language]] like [[CSharp]] or even a compiled language like [[Go]]. There is just a single exception so far: [[Rust]].

GraalSpringTour - 2022-03-21 12:42:44

Summary:


[[Image:Labská.jpg|thumb|right]]
[[Image:Luční.jpg|thumb|right]]

== Canceling Luční bouda ==

I am at Labská bouda, but exhausted. Weather isn't great. I will not make it to Luční, sorry.

I got back home, but it was all a bit tough. First of all I had to start in 300m altitude lower than originally expected - not in [[wikipedia:cs:Horní_Mísečky|Horní Mísečky]], but in [[wikipedia:Špindlerův_Mlýn|Špindlerův Mlýn]] due to Saturday bus schedule. Climbing up was initially fine, but the closer to the top, the more windy and foggy it all got. In spite of that at 11:00 I had ski-skated up to [[wikipedia:Vrbatova_bouda|Vrbatova bouda]]. Honza Horváth (the only co-worker who RSVPed) called that the road from [[wikipedia:Rokytnice_nad_Jizerou|Rokytnice nad Jizerou]] was icy and he'd be delayed. Anyway at 11:30 I got to [https://www.labskabouda.cz/ Labská bouda] - however, to my horror, the cottage was closed. To fulfill my promise of ''happy hour'' (up till 12:30) I had to wait outside trying to contact Honza. When he finally picked the phone up (couldn't sooner as it was too windy to even pick a phone) he was in a shelter kilometer away from [https://www.labskabouda.cz/ Labská bouda] - I encouraged him to continue hoping he would show up in ten minutes. However he didn't and I was freezing for another half an hour. I gave up and went towards [[wikipedia:Martinova_bouda|Martinovka]] to rescue at least myself thinking about calling a rescue mission for Honza (not even picking a phone again). Finally Honza called - he had got lost and realized that only when crossing the Polish border. Honza sounded freezing and tired as well, so we called the meeting off and decided to return back where each of us started. I reached [[wikipedia:Martinova_bouda|Martinovka]], healed my freeze wounds and then quite tired continued down to [[wikipedia:Špindlerův_Mlýn|Špindlerův Mlýn]] bus station.

{{#ev:youtube|NUJCXi8y9No}}

If anyone was waiting for a ''happy hour'' at [https://www.lucnibouda.cz/en/ Luční bouda] then I deeply apologize! I was expecting nice spring weather when planning the [[GraalSpringTour]], but instead of that I got a typical ''Hanč & Vrbata'' storm. The prospect of leaving [[wikipedia:Martinova_bouda|Martinovka]] and going up to the roof of [[wikipedia:Giant_Mountains|Krkonoše]] again was killing me. I wouldn't make it to [https://www.lucnibouda.cz/en/ Luční bouda]. Let's plan something simpler next time!

== Celebrate ==

Let's celebrate [[GraalVM]]. The past, the spring, the future. Let's have a cross-country skiing tour. When? '''Saturday, April 2, 2022'''. Let's meet at two cottages in [[wikipedia:Giant_Mountains|Krkonoše]]:

* '''11.00''' - '''12.30''' [https://www.labskabouda.cz/ Labská bouda] - let's have a snack and a beer and then move on to
* '''16.30''' - '''18.30''' [https://www.lucnibouda.cz/en/ Luční bouda] - let's have a beer, dinner and either stay or move on

Getting there is up to everyone's own skills. People often travel to [[wikipedia:Harrachov|Harrachov]] and then walk/ski around Vosecká bouda to Labská bouda. Or they start in [[wikipedia:Rokytnice_nad_Jizerou|Rokytnice nad Jizerou]] and use the chair lift to get to Lysá hora. [[I]] plan to go from [[wikipedia:cs:Horní_Mísečky|Horní Mísečky]]. Remember, there is going to be some snow (view from [https://www.spindleruv-mlyn.com/cz/webkamera/34-webkamera-labska-bouda-okoli-spindlerova-mlyna-krkonose/ Labská], [https://kamery.humlnet.cz/cz/kamery/lucni-bouda/ Luční]). Hopefully the weather is going to be acceptable.

[[wikipedia:Špindlerův_Mlýn|Špindlerův Mlýn]] is a suitable starting point to get to both locations. Getting to Luční bouda is also possible from [[wikipedia:Pec_pod_Sněžkou|Pec pod Sněžkou]] - just walk the hill up or from Polish side - [[wikipedia:Karpacz|Karpacz]] - either walking (around [[wikipedia:Mały_Staw|Malý staw]]) or taking the chair lift to Kopa. Both locations offer accommodation. According to the best tourist maps [https://mapy.cz/s/kumevavesa it is three hour trip] from one location to the other.

To make [[GraalSpringTour]] more attractive [[I]] plan to buy beers and food for you, if you show up at the above mentioned happy hours and show an evidence of your positive attitude towards [[GraalVM]]!