1 12 13 package org.eclipse.jdt.internal.compiler.apt.dispatch; 14 15 import java.io.IOException ; 16 import java.io.OutputStream ; 17 import java.io.Writer ; 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 34 public class HookedJavaFileObject extends 35 ForwardingJavaFileObject<JavaFileObject> 36 { 37 private class ForwardingWriter extends Writer { 40 private final Writer _w; 41 ForwardingWriter(Writer w) { 42 _w = w; 43 } 44 @Override 45 public Writer append(char c) throws IOException { 46 return _w.append(c); 47 } 48 @Override 49 public Writer append(CharSequence csq, int start, int end) 50 throws IOException { 51 return _w.append(csq, start, end); 52 } 53 @Override 54 public Writer append(CharSequence csq) throws IOException { 55 return _w.append(csq); 56 } 57 @Override 60 public void close() throws IOException { 61 _w.close(); 62 closed(); 63 } 64 @Override 65 public void flush() throws IOException { 66 _w.flush(); 67 } 68 @Override 69 public void write(char[] cbuf) throws IOException { 70 _w.write(cbuf); 71 } 72 @Override 73 public void write(int c) throws IOException { 74 _w.write(c); 75 } 76 @Override 77 public void write(String str, int off, int len) 78 throws IOException { 79 _w.write(str, off, len); 80 } 81 @Override 82 public void write(String str) throws IOException { 83 _w.write(str); 84 } 85 @Override 86 public void write(char[] cbuf, int off, int len) 87 throws IOException { 88 _w.write(cbuf, off, len); 89 } 90 @Override 91 protected Object clone() throws CloneNotSupportedException { 92 return new ForwardingWriter(this._w); 93 } 94 @Override 95 public int hashCode() { 96 return _w.hashCode(); 97 } 98 @Override 99 public boolean equals(Object 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 115 public String toString() { 116 return "ForwardingWriter wrapping " + _w.toString(); } 118 } 119 120 private class ForwardingOutputStream extends OutputStream { 123 private final OutputStream _os; 124 125 ForwardingOutputStream(OutputStream os) { 126 _os = os; 127 } 128 129 @Override 130 public void close() throws IOException { 131 _os.close(); 132 closed(); 133 } 134 @Override 135 public void flush() throws IOException { 136 _os.flush(); 137 } 138 @Override 139 public void write(byte[] b, int off, int len) throws IOException { 140 _os.write(b, off, len); 141 } 142 @Override 143 public void write(byte[] b) throws IOException { 144 _os.write(b); 145 } 146 @Override 147 public void write(int b) throws IOException { 148 _os.write(b); 149 } 150 @Override 151 protected Object clone() throws CloneNotSupportedException { 152 return new ForwardingOutputStream(this._os); 153 } 154 @Override 155 public int hashCode() { 156 return _os.hashCode(); 157 } 158 @Override 159 public boolean equals(Object 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 175 public String toString() { 176 return "ForwardingOutputStream wrapping " + _os.toString(); } 178 } 179 180 183 protected final BatchFilerImpl _filer; 184 185 190 protected final String _fileName; 191 192 195 private boolean _closed = false; 196 197 public HookedJavaFileObject(JavaFileObject fileObject, String fileName, BatchFilerImpl filer) { 198 super(fileObject); 199 _filer = filer; 200 _fileName = fileName; 201 } 202 203 @Override 204 public OutputStream openOutputStream() throws IOException { 205 return new ForwardingOutputStream(super.openOutputStream()); 206 } 207 208 @Override 209 public Writer openWriter() throws IOException { 210 return new ForwardingWriter(super.openWriter()); 211 } 212 213 protected void closed() { 214 if (!_closed) { 215 _closed = true; 216 switch(this.getKind()) { 218 case SOURCE : 219 CompilationUnit unit = new CompilationUnit(null, _fileName, null ); 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 } catch (IOException e) { 229 } 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 |