Symbolic annotation processor

bsrbnd bsrbnd at
Thu Mar 17 11:26:34 UTC 2016


After a very fast and superficial reading of the JLS, I wrote some
jtreg tests for points that might be impacted
by the symbolic-processor. Source code of the tests, grouped in two
files for more readability, can be found at:

Some of them show a bit more precisely the benefit of incorporating in
some way the symbolic programming paradigm
(the ability to manipulate the symbols of a language) to enhance the
reflection (and perhaps even more...).

As generics are earased during runtime, it could be hard to get the
reflective elements of members using generics
heavily (and even harder to maintain).
Consider the following example inspired by JLS8, chapter 4.4:

class MyTypeVariable {
    public static class C {}
    public static interface I {}

    @Symbolic private <T extends C & I> void myMethod(T t, T[] u) {}

Without the symbolic view of members, you have to write something like
the following to get the reflective object:
Method m = MyTypeVariable.class.getDeclaredMethod("myMethod",

But, with the symbolic-processor, you delegate the generics and
erasures handling to the compiler:
Method m = MyTypeVariable.$myMethod.reflect();

This is, of course, easier to write and to maintain.

I've also tried to go further with this paradigm by introducing
dynamically constructed symbolic expressions
(a bit like LISP does) and evaluate them on a particular instance, for example:

class DynamicExpression {
    @Symbolic private Integer a = 3;
    @Symbolic private Integer b = 2;
    @Symbolic private Integer c = 1;

    @Symbolic public Integer add(Integer i, Integer j) {return i + j;}
    @Symbolic public Integer sub(Integer i, Integer j) {return i - j;}

MethodSymbol $symbolicExpression = $add.apply($a, $sub.apply($b, $c));
Integer d = (Integer)$symbolicExpression.evaluate(new DynamicExpression());

This expression evaluates naturally to 4 for this instance.

The full running example can be found at:

Symbolic expressions could be useful for evolutionary algorithms used
in genetic programming, where dynamic
expressions are iteratively crossed-over and mutated to find the best
approximation of a solution.

I hope that I didn't miss anything in the JLS and that something like
this hasn't already been incorporated
in any existing project.



2015-12-10 14:50 GMT+01:00 bsrbnd <bsrbnd at>:
> Hi,
> Following our discussion (October 2015), I wrote an
> annotation-processor (only a draft for now) that gives a symbolic view
> of class members, for example:
> @Symbolic private Object myField;
> @Symbolic("_") private void myMethod(Object param) {}
> We can either access the name or the reflective object of members the
> following way:
> String name = $myField.toString(); // $ is the default prefix
> Method m = _myMethod.reflect(); // _ is a custom prefix, useful for
> overloaded identifiers
> You can find a simple example here:
> Full source code of the annotation-processor prototype is given there:
> Comments are welcome, but I know this is probably not the right
> mailing-list for that.
> Regards,
> Bernard

More information about the compiler-dev mailing list