0

How can I process annotations on synthetic elements? RoundEnvironment.getElementsAnnotatedWith doesn't seem to return any synthetic elements.

Grisu47
  • 530
  • 5
  • 16

1 Answers1

0

I don't fully understand what synthetic elements are, but I do know about annotations. Reflections would be your ticket for acquiring annotations from: class, method, or variable in java. Then you can do whatever it is you'd like from it. This is a good example How to get class annotation in java?

Basically you'll need to get the Class, which contains your var, then locate the var and use getAnnotations method.

Clazz z = MyClass.class;
Field[] f = z.getDeclaredFields();
f[0].setAccessible(true);
Annotation[] a = f[0].getAnnotations ();

// Then stuff

Potato
  • 628
  • 5
  • 8
  • Synthetic elements are elements generated by the compiler that you normally can't access in source code. Your solution doesn't work for several reasons: - It uses runtime reflection but I need to do compile-time annotation processing - It assumes that you already have a class object to go off. My first idea was to recurse the element tree manually instead of using `RoundEnvironment.getElementsAnnotatedWith` but there doesn't seem to be a reliable way to get all the root elements because `Processor.process` is only called if there are supported annotations present on non-synthetic elements – Grisu47 Oct 09 '18 at 10:19