JaroslavTulach at 14:20, 19 April 2023 - 2023-04-19 14:20:31

←Older revision Revision as of 14:20, 19 April 2023
Line 103: Line 103:
$ /jdk-11/bin/java --limit-modules java.base,jdk.zipfs -cp compiler.jar:. ModernJavaCompiler
$ /jdk-11/bin/java --limit-modules java.base,jdk.zipfs -cp compiler.jar:. ModernJavaCompiler
Compiled OK
Compiled OK
-
/jdk-11/bin/java R
+
$ /jdk-11/bin/java R
R[name=Hello World!]
R[name=Hello World!]
</source>
</source>

JaroslavTulach at 14:20, 19 April 2023 - 2023-04-19 14:20:11

←Older revision Revision as of 14:20, 19 April 2023
Line 102: Line 102:
$ curl -o compiler.jar https://repo1.maven.org/maven2/org/frgaal/compiler/20.0.0/compiler-20.0.0.jar
$ curl -o compiler.jar https://repo1.maven.org/maven2/org/frgaal/compiler/20.0.0/compiler-20.0.0.jar
$ /jdk-11/bin/java --limit-modules java.base,jdk.zipfs -cp compiler.jar:. ModernJavaCompiler
$ /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
Compiled OK
/jdk-11/bin/java R
/jdk-11/bin/java R

JaroslavTulach at 14:19, 19 April 2023 - 2023-04-19 14:19:46

←Older revision Revision as of 14:19, 19 April 2023
Line 10: Line 10:
import java.net.URI;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URISyntaxException;
 +
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Collections;
import java.util.Collections;
Line 27: Line 28:
public static void main(String... args) throws Exception {
public static void main(String... args) throws Exception {
JavaCompiler compiler = javax.tools.ToolProvider.getSystemJavaCompiler();
JavaCompiler compiler = javax.tools.ToolProvider.getSystemJavaCompiler();
-
List<String> arguments = Arrays.asList(args);
+
List<String> arguments = new ArrayList<>(Arrays.asList(args));
 +
arguments.add("-d");
 +
arguments.add(".");
List<JavaFileObject> sources = Collections.singletonList(new Src(CODE));
List<JavaFileObject> sources = Collections.singletonList(new Src(CODE));
JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, arguments, null, sources);
JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, arguments, null, sources);
Line 97: Line 100:
<source lang="bash">
<source lang="bash">
-
$ mkdir -p META-INF/services
+
$ curl -o compiler.jar https://repo1.maven.org/maven2/org/frgaal/compiler/20.0.0/compiler-20.0.0.jar
-
$ 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
$ /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: [options] system modules path not set in conjunction with -source 17
Line 111: Line 112:
Hey, using '''record''' on JDK-11!
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.
+
Having '''record''' (and other [[JDK]]-20 language features) available on old JDKs is great, isn't it?
Embed [[Frgaal]] into your own programs and use the latest [[Java]] features on any [[JDK]]!
Embed [[Frgaal]] into your own programs and use the latest [[Java]] features on any [[JDK]]!

JaroslavTulach: /* Can I use Frgaal via {{JDK|javax/tools|ToolProvider}} API? */ - 2022-04-25 16:55:24

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

←Older revision Revision as of 16:55, 25 April 2022
Line 105: Line 105:
2 warnings
2 warnings
Compiled OK
Compiled OK
-
~/bin/jdk-11/bin/java R
+
/jdk-11/bin/java R
R[name=Hello World!]
R[name=Hello World!]
</source>
</source>

JaroslavTulach: /* Can I use Frgaal via {{JDK|javax/tools|ToolProvider}} API? */ - 2022-04-24 13:54:51

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

←Older revision Revision as of 13:54, 24 April 2022
Line 4: Line 4:
=== Can I use [[Frgaal]] via {{JDK|javax/tools|ToolProvider}} [[API]]? ===
=== Can I use [[Frgaal]] via {{JDK|javax/tools|ToolProvider}} [[API]]? ===
-
Let's imagine we have a following code (that invokes the {{JDK|javax/tool|JavaCompiler}} via {{JDK|javax/tool|ToolProvider}} for a custom ''CODE'' snippet defining {{JDK|java/lang|Record}} with main method:
+
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">
<source lang="java">

JaroslavTulach at 07:07, 23 April 2022 - 2022-04-23 07:07:18

←Older revision Revision as of 07:07, 23 April 2022
Line 87: Line 87:
Can we fix that? Sure...
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 use it to disable standard [[JavaC]]:
+
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">
<source lang="bash">

JaroslavTulach at 07:06, 23 April 2022 - 2022-04-23 07:06:01

←Older revision Revision as of 07:06, 23 April 2022
Line 84: Line 84:
^
^
</source>
</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 use it to disable standard [[JavaC]]:
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 use it to disable standard [[JavaC]]:

JaroslavTulach at 07:04, 23 April 2022 - 2022-04-23 07:04:08

←Older revision Revision as of 07:04, 23 April 2022
Line 18: Line 18:
class ModernJavaCompiler {
class ModernJavaCompiler {
-
private static final String CODE =
+
private static final String CODE = ""
-
"record R (String name) {\n"
+
+ "record R (String name) {\n"
+ " public static void main(String... args) {\n"
+ " public static void main(String... args) {\n"
+ " System.out.println(new R(\"Hello World!\"));\n"
+ " System.out.println(new R(\"Hello World!\"));\n"

JaroslavTulach at 07:03, 23 April 2022 - 2022-04-23 07:03:49

←Older revision Revision as of 07:03, 23 April 2022
Line 18: Line 18:
class ModernJavaCompiler {
class ModernJavaCompiler {
-
private static final String CODE = "record R (String name) {\n"
+
private static final String CODE =
 +
"record R (String name) {\n"
+ " public static void main(String... args) {\n"
+ " public static void main(String... args) {\n"
+ " System.out.println(new R(\"Hello World!\"));\n"
+ " System.out.println(new R(\"Hello World!\"));\n"

JaroslavTulach: /* Can I use Frgaal via {{JDK|javax/tools|ToolProvider}} API? */ - 2022-04-23 07:03:33

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

←Older revision Revision as of 07:03, 23 April 2022
Line 4: Line 4:
=== Can I use [[Frgaal]] via {{JDK|javax/tools|ToolProvider}} [[API]]? ===
=== Can I use [[Frgaal]] via {{JDK|javax/tools|ToolProvider}} [[API]]? ===
-
Let's imagine we have a following code (that invokes the {{JDK|javax/tool|JavaCompiler}} via {{JDK|javax/tool|ToolProvider}} for a custom ''CODE'' snippet that uses [[JDK]]17 {{JDK|java/lang|Record}}:
+
Let's imagine we have a following code (that invokes 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">
<source lang="java">