Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Passing Java objects by reference
Passing Java objects by reference [message #29033] Thu, 26 July 2007 12:23 Go to next message
J F is currently offline J FFriend
Messages: 242
Registered: July 2009
Senior Member
Hi,

Previously when using JET1 i used the <%@include %> directive to insert
code into the java code for the template.
I was naturally using scriplets to aid with processing, and the
parameterization of my "included" template consisted of making sure I used
the same variable names consistently before invoking the included template
E.g.

myIncludedTemplate.jetInc:

<%=config.getName()%>


myMain.jet

<%Config config = genModel.getConfig();%>
<%@ include file="myIncludedTemplate.jetInc"%>

I have been experimenting with the control tag <c:include/> but the only
way I seem to have of passing Objects by reference is by explicitly
setting the implicit context object's map of variables;

<%config.setVariable("config", config)%>

Is there any other way of passing values by reference?
Does the template complier do any "magic" with variables as in JSP2.0 ?
Can I develope a Custom Tag which takes Java Object references set at
runtime as values for its attributes ? Are there any tutorials on
developing a JET custom tag library?

Thanks
Re: Passing Java objects by reference [message #29110 is a reply to message #29033] Thu, 26 July 2007 14:27 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
JF:

Short answer: There isn't any easy way to pass Java objects by reference.

Some explanation: My initial bias has been that Java in templates is not a
good idea. In my mind, a good template primarily expresses the domain of the
template, with only small markups for substitution and control. A bit of
Java is alright, but it is very easy for it to get out of control.

My bias, of course, has affected what functionality is included. For
example, the tag library support doesn't include any JSP 2.0-like abilities
to define variables, and, as you have noted, it is hard to integrate Java
objects with the tags at all.

Having said that, I am open to ideas of improving support for Java, and how
it interacts with tags. Some ideas I have had (but not refined) are:

1) allow the declaration of template arguments, and the corresponding
invocation of templates by explicitly passing these arguments. Ideally, this
should include some option for specifying the Java type, and whether a Java
variable would be created.

2) allow a custom tag to cause the compiler to create or set a Java variable
based on a context value.

I'm open to all ideas (and, even better, contributed implementations).

Paul

"JF" <jfrancis@his.co.uk> wrote in message
news:ec8ad77bfbba70ae745d8a5c30173f02$1@www.eclipse.org...
> Hi,
>
> Previously when using JET1 i used the <%@include %> directive to insert
> code into the java code for the template.
> I was naturally using scriplets to aid with processing, and the
> parameterization of my "included" template consisted of making sure I used
> the same variable names consistently before invoking the included template
> E.g.
>
> myIncludedTemplate.jetInc:
>
> <%=config.getName()%>
>
>
> myMain.jet
>
> <%Config config = genModel.getConfig();%>
> <%@ include file="myIncludedTemplate.jetInc"%>
>
> I have been experimenting with the control tag <c:include/> but the only
> way I seem to have of passing Objects by reference is by explicitly
> setting the implicit context object's map of variables;
>
> <%config.setVariable("config", config)%>
>
> Is there any other way of passing values by reference?
> Does the template complier do any "magic" with variables as in JSP2.0 ?
> Can I develope a Custom Tag which takes Java Object references set at
> runtime as values for its attributes ? Are there any tutorials on
> developing a JET custom tag library?
>
> Thanks
>
Re: Passing Java objects by reference [message #29300 is a reply to message #29110] Thu, 26 July 2007 15:57 Go to previous message
J F is currently offline J FFriend
Messages: 242
Registered: July 2009
Senior Member
I agree with your approach (bias). Its a shame I am coming from a
different angle with JET1 code.

Either of your 2 ideas for improving Java support would be useful.
The most useful thing about the JET Editors is their ability to feedback (
syntax colour highlighting ) so an approach that keeps that ability is
most useful. I think adding parameters to a template would be useful and
could be made to work as a STATIC compilation time feature
to aid Java like type safety and error feedback in the editor to us Java
programmers;

So we might introduce a new directive
<%@ param="name" type="java.lang.String" %>
<%@ param="config" type="myPackage.Config" %>
which would then emit java code as follows:

public void generate(final JET2Context context, final JET2Writer __out) {
String name=(String)context.getVariable("name");
Config config=(Config)context.getVariable("config");

If this definition was stored in the Eclipse Registry then you can imagine
a template creator calling a KNOWN template at development time as follows:

<c:invokeTemplateWithParams id="a.b.c" >
<c:passParam name="name" value='<%="aString"%>'/>
<c:passParam name="config" value='<%=aConfig%>'/>
</c:invokeTemplateWithParams>


This could cause a stub function to emited into the template

public void doTemplate_a_b_c(final JET2Context context, final JET2Writer
__out, String name, Config config)
{
context.setVariable("name", name);
... //normal template invocation code...


which could then be called in the generate body at the point where the
custom tag was placed

doTemplatea_b_c(context, out, "aString", aConfig);

By only allowing expressions as parameters it should be easier to just
"dump" the java code.


If the User passes the incorrect types for these values then they would
get appropriate feedback as normal, and resticting parameters to be Java
expressions simplifies the work that has to be done.

I'm working round the problem by using context.setVariable and getVariable
at the momment, and don't have enough time to do the work, but perhaps you
could suggest where I might start implementing this when I get the time;
I see you use JET to write the parser/compiler itself is there an easy way
in there... the addition of stuff to the Eclipse Registry and extension
points extension should be easier for me.

Thanks
Previous Topic:[JET2] Re: JET2: Invalid Java source folder
Next Topic:[JET2] Generating multiple files from an input model
Goto Forum:
  


Current Time: Fri Nov 28 20:53:12 GMT 2014

Powered by FUDForum. Page generated in 0.01969 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software