1 33 34 package edu.rice.cs.drjava.model.compiler; 35 36 import java.util.LinkedList ; 37 import java.lang.reflect.Field ; 38 39 import edu.rice.cs.util.Log; 40 41 import edu.rice.cs.util.swing.Utilities; 42 import edu.rice.cs.util.UnexpectedException; 43 44 49 public class CompilerRegistry { 50 51 52 53 public static final String [] JAVA_16_COMPILERS = { 54 "edu.rice.cs.drjava.model.compiler.Javac160FromSetLocation", 56 "edu.rice.cs.drjava.model.compiler.Javac160FromClasspath", 57 "edu.rice.cs.drjava.model.compiler.Javac160FromToolsJar" 58 }; 59 60 public static final String [] JAVA_15_COMPILERS = { 61 "edu.rice.cs.drjava.model.compiler.Javac150FromSetLocation", 63 "edu.rice.cs.drjava.model.compiler.Javac150FromClasspath", 64 "edu.rice.cs.drjava.model.compiler.Javac150FromToolsJar" 65 }; 66 67 68 public static final String [] JAVA_14_COMPILERS = { 69 "edu.rice.cs.drjava.model.compiler.Javac141FromSetLocation", 71 "edu.rice.cs.drjava.model.compiler.Javac141FromClasspath", 72 "edu.rice.cs.drjava.model.compiler.Javac141FromToolsJar" 73 }; 74 75 76 static final String [][] DEFAULT_COMPILERS = { 77 JAVA_16_COMPILERS, 79 JAVA_15_COMPILERS, 81 JAVA_14_COMPILERS 83 }; 84 85 86 public static final CompilerRegistry ONLY = new CompilerRegistry(); 87 88 private final static Log _log = new Log("CompilerTest.txt", false); 89 90 91 private volatile ClassLoader _baseClassLoader; 92 93 94 private final String [] _candidateCompilers; 95 96 97 private CompilerInterface _activeCompiler = NoCompilerAvailable.ONLY; 98 99 100 private CompilerRegistry() { 101 _baseClassLoader = getClass().getClassLoader(); 102 103 String version = CompilerProxy.VERSION; 105 if (version.equals("1.4")) _candidateCompilers = JAVA_14_COMPILERS; 106 else if (version.equals("1.5")) _candidateCompilers = JAVA_15_COMPILERS; 107 else if (version.equals("1.6")) _candidateCompilers = JAVA_16_COMPILERS; 108 else _candidateCompilers = null; 109 } 110 111 112 public void setBaseClassLoader(ClassLoader l) { _baseClassLoader = l; } 113 114 115 public ClassLoader getBaseClassLoader() { return _baseClassLoader; } 116 117 122 public CompilerInterface[] getAvailableCompilers() { 123 LinkedList <CompilerInterface> availableCompilers = new LinkedList <CompilerInterface>(); 124 125 if (_candidateCompilers == null) throw new 126 UnexpectedException("Java specification version " + CompilerProxy.VERSION + "is not supported. Must be 1.4, 1.5, or 1.6"); 127 128 for (String name : _candidateCompilers) { 129 _log.log("CompilerRegistry.getAvailableCompilers is checking compiler: " + name); 130 try { if (_createCompiler(name, availableCompilers)) continue; } 131 catch (Throwable t) { 132 _log.log("Compiler " + name + " failed to load:"); 134 } 137 } 138 139 if (availableCompilers.size() == 0) availableCompilers.add(NoCompilerAvailable.ONLY); 140 141 _log.log("CompilerRegistry.getAvailableCompilers() returning " + availableCompilers); 142 143 return availableCompilers.toArray(new CompilerInterface[availableCompilers.size()]); 144 } 145 146 private boolean _createCompiler(String name, LinkedList <CompilerInterface> availableCompilers) throws Throwable { 147 _log.log("CompilerRegistry._createCompiler(" + name + ", " + availableCompilers +") called"); 148 CompilerInterface compiler = _instantiateCompiler(name); 149 if (compiler.isAvailable()) { 150 _log.log("Compiler " + this + " is available: added to compile list"); 151 152 if (_activeCompiler == NoCompilerAvailable.ONLY) { 155 _activeCompiler = compiler; 157 } 158 availableCompilers.add(compiler); 160 return true; 161 } 162 else { 163 _log.log("Compiler " + this + " is NOT available."); 164 return false; 165 } 166 } 167 168 public boolean isNoCompilerAvailable() { return getActiveCompiler() == NoCompilerAvailable.ONLY; } 169 170 176 public void setActiveCompiler(CompilerInterface compiler) { 177 if (compiler == null) { 178 throw new IllegalArgumentException ("Cannot set active compiler to null."); 180 } 181 else _activeCompiler = compiler; 182 } 183 184 188 public CompilerInterface getActiveCompiler() { 189 if (_activeCompiler == NoCompilerAvailable.ONLY) getAvailableCompilers(); 191 192 194 if (_activeCompiler.isAvailable()) return _activeCompiler; 195 return NoCompilerAvailable.ONLY; 196 } 197 198 205 private CompilerInterface _instantiateCompiler(String name) throws Throwable { 206 _log.log("CompilerRegistry._instantiateCompiler using class loader " + _baseClassLoader + " to load " + name); 207 Class <?> clazz = _baseClassLoader.loadClass(name); 208 _log.log("Loaded compiler named " + name + " with class name " + clazz); 209 return createCompiler(clazz); 210 } 211 212 public static CompilerInterface createCompiler(Class clazz) throws Throwable { 213 try { 214 _log.log("CompilerRegistry.createCompiler(" + clazz + ") called"); 215 Field field = clazz.getField("ONLY"); 216 _log.log(clazz + ".ONLY = " + field); 217 Object val = field.get(null); _log.log("createCompiler(" + clazz + ") returning " + val); 219 return (CompilerInterface) val; 220 } 221 catch (Throwable t) { 222 _log.log("createCompiler threw exception " + t); 224 return (CompilerInterface) clazz.newInstance(); 225 } 226 } 227 } 228 | Popular Tags |