1 package polyglot.types; 2 3 import polyglot.frontend.Compiler; 4 import polyglot.frontend.ExtensionInfo; 5 import polyglot.frontend.FileSource; 6 import polyglot.main.Report; 7 import polyglot.types.reflect.ClassFile; 8 import polyglot.types.reflect.ClassFileLoader; 9 10 77 public class SourceClassResolver extends LoadedClassResolver 78 { 79 Compiler compiler; 80 ExtensionInfo ext; 81 82 92 public SourceClassResolver(Compiler compiler, ExtensionInfo ext, 93 String classpath, ClassFileLoader loader, 94 boolean allowRawClasses) 95 { 96 super(ext.typeSystem(), classpath, loader, ext.version(), allowRawClasses); 97 this.compiler = compiler; 98 this.ext = ext; 99 } 100 101 public boolean packageExists(String name) { 102 if (super.packageExists(name)) { 103 return true; 104 } 105 110 return false; 111 } 112 113 116 public Named find(String name) throws SemanticException { 117 if (Report.should_report(report_topics, 3)) 118 Report.report(3, "SourceCR.find(" + name + ")"); 119 120 ClassFile clazz = null; 121 ClassFile encodedClazz = null; 122 FileSource source = null; 123 124 clazz = loadFile(name); 126 if (clazz != null) { 127 if (clazz.encodedClassType(version.name()) != null) { 129 if (Report.should_report(report_topics, 4)) 130 Report.report(4, "Class " + name + " has encoded type info"); 131 encodedClazz = clazz; 132 } 133 } 134 135 if (clazz != null && ! this.allowRawClasses) { 136 clazz = null; 137 } 138 139 source = ext.sourceLoader().classSource(name); 141 if (Report.should_report(report_topics, 4)) { 142 if (source == null) 143 Report.report(4, "Class " + name + " not found in source file"); 144 else 145 Report.report(4, "Class " + name + " found in source " + source); 146 } 147 148 if (encodedClazz != null) { 150 if (Report.should_report(report_topics, 4)) 151 Report.report(4, "Not using raw class file for " + name); 152 clazz = null; 153 } 154 155 if (clazz != null && source != null) { 158 long classModTime = clazz.rawSourceLastModified(); 159 long sourceModTime = source.lastModified().getTime(); 160 161 if (classModTime < sourceModTime) { 162 if (Report.should_report(report_topics, 3)) 163 Report.report(3, "Source file version is newer than compiled for " + 164 name + "."); 165 clazz = null; 166 } 167 else { 168 source = null; 169 } 170 } 171 172 if (encodedClazz != null && source != null) { 173 long classModTime = encodedClazz.sourceLastModified(version.name()); 174 long sourceModTime = source.lastModified().getTime(); 175 176 int comp = checkCompilerVersion(encodedClazz.compilerVersion(version.name())); 177 178 if (classModTime < sourceModTime) { 179 if (Report.should_report(report_topics, 3)) 180 Report.report(3, "Source file version is newer than compiled for " + 181 name + "."); 182 encodedClazz = null; 183 } 184 else if (comp != COMPATIBLE) { 185 if (Report.should_report(report_topics, 3)) 187 Report.report(3, "Incompatible source file version for " + name + "."); 188 encodedClazz = null; 189 } 190 } 191 192 if (encodedClazz != null) { 193 if (Report.should_report(report_topics, 4)) 194 Report.report(4, "Using encoded class type for " + name); 195 try { 196 return getEncodedType(encodedClazz, name); 197 } 198 catch (BadSerializationException e) { 199 throw e; 200 } 201 catch (SemanticException e) { 202 if (Report.should_report(report_topics, 4)) 203 Report.report(4, "Could not load encoded class " + name); 204 encodedClazz = null; 205 } 206 } 207 208 210 if (clazz != null) { 211 if (Report.should_report(report_topics, 4)) 212 Report.report(4, "Using raw class file for " + name); 213 return clazz.type(ts); 214 } 215 216 if (source != null) { 217 if (Report.should_report(report_topics, 4)) 218 Report.report(4, "Using source file for " + name); 219 return getTypeFromSource(source, name); 220 } 221 222 if (clazz != null && !this.allowRawClasses) { 223 throw new SemanticException("Class \"" + name + "\" not found." 226 + " A class file was found, but it did not contain appropriate" 227 + " information for the Polyglot-based compiler " + 228 ext.compilerName() + ". Try using " + ext.compilerName() 229 + " to recompile the source code."); 230 } 231 throw new NoClassException(name); 232 } 233 234 237 protected Named getTypeFromSource(FileSource source, String name) 238 throws SemanticException 239 { 240 ext.readSource(source); 242 243 return ts.parsedResolver().find(name); 247 } 248 } 249 | Popular Tags |