1 11 12 package org.eclipse.jdt.apt.core.internal.env; 13 14 import java.io.PrintWriter ; 15 import java.io.StringWriter ; 16 import java.util.Collections ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.jdt.apt.core.env.Phase; 20 import org.eclipse.jdt.apt.core.internal.AptPlugin; 21 import org.eclipse.jdt.apt.core.internal.generatedfile.FileGenerationResult; 22 import org.eclipse.jdt.apt.core.internal.generatedfile.GeneratedFileManager; 23 import org.eclipse.jdt.core.ICompilationUnit; 24 25 public class JavaSourceFilePrintWriter extends PrintWriter { 26 27 private final StringWriter _sw; 28 private final String _typeName; 29 private final AbstractCompilationEnv _env; 30 31 34 public JavaSourceFilePrintWriter( String typeName, StringWriter sw, AbstractCompilationEnv env ) 35 throws CoreException 36 { 37 super( sw ); 38 _sw = sw; 39 _typeName = typeName; 40 _env = env; 41 _env.validateTypeName(typeName); 42 } 43 44 public void close() 45 { 46 try { 47 String contents = _sw.toString(); 48 super.close(); 49 GeneratedFileManager gfm = _env.getAptProject().getGeneratedFileManager(); 50 Phase phase = _env.getPhase(); 51 52 FileGenerationResult result = null; 53 if ( phase == Phase.RECONCILE && _env.currentProcessorSupportsRTTG() ) 54 { 55 ReconcileEnv reconcileEnv = (ReconcileEnv)_env; 56 ICompilationUnit parentCompilationUnit = reconcileEnv.getCompilationUnit(); 57 result = gfm.generateFileDuringReconcile( 58 parentCompilationUnit, _typeName, contents ); 59 } 62 else if ( phase == Phase.BUILD) { 63 result = gfm.generateFileDuringBuild( 64 Collections.singletonList(_env.getFile()), _typeName, contents, 65 _env.currentProcessorSupportsRTTG(), null ); 66 } 67 if (result != null) { 68 _env.addGeneratedSourceFile(result.getFile(), result.isModified()); 69 } 70 } 71 catch (CoreException ce) { 72 AptPlugin.log(ce, "Unable to generate type when JavaSourceFilePrintWriter was closed"); } 74 } 75 } 76 | Popular Tags |