Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can 'groovyc' be told to just generate stubs? (Joint-compile of Java+Groovy+Kotlin)

I am trying to get joint/mixed compilation between all of Java, Groovy and Kotlin. I am currently working on a theory that it should be possible if groovyc can be made to just emit stub-files as a first stage.

Can it? Otherwise, any pointers towards making such a feature out of the sources of Groovy?

Here's the idea of full mixed compilation:

  1. Have Groovyc create Java-stubs for all its classes. AFAIU, it does this "blind", file by file, without seeing any other sources. (This is the kicker, and what this question is about: Have groovyc (or some part of it) generate only the stubs).
  2. Have Kotlinc compile Kotlin-files, basing typing on the Java files for Java, and the Java-stubs for Groovy (by adding them to kotlinc's classpath)
  3. Have Javac compile Java-files, basing typing on the Java-stubs of Groovy, and the class-files of Kotlin (by adding them to javac's classpath, or employ -sourcepath for the Java-stubs).
  4. Have Groovyc compile Groovy-files, basing typing on class-files for Java, and the class-files for Kotlin (by adding them to groovyc's classpath).
like image 785
stolsvik Avatar asked Dec 13 '18 10:12

stolsvik


People also ask

Is Groovy interpreted or compiled?

Groovy is a compiled language, but it allows you to execute scripts. Calling groovy like you did compiles and runs the script.

How Groovy compiler works?

The Groovy compiler seems to compile directly from source to bytecode: groovyc is the Groovy compiler command line tool. It allows you to compile Groovy sources into bytecode. It plays the same role as javac in the Java world.

How do I compile a Groovy script?

For most Groovy scripts I use, I simply run the script from its Groovy source code as-is and allow the compilation to take place implicitly. However, it can be helpful at times to use groovyc to compile Groovy code into . class files and then execute those . class files via the normal Java launcher (java).


1 Answers

This is relatively easy to do programmatically but not something currently supported from the command line. For programmatic usage, you'd need something like [1], but with the phase on the referenced line changed to SEMANTIC_ANALYSIS. You'd also need to keep the stubs as per [2].

To enable this from the command line, the FileSystemCompiler would need to know about a finishing phase and the unit.compile() statements at [3] would need to be made aware of that phase. But I haven't actually tried those changes myself.

[1] https://github.com/apache/groovy/blob/master/src/test/groovy/bugs/Groovy6086Bug.groovy#L63

[2] https://github.com/apache/groovy/blob/master/src/test/groovy/bugs/Groovy9031.groovy#L33

[3] https://github.com/apache/groovy/blob/master/src/main/java/org/codehaus/groovy/tools/FileSystemCompiler.java#L309-L317

like image 149
Paul King Avatar answered Oct 17 '22 16:10

Paul King