#668 Java FFI - use Java annotations

brian Mon 13 Jul 2009

It would be desirable to annotate Fan classes and slots with Java annotations. This would be especially useful to developers writing J2EE classes in Fan.

The obvious mechanism would be to utilize facets which have the similar semantics and syntax as Java annotations.

This feature needs to be taken into account during symbol/facet redesign.

brian Mon 13 Jul 2009

Promoted to ticket #668 and assigned to brian

liamstask Thu 10 Jun 2010

Thought I'd bump this. With some servlet support in place, next up is integrating with some annotation based APIs (unfortunately). Any chance of this sneaking in sometime in the near future?

brian Thu 10 Jun 2010

I've messed around with it a bit, but putting all the bytecode and class support in place looks to be a really big feature with a lot of upfront design work. So I wasn't really targeting this feature for 1.0. If anyone is interested, prototyping a solution would probably go a long way to accelerating this.

liamstask Fri 11 Jun 2010

Bummer :)

Is there a document/thread that captures any of the design thought/discussion that has gone into it thus far (if any)? I'm not sure I'll be able to to help much, but it would be helpful to be aware of any existing context while taking a look.

brian Fri 11 Jun 2010

The new facet design we did earlier this year was designed to make integrating Java annotations easy. Here is the proposal:


Consider the following Java annotations:

package com.acme
public @interface AnnoA { }
public @interface AnnoB { String value(); }
public @interface AnnoC { long x(); String y(); }


The Fantom usage of these annotations would be importing the types into the namespace via standard Java FFI. Then using the Java types as Fantom facets:

import [java] com.acme

@AnnoB { value = "some value }
@AnnoC { x = 45; y = "foo" }
class Fan {}

This portion of the feature requires enhancing the JavaBridge in the compiler to map Java annotation types to Fantom facet types. I suspect it will require a few tweaks to the core compiler and CBridge class to provide the appropriate FFI hooks.

Java Runtime

The Java annotations would now be declared in fcode as normal Fantom facets, which are declared in a special fcode attribute as a standardized Fantom serialization string. The Java runtime would need to be enhanced to detect Facets which are also Java annotations and emit the appropriate Java classfile attributes.

If this is done correctly then the facet/annotations are reflected on the Fantom side as normal facets and reflected on the Java side as normal annotations.

brian Mon 13 Sep 2010

Ticket resolved in 1.0.55

This feature is finally complete for the upcoming build. I expect the ability to use Java annotations in Fantom code was probably one of the biggest holes in the Java FFI.

I tried to be pretty thorough in our annotation support with all the various types supported by Annotations:

Java                     Fantom
----                     -------
boolean                  sys::Bool literal
byte, short, int, long   sys::Int literal
float, double            sys::Float literal
String                   sys::Str literal
Class                    sys::Type literal
enum                     Java FFI enum field access
arrays of above          sys::List literal of above

For example, given the Java annotation:

public @interface TestAnno
  int count() default 0;
  String str() default "";
  Class cls() default Object.class;
  ElementType[] enums() default {};

The annotation would be applied in Fantom as follows:

  count = 123
  str   = "string value"
  cls   = Str#
  elems = [ElementType.FIELD, ElementType.METHOD]

See testJava::AnnotationsTest for examples of all the types (primitives, primitive arrays, etc).

When you apply a Java Annotation to a Fantom type or slot as facet, the Java runtime will generate the appropriate bytecode so that is shows up in Java reflection. However those annotations do not show up in Fantom reflection. Note that Fantom does not currently support facets (or annotations) on parameters or local variables.

Login or Signup to reply.