Modify Java source file at compile time, possible?

18 views
Skip to first unread message

Kevin Qiu

unread,
Jun 11, 2010, 12:09:50 PM6/11/10
to google-we...@googlegroups.com
Hi,

I know GWT can generate Java source code with deferred binding. But sometimes, I don't want to generate a new implementation, but based on some annotations, I want to have the ability of injecting code at compile time.

e.g., with the class below

public class Foo {
  @Log
  public void bar() {
    @CheckPermission
    dosomething();
  }
}

At compile time, I want to have the ability to get the annotations available on items, and inject code according to my application logic. So here, the compiler should see:
public class Foo {
  public void bar() {
    log("begin");
    if (hasPermission()) {
      doSomething();
    } else {
      error();
    }
  }
}

with the extra code being injected. I'm wondering if anything in GWT allows me to do that?

Olivier Monaco

unread,
Jun 11, 2010, 5:58:17 PM6/11/10
to Google Web Toolkit
Hi Kevin,

You can rewrite your Foo class as:

public class Foo implements MagicClass {
@Trace
@Secured
public void bar() {
}
}

Then, associate a generator for sub-types of MagicClass and use
GWT.create to instantiate a new Foo.

GWT.create(Foo.class);

So your generator will be called. It can generate a FooImpl class that
extends the Foo class and override the bar method as follow:

public class FooImpl extends Foo {
public void bar() {
Log.log("begin of bar");
try {
SecurityContext.checkAuthorized();
super.bar();
}
finally {
Log.log("end of bar");
}
}
}

Where "Log.log" print some logs and "SecurityContext.checkAuthorized"
throws an error is the user is not authorized. This could be funny and
not really hard to do.

Olivier

Kevin Qiu

unread,
Jun 11, 2010, 7:16:01 PM6/11/10
to google-we...@googlegroups.com
thanks for the reply. the problem is (I should've pointed out) that my Foo class is already managed through dependency injection container (gin), which means there's already a deferred binding implementation generated by ginjector.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


Stefan Bachert

unread,
Jun 13, 2010, 5:53:22 AM6/13/10
to Google Web Toolkit
Hi Kevin,

You must decide which type is implemented by which generator.
It can be only one.

Cascading generators, that mean one generator creates code which
itself uses deferred binding it possible.
I am doing this with my dialog layout generator with any problems.

So your problem is rather to organize your types well.

Stefan Bachert
http://gwtworld.de


On Jun 12, 1:16 am, Kevin Qiu <kevin.jing....@gmail.com> wrote:
> thanks for the reply. the problem is (I should've pointed out) that my Foo
> class is already managed through dependency injection container (gin), which
> means there's already a deferred binding implementation generated by
> ginjector.
>
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>
> > .

Olivier Monaco

unread,
Jun 13, 2010, 8:10:18 PM6/13/10
to Google Web Toolkit
Not sure it's a problem. It's depends on you exact use case. Can you
give me a more exact example?

Olivier

On 12 juin, 01:16, Kevin Qiu <kevin.jing....@gmail.com> wrote:
> thanks for the reply. the problem is (I should've pointed out) that my Foo
> class is already managed through dependency injection container (gin), which
> means there's already a deferred binding implementation generated by
> ginjector.
>
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsu...@googlegroups.com>
> > .

Kevin Qiu

unread,
Jun 14, 2010, 10:14:37 AM6/14/10
to google-we...@googlegroups.com
e.g.,

class Foo implements InterfaceA, InterfaceB {

}

and in .gwt.xml file, you have
<generate-with class="generatorA">
  <when-type-assignable class="InterfaceA" />
</generate-with>

<generate-with class="generatorB">
  <when-type-assignable class="InterfaceB" />
</generate-with>

How do I write generatorA and generatorB so that they generate the same file, but cascading the effects? Obviously, I'm only looking for general directions, not specific details.

Thanks,

To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Olivier Monaco

unread,
Jun 22, 2010, 1:27:25 PM6/22/10
to Google Web Toolkit
Kevin,

It depends one the which generator can know which interface.

If the generators can know both interfaces, you can create only one
generator. All code generator is pushed to sub classes like
IntrerfaceAWriter and InterfaceBWriter. The generator looks for
several interfaces from the marker interface and call the
corresponding writers.

If one generator (generatorB) can know the other interface (intefaceA)
but not the reverse, it can add a field like:

private InterfaceA wrapped = GWT.create(InterfaceA.class);

Then it implements all InterfaceA's methods and forward call to the
wrapped field.

If none of them can know the other interface (like generators from two
frameworks), you can create an InterfaceC that extends InterfaceA and
InterfaceB. You add a generator that proxy all call to some wrapped
field like in the previous case, but for the two interfaces.

Do you like it?

Olivier

On 14 juin, 16:14, Kevin Qiu <kevin.jing....@gmail.com> wrote:
> e.g.,
>
> class Foo implements InterfaceA, InterfaceB {
>
> }
>
> and in .gwt.xml file, you have
> <generate-with class="generatorA">
>   <when-type-assignable class="InterfaceA" />
> </generate-with>
>
> <generate-with class="generatorB">
>   <when-type-assignable class="InterfaceB" />
> </generate-with>
>
> How do I write generatorA and generatorB so that they generate the same
> file, but cascading the effects? Obviously, I'm only looking for general
> directions, not specific details.
>
> Thanks,
>
> > <google-web-toolkit%2Bunsu...@googlegroups.com<google-web-toolkit%252Buns...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages