1

I have a custom generator that is creating my own deferred binding layer in gwt, but I need to use the generated code in a pure java module as well. Since my gwt generator is already emitting "the current binding setup" as per annotations on classes, I want it to also generate the equivalent boilerplate for my pure-java instantiator.

All I need is for my linker stage or even the generator stage to emit the generated file into my working source package {like the android generator does for R.java}. How do I arbitrarily emit java files into my source folders? Should I just use java.io.File? I would prefer to take compiler flags like soyc so users can define where they want the generated source.

Ajax
  • 2,465
  • 23
  • 20
  • Please see this thread http://stackoverflow.com/questions/5975510/debug-view-source-of-gwt-generated-java-code – krishnakumarp May 01 '12 at 08:10
  • Hm. I already knew about using -gen to emit source into a different directory. I was hoping to exclude all of my other generated source, and just spit out the required files during linking. Perhaps I will setup a maven execution that runs only the externally required sources, and have it send along the project build path so it's puts it in the correct location. – Ajax May 01 '12 at 08:38
  • I've since written a maven mojo that allows me to target only the generated code I specifically want moved to the src folder... But there are new hurdles to cross, http://stackoverflow.com/questions/10836760/force-gwt-compiler-to-stop-pruning-invalid-compilationunits namely, preventing invalid units from being removed by the compiler. – Ajax May 31 '12 at 15:40
  • I need to do something similar, did you ever find a way of using deferred binding generated code in pure Java modules? – funkybro Jul 24 '13 at 08:22
  • Yes. Override StandardGeneratorContext$GeneratedUnitWithFile. You can save copies anywhere you like. Recommend using a ThreadLocal to share state statically. – Ajax Jul 24 '13 at 15:31

1 Answers1

0

Since nobody else answered, I'll toss this hack out there.

Override StandardGeneratorContext$GeneratedUnitWithFile. Look for:

  FileOutputStream fos = null;
  try {
    fos = new FileOutputStream(file);
    diskCache.transferToStream(sourceToken, fos);
  } 

add

    File newFile = rebaseFile(file); // Make a copy wherever you need 
    diskCache.transferToStream(sourceToken, fos);

use this to rebase saved files however you want. I recommend using a static ThreadLocal to tell the generator where to save copies. This is called when the generator subsystem calls .finish() on a StandardGeneratorContext (usually at the end of a GWT.create); you can call it manually, so long as you don't have any pending PrintWriters to commit.

Ajax
  • 2,465
  • 23
  • 20