|
Re: Custom tag, SortImportStatments [message #44016 is a reply to message #43980] |
Mon, 07 April 2008 12:40   |
Paul Elder Messages: 849 Registered: July 2009 |
Senior Member |
|
|
Markus:
A couple of ideas:
1) Have you played at all with the java:importsLocation and java:imports
Tags? They can do much of the imports management for you. Here's how you use
it...
package ...;
import ...; // static imports...
<java:importsLocation package="..."/>
.... then in your code...
<java:import>someFQClassName</java:import>
This would do everything, but sort your imports in the right order. These
tags use EMF's codegen's ImportManager, which sorts imports purely
alphabetically and does not rewrite statically imported classes. IF your
java.* imports are all static (i.e. known when you write the template, take
the following approach:
package ...;
import java....; // all your Java imports...
<java:importsLocation package="..."/>
.... use <java:import>...</java:import> around all your other types.
Getting more involved would be figuring out how to subclass ImportManager
and get the imports collection sorted the way you want. This would probably
involve cloning the current java:importsLocation tag.
2) If you are not using the ImportManager tags, then writing a JET custom
tag to sort its contents is not difficult. Your sortImport tag would be
declared as a 'functionTag'. This is a simple for of a container tag that
takes is content as an input, and then writes a compute result. You end up
having to implement a single function:
public class SortImportsTag extends AbstractFunctionTag {
public String doFunction(TagInfo td, JET2Context context, String
bodyContent)
throws JET2TagException {
// TODO parse through bodyContent, sort the imports as you like, and return
the sorted result.
return null;
}
}
"Markus Backman" <markus.backman@swedbank.se> wrote in message
news:4cfc478221d79cae753094d4ea306956$1@www.eclipse.org...
> Hi
>
> I have implemented a JET2 plugin that from a xml model generates an EJB
> component that follows my companies architeture and guidelines. This works
> great and will certainly reduce the development time of new EJB services.
> But we also uses Checkstyle to control the sourcecode syntax and this
> creates a problem as one of the checkstyle rules that is in use controls
> the import order. That is that import java.* comes first then import
> packages in alpabetic order. As the model input defines what package the
> generated classes will belong to it is impossible to create a right import
> order in the template for all model input.
>
> For example
>
> import se.swedbank.infrastructure.....NotGeneratedClass;
> import se.swedbank.<c:get select="$pkg" />.....AGenerateClass;
>
> If the model for example sets a $pkg with the name "agreement" checkstyle
> will complain at the newly generated code as se.swedbank.i is alpabeticly
> after se.swedbank.a.
> So I am thinking of creating a custom taglib that looks something like
> this.
>
> <cust:sortImport>
> import se.swedbank.infrastructure.....NotGeneratedClass;
> import se.swedbank.<c:get select="$pkg" />.....AGenerateClass;
> </cust:sortImport>
>
> And after evaluation it will print the following in the generated class
>
> import se.swedbank.agreement.....AGenerateClass;
> import se.swedbank.infrastructure.....NotGeneratedClass;
>
> And so to my questions. Is this possible? Is AbstractContainerTag the
> baseclass to use? Can a ContainerTag handle a body that contains other
> tags? Will the tags within the body be evaluated first? Is there any code
> example that could point me in the right direction?
>
> Best regards,
> Markus
>
>
|
|
|
|
Powered by
FUDForum. Page generated in 0.02205 seconds