KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > env > JavaSourceFilePrintWriter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * mkaufman@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.core.internal.env;
13
14 import java.io.PrintWriter JavaDoc;
15 import java.io.StringWriter JavaDoc;
16 import java.util.Collections JavaDoc;
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 JavaDoc {
26
27     private final StringWriter JavaDoc _sw;
28     private final String JavaDoc _typeName;
29     private final AbstractCompilationEnv _env;
30     
31     /**
32      * @throws CoreException if type name is not valid
33      */

34     public JavaSourceFilePrintWriter( String JavaDoc typeName, StringWriter JavaDoc 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 JavaDoc 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                 // Need to call ReconcileContext.resetAst() for this to be effective;
60
// that will happen in ReconcileEnv.close().
61
}
62             else if ( phase == Phase.BUILD) {
63                 result = gfm.generateFileDuringBuild(
64                         Collections.singletonList(_env.getFile()), _typeName, contents,
65                         _env.currentProcessorSupportsRTTG(), null /* progress monitor */ );
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"); //$NON-NLS-1$
73
}
74     }
75 }
76
Popular Tags