Jump to content

GNU Compiler for Java

From Wikipedia, the free encyclopedia

Template:Short description Template:Use mdy dates Template:Infobox software The GNU Compiler for Java (GCJ) is a discontinued free compiler for the Java programming language. It was part of the GNU Compiler Collection.[1][2]

GCJ compiles Java source code to Java virtual machine (JVM) bytecode or to machine code for a number of CPU architectures. It could also compile class files and whole JARs that contain bytecode into machine code.[3][4]

History

The GCJ runtime-libraries original source is from GNU Classpath project, but there is a code difference between the Template:Mono libraries. GCJ 4.3 uses the Eclipse Compiler for Java as a front-end.[5]

In 2007, a lot of work was done to implement support for Java's two graphical APIs in GNU Classpath: AWT and Swing. Software support for AWT was incomplete. "Once AWT support is working then Swing support can be considered. There is at least one free-software partial implementations of Swing that may be usable.".[6] By the time of its removal, GCC had support for up to version 1.4 of Java (and thus lacked support for most of the later features, such as generics, annotations, or JPMS modules; despite this, portions of the [[New I/O (Java)|Template:Mono library]] were implemented).[7]

As of 2015, there were no new developments announced from GCJ and the product was in maintenance mode, with open-source Java toolchain development mostly happening within OpenJDK.[8] GCJ was removed from the GCC trunk on September 30, 2016.[9][10] Announcement of its removal was made with the release of the GCC 7.1, which does not contain it.[11] GCJ remains part of GCC 6.

In 2023, an developer named Max Downey Twiss began independent work on a revival for GCJ, with GNU Classpath replaced with OpenJDK, with the goal of restoring GCJ as a native machine-code compiler for Java.[12][13][14]

Performance

The compilation function in GCJ should have a faster start-up time than the equivalent bytecode launched in a JVM when compiling Java code into machine code.[15]

Compiled Native Interface

The Compiled Native Interface (CNI), previously named "Cygnus Native Interface", is a software framework for the GCJ that allows Java code to call, and be called by, native applications (programs specific to a hardware and operating-system platform) and libraries written in C++.[16][17] It could work with C++ up to C++17 (the latest version of C++ supported by GCC 6.5, the final version of GCJ).

CNI closely resembles the Java Native Interface (JNI) framework which comes as a standard with various Java virtual machines.

GCJ additionally supports a "Java linkage" with <syntaxhighlight lang="cpp" inline>extern "Java"</syntaxhighlight>, which is used when referencing Java code compiled by GCJ and used in C++ code to indicate that a symbol that is consumed originates from Java.[18]

For example, consider the following Java code: <syntaxhighlight lang="java"> package org.wikipedia.examples;

public class Foo {

   public int add(int a, int b) {
       return a + b;
   }

} </syntaxhighlight>

Then, from C++ it can be consumed as so: <syntaxhighlight lang="cpp">

  1. include <gcj/cni.h>
  2. include <java/lang/Object.h>

// Automatically generated header

  1. include <org/wikipedia/examples/Foo.h>

using namespace java::lang;

using org::wikipedia::examples::Foo;

extern "Java" {

int addThroughJava(int a, int b) {

   Foo* foo = new Foo();
   return foo->add(a, b);
   // Note: foo should not be deleted, as it is garbage collected

}

namespace org::wikipedia::examples {

   // Inheriting a Java object
   // Only single inheritance supported
   class Bar: public Object {
       // ...
   };

}

} </syntaxhighlight>

An implementation of Template:Mono (which contains the C++ headers for the Java Class Library) may be found here (in a GCC 6 release). Java classes are exposed to C++ as (auto-generated) header files by one class per header[19], preserving namespace structure per Java package.[20]

All Java reference types X are translated to a pointer X* in C++. For example, java.lang.String becomes java::lang::String*.[21] Because Java classes are managed by the Java garbage collector, they should not be deleted using the <syntaxhighlight lang="cpp" inline >delete</syntaxhighlight> operator from C++, even though they are both instantiated using <syntaxhighlight lang="cpp" inline>new</syntaxhighlight>[22]; the Java objects are automatically deleted by the garbage collector.[23] Java classes, when used in C++, maintain the same field order and alignment, and will not widen 8-bit and 16-bit native types to 32 bits.[24]

Java reflection on Java objects in C++ possible using CNI, featuring the jfieldID and jmethodID (as seen in JNI).[25] (Java already offers java.lang.reflect.Field/java.lang.reflect.Method, but these are not used by GCJ.) While Java generics were never implemented by the time GCJ was removed, some discussions regarding how they would operate with C++ (for example, directly translating generics to templates or omitting them entirely, which the JVM does at runtime) had taken place.

Java primitives may map exactly to the corresponding C/C++ primitive. However, in the case where they do not (as C/C++ do not specify the exact width of integer types), typedefs are provided as the following:[26]

Java type C/C++ alias Description Closest C/C++ type
<syntaxhighlight lang="java" inline>char</syntaxhighlight> jchar 16-bit Unicode character <syntaxhighlight lang="cpp" inline>char16_t</syntaxhighlight>
<syntaxhighlight lang="java" inline>boolean</syntaxhighlight> jboolean Boolean (logical true/false value) <syntaxhighlight lang="cpp" inline>bool</syntaxhighlight>
<syntaxhighlight lang="java" inline>byte</syntaxhighlight> jbyte 8-bit signed integer <syntaxhighlight lang="cpp" inline>char</syntaxhighlight> or <syntaxhighlight lang="cpp" inline>int8_t</syntaxhighlight>
<syntaxhighlight lang="java" inline>short</syntaxhighlight> jshort 16-bit signed integer <syntaxhighlight lang="cpp" inline>short</syntaxhighlight> or <syntaxhighlight lang="cpp" inline>int16_t</syntaxhighlight>
<syntaxhighlight lang="java" inline>int</syntaxhighlight> jint 32-bit signed integer <syntaxhighlight lang="cpp" inline>int</syntaxhighlight> or <syntaxhighlight lang="cpp" inline>int32_t</syntaxhighlight>
<syntaxhighlight lang="java" inline>long</syntaxhighlight> jlong 64-bit signed integer <syntaxhighlight lang="cpp" inline>long</syntaxhighlight> or <syntaxhighlight lang="cpp" inline>int64_t</syntaxhighlight>
<syntaxhighlight lang="java" inline>float</syntaxhighlight> jfloat 32-bit IEEE floating-point number <syntaxhighlight lang="cpp" inline>float</syntaxhighlight> or <syntaxhighlight lang="cpp" inline>float32_t</syntaxhighlight>
<syntaxhighlight lang="java" inline>double</syntaxhighlight> jdouble 64-bit IEEE floating-point number <syntaxhighlight lang="cpp" inline>double</syntaxhighlight> or <syntaxhighlight lang="cpp" inline>float64_t</syntaxhighlight>

<syntaxhighlight lang="cpp" inline>void</syntaxhighlight> maps exactly the same between languages, as it does not refer to any value. It was recommended to use these Java typedefs over the native C++ types, to avoid possible errors.[26]

Java arrays are implemented with the class template JArray<T>, and inherit java::lang::Object (and implement <syntaxhighlight lang="cpp" inline>operator[]</syntaxhighlight>). Additional typedefs for each pointer to array of Java primitives are provided.[27]

Variadic functions in Java are internally represented as arrays; thus, in GCJ a signature with variadic parameter like <syntaxhighlight lang="java" inline>void f(T... args);</syntaxhighlight> is generated as <syntaxhighlight lang="cpp" inline>void f(JArray<T> args);</syntaxhighlight>.

Additionally, common classes receive the following typedefs:[21]

Java type C/C++ alias Description
java.lang.Object jobject (for java::lang::Object*) Universal supertype of all reference types, representing an object
java.lang.String jstring (for java::lang::String*) String type
java.lang.Class jclass (for java::lang::Class*)Template:Efn Runtime state and metadata of any reference type in Java

Note that in JNI, jobject, jstring and jclass were opaque handles without any data[28][29]; in CNI, the Java classes they point to have data.

Additionally, all Java classes X expose a static field <syntaxhighlight lang="cpp" inline>X::class$</syntaxhighlight> in C++, which behaves equivalently to <syntaxhighlight lang="java" inline>X.class</syntaxhighlight> in Java. However, the class$ field is of type java::lang::Class (not java::lang::Class*). The Java <syntaxhighlight lang="java" inline>null</syntaxhighlight> was originally translated to the older C++ <syntaxhighlight lang="cpp" inline>NULL</syntaxhighlight> (however the null pointer constant <syntaxhighlight lang="cpp" inline>nullptr</syntaxhighlight> may be substituted due to implicit conversions).[30] Additional classes, gnu::gcj::RawData and gnu::gcj::RawDataManaged respectively denote non-scanned reference types (which will not be checked by the compiler and can store any C/C++ data structure), and data which may be marked by the memory manager and be garbage-collected.[31]

In C++, java::lang::String* (or jstring) must be created manually using CNI utility functions (analogous to those of JNI); they cannot be implicitly converted from <syntaxhighlight lang="cpp" inline>const char*</syntaxhighlight> or C++ std::string.[32]

The Java <syntaxhighlight lang="java" inline>synchronized</syntaxhighlight> blocks are implemented in C++ using the ::JvSynchronize class (which lives in the global namespace).[33] For example, the following is equivalent to Java <syntaxhighlight lang="java" inline>synchronized (obj) { /* code... */ }</syntaxhighlight> <syntaxhighlight lang="cpp"> {

   JvSynchronize _(obj); // create dummy lock, destroyed upon scope end
   // code...

} </syntaxhighlight>

In C++, any class which enforces the Java inheritance model must inherit the java::lang::Object base class, and may inherit at most one base class. Although it was planned, interface inheritance awareness was never implemented in GCJ by the time of its removal.[34] Despite this, an attribute <syntaxhighlight lang="cpp" inline>gnu::java_interface</syntaxhighlight> existed to mark a class as a Java interface, along with a pragma <syntaxhighlight lang="cpp" inline>#pragma interface</syntaxhighlight>.[18] Methods of interfaces, however, could be implemented, but calling a method of a superinterface required casting the class back to the superinterface.[34] Additionally, Java access specifiers were never enforced strictly in C++; only <syntaxhighlight lang="cpp" inline>private</syntaxhighlight> fields and methods from Java were properly mapped to <syntaxhighlight lang="cpp" inline>private</syntaxhighlight> in C++, but everything else was mapped to <syntaxhighlight lang="cpp" inline>public</syntaxhighlight>.[24]

Methods map directly between Java and C++ with the same names and corresponding types, and preserved static methods (still called using :: for resolution). Name mangling for overloaded methods use the same encoding scheme between Java and C++. While Java does not allow a constructor to be a native method itself, a constructor may call a native method.[35]

Failed allocation of a Java class will throw a java::lang::OutOfMemoryError.[36]

Template:Mono use POSIX signals internally, and using the same signal may interfere with Template:Mono or even cause it to fail. For example, SIGSEGV generates a java::lang::NullPointerException, java::lang::Runtime::exec() makes use of SIGCHLD, while other signals (potentially platform-dependent) may be used by the memory manager or java::lang::Thread::interrupt().[31]

Comparison of language use

The authors of CNI claim for various advantages over JNI:[37]

Template:Cquote

CNI depends on Java classes appearing as C++ classes. For example,[38] given a Java class,

<syntaxhighlight lang="java"> package org.wikipedia.examples;

public class MyInteger {

   private int i; 
   public MyInteger(int i) { 
       this.i = i; 
   }
   public int get() {
       return i;
   }
   public void set(int j) {
       i = j;
   }
   public static final MyInteger ZERO = new MyInteger(0);

} </syntaxhighlight>

one can use the class thus:

<syntaxhighlight lang="cpp">

  1. include <gcj/cni.h>
  2. include <org/wikipedia/examples/MyInteger.h>

using org::wikipedia::examples::MyInteger;

MyInteger* mult(MyInteger& p, jint k) {

   if (k == 0) {
       return MyInteger::ZERO; // Static member access.
   }
   return new MyInteger(p.get() * k);

} </syntaxhighlight>

GCJ allows the usage of Java exceptions in C++, but they cannot be used in the same translation unit with C++ exceptions.[39] The pragma <syntaxhighlight lang="cpp" inline>#pragma GCC java_exceptions</syntaxhighlight> must be used to ensure destructors are called upon propagating a Java exception through C++.[40] Despite this, no support for <syntaxhighlight lang="java" inline>finally</syntaxhighlight> blocks were ever included. Java exceptions are caught by pointer rather than by reference, because Java references are represented in CNI through pointers. Additionally, they are not <syntaxhighlight lang="cpp" inline>const</syntaxhighlight>-qualified because Java objects are inherently mutable references.[41]

For example, the following catches a Java exception, while using the Invocation API: [30] <syntaxhighlight lang="cpp">

  1. pragma GCC java_exceptions
  1. include <gcj/cni.h>
  2. include <java/lang/Integer.h>
  3. include <java/lang/Math.h>
  4. include <java/lang/NumberFormatException.h>
  5. include <java/lang/String.h>
  6. include <java/lang/System.h>
  7. include <java/io/File.h>
  8. include <java/io/IOException.h>
  9. include <java/net/URI.h>

using namespace java::lang; using java::io::File; using java::io::IOException; using java::net::URI;

// Helper literal to create java::lang::String* String* operator""_js(const char* s, size_t len) {

   return ::JvNewStringLatin1(s, len);

}

int main(int argc, char* argv[]) {

   try {
       ::JvCreateJavaVM(nullptr);
       ::JvAttachCurrentThread(nullptr, nullptr);
       ::JvInitClass(&System::class$);
       ::JvInitClass(&Math::class$);
       JArray<String*>* args = ::JvConvertArgv(argc, argv); // String[] args
       jdouble one = Math::sin(Math::PI / 2);
       URI* location = new URI("file:///C:/temp/example.txt"_js);
       File* file = new File(location);
       System::out->println(file->getAbsolutePath());
       String* invalid = "not a number"_js;
       jint result = Integer::parseInt(invalid);
   } catch (NumberFormatException* e) {
       System::err->println("Failed to format number: "_js->concat(e->getMessage()));
       e->printStackTrace();
   } catch (IOException* e) {
       System::err->println("Failed I/O operation: "_js->concat(e->getMessage()));
       e->printStackTrace();
   }
   return 0;

} </syntaxhighlight>

Note that Java classes with static fields, such as java::lang::System and java::lang::Math, must first be initialized using a ::JvInitClass() function call.

See also

Template:Portal

Notes

Template:Notelist

References

Template:Reflist

  • {{#invoke:citation/CS1|citation

|CitationClass=web }}

  • {{#invoke:citation/CS1|citation

|CitationClass=web }}

  • {{#invoke:citation/CS1|citation

|CitationClass=web }}

  • {{#invoke:citation/CS1|citation

|CitationClass=web }}

Template:Java Virtual Machine Template:Java (software platform)

  1. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  2. ↑ Template:Cite book
  3. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  4. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  5. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  6. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  7. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  8. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  9. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  10. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  11. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  12. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  13. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  14. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  15. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  16. ↑ Template:Cite journal
  17. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  18. ↑ 18.0 18.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  19. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  20. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  21. ↑ 21.0 21.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  22. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  23. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  24. ↑ 24.0 24.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  25. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  26. ↑ 26.0 26.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  27. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  28. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  29. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  30. ↑ 30.0 30.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  31. ↑ 31.0 31.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  32. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  33. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  34. ↑ 34.0 34.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  35. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  36. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  37. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  38. ↑ The example comes from: https://gcc.gnu.org/onlinedocs/gcj/Objects-and-Classes.html#Objects-and-Classes Template:Webarchive
  39. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  40. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  41. ↑ Template:Cite conference