Bug 444437 - [aa] CodeGenerationParticipant should pass in immutable target elements
Summary: [aa] CodeGenerationParticipant should pass in immutable target elements
Status: REOPENED
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: 2.8.0   Edit
Hardware: PC Mac OS X
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-18 05:15 EDT by Sven Efftinge CLA
Modified: 2016-06-10 05:56 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sven Efftinge CLA 2014-09-18 05:15:56 EDT
Currently during code generation the source tree is passed in and there's no way to navigate to the target Java tree.

I think we should change this to be the same as in ValidationParticipant.
Comment 1 Stefan Oehme CLA 2015-01-09 08:19:24 EST
Pushed to review

https://git.eclipse.org/r/#/c/39295/
Comment 2 Sebastian Zarnekow CLA 2015-01-12 02:24:58 EST
I'm not convinced by this change. Also it appears to be incomplete, unfortunately.

This is how the CodeGenerationParticipant is currently described:

/**
 * Invoked by the compiler during the code generation phase.
 * 
 * @param annotatedSourceElements the immutable source representation (i.e. the Xtend AST) of the annotated elements
 * @param context a {@link TransformationContext} providing useful services.
 */
void doGenerateCode(List<? extends T> annotatedSourceElements, @Extension CodeGenerationContext context);

Emphasize is on the parameter name 'annotatedSourceElements' and the described semantics.

Changing that will introduce the problem that there may no longer be any target elements, since the AA processor may have removed all associations. Also it's not clear what the type parameter T should be in case the processor created different kinds of elements and doesn't have a primary generated Java element. This problem also applies to the validation participant.

I'd rather add #getAllGeneratedJavaElements to the Tracibility service and pass the source elements to both the validation participant and the code generation participant. This would make the contract for the impl clearer.
Comment 3 Sven Efftinge CLA 2015-01-19 05:45:36 EST
You are right about the problematic corner cases, so passing in the annotated source elements would be correct. 

I'd like to skip the 'All' and go with getGeneratedJavaElements. Also 
Tracability#getPrimaryGeneratedJavaElement(Element) is typed to Element which makes it super inconvenient to work with. We should change it to be parameterized i.e. what goes in goes out, which means we cannot rely on the fuzzy contract to consider the first associated element being the primary. Instead we need to mark the element that was translated from the primary Xtend compiler explicitly. As a result it could be that getPrimaryJavaElement returns null while getJavaElements doesn't return an empty list.
Comment 4 Sebastian Zarnekow CLA 2015-01-19 05:51:07 EST
(In reply to Sven Efftinge from comment #3)
> Also 
> Tracability#getPrimaryGeneratedJavaElement(Element) is typed to Element
> which makes it super inconvenient to work with. We should change it to be
> parameterized i.e. what goes in goes out

How would that parametrization look like? I think anything can be marked as 'primary' element, being it a class inferred from an formal parameter or a field inferred from a method.

Should that be <E extends Element> E getPrimaryGeneratedJavaElement(Element in, Class<E> c). Or the unsafe version of <E extends Element> E getPrimaryGeneratedJavaElement(Element in)?
Comment 5 Sven Efftinge CLA 2015-01-19 07:47:19 EST
(In reply to Sebastian Zarnekow from comment #4)
> (In reply to Sven Efftinge from comment #3)
> > Also 
> > Tracability#getPrimaryGeneratedJavaElement(Element) is typed to Element
> > which makes it super inconvenient to work with. We should change it to be
> > parameterized i.e. what goes in goes out
> 
> How would that parametrization look like? I think anything can be marked as
> 'primary' element, being it a class inferred from an formal parameter or a
> field inferred from a method.
> 
> Should that be <E extends Element> E getPrimaryGeneratedJavaElement(Element
> in, Class<E> c). Or the unsafe version of <E extends Element> E
> getPrimaryGeneratedJavaElement(Element in)?

I meant that only the XtendCompiler marks elements as primary.
So I think it could be 

<E extends Element> E getPrimaryGeneratedJavaElement(E in)
Comment 6 Sebastian Zarnekow CLA 2015-01-19 08:27:00 EST
(In reply to Sven Efftinge from comment #5)
> I meant that only the XtendCompiler marks elements as primary.
> So I think it could be 
> 
> <E extends Element> E getPrimaryGeneratedJavaElement(E in)

I see, there wouldn't be an AA API that allows to mark an element as primary. I'm not sure how this aligns with other features like find references, where we use the "primary" element on the JVM side to find the references to a declaration.
Comment 7 Sven Efftinge CLA 2015-01-19 08:43:16 EST
(In reply to Sebastian Zarnekow from comment #6)
> I see, there wouldn't be an AA API that allows to mark an element as
> primary. I'm not sure how this aligns with other features like find
> references, where we use the "primary" element on the JVM side to find the
> references to a declaration.

I think having an explicit API is the way to go as suggested in bug #457146.