1 18 19 package org.apache.tools.ant.taskdefs.compilers; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.Task; 24 import org.apache.tools.ant.util.ClasspathUtils; 25 import org.apache.tools.ant.util.JavaEnvUtils; 26 27 32 public final class CompilerAdapterFactory { 33 private static final String MODERN_COMPILER = "com.sun.tools.javac.Main"; 34 35 36 private CompilerAdapterFactory() { 37 } 38 39 64 public static CompilerAdapter getCompiler(String compilerType, Task task) 65 throws BuildException { 66 boolean isClassicCompilerSupported = true; 67 if (!JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2) 69 && !JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) { 70 isClassicCompilerSupported = false; 71 } 72 73 if (compilerType.equalsIgnoreCase("jikes")) { 74 return new Jikes(); 75 } 76 if (compilerType.equalsIgnoreCase("extJavac")) { 77 return new JavacExternal(); 78 } 79 if (compilerType.equalsIgnoreCase("classic") 80 || compilerType.equalsIgnoreCase("javac1.1") 81 || compilerType.equalsIgnoreCase("javac1.2")) { 82 if (isClassicCompilerSupported) { 83 return new Javac12(); 84 } else { 85 task.log("This version of java does " 86 + "not support the classic " 87 + "compiler; upgrading to modern", 88 Project.MSG_WARN); 89 compilerType = "modern"; 90 } 91 } 92 if (compilerType.equalsIgnoreCase("modern") 95 || compilerType.equalsIgnoreCase("javac1.3") 96 || compilerType.equalsIgnoreCase("javac1.4") 97 || compilerType.equalsIgnoreCase("javac1.5") 98 || compilerType.equalsIgnoreCase("javac1.6")) { 99 if (doesModernCompilerExist()) { 101 return new Javac13(); 102 } else { 103 if (isClassicCompilerSupported) { 104 task.log("Modern compiler not found - looking for " 105 + "classic compiler", Project.MSG_WARN); 106 return new Javac12(); 107 } else { 108 throw new BuildException("Unable to find a javac " 109 + "compiler;\n" 110 + MODERN_COMPILER 111 + " is not on the " 112 + "classpath.\n" 113 + "Perhaps JAVA_HOME does not" 114 + " point to the JDK.\n" 115 + "It is currently set to \"" 116 + JavaEnvUtils.getJavaHome() 117 + "\""); 118 } 119 } 120 } 121 122 if (compilerType.equalsIgnoreCase("jvc") 123 || compilerType.equalsIgnoreCase("microsoft")) { 124 return new Jvc(); 125 } 126 if (compilerType.equalsIgnoreCase("kjc")) { 127 return new Kjc(); 128 } 129 if (compilerType.equalsIgnoreCase("gcj")) { 130 return new Gcj(); 131 } 132 if (compilerType.equalsIgnoreCase("sj") 133 || compilerType.equalsIgnoreCase("symantec")) { 134 return new Sj(); 135 } 136 return resolveClassName(compilerType); 137 } 138 139 143 private static boolean doesModernCompilerExist() { 144 try { 145 Class.forName(MODERN_COMPILER); 146 return true; 147 } catch (ClassNotFoundException cnfe) { 148 try { 149 ClassLoader cl = CompilerAdapterFactory.class.getClassLoader(); 150 if (cl != null) { 151 cl.loadClass(MODERN_COMPILER); 152 return true; 153 } 154 } catch (ClassNotFoundException cnfe2) { 155 } 157 } 158 return false; 159 } 160 161 169 private static CompilerAdapter resolveClassName(String className) 170 throws BuildException { 171 return (CompilerAdapter) ClasspathUtils.newInstance(className, 172 CompilerAdapterFactory.class.getClassLoader(), 173 CompilerAdapter.class); 174 } 175 176 } 177 | Popular Tags |