package org.eclipse.jdt.core;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Commandline.Argument;
import org.apache.tools.ant.util.JavaEnvUtils;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.antadapter.AntAdapterMessages;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
import org.eclipse.jdt.internal.compiler.util.Util;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class JDTCompilerAdapter extends DefaultCompilerAdapter {
private static final char[] SEPARATOR_CHARS = new char[] { '/', '\\' };
private static final char[] ADAPTER_PREFIX = "#ADAPTER#".toCharArray();
private static final char[] ADAPTER_ENCODING = "ENCODING#".toCharArray();
private static final char[] ADAPTER_ACCESS = "ACCESS#".toCharArray();
private static String compilerClass = "org.eclipse.jdt.internal.compiler.batch.Main";
String logFileName;
Map customDefaultOptions;
private Map fileEncodings = null;
private Map dirEncodings = null;
private List accessRules = null;
@Override
public boolean execute() throws BuildException {
this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.usingJDTCompiler"), Project.MSG_VERBOSE);
Commandline cmd = setupJavacCommand();
try {
Class c = Class.forName(compilerClass);
Constructor batchCompilerConstructor = c.getConstructor(new Class[] { PrintWriter.class, PrintWriter.class, Boolean.TYPE, Map.class});
Object batchCompilerInstance = batchCompilerConstructor.newInstance(new Object[] {new PrintWriter(System.out), new PrintWriter(System.err), Boolean.TRUE, this.customDefaultOptions});
Method compile = c.getMethod("compile", new Class[] {String[].class});
Object result = compile.invoke(batchCompilerInstance, new Object[] { cmd.getArguments()});
final boolean resultValue = ((Boolean) result).booleanValue();
if (!resultValue && this.logFileName != null) {
this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.error.compilationFailed", this.logFileName));
}
return resultValue;
} catch (ClassNotFoundException cnfe) {
throw new BuildException(AntAdapterMessages.getString("ant.jdtadapter.error.cannotFindJDTCompiler"), cnfe);
} catch (Exception ex) {
throw new BuildException(ex);
}
}
@Override
protected Commandline setupJavacCommand() throws BuildException {
Commandline cmd = new Commandline();
this.customDefaultOptions = new CompilerOptions().getMap();
Class javacClass = Javac.class;
String [] compilerArgs = processCompilerArguments(javacClass);
cmd.createArgument().setValue("-noExit");
if (this.bootclasspath != null) {
cmd.createArgument().setValue("-bootclasspath");
if (this.bootclasspath.size() != 0) {
cmd.createArgument().setPath(this.bootclasspath);
} else {
cmd.createArgument().setValue(Util.EMPTY_STRING);
}
}
if (this.extdirs != null) {
cmd.createArgument().setValue("-extdirs");
cmd.createArgument().setPath(this.extdirs);
}
Path classpath = new Path(this.project);
classpath.append(getCompileClasspath());
cmd.createArgument().setValue("-classpath");
createClasspathArgument(cmd, classpath);
Path sourcepath = null;
Method getSourcepathMethod = null;
try {
getSourcepathMethod = javacClass.getMethod("getSourcepath", (Class[]) null);
} catch(NoSuchMethodException e) {
}
Path compileSourcePath = null;
if (getSourcepathMethod != null) {
try {
compileSourcePath = (Path) getSourcepathMethod.invoke(this.attributes, (Object[]) null);
} catch (IllegalAccessException | InvocationTargetException e) {
}
}
if (compileSourcePath != null) {
sourcepath = compileSourcePath;
} else {
sourcepath = this.src;
}
cmd.createArgument().setValue("-sourcepath");
createClasspathArgument(cmd, sourcepath);
final String javaVersion = JavaEnvUtils.getJavaVersion();
String memoryParameterPrefix = javaVersion.equals(JavaEnvUtils.JAVA_1_1) ? "-J-" : "-J-X";
if (this.memoryInitialSize != null) {
if (!this.attributes.isForkedJavac()) {
this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.ignoringMemoryInitialSize"), Project.MSG_WARN);
} else {
cmd.createArgument().setValue(memoryParameterPrefix
+ "ms" + this.memoryInitialSize);
}
}
if (this.memoryMaximumSize != null) {
if (!this.attributes.isForkedJavac()) {
this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.ignoringMemoryMaximumSize"), Project.MSG_WARN);
} else {
cmd.createArgument().setValue(memoryParameterPrefix
+ "mx" + this.memoryMaximumSize);
}
}
if (this.debug) {
Method getDebugLevelMethod = null;
try {
getDebugLevelMethod = javacClass.getMethod("getDebugLevel", (Class[]) null);
} catch(NoSuchMethodException e) {
}
String debugLevel = null;
if (getDebugLevelMethod != null) {
try {
debugLevel = (String) getDebugLevelMethod.invoke(this.attributes, (Object[]) null);
} catch (IllegalAccessException | InvocationTargetException e) {
}
}
if (debugLevel != null) {
this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE);
this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.DO_NOT_GENERATE);
this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.DO_NOT_GENERATE);
if (debugLevel.length() != 0) {
if (debugLevel.indexOf("vars") != -1) {
this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
}
if (debugLevel.indexOf("lines") != -1) {
this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
}
if (debugLevel.indexOf("source") != -1) {
this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.GENERATE);
}
}
} else {
this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.GENERATE);
}
} else {
this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE);
this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.DO_NOT_GENERATE);
this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.DO_NOT_GENERATE);
}
if (this.attributes.getNowarn()) {
Object[] entries = this.customDefaultOptions.entrySet().toArray();
for (int i = 0, max = entries.length; i < max; i++) {
Map.Entry entry = (Map.Entry) entries[i];
if (!(entry.getKey() instanceof String))
continue;
if (!(entry.getValue() instanceof String))
continue;
if (((String) entry.getValue()).equals(CompilerOptions.WARNING)) {
this.customDefaultOptions.put(entry.getKey(), CompilerOptions.IGNORE);
}
}
this.customDefaultOptions.put(CompilerOptions.OPTION_TaskTags, Util.EMPTY_STRING);
if (this.deprecation) {
this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.WARNING);
this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationInDeprecatedCode, CompilerOptions.ENABLED);
this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.ENABLED);
}
} else if (this.deprecation) {
this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.WARNING);
this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationInDeprecatedCode, CompilerOptions.ENABLED);
this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.ENABLED);
} else {
this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE);
this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationInDeprecatedCode, CompilerOptions.DISABLED);
this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.DISABLED);
}
if (this.destDir != null) {
cmd.createArgument().setValue("-d");
cmd.createArgument().setFile(this.destDir.getAbsoluteFile());
}
if (this.verbose) {
cmd.createArgument().setValue("-verbose");
}
if (!this.attributes.getFailonerror()) {
cmd.createArgument().setValue("-proceedOnError");
}
if (this.target != null) {
this.customDefaultOptions.put(CompilerOptions.OPTION_TargetPlatform, this.target);
}
String source = this.attributes.getSource();
if (source != null) {
this.customDefaultOptions.put(CompilerOptions.OPTION_Source, source);
this.customDefaultOptions.put(CompilerOptions.OPTION_Compliance, source);
}
if (compilerArgs != null) {
final int length = compilerArgs.length;
if (length != 0) {
for (int i = 0, max = length; i < max; i++) {
String arg = compilerArgs[i];
if (this.logFileName == null && "-log".equals(arg) && ((i + 1) < max)) {
this.logFileName = compilerArgs[i + 1];
}
cmd.createArgument().setValue(arg);
}
}
}
if (this.encoding != null) {
cmd.createArgument().setValue("-encoding");
cmd.createArgument().setValue(this.encoding);
}
logAndAddFilesToCompile(cmd);
return cmd;
}
private String[] processCompilerArguments(Class javacClass) {
Method getCurrentCompilerArgsMethod = null;
try {
getCurrentCompilerArgsMethod = javacClass.getMethod("getCurrentCompilerArgs", (Class[]) null);
} catch (NoSuchMethodException e) {
}
String[] compilerArgs = null;
if (getCurrentCompilerArgsMethod != null) {
try {
compilerArgs = (String[]) getCurrentCompilerArgsMethod.invoke(this.attributes, (Object[]) null);
} catch (IllegalAccessException | InvocationTargetException e) {
}
}
if (compilerArgs != null) checkCompilerArgs(compilerArgs);
return compilerArgs;
}
private void checkCompilerArgs(String[] args) {
for (int i = 0; i < args.length; i++) {
if (args[i].charAt(0) == '@') {
try {
char[] content = Util.getFileCharContent(new File(args[i].substring(1)), null);
int offset = 0;
int prefixLength = ADAPTER_PREFIX.length;
while ((offset = CharOperation.indexOf(ADAPTER_PREFIX, content, true, offset)) > -1) {
int start = offset + prefixLength;
int end = CharOperation.indexOf('\n', content, start);
if (end == -1)
end = content.length;
while (CharOperation.isWhitespace(content[end])) {
end--;
}
if (CharOperation.equals(ADAPTER_ENCODING, content, start, start + ADAPTER_ENCODING.length)) {
CharOperation.replace(content, SEPARATOR_CHARS, File.separatorChar, start, end + 1);
start += ADAPTER_ENCODING.length;
int encodeStart = CharOperation.lastIndexOf('[', content, start, end);
if (start < encodeStart && encodeStart < end) {
boolean isFile = CharOperation.equals(SuffixConstants.SUFFIX_java, content, encodeStart - 5, encodeStart, false);
String str = String.valueOf(content, start, encodeStart - start);
String enc = String.valueOf(content, encodeStart, end - encodeStart + 1);
if (isFile) {
if (this.fileEncodings == null)
this.fileEncodings = new HashMap();
this.fileEncodings.put(str, enc);
} else {
if (this.dirEncodings == null)
this.dirEncodings = new HashMap();
this.dirEncodings.put(str, enc);
}
}
} else if (CharOperation.equals(ADAPTER_ACCESS, content, start, start + ADAPTER_ACCESS.length)) {
start += ADAPTER_ACCESS.length;
int accessStart = CharOperation.indexOf('[', content, start, end);
CharOperation.replace(content, SEPARATOR_CHARS, File.separatorChar, start, accessStart);
if (start < accessStart && accessStart < end) {
String path = String.valueOf(content, start, accessStart - start);
String access = String.valueOf(content, accessStart, end - accessStart + 1);
if (this.accessRules == null)
this.accessRules = new ArrayList();
this.accessRules.add(path);
this.accessRules.add(access);
}
}
offset = end;
}
} catch (IOException e) {
}
}
}
}
private void createClasspathArgument(Commandline cmd, Path classpath) {
Argument arg = cmd.createArgument();
final String[] pathElements = classpath.list();
if (pathElements.length == 0) {
arg.setValue(Util.EMPTY_STRING);
return;
}
if (this.accessRules == null) {
arg.setPath(classpath);
return;
}
int rulesLength = this.accessRules.size();
String[] rules = (String[]) this.accessRules.toArray(new String[rulesLength]);
int nextRule = 0;
final StringBuffer result = new StringBuffer();
for (int i = 0, max = pathElements.length; i < max; i++) {
if (i > 0)
result.append(File.pathSeparatorChar);
String pathElement = pathElements[i];
result.append(pathElement);
for (int j = nextRule; j < rulesLength; j += 2) {
String rule = rules[j];
if (pathElement.endsWith(rule)) {
result.append(rules[j + 1]);
nextRule = j + 2;
break;
}
if (rule.endsWith(File.separator)) {
int ruleLength = rule.length();
if (pathElement.regionMatches(false, pathElement.length() - ruleLength + 1, rule, 0, ruleLength - 1)) {
result.append(rules[j + 1]);
nextRule = j + 2;
break;
}
} else if (pathElement.endsWith(File.separator)) {
int ruleLength = rule.length();
if (pathElement.regionMatches(false, pathElement.length() - ruleLength - 1, rule, 0, ruleLength)) {
result.append(rules[j + 1]);
nextRule = j + 2;
break;
}
}
}
}
arg.setValue(result.toString());
}
@Override
protected void logAndAddFilesToCompile(Commandline cmd) {
this.attributes.log("Compilation " + cmd.describeArguments(),
Project.MSG_VERBOSE);
StringBuffer niceSourceList = new StringBuffer("File");
if (this.compileList.length != 1) {
niceSourceList.append("s");
}
niceSourceList.append(" to be compiled:");
niceSourceList.append(System.lineSeparator());
String[] encodedFiles = null, encodedDirs = null;
int encodedFilesLength = 0, encodedDirsLength = 0;
if (this.fileEncodings != null) {
encodedFilesLength = this.fileEncodings.size();
encodedFiles = new String[encodedFilesLength];
this.fileEncodings.keySet().toArray(encodedFiles);
}
if (this.dirEncodings != null) {
encodedDirsLength = this.dirEncodings.size();
encodedDirs = new String[encodedDirsLength];
this.dirEncodings.keySet().toArray(encodedDirs);
Comparator comparator = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return ((String) o2).length() - ((String) o1).length();
}
};
Arrays.sort(encodedDirs, comparator);
}
for (int i = 0; i < this.compileList.length; i++) {
String arg = this.compileList[i].getAbsolutePath();
boolean encoded = false;
if (encodedFiles != null) {
for (int j = 0; j < encodedFilesLength; j++) {
if (arg.endsWith(encodedFiles[j])) {
arg = arg + (String) this.fileEncodings.get(encodedFiles[j]);
if (j < encodedFilesLength - 1) {
System.arraycopy(encodedFiles, j + 1, encodedFiles, j, encodedFilesLength - j - 1);
}
encodedFiles[--encodedFilesLength] = null;
encoded = true;
break;
}
}
}
if (!encoded && encodedDirs != null) {
for (int j = 0; j < encodedDirsLength; j++) {
if (arg.lastIndexOf(encodedDirs[j]) != -1) {
arg = arg + (String) this.dirEncodings.get(encodedDirs[j]);
break;
}
}
}
cmd.createArgument().setValue(arg);
niceSourceList.append(" " + arg + System.lineSeparator());
}
this.attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE);
}
}