1 12 13 package org.eclipse.jdt.internal.compiler.tool; 14 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.FileOutputStream ; 18 import java.io.FileReader ; 19 import java.io.FileWriter ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.Reader ; 24 import java.io.Writer ; 25 import java.net.URI ; 26 import java.nio.charset.Charset ; 27 28 import javax.lang.model.element.Modifier; 29 import javax.lang.model.element.NestingKind; 30 import javax.tools.SimpleJavaFileObject; 31 32 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 33 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; 34 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; 35 36 39 public class EclipseFileObject extends SimpleJavaFileObject { 40 private File f; 41 private Charset charset; 42 private boolean parentsExist; 44 public EclipseFileObject(String className, URI uri, Kind kind, Charset charset) { 45 super(uri, kind); 46 this.f = new File (this.uri); 47 this.charset = charset; 48 this.parentsExist = false; 49 } 50 51 54 public Modifier getAccessLevel() { 55 if (getKind() != Kind.CLASS) { 57 return null; 58 } 59 ClassFileReader reader = null; 60 try { 61 reader = ClassFileReader.read(this.f); 62 } catch (ClassFormatException e) { 63 } catch (IOException e) { 65 } 67 if (reader == null) { 68 return null; 69 } 70 final int accessFlags = reader.accessFlags(); 71 if ((accessFlags & ClassFileConstants.AccPublic) != 0) { 72 return Modifier.PUBLIC; 73 } 74 if ((accessFlags & ClassFileConstants.AccAbstract) != 0) { 75 return Modifier.ABSTRACT; 76 } 77 if ((accessFlags & ClassFileConstants.AccFinal) != 0) { 78 return Modifier.FINAL; 79 } 80 return null; 81 } 82 83 86 public NestingKind getNestingKind() { 87 switch(kind) { 88 case SOURCE : 89 return NestingKind.TOP_LEVEL; 90 case CLASS : 91 ClassFileReader reader = null; 92 try { 93 reader = ClassFileReader.read(this.f); 94 } catch (ClassFormatException e) { 95 } catch (IOException e) { 97 } 99 if (reader == null) { 100 return null; 101 } 102 if (reader.isAnonymous()) { 103 return NestingKind.ANONYMOUS; 104 } 105 if (reader.isLocal()) { 106 return NestingKind.LOCAL; 107 } 108 if (reader.isMember()) { 109 return NestingKind.MEMBER; 110 } 111 return NestingKind.TOP_LEVEL; 112 default: 113 return null; 114 } 115 } 116 117 120 public boolean delete() { 121 return this.f.delete(); 122 } 123 124 public boolean equals(Object o) { 125 if (!(o instanceof EclipseFileObject)) { 126 return false; 127 } 128 EclipseFileObject eclipseFileObject = (EclipseFileObject) o; 129 return eclipseFileObject.toUri().equals(this.uri); 130 } 131 132 135 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 136 return Util.getCharContents(this, ignoreEncodingErrors, org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(this.f), this.charset.toString()); 137 } 138 139 142 public long getLastModified() { 143 return this.f.lastModified(); 144 } 145 146 public String getName() { 147 return this.f.getPath(); 148 } 149 150 public int hashCode() { 151 return f.hashCode(); 152 } 153 154 157 public InputStream openInputStream() throws IOException { 158 return new FileInputStream (this.f); 160 } 161 162 165 public OutputStream openOutputStream() throws IOException { 166 ensureParentDirectoriesExist(); 167 return new FileOutputStream (this.f); 168 } 169 170 173 public Reader openReader(boolean ignoreEncodingErrors) throws IOException { 174 return new FileReader (this.f); 175 } 176 177 180 public Writer openWriter() throws IOException { 181 ensureParentDirectoriesExist(); 182 return new FileWriter (this.f); 183 } 184 185 @Override 186 public String toString() { 187 return this.f.getAbsolutePath(); 188 } 189 190 private void ensureParentDirectoriesExist() throws IOException { 191 if (!this.parentsExist) { 192 File parent = f.getParentFile(); 193 if (parent != null && !parent.exists()) { 194 if (!parent.mkdirs()) { 195 if (!parent.exists() || !parent.isDirectory()) 197 throw new IOException ("Unable to create parent directories for " + f); } 199 } 200 this.parentsExist = true; 201 } 202 } 203 204 205 } 206 | Popular Tags |