mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
classes not compatible with jdk7 removed from jps
This commit is contained in:
@@ -4,7 +4,6 @@ import com.intellij.ant.InstrumentationUtil
|
||||
import com.intellij.ant.InstrumentationUtil.FormInstrumenter
|
||||
import com.intellij.ant.PrefixedPath
|
||||
import com.intellij.compiler.instrumentation.InstrumentationClassFinder
|
||||
import org.jetbrains.jps.builders.javacApi.Java16ApiCompilerRunner
|
||||
import org.jetbrains.jps.*
|
||||
|
||||
/**
|
||||
@@ -26,17 +25,6 @@ class JavacBuilder implements ModuleBuilder, ModuleCycleBuilder {
|
||||
String sourceLevel = module.languageLevel
|
||||
String targetLevel = module.languageLevel
|
||||
String customArgs = module["javac_args"]; // it seems javac_args property is not set, can we drop it?
|
||||
if (projectBuilder.useInProcessJavac) {
|
||||
String version = System.getProperty("java.version")
|
||||
if (true) {
|
||||
if (Java16ApiCompilerRunner.compile(module, projectBuilder, state, sourceLevel, targetLevel, customArgs)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
else {
|
||||
projectBuilder.info("In-process Javac won't be used for '${module.name}', because Java version ($version) doesn't match to source level ($sourceLevel)")
|
||||
}
|
||||
}
|
||||
|
||||
def params = [:]
|
||||
params.destdir = state.targetFolder
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
package org.jetbrains.jps.builders.javacApi
|
||||
|
||||
import org.jetbrains.jps.ModuleBuildState
|
||||
import org.jetbrains.jps.ModuleChunk
|
||||
import org.jetbrains.jps.ProjectBuilder
|
||||
import org.jetbrains.jps.Sdk
|
||||
import org.jetbrains.jps.builders.JavaFileCollector
|
||||
|
||||
import javax.tools.JavaCompiler
|
||||
import javax.tools.JavaCompiler.CompilationTask
|
||||
import javax.tools.JavaFileObject
|
||||
import javax.tools.StandardLocation
|
||||
import javax.tools.ToolProvider
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
class Java16ApiCompiler {
|
||||
private static instance
|
||||
private OptimizedFileManager fileManager
|
||||
private JavaCompiler compiler
|
||||
|
||||
static Java16ApiCompiler getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new Java16ApiCompiler()
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
def Java16ApiCompiler() {
|
||||
compiler = ToolProvider.getSystemJavaCompiler()
|
||||
fileManager = new OptimizedFileManager();
|
||||
}
|
||||
|
||||
def compile(ModuleChunk chunk, ProjectBuilder projectBuilder, ModuleBuildState state, String sourceLevel, String targetLevel, String customArgs) {
|
||||
List<String> options = []
|
||||
|
||||
if (customArgs != null) {
|
||||
options << customArgs
|
||||
}
|
||||
|
||||
if (sourceLevel != null) {
|
||||
options << "-source"
|
||||
options << sourceLevel
|
||||
}
|
||||
if (targetLevel != null) {
|
||||
options << "-target"
|
||||
options << targetLevel
|
||||
}
|
||||
options << "-g"
|
||||
options << "-nowarn"
|
||||
|
||||
List<File> filesToCompile = []
|
||||
|
||||
Set<File> excluded = state.excludes.collect { new File(it.toString()) }
|
||||
state.sourceRoots.each {
|
||||
JavaFileCollector.collectRecursively(new File(it.toString()), filesToCompile, excluded)
|
||||
}
|
||||
|
||||
if (filesToCompile.size() > 0) {
|
||||
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, [new File(state.targetFolder)])
|
||||
List<File> classpath = []
|
||||
List<File> bootclasspath = []
|
||||
StringBuilder cp = new StringBuilder()
|
||||
|
||||
Sdk sdk = chunk.getSdk()
|
||||
|
||||
if (sdk != null) {
|
||||
sdk.classpath.each { bootclasspath << new File(String.valueOf(it)) }
|
||||
|
||||
fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootclasspath)
|
||||
}
|
||||
|
||||
state.classpath.each {
|
||||
classpath << new File(String.valueOf(it))
|
||||
cp.append(String.valueOf(it))
|
||||
cp.append(File.pathSeparator)
|
||||
}
|
||||
|
||||
cp.append(state.targetFolder)
|
||||
|
||||
fileManager.setLocation(StandardLocation.CLASS_PATH, classpath)
|
||||
fileManager.setProperties(state.callback, toURLs(cp.toString()))
|
||||
|
||||
Iterable<? extends JavaFileObject> toCompile = fileManager.getJavaFileObjectsFromFiles(filesToCompile)
|
||||
StringWriter out = new StringWriter()
|
||||
CompilationTask task = compiler.getTask(new PrintWriter(out), fileManager, null, options, null, toCompile)
|
||||
|
||||
if (!task.call()) {
|
||||
projectBuilder.buildInfoPrinter.printCompilationErrors(projectBuilder, "javac", out.toString())
|
||||
projectBuilder.error("Compilation failed")
|
||||
}
|
||||
else {
|
||||
System.out.println(out.toString());
|
||||
}
|
||||
projectBuilder.listeners*.onJavaFilesCompiled(chunk, filesToCompile.size())
|
||||
}
|
||||
else {
|
||||
projectBuilder.info("No java source files found in '${chunk.name}', skipping compilation")
|
||||
}
|
||||
}
|
||||
|
||||
private URL[] toURLs(final String classPath) {
|
||||
final List urls = new ArrayList();
|
||||
for (StringTokenizer tokenizer = new StringTokenizer(classPath, File.pathSeparator); tokenizer.hasMoreTokens();) {
|
||||
final String s = tokenizer.nextToken();
|
||||
urls.add(new File(s).toURL());
|
||||
}
|
||||
return (URL[])urls.toArray(new URL[urls.size()]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package org.jetbrains.jps.builders.javacApi
|
||||
|
||||
import org.jetbrains.jps.ModuleBuildState
|
||||
import org.jetbrains.jps.ModuleChunk
|
||||
import org.jetbrains.jps.ProjectBuilder
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
class Java16ApiCompilerRunner {
|
||||
private static boolean notAvailable
|
||||
|
||||
static boolean compile(ModuleChunk chunk, ProjectBuilder projectBuilder, ModuleBuildState state, String sourceLevel, String targetLevel, String customArgs) {
|
||||
if (notAvailable) {
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
Java16ApiCompiler compiler = Java16ApiCompiler.getInstance()
|
||||
compiler.compile(chunk, projectBuilder, state, sourceLevel, targetLevel, customArgs)
|
||||
return true
|
||||
}
|
||||
catch (NoClassDefFoundError error) {
|
||||
projectBuilder.warning("Java 1.6 API compiler is not available")
|
||||
notAvailable = true
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace()
|
||||
projectBuilder.warning("Compilation failed with exception for '${chunk.name}'")
|
||||
throw e
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -1,257 +0,0 @@
|
||||
package org.jetbrains.jps.builders.javacApi;
|
||||
|
||||
import com.intellij.ant.InstrumentationUtil;
|
||||
import com.intellij.compiler.instrumentation.InstrumentationClassFinder;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
import com.sun.tools.javac.util.DefaultFileManager;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.ListBuffer;
|
||||
import org.jetbrains.asm4.ClassReader;
|
||||
import org.jetbrains.ether.dependencyView.Callbacks;
|
||||
import org.jetbrains.jps.PathUtil;
|
||||
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.tools.*;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
public class OptimizedFileManager extends DefaultFileManager {
|
||||
private interface DelayedClassFileWriter {
|
||||
public void commit() throws IOException;
|
||||
}
|
||||
|
||||
private java.util.List<DelayedClassFileWriter> myWriters = new ArrayList<DelayedClassFileWriter>();
|
||||
private boolean myUseZipFileIndex;
|
||||
private final Map<File, Archive> myArchives;
|
||||
private final Map<File, Boolean> myIsFile = new ConcurrentHashMap<File, Boolean>();
|
||||
private Callbacks.Backend callback;
|
||||
private InstrumentationClassFinder classFinder;
|
||||
private Map<String, byte[]> myCompiledClasses = new HashMap<String, byte[]>();
|
||||
|
||||
public void setProperties(final Callbacks.Backend c, final URL[] classpath) {
|
||||
callback = c;
|
||||
classFinder = new InstrumentationClassFinder(classpath) {
|
||||
protected InputStream lookupClassBeforeClasspath(String internalClassName) {
|
||||
final byte[] bytes = myCompiledClasses.get(internalClassName);
|
||||
return bytes != null? new ByteArrayInputStream(bytes) : null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public OptimizedFileManager() {
|
||||
super(new Context(), true, null);
|
||||
try {
|
||||
final Field archivesField = DefaultFileManager.class.getDeclaredField("archives");
|
||||
archivesField.setAccessible(true);
|
||||
myArchives = (Map<File, Archive>) archivesField.get(this);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
final Field useZipFileIndexField = DefaultFileManager.class.getDeclaredField("useZipFileIndex");
|
||||
useZipFileIndexField.setAccessible(true);
|
||||
myUseZipFileIndex = (Boolean) useZipFileIndexField.get(this);
|
||||
} catch (Exception e) {
|
||||
myUseZipFileIndex = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
|
||||
Iterable<? extends File> path = getLocation(location);
|
||||
if (path == null) return Collections.emptyList();
|
||||
|
||||
String relativePath = packageName.replace('.', File.separatorChar);
|
||||
ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>();
|
||||
|
||||
for (File root : path) {
|
||||
Archive archive = myArchives.get(root);
|
||||
final boolean isFile;
|
||||
if (archive != null) {
|
||||
isFile = true;
|
||||
} else {
|
||||
Boolean cachedIsFile = myIsFile.get(root);
|
||||
if (cachedIsFile == null) {
|
||||
cachedIsFile = root.isFile();
|
||||
myIsFile.put(root, cachedIsFile);
|
||||
}
|
||||
isFile = cachedIsFile.booleanValue();
|
||||
}
|
||||
if (isFile) {
|
||||
collectFromArchive(root, archive, relativePath, kinds, recurse, results);
|
||||
} else {
|
||||
File directory = relativePath.length() != 0 ? new File(root, relativePath) : root;
|
||||
collectFromDirectory(directory, kinds, recurse, results);
|
||||
}
|
||||
}
|
||||
|
||||
return results.toList();
|
||||
}
|
||||
|
||||
private void collectFromArchive(File root, Archive archive, String relativePath, Set<JavaFileObject.Kind> kinds, boolean recurse, ListBuffer<JavaFileObject> result) {
|
||||
if (archive == null) {
|
||||
try {
|
||||
archive = openArchive(root);
|
||||
} catch (IOException ex) {
|
||||
log.error("error.reading.file", root, ex.getLocalizedMessage());
|
||||
return;
|
||||
}
|
||||
}
|
||||
String separator = myUseZipFileIndex ? File.separator : "/";
|
||||
if (relativePath.length() != 0) {
|
||||
if (!myUseZipFileIndex) {
|
||||
relativePath = relativePath.replace('\\', '/');
|
||||
}
|
||||
if (!relativePath.endsWith(separator)) relativePath = relativePath + separator;
|
||||
}
|
||||
|
||||
collectArchiveFiles(archive, relativePath, kinds, result);
|
||||
if (recurse) {
|
||||
for (String s : archive.getSubdirectories()) {
|
||||
if (s.startsWith(relativePath) && !s.equals(relativePath)) {
|
||||
if (!s.endsWith(separator)) {
|
||||
s += separator;
|
||||
}
|
||||
collectArchiveFiles(archive, s, kinds, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void collectFromDirectory(File directory, Set<JavaFileObject.Kind> fileKinds,
|
||||
boolean recurse, ListBuffer<JavaFileObject> result) {
|
||||
File[] children = directory.listFiles();
|
||||
if (children == null) return;
|
||||
|
||||
for (File child : children) {
|
||||
String name = child.getName();
|
||||
if (child.isDirectory()) {
|
||||
if (recurse && SourceVersion.isIdentifier(name)) {
|
||||
collectFromDirectory(directory, fileKinds, recurse, result);
|
||||
}
|
||||
} else {
|
||||
if (isValidFile(name, fileKinds)) {
|
||||
JavaFileObject fe = getRegularFile(child);
|
||||
result.append(fe);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void collectArchiveFiles(Archive archive, String relativePath, Set<JavaFileObject.Kind> fileKinds, ListBuffer<JavaFileObject> result) {
|
||||
List<String> files = archive.getFiles(relativePath);
|
||||
if (files != null) {
|
||||
for (String file; !files.isEmpty(); files = files.tail) {
|
||||
file = files.head;
|
||||
if (isValidFile(file, fileKinds)) {
|
||||
result.append(archive.getFileObject(relativePath, file));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidFile(String name, Set<JavaFileObject.Kind> fileKinds) {
|
||||
int dot = name.lastIndexOf(".");
|
||||
JavaFileObject.Kind kind = getKind(dot == -1 ? name : name.substring(dot));
|
||||
return fileKinds.contains(kind);
|
||||
}
|
||||
|
||||
//actually Javac doesn't check if this method returns null. It always get substring of the returned string starting from the last dot.
|
||||
@Override
|
||||
public String inferBinaryName(Location location, JavaFileObject file) {
|
||||
final String name = file.getName();
|
||||
int dot = name.lastIndexOf('.');
|
||||
final String relativePath = dot != -1 ? name.substring(0, dot) : name;
|
||||
return relativePath.replace(File.separatorChar, '.');
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaFileObject getJavaFileForOutput(Location location, final String className, final JavaFileObject.Kind kind, FileObject fileObject) throws IOException {
|
||||
final JavaFileObject result = super.getJavaFileForOutput(location, className, kind, fileObject);
|
||||
final String classFileName = PathUtil.toPath(result.toUri());
|
||||
final String sourceFileName = PathUtil.toPath(fileObject.toUri());
|
||||
|
||||
return new ForwardingJavaFileObject<JavaFileObject>(result) {
|
||||
private OutputStream superOpenOutputStream() throws IOException {
|
||||
return super.openOutputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream openOutputStream() throws IOException {
|
||||
return new OutputStream() {
|
||||
public void flush() throws IOException {
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
}
|
||||
|
||||
public void write(int b) throws IOException {
|
||||
assert (false);
|
||||
}
|
||||
|
||||
public void write(byte[] b) throws IOException {
|
||||
assert (false);
|
||||
}
|
||||
|
||||
public void write(final byte[] b, final int off, final int len) throws IOException {
|
||||
final byte[] buffer = Arrays.copyOfRange(b, off, len);
|
||||
|
||||
if (kind.equals(JavaFileObject.Kind.CLASS)) {
|
||||
myCompiledClasses.put(className.replace('.', '/'), buffer);
|
||||
|
||||
if (callback != null) {
|
||||
final ClassReader reader = new ClassReader(buffer);
|
||||
callback.associate(classFileName, sourceFileName, reader);
|
||||
}
|
||||
|
||||
myWriters.add(new DelayedClassFileWriter() {
|
||||
public void commit() throws IOException {
|
||||
final OutputStream result = superOpenOutputStream();
|
||||
|
||||
final byte[] instrumented = InstrumentationUtil.instrumentNotNull(buffer, classFinder);
|
||||
|
||||
if (instrumented != null) {
|
||||
result.write(instrumented);
|
||||
} else {
|
||||
result.write(buffer);
|
||||
}
|
||||
|
||||
result.close();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
final OutputStream result = superOpenOutputStream();
|
||||
result.write(buffer);
|
||||
result.close();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
super.flush();
|
||||
|
||||
for (DelayedClassFileWriter f : myWriters) {
|
||||
try {
|
||||
f.commit();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
myWriters.clear();
|
||||
if (classFinder != null) {
|
||||
classFinder.releaseResources();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user