KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > dispatch > HookedJavaFileObject


1 /*******************************************************************************
2  * Copyright (c) 2006, 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  * wharley@bea.com - initial API and implementation
10  *
11  *******************************************************************************/

12
13 package org.eclipse.jdt.internal.compiler.apt.dispatch;
14
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStream JavaDoc;
17 import java.io.Writer JavaDoc;
18
19 import javax.tools.ForwardingJavaFileObject;
20 import javax.tools.JavaFileObject;
21
22 import org.eclipse.jdt.core.compiler.CharOperation;
23 import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
24 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
25 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
26 import org.eclipse.jdt.internal.compiler.env.IBinaryType;
27 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
28
29 /**
30  * A delegating JavaFileObject that hooks the close() methods of the Writer
31  * or OutputStream objects that it produces, and notifies the annotation
32  * dispatch manager when a new compilation unit is produced.
33  */

34 public class HookedJavaFileObject extends
35         ForwardingJavaFileObject<JavaFileObject>
36 {
37     // A delegating Writer that passes all commands to its contained Writer,
38
// but hooks close() to notify the annotation dispatch manager of the new unit.
39
private class ForwardingWriter extends Writer JavaDoc {
40         private final Writer JavaDoc _w;
41         ForwardingWriter(Writer JavaDoc w) {
42             _w = w;
43         }
44         @Override JavaDoc
45         public Writer JavaDoc append(char c) throws IOException JavaDoc {
46             return _w.append(c);
47         }
48         @Override JavaDoc
49         public Writer JavaDoc append(CharSequence JavaDoc csq, int start, int end)
50                 throws IOException JavaDoc {
51             return _w.append(csq, start, end);
52         }
53         @Override JavaDoc
54         public Writer JavaDoc append(CharSequence JavaDoc csq) throws IOException JavaDoc {
55             return _w.append(csq);
56         }
57         // This is the only interesting method - it has to notify the
58
// dispatch manager of the new file.
59
@Override JavaDoc
60         public void close() throws IOException JavaDoc {
61             _w.close();
62             closed();
63         }
64         @Override JavaDoc
65         public void flush() throws IOException JavaDoc {
66             _w.flush();
67         }
68         @Override JavaDoc
69         public void write(char[] cbuf) throws IOException JavaDoc {
70             _w.write(cbuf);
71         }
72         @Override JavaDoc
73         public void write(int c) throws IOException JavaDoc {
74             _w.write(c);
75         }
76         @Override JavaDoc
77         public void write(String JavaDoc str, int off, int len)
78                 throws IOException JavaDoc {
79             _w.write(str, off, len);
80         }
81         @Override JavaDoc
82         public void write(String JavaDoc str) throws IOException JavaDoc {
83             _w.write(str);
84         }
85         @Override JavaDoc
86         public void write(char[] cbuf, int off, int len)
87         throws IOException JavaDoc {
88             _w.write(cbuf, off, len);
89         }
90         @Override JavaDoc
91         protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
92             return new ForwardingWriter(this._w);
93         }
94         @Override JavaDoc
95         public int hashCode() {
96             return _w.hashCode();
97         }
98         @Override JavaDoc
99         public boolean equals(Object JavaDoc obj) {
100             if (this == obj)
101                 return true;
102             if (obj == null)
103                 return false;
104             if (getClass() != obj.getClass())
105                 return false;
106             final ForwardingWriter other = (ForwardingWriter) obj;
107             if (_w == null) {
108                 if (other._w != null)
109                     return false;
110             } else if (!_w.equals(other._w))
111                 return false;
112             return true;
113         }
114         @Override JavaDoc
115         public String JavaDoc toString() {
116             return "ForwardingWriter wrapping " + _w.toString(); //$NON-NLS-1$
117
}
118     }
119     
120     // A delegating Writer that passes all commands to its contained Writer,
121
// but hooks close() to notify the annotation dispatch manager of the new unit.
122
private class ForwardingOutputStream extends OutputStream JavaDoc {
123         private final OutputStream JavaDoc _os;
124         
125         ForwardingOutputStream(OutputStream JavaDoc os) {
126             _os = os;
127         }
128         
129         @Override JavaDoc
130         public void close() throws IOException JavaDoc {
131             _os.close();
132             closed();
133         }
134         @Override JavaDoc
135         public void flush() throws IOException JavaDoc {
136             _os.flush();
137         }
138         @Override JavaDoc
139         public void write(byte[] b, int off, int len) throws IOException JavaDoc {
140             _os.write(b, off, len);
141         }
142         @Override JavaDoc
143         public void write(byte[] b) throws IOException JavaDoc {
144             _os.write(b);
145         }
146         @Override JavaDoc
147         public void write(int b) throws IOException JavaDoc {
148             _os.write(b);
149         }
150         @Override JavaDoc
151         protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
152             return new ForwardingOutputStream(this._os);
153         }
154         @Override JavaDoc
155         public int hashCode() {
156             return _os.hashCode();
157         }
158         @Override JavaDoc
159         public boolean equals(Object JavaDoc obj) {
160             if (this == obj)
161                 return true;
162             if (obj == null)
163                 return false;
164             if (getClass() != obj.getClass())
165                 return false;
166             final ForwardingOutputStream other = (ForwardingOutputStream) obj;
167             if (_os == null) {
168                 if (other._os != null)
169                     return false;
170             } else if (!_os.equals(other._os))
171                 return false;
172             return true;
173         }
174         @Override JavaDoc
175         public String JavaDoc toString() {
176             return "ForwardingOutputStream wrapping " + _os.toString(); //$NON-NLS-1$
177
}
178     }
179     
180     /**
181      * The Filer implementation that we need to notify when a new file is created.
182      */

183     protected final BatchFilerImpl _filer;
184     
185     /**
186      * The name of the file that is created; this is passed to the CompilationUnit constructor,
187      * and ultimately to the java.io.File constructor, so it is a normal pathname, just like
188      * what would be on the compiler command line.
189      */

190     protected final String JavaDoc _fileName;
191     
192     /**
193      * A compilation unit is created when the writer or stream is closed. Only do this once.
194      */

195     private boolean _closed = false;
196     
197     public HookedJavaFileObject(JavaFileObject fileObject, String JavaDoc fileName, BatchFilerImpl filer) {
198         super(fileObject);
199         _filer = filer;
200         _fileName = fileName;
201     }
202
203     @Override JavaDoc
204     public OutputStream JavaDoc openOutputStream() throws IOException JavaDoc {
205         return new ForwardingOutputStream(super.openOutputStream());
206     }
207
208     @Override JavaDoc
209     public Writer JavaDoc openWriter() throws IOException JavaDoc {
210         return new ForwardingWriter(super.openWriter());
211     }
212     
213     protected void closed() {
214         if (!_closed) {
215             _closed = true;
216             //TODO: support encoding
217
switch(this.getKind()) {
218                 case SOURCE :
219                     CompilationUnit unit = new CompilationUnit(null, _fileName, null /* encoding */);
220                     _filer.addNewUnit(unit);
221                     break;
222                 case CLASS :
223                     IBinaryType binaryType = null;
224                     try {
225                         binaryType = ClassFileReader.read(_fileName);
226                     } catch (ClassFormatException e) {
227                         // ignore
228
} catch (IOException JavaDoc e) {
229                         // ignore
230
}
231                     if (binaryType != null) {
232                         char[] name = binaryType.getName();
233                         ReferenceBinding type = this._filer._env._compiler.lookupEnvironment.getType(CharOperation.splitOn('/', name));
234                         if (type != null && type.isValidBinding() && type.isBinaryBinding()) {
235                             _filer.addNewClassFile(type);
236                         }
237                     }
238             }
239         }
240     }
241 }
242
Popular Tags