You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jason Weinstein <ja...@hotmail.com> on 2009/07/17 11:11:34 UTC
New Tag - Looking for Comments (Maintain user code during
regeneration of files)
A common problem with templating systems is that they overwrite changes made to the generated files, when things are regenerated.
This simple tag eliminates a portion of this problem.
A simple example:
Template:
<?xml version="1.0"?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:s="jelly:slot">
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
public class ${className} extends TagSupport {
public void doTag(XMLOutput output) throws JellyTagException {
<s:slot id="//[[100]] - user code" overwrite="false" evaluate="true" buffer="${fileText}">
getBody().run(context, output);
</s:slot>
}
}
</j:jelly>
Outputs:
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
public class FooTag extends TagSupport {
public void doTag(XMLOutput output) throws JellyTagException {
//[[100]] - user code
getBody().run(context, output);
//[[100]] - user code
}
}
If the user now changes this file and adds a print statement the statement will survive regeneration.
public void doTag(XMLOutput output) throws JellyTagException {
//[[100]] - user code
System.out.println("entering doTag");
getBody().run(context, output);
//[[100]] - user code
}
This tag allows the user to make changes in between unique id tags in the generated file and to have those changes persist after regeneration. Obvious issues exist in the above example when parameter names or numbers change. However if a generate and compile step are combined this will give an early clue that the user code will need to be changed.
Basically it is using the generated file as a Context (Key, Vals) for regenerated files.
<s:slot id="//[[100]] - user code" overwrite="false" evaluate="true" buffer="${fileText}">
getBody().run(context, output);
</s:slot>
The id is the key, overwrite indicates whether the code in between should be overwritten, evaluate indicates whether the code in between should be evaled as a script (only for new files) and the buffer contains the original text of the file being regenerated.
If anyone has any ideas on improving this simple code i'd be glad to hear suggestions. The code was done very quickly so even simple suggestions may be helpful. Changes to inputs, names, types, etc...
_________________________________________________________________
Lauren found her dream laptop. Find the PC that�s right for you.
http://www.microsoft.com/windows/choosepc/?ocid=ftp_val_wl_290
Re: New Tag - Looking for Comments (Maintain user code during regeneration of files)
Posted by Paul Libbrecht <pa...@activemath.org>.
Jason,
A matter of protocol: what you describe is somewhat out of the world
of any applicable library, I feel... or is it for jelly? please put
the right subject and do not "steal threads" by answering a mail into
another topic.
The thing you describe seems to be somewhat of a possibly useful
pattern to be able to bring back changes on generated files where it
is easier to make (nothing to do with java code btw) into a source.
It is not a tag only though! It definitely is something that would
remanipulate the source (be it xml, xsl, jelly or any other) based on
the output.
I suppose jelly could have advantage out of that.
I am convinced xslt's may have one as well.
paul
Le 17-juil.-09 à 05:11, Jason Weinstein a écrit :
>
> A common problem with templating systems is that they overwrite
> changes made to the generated files, when things are regenerated.
>
> This simple tag eliminates a portion of this problem.
>
> A simple example:
>
> Template:
>
> <?xml version="1.0"?>
> <j:jelly trim="false" xmlns:j="jelly:core" xmlns:s="jelly:slot">
> import org.apache.commons.jelly.JellyTagException;
> import org.apache.commons.jelly.TagSupport;
> import org.apache.commons.jelly.XMLOutput;
>
> public class ${className} extends TagSupport {
> public void doTag(XMLOutput output) throws JellyTagException {
> <s:slot id="//[[100]] - user code" overwrite="false"
> evaluate="true" buffer="${fileText}">
> getBody().run(context, output);
> </s:slot>
> }
> }
> </j:jelly>
>
> Outputs:
>
> import org.apache.commons.jelly.JellyTagException;
> import org.apache.commons.jelly.TagSupport;
> import org.apache.commons.jelly.XMLOutput;
>
> public class FooTag extends TagSupport {
> public void doTag(XMLOutput output) throws JellyTagException {
> //[[100]] - user code
> getBody().run(context, output);
> //[[100]] - user code
> }
> }
>
> If the user now changes this file and adds a print statement the
> statement will survive regeneration.
>
> public void doTag(XMLOutput output) throws JellyTagException {
> //[[100]] - user code
> System.out.println("entering doTag");
> getBody().run(context, output);
> //[[100]] - user code
> }
>
>
> This tag allows the user to make changes in between unique id tags
> in the generated file and to have those changes persist after
> regeneration. Obvious issues exist in the above example when
> parameter names or numbers change. However if a generate and compile
> step are combined this will give an early clue that the user code
> will need to be changed.
>
> Basically it is using the generated file as a Context (Key, Vals)
> for regenerated files.
>
> <s:slot id="//[[100]] - user code" overwrite="false"
> evaluate="true" buffer="${fileText}">
> getBody().run(context, output);
> </s:slot>
>
> The id is the key, overwrite indicates whether the code in between
> should be overwritten, evaluate indicates whether the code in
> between should be evaled as a script (only for new files) and the
> buffer contains the original text of the file being regenerated.
>
> If anyone has any ideas on improving this simple code i'd be glad to
> hear suggestions. The code was done very quickly so even simple
> suggestions may be helpful. Changes to inputs, names, types, etc...
>
> Lauren found her dream laptop. Find the PC that’s right for you.
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
Re: [OT] New Tag - Looking for Comments (Maintain user code during
regeneration of files)
Posted by Rahul Akolkar <ra...@gmail.com>.
Please take this to a more appropriate forum at this point. I've added
an OT marker (please use it in subjects when appropriate).
Thanks,
-Rahul
On Mon, Jul 20, 2009 at 8:40 PM, Ted Dunning<te...@gmail.com> wrote:
> On Mon, Jul 20, 2009 at 4:23 PM, Jason Weinstein <jasonweinstein@hotmail.com
>> wrote:
>
>> Ok so another not so good example. I was not suggesting an
>> implementation for webservices. If you want to use a config file, some IOC
>> technique, you're more than welcome to use a config file.
>>
>
> I was not suggesting use of a config file. I was suggesting the use of
> standard language facilities such as class extension. Thrift is a
> proof-by-example that this is possible in many target languages.
>
<big-snip/>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: New Tag - Looking for Comments (Maintain user code during
regeneration of files)
Posted by Ted Dunning <te...@gmail.com>.
On Mon, Jul 20, 2009 at 4:23 PM, Jason Weinstein <jasonweinstein@hotmail.com
> wrote:
> Ok so another not so good example. I was not suggesting an
> implementation for webservices. If you want to use a config file, some IOC
> technique, you're more than welcome to use a config file.
>
I was not suggesting use of a config file. I was suggesting the use of
standard language facilities such as class extension. Thrift is a
proof-by-example that this is possible in many target languages.
> .... However this [avoiding rewriting of generated code] is not always the
> case.
>
Inability to avoid over-writing is a show-stopper bug in my book.
> Still back to the example at hand, given wsdl-first (contract first)
> code generation, in your case the code generator would generate
> something like...
>
> The config file contains things like you describe: an endpoint name, and
> which class is implementing the endpoint, etc
>
And here is your first chance to avoid changing generated code. Just extend
the sample implementation and provide that extension in the config file.
> ... Lets say you hand add more entries. Next time you run code gen you will
> overwrite this file and lose your hand entries. Now what i am
> suggesting has to do with "jelly" and its use as a code generator. In
> the above case the file generated would look something like:
>
Don't do this. Write your own config file. Using the code generators
config file is asking for disaster to strike.
> Similarly you can do the same for method bodies, etc. When people make
> extensive use of code gen not just to gen the wsdl or java files but
> other framework code, it becomes important for the user to be able to
> overwrite/workaround generated code as it is not always correct and
> what the user wants.
>
I have heard this said before. I have never (in nearly 25 years of using
code generation tools) seen a case where it was absolutely required.
Moreover, I have never seen a case where it did not lead to horrific
maintenance nightmares.
>
> Anyway using the same example as before but in this context, lets assume
> the generator generated this file
>
>
> ... java file deleted ...
>
This is a non-issue. Use inheritance.
... public int doubleIt(int numberToDouble) {
> // [[100-usercode]]
> //enter your code here - it will not be overwritten next time you
> codegen
> // [[100-usercode]]
> }
> }
>
> In fact what i described allows the code generator to flag files which
> have been modified by users potentially alerting them to incompatible
> changes they may have made.
>
This leads to a horror in terms of file dependencies because the output of
the code generator depends on the output of the code generator.
Using inheritance to over-ride the behavior breaks this dependency cycle and
avoids the problem entirely.
This is commonly referred to as Roundtripping (which is not necessarily a
> bad thing) and most code generators do not support it. I have presented a
> way to support simple roundtripping needs in code generators like "jelly".
>
This is a key point and I think we dramatically differ on this.
You say that round-tripping is not necessarily a bad thing and I say that it
is *necessarily* a bad thing. As in, it is not a good thing. Indeed, it
is a bad thing.
--
Ted Dunning, CTO
DeepDyve
RE: New Tag - Looking for Comments (Maintain user code during
regeneration of files)
Posted by Jason Weinstein <ja...@hotmail.com>.
Ok so another not so good example. I was not suggesting an
implementation for webservices. If you want to use a config file, some IOC
technique, you're more than welcome to use a config file.
Also i agree in the ideal world the generated code should not be
touched, that is the generated code should be designed to allow for
overwriting things via config files and such. It needs to be properly
designed code. However this is not always the case.
Still back to the example at hand, given wsdl-first (contract first)
code generation, in your case the code generator would generate
something like the
1) server side web service plumming
2) the service interface
3) a config file(s) and
4) in more sophisticated cases an implementation class which implements the service interface
The config file contains things like you describe: an endpoint name, and which class is implementing the endpoint, etc
A quick web search shows what such a file may look like with jaxws
From: http://www.jroller.com/gmazza/entry/creating_a_wsdl_first_web1
(Metro only) sun-jaxws.xml:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="Endpoint_for_DoubleIt"
implementation="com.mycompany.webservice.service.DoubleItPortTypeImpl"
wsdl="WEB-INF/wsdl/DoubleIt.wsdl"
service="{http://www.example.org/contract/DoubleIt}DoubleItService"
port="{http://www.example.org/contract/DoubleIt}DoubleItPort"
url-pattern="/services/doubleit"/>
</endpoints>
What happens when you edit the sun-jaxws.xml file to add say more
endpoint definitions. I'm assuming it can contain more than 1. The code
generator you have written knows only how to write this one entry. Lets
say you hand add more entries. Next time you run code gen you will
overwrite this file and lose your hand entries. Now what i am
suggesting has to do with "jelly" and its use as a code generator. In
the above case the file generated would look something like:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="Endpoint_for_DoubleIt"
implementation="com.mycompany.webservice.service.DoubleItPortTypeImpl"
wsdl="WEB-INF/wsdl/DoubleIt.wsdl"
service="{http://www.example.org/contract/DoubleIt}DoubleItService"
port="{http://www.example.org/contract/DoubleIt}DoubleItPort"
url-pattern="/services/doubleit"/>
<!-- [[100]]-->
where any code written here will be maintained through code gen cycles.
<!-- [[100]]-->
</endpoints>
Similarly you can do the same for method bodies, etc. When people make
extensive use of code gen not just to gen the wsdl or java files but
other framework code, it becomes important for the user to be able to
overwrite/workaround generated code as it is not always correct and
what the user wants. A simple workaround is to allow them to plugin a
different generator for specific files.
Anyway using the same example as before but in this context, lets assume the generator generated this file
package com.mycompany.webservice.service;
import javax.jws.WebService;
import org.example.contract.doubleit.DoubleItPortType;
@WebService(targetNamespace = "http://www.example.org/contract/DoubleIt",
portName="DoubleItPort",
serviceName="DoubleItService",
endpointInterface="org.example.contract.doubleit.DoubleItPortType")
public class DoubleItPortTypeImpl implements DoubleItPortType {
public int doubleIt(int numberToDouble) {
return numberToDouble * 2;
}
}
What i suggest is a way for the user to edit the method body and
maintain his her changes over code gen cycles. Of course the user has
the option to overwrite if wants.
Or in a similar example, instead of getting this
package com.mycompany.webservice.service;
import javax.jws.WebService;
import org.example.contract.doubleit.DoubleItPortType;
@WebService(targetNamespace = "http://www.example.org/contract/DoubleIt",
portName="DoubleItPort",
serviceName="DoubleItService",
endpointInterface="org.example.contract.doubleit.DoubleItPortType")
public class DoubleItPortTypeImpl implements DoubleItPortType {
public int doubleIt(int numberToDouble) {
// enter your code here - but watch out it will be overwritten next time you code gen
}
}
You would get this
package com.mycompany.webservice.service;
import javax.jws.WebService;
import org.example.contract.doubleit.DoubleItPortType;
@WebService(targetNamespace = "http://www.example.org/contract/DoubleIt",
portName="DoubleItPort",
serviceName="DoubleItService",
endpointInterface="org.example.contract.doubleit.DoubleItPortType")
public class DoubleItPortTypeImpl implements DoubleItPortType {
public int doubleIt(int numberToDouble) {
// [[100-usercode]]
//enter your code here - it will not be overwritten next time you codegen
// [[100-usercode]]
}
}
In fact what i described allows the code generator to flag files which
have been modified by users potentially alerting them to incompatible
changes they may have made.
This is commonly referred to as Roundtripping (which is not necessarily a bad thing) and most code generators do not support it. I have presented a way to support simple roundtripping needs in code generators like "jelly".
I appreciate your comments and the discussion you've put forth on your thoughts for good design.
> Date: Mon, 20 Jul 2009 23:42:27 +0200
> Subject: Re: New Tag - Looking for Comments (Maintain user code during regeneration of files)
> From: rosenberg.leon@googlemail.com
> To: user@commons.apache.org
>
> I must second Ted on this.
> Talking about WSDL - consider IDL compilers, they usually generate an
> Interface (operations) and a basic BOA (later POA, which stands for
> basic/portable object adapter). You have the choice between extending
> the BOA or implementing the operations interface and "tie" your impl
> to a standart BOA implementation. In both case you never touch
> generated code, which gives you an additional advantage of using same
> code with different orbs, even the underlying generated code differs
> from orb to orb and from version to version.
>
> Of course, if you are working with generators and edit generated code
> for years, and never had any troubles, this may be a good solution for
> you, but generally speaking ... there is some room for improvement...
>
> Btw, I'm also writing code generators for many different purposes for
> years, starting with generating a corba or rmi based code distribution
> from a java interface, and up to generating a complete struts
> application out of two xml files, so I'm absolutely PRO code
> generation, as long as it never touched by the developer :-)
>
>
> best regards
> Leon
>
> On Mon, Jul 20, 2009 at 11:25 PM, Ted Dunning<te...@gmail.com> wrote:
> > What you describe is exceedingly bad practice.
> >
> > Much better to take the approach that thrift does where the generated code
> > is never touched by the developer. The thrift code generator generates
> > portable marshaling code and an interface. The programmer implements the
> > interface and injects their implementation and a transport into a server at
> > server construction time. Clients inject a transport into a client
> > interface.
> >
> > This separates generated code from hand-written code and is a very effective
> > approach. Changes to the thrift IDL are easily propagated into Java (or any
> > other supported language). Changes to the implementation are preserved with
> > no fancy footwork required.
> >
> > With the approach you describe it is exceedingly difficult to guarantee that
> > the developer's code is preserved.
> >
> > On Mon, Jul 20, 2009 at 12:09 PM, Jason Weinstein <
> > jasonweinstein@hotmail.com> wrote:
> >
> >> On the server side you generate a set of boilerplate implementation classes
> >> which include a set of stubs for the web service interfaces. The developer
> >> then codes in the implementation for each method. Quite often changes are
> >> made to the wsdl and the files need to be regenerated. In this case it would
> >> be nice to maintain the existing developers code.
> >>
> >
> >
> >
> > --
> > Ted Dunning, CTO
> > DeepDyve
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
_________________________________________________________________
Windows Live™ Hotmail®: Celebrate the moment with your favorite sports pics. Check it out.
http://www.windowslive.com/Online/Hotmail/Campaign/QuickAdd?ocid=TXT_TAGLM_WL_QA_HM_sports_photos_072009&cat=sports
Re: New Tag - Looking for Comments (Maintain user code during
regeneration of files)
Posted by Leon Rosenberg <ro...@googlemail.com>.
I must second Ted on this.
Talking about WSDL - consider IDL compilers, they usually generate an
Interface (operations) and a basic BOA (later POA, which stands for
basic/portable object adapter). You have the choice between extending
the BOA or implementing the operations interface and "tie" your impl
to a standart BOA implementation. In both case you never touch
generated code, which gives you an additional advantage of using same
code with different orbs, even the underlying generated code differs
from orb to orb and from version to version.
Of course, if you are working with generators and edit generated code
for years, and never had any troubles, this may be a good solution for
you, but generally speaking ... there is some room for improvement...
Btw, I'm also writing code generators for many different purposes for
years, starting with generating a corba or rmi based code distribution
from a java interface, and up to generating a complete struts
application out of two xml files, so I'm absolutely PRO code
generation, as long as it never touched by the developer :-)
best regards
Leon
On Mon, Jul 20, 2009 at 11:25 PM, Ted Dunning<te...@gmail.com> wrote:
> What you describe is exceedingly bad practice.
>
> Much better to take the approach that thrift does where the generated code
> is never touched by the developer. The thrift code generator generates
> portable marshaling code and an interface. The programmer implements the
> interface and injects their implementation and a transport into a server at
> server construction time. Clients inject a transport into a client
> interface.
>
> This separates generated code from hand-written code and is a very effective
> approach. Changes to the thrift IDL are easily propagated into Java (or any
> other supported language). Changes to the implementation are preserved with
> no fancy footwork required.
>
> With the approach you describe it is exceedingly difficult to guarantee that
> the developer's code is preserved.
>
> On Mon, Jul 20, 2009 at 12:09 PM, Jason Weinstein <
> jasonweinstein@hotmail.com> wrote:
>
>> On the server side you generate a set of boilerplate implementation classes
>> which include a set of stubs for the web service interfaces. The developer
>> then codes in the implementation for each method. Quite often changes are
>> made to the wsdl and the files need to be regenerated. In this case it would
>> be nice to maintain the existing developers code.
>>
>
>
>
> --
> Ted Dunning, CTO
> DeepDyve
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: New Tag - Looking for Comments (Maintain user code during
regeneration of files)
Posted by Ted Dunning <te...@gmail.com>.
What you describe is exceedingly bad practice.
Much better to take the approach that thrift does where the generated code
is never touched by the developer. The thrift code generator generates
portable marshaling code and an interface. The programmer implements the
interface and injects their implementation and a transport into a server at
server construction time. Clients inject a transport into a client
interface.
This separates generated code from hand-written code and is a very effective
approach. Changes to the thrift IDL are easily propagated into Java (or any
other supported language). Changes to the implementation are preserved with
no fancy footwork required.
With the approach you describe it is exceedingly difficult to guarantee that
the developer's code is preserved.
On Mon, Jul 20, 2009 at 12:09 PM, Jason Weinstein <
jasonweinstein@hotmail.com> wrote:
> On the server side you generate a set of boilerplate implementation classes
> which include a set of stubs for the web service interfaces. The developer
> then codes in the implementation for each method. Quite often changes are
> made to the wsdl and the files need to be regenerated. In this case it would
> be nice to maintain the existing developers code.
>
--
Ted Dunning, CTO
DeepDyve
RE: New Tag - Looking for Comments (Maintain user code during
regeneration of files)
Posted by Jason Weinstein <ja...@hotmail.com>.
Leon,
I have been using code generation for many years now in projects/products. I have generated every file type imaginable from java files, including all java patterns, factories, value objects, etc, to jsps, to xml config, wsdl and xsds, to stored procedures, and database sql, to entire frameworks, and generally this happens with the push of a button.
I appreciate your comments. However, if you have not successfully used code gen i might suggest you take a second look.
Think wsdl to java. On the server side you generate a set of boilerplate implementation classes which include a set of stubs for the web service interfaces. The developer then codes in the implementation for each method. Quite often changes are made to the wsdl and the files need to be regenerated. In this case it would be nice to maintain the existing developers code.
If it helps perhaps i messed up my example:
Perhaps a better one
package ${packageName};
public class FooFactory {
private Properties props = PropertyLoader.load("${packageName}/config.xml");
public Object create() throws Exception {
Foo foo = new Foo();
foo.configure(props);
return foo;
}
}
package ${packageName};
public class Foo implements Configurable, BusinessAPI {
public void configure(Properties props) {
...
}
public void doSomething() {...}
}
config.xml
foo.propertyA = x
foo.propertyB = y
Here we generate a Factory, a BusinessAPI, and a config file
Anyway code generators are capable of generating a large number of files, 100s or 1000s, and in any format: java, xml, wsdl, c#, etc. Code generation is a tool and should be used in conjunction with other best practice techniques from good design to externalizing properties etc. There are pros and cons to code generation which depend in large part on how you are using it and what you are using it for. The critical thing of course is that you are doing things correctly.
> Date: Mon, 20 Jul 2009 14:38:11 +0200
> Subject: Re: New Tag - Looking for Comments (Maintain user code during regeneration of files)
> From: rosenberg.leon@googlemail.com
> To: user@commons.apache.org
>
> Hello Jason,
>
> thank you very much for the detailed answer.
>
> On Mon, Jul 20, 2009 at 12:42 AM, Jason
> Weinstein<ja...@hotmail.com> wrote:
> >
> > Hi Leon,
> >
> > Often generated files are only a starting point.
>
> An interesting point. Actually the mess that happens if you try to
> "regenerate" edited generated file is the main agrument against code
> generation (see also rod johnsons j2ee development without ejb). So
> why would someone use a system that actually relies on changing
> generated code?
> Giving 2 examples: How many real life projects REALLY use code
> generated by rational rose et al?
> But, millions of sites are using jsps on the daily basis, as an
> example of code generator jsp -> jsp.java and i think that noone
> actually edits and changes code generated by jasper or other jsp
> generator / compiler.
> So maybe instead of allowing user to alter generated code and even
> persist those changes over generation cycles, it makes more sense to
> parametrize either the generator or the generated code, so there is no
> need to edit it? Staying with your it would be :
> public class Factory {
> private String className = RuntimeConfiguration.get("className");
>
> public Object create() throws Exception {
> return Class.forName(className).newInstance();
> }
> This way you at least can rely that the result of a generation is
> predictable and repeatable for the defined version of the generator.
>
> regards
> Leon
>
>
> >
> > Example
> >
> > Template
> >
> > public class Factory {
> >
> > private String className = "${className}";
> >
> >
> >
> > public Object create() throws Exception {
> >
> > return Class.forName(className).newInstance();
> >
> > }
> >
> > }
> >
> >
> > First time template generates:
> >
> > public class Factory {
> > private String className = "foo";
> >
> > public Object create() throws Exception {
> > return Class.forName(className).newInstance();
> > }
> > }
> >
> > User may change this generated file to be:
> >
> > public class Factory {
> >
> > private String className = "foo";
> >
> >
> >
> > public Object create() throws Exception {
> > Object o = Class.forName(className).newInstance();
> >
> > System.out.println("creating object " + o);
> > Statistic.createdObject(className, +1);
> > return o;
> >
> > }
> >
> > }
> >
> > Template may be updated to generate:
> >
> > public class Factory {
> >
> > private String className = "foo";
> >
> >
> >
> > public Object create() throws Exception {
> >
> >
> > return Class.forName(className).newInstance();
> >
> >
> > }
> >
> >
> >
> > public Object create(ClassLoader cl) throws Exception {
> >
> >
> > return Class.forName(className, cl, true).newInstance();
> >
> >
> > }
> >
> >
> > }
> >
> > When user code is maintained the generated file would be
> >
> > public class Factory {
> >
> > private String className = "foo";
> >
> >
> >
> > public Object create() throws Exception {
> > Object o = Class.forName(className).newInstance();
> >
> >
> > System.out.println("creating object " + o);
> >
> > Statistic.createdObject(className, +1);
> >
> > return o;
> >
> >
> > }
> >
> >
> >
> > public Object create(ClassLoader cl) throws Exception {
> >
> >
> >
> > return Class.forName(className, cl, true).newInstance();
> >
> >
> >
> > }
> >
> >
> >
> > }
> >
> > And not (missing user code)
> >
> > public class Factory {
> >
> >
> > private String className = "foo";
> >
> >
> >
> >
> >
> > public Object create() throws Exception {
> >
> >
> >
> > return Class.forName(className).newInstance();
> >
> >
> >
> > }
> >
> >
> >
> >
> >
> > public Object create(ClassLoader cl) throws Exception {
> >
> >
> >
> > return Class.forName(className, cl, true).newInstance();
> >
> >
> >
> > }
> >
> >
> >
> > }
> >
> >
> > Many times generated files need updating. Sometimes generated files are just starting points meant for the user to fill in and sometimes they are meant to be final results but developers/users may wish to make mods to them anyways.
> >
> > The above is a simplistic example, but a user mod could be anything from adding an annotation to a log statement to employing an optimization such as a cache to adding a new method, etc.
> >
> > What i presented, was a simple practical way to allow user mods to be maintained during file regeneration.
> >
> > In conclusion, very often not everything is accounted for during generation. Sometimes the files need to be regenerated as templates change. What was presented was a way to maintain user changes in the generated files as files are regenerated. When combined with a compile step this also allows an easy way to flag incompatible user changes, or simply flag that the file was changed by the user.
> >
> >
> > These types of comments often need work arounds in real systems as generated code does not always meet user requirements or may even have bugs or other limitations.
> >
> > // user generated code - do not modify - overwrites
> >
> >
> >
> >
> >
> >
> >
> > A couple side thoughts
> >
> > <use as template> - way to use different template during codegen, useful as workaround for end user in prod system
> > <crc file> - way to test for user mods, and flag errors, may skip whitespace, etc
> >
> >> Date: Sun, 19 Jul 2009 14:04:53 +0200
> >> Subject: Re: New Tag - Looking for Comments (Maintain user code during regeneration of files)
> >> From: rosenberg.leon@googlemail.com
> >> To: user@commons.apache.org
> >>
> >> On Fri, Jul 17, 2009 at 11:11 AM, Jason
> >> Weinstein<ja...@hotmail.com> wrote:
> >> >
> >> > A common problem with templating systems is that they overwrite changes made
> >> > to the generated files, when things are regenerated.
> >>
> >> Hello,
> >>
> >> maybe I don't understand your point, but why should someone change
> >> generated files?
> >>
> >> Leon
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: user-help@commons.apache.org
> >>
> >
> > _________________________________________________________________
> > Windows Live™ Hotmail®: Celebrate the moment with your favorite sports pics. Check it out.
> > http://www.windowslive.com/Online/Hotmail/Campaign/QuickAdd?ocid=TXT_TAGLM_WL_QA_HM_sports_photos_072009&cat=sports
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
_________________________________________________________________
Windows Live™ Hotmail®: Search, add, and share the web’s latest sports videos. Check it out.
http://www.windowslive.com/Online/Hotmail/Campaign/QuickAdd?ocid=TXT_TAGLM_WL_QA_HM_sports_videos_072009&cat=sports
Re: New Tag - Looking for Comments (Maintain user code during
regeneration of files)
Posted by Leon Rosenberg <ro...@googlemail.com>.
Hello Jason,
thank you very much for the detailed answer.
On Mon, Jul 20, 2009 at 12:42 AM, Jason
Weinstein<ja...@hotmail.com> wrote:
>
> Hi Leon,
>
> Often generated files are only a starting point.
An interesting point. Actually the mess that happens if you try to
"regenerate" edited generated file is the main agrument against code
generation (see also rod johnsons j2ee development without ejb). So
why would someone use a system that actually relies on changing
generated code?
Giving 2 examples: How many real life projects REALLY use code
generated by rational rose et al?
But, millions of sites are using jsps on the daily basis, as an
example of code generator jsp -> jsp.java and i think that noone
actually edits and changes code generated by jasper or other jsp
generator / compiler.
So maybe instead of allowing user to alter generated code and even
persist those changes over generation cycles, it makes more sense to
parametrize either the generator or the generated code, so there is no
need to edit it? Staying with your it would be :
public class Factory {
private String className = RuntimeConfiguration.get("className");
public Object create() throws Exception {
return Class.forName(className).newInstance();
}
This way you at least can rely that the result of a generation is
predictable and repeatable for the defined version of the generator.
regards
Leon
>
> Example
>
> Template
>
> public class Factory {
>
> private String className = "${className}";
>
>
>
> public Object create() throws Exception {
>
> return Class.forName(className).newInstance();
>
> }
>
> }
>
>
> First time template generates:
>
> public class Factory {
> private String className = "foo";
>
> public Object create() throws Exception {
> return Class.forName(className).newInstance();
> }
> }
>
> User may change this generated file to be:
>
> public class Factory {
>
> private String className = "foo";
>
>
>
> public Object create() throws Exception {
> Object o = Class.forName(className).newInstance();
>
> System.out.println("creating object " + o);
> Statistic.createdObject(className, +1);
> return o;
>
> }
>
> }
>
> Template may be updated to generate:
>
> public class Factory {
>
> private String className = "foo";
>
>
>
> public Object create() throws Exception {
>
>
> return Class.forName(className).newInstance();
>
>
> }
>
>
>
> public Object create(ClassLoader cl) throws Exception {
>
>
> return Class.forName(className, cl, true).newInstance();
>
>
> }
>
>
> }
>
> When user code is maintained the generated file would be
>
> public class Factory {
>
> private String className = "foo";
>
>
>
> public Object create() throws Exception {
> Object o = Class.forName(className).newInstance();
>
>
> System.out.println("creating object " + o);
>
> Statistic.createdObject(className, +1);
>
> return o;
>
>
> }
>
>
>
> public Object create(ClassLoader cl) throws Exception {
>
>
>
> return Class.forName(className, cl, true).newInstance();
>
>
>
> }
>
>
>
> }
>
> And not (missing user code)
>
> public class Factory {
>
>
> private String className = "foo";
>
>
>
>
>
> public Object create() throws Exception {
>
>
>
> return Class.forName(className).newInstance();
>
>
>
> }
>
>
>
>
>
> public Object create(ClassLoader cl) throws Exception {
>
>
>
> return Class.forName(className, cl, true).newInstance();
>
>
>
> }
>
>
>
> }
>
>
> Many times generated files need updating. Sometimes generated files are just starting points meant for the user to fill in and sometimes they are meant to be final results but developers/users may wish to make mods to them anyways.
>
> The above is a simplistic example, but a user mod could be anything from adding an annotation to a log statement to employing an optimization such as a cache to adding a new method, etc.
>
> What i presented, was a simple practical way to allow user mods to be maintained during file regeneration.
>
> In conclusion, very often not everything is accounted for during generation. Sometimes the files need to be regenerated as templates change. What was presented was a way to maintain user changes in the generated files as files are regenerated. When combined with a compile step this also allows an easy way to flag incompatible user changes, or simply flag that the file was changed by the user.
>
>
> These types of comments often need work arounds in real systems as generated code does not always meet user requirements or may even have bugs or other limitations.
>
> // user generated code - do not modify - overwrites
>
>
>
>
>
>
>
> A couple side thoughts
>
> <use as template> - way to use different template during codegen, useful as workaround for end user in prod system
> <crc file> - way to test for user mods, and flag errors, may skip whitespace, etc
>
>> Date: Sun, 19 Jul 2009 14:04:53 +0200
>> Subject: Re: New Tag - Looking for Comments (Maintain user code during regeneration of files)
>> From: rosenberg.leon@googlemail.com
>> To: user@commons.apache.org
>>
>> On Fri, Jul 17, 2009 at 11:11 AM, Jason
>> Weinstein<ja...@hotmail.com> wrote:
>> >
>> > A common problem with templating systems is that they overwrite changes made
>> > to the generated files, when things are regenerated.
>>
>> Hello,
>>
>> maybe I don't understand your point, but why should someone change
>> generated files?
>>
>> Leon
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>
> _________________________________________________________________
> Windows Live™ Hotmail®: Celebrate the moment with your favorite sports pics. Check it out.
> http://www.windowslive.com/Online/Hotmail/Campaign/QuickAdd?ocid=TXT_TAGLM_WL_QA_HM_sports_photos_072009&cat=sports
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
RE: New Tag - Looking for Comments (Maintain user code during
regeneration of files)
Posted by Jason Weinstein <ja...@hotmail.com>.
Hi Leon,
Often generated files are only a starting point.
Example
Template
public class Factory {
private String className = "${className}";
public Object create() throws Exception {
return Class.forName(className).newInstance();
}
}
First time template generates:
public class Factory {
private String className = "foo";
public Object create() throws Exception {
return Class.forName(className).newInstance();
}
}
User may change this generated file to be:
public class Factory {
private String className = "foo";
public Object create() throws Exception {
Object o = Class.forName(className).newInstance();
System.out.println("creating object " + o);
Statistic.createdObject(className, +1);
return o;
}
}
Template may be updated to generate:
public class Factory {
private String className = "foo";
public Object create() throws Exception {
return Class.forName(className).newInstance();
}
public Object create(ClassLoader cl) throws Exception {
return Class.forName(className, cl, true).newInstance();
}
}
When user code is maintained the generated file would be
public class Factory {
private String className = "foo";
public Object create() throws Exception {
Object o = Class.forName(className).newInstance();
System.out.println("creating object " + o);
Statistic.createdObject(className, +1);
return o;
}
public Object create(ClassLoader cl) throws Exception {
return Class.forName(className, cl, true).newInstance();
}
}
And not (missing user code)
public class Factory {
private String className = "foo";
public Object create() throws Exception {
return Class.forName(className).newInstance();
}
public Object create(ClassLoader cl) throws Exception {
return Class.forName(className, cl, true).newInstance();
}
}
Many times generated files need updating. Sometimes generated files are just starting points meant for the user to fill in and sometimes they are meant to be final results but developers/users may wish to make mods to them anyways.
The above is a simplistic example, but a user mod could be anything from adding an annotation to a log statement to employing an optimization such as a cache to adding a new method, etc.
What i presented, was a simple practical way to allow user mods to be maintained during file regeneration.
In conclusion, very often not everything is accounted for during generation. Sometimes the files need to be regenerated as templates change. What was presented was a way to maintain user changes in the generated files as files are regenerated. When combined with a compile step this also allows an easy way to flag incompatible user changes, or simply flag that the file was changed by the user.
These types of comments often need work arounds in real systems as generated code does not always meet user requirements or may even have bugs or other limitations.
// user generated code - do not modify - overwrites
A couple side thoughts
<use as template> - way to use different template during codegen, useful as workaround for end user in prod system
<crc file> - way to test for user mods, and flag errors, may skip whitespace, etc
> Date: Sun, 19 Jul 2009 14:04:53 +0200
> Subject: Re: New Tag - Looking for Comments (Maintain user code during regeneration of files)
> From: rosenberg.leon@googlemail.com
> To: user@commons.apache.org
>
> On Fri, Jul 17, 2009 at 11:11 AM, Jason
> Weinstein<ja...@hotmail.com> wrote:
> >
> > A common problem with templating systems is that they overwrite changes made
> > to the generated files, when things are regenerated.
>
> Hello,
>
> maybe I don't understand your point, but why should someone change
> generated files?
>
> Leon
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
_________________________________________________________________
Windows Live™ Hotmail®: Celebrate the moment with your favorite sports pics. Check it out.
http://www.windowslive.com/Online/Hotmail/Campaign/QuickAdd?ocid=TXT_TAGLM_WL_QA_HM_sports_photos_072009&cat=sports
Re: New Tag - Looking for Comments (Maintain user code during
regeneration of files)
Posted by Leon Rosenberg <ro...@googlemail.com>.
On Fri, Jul 17, 2009 at 11:11 AM, Jason
Weinstein<ja...@hotmail.com> wrote:
>
> A common problem with templating systems is that they overwrite changes made
> to the generated files, when things are regenerated.
Hello,
maybe I don't understand your point, but why should someone change
generated files?
Leon
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org