resolve all descriptors from libraries

(basic)
This commit is contained in:
Stepan Koltsov
2012-04-19 14:22:58 +04:00
parent bfdba9cebc
commit e698b0eec4
2 changed files with 256 additions and 0 deletions
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.compiler.longTest;
import org.jetbrains.annotations.NotNull;
/**
* @author Stepan Koltsov
*/
public class LibFromMaven {
@NotNull
private final String org;
@NotNull
private final String module;
@NotNull
private final String rev;
public LibFromMaven(@NotNull String org, @NotNull String module, @NotNull String rev) {
this.org = org;
this.module = module;
this.rev = rev;
}
@NotNull
public String getOrg() {
return org;
}
@NotNull
public String getModule() {
return module;
}
@NotNull
public String getRev() {
return rev;
}
@Override
public String toString() {
return org + "/" + module + "/" + rev;
}
}
@@ -0,0 +1,199 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.compiler.longTest;
import com.google.common.io.ByteStreams;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.CompileCompilerDependenciesTest;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.TimeUtils;
import org.jetbrains.jet.compiler.JetCoreEnvironment;
import org.jetbrains.jet.di.InjectorForJavaSemanticServices;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.resolve.FqName;
import org.jetbrains.jet.lang.resolve.java.CompilerDependencies;
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
import org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @author Stepan Koltsov
*/
public class ResolveDescriptorsFromExternalLibraries {
@NotNull
private final CompilerDependencies compilerDependencies;
public static void main(String[] args) throws Exception {
new ResolveDescriptorsFromExternalLibraries().run();
System.out.println("$");
}
public ResolveDescriptorsFromExternalLibraries() {
System.out.println("Getting compiler dependencies");
compilerDependencies = CompileCompilerDependenciesTest.compilerDependenciesForTests(CompilerSpecialMode.REGULAR);
}
private void run() throws Exception {
testLibrary("com.google.guava", "guava", "12.0-rc2");
testLibrary("org.springframework", "spring-core", "3.1.1.RELEASE");
}
private void testLibrary(@NotNull String org, @NotNull String module, @NotNull String rev) throws Exception {
LibFromMaven lib = new LibFromMaven(org, module, rev);
File jar = getLibrary(lib);
System.out.println("Testing library " + lib + "...");
System.out.println("Using file " + jar);
long start = System.currentTimeMillis();
Disposable junk = new Disposable() {
@Override
public void dispose() { }
};
JetCoreEnvironment jetCoreEnvironment = JetTestUtils.createEnvironmentWithMockJdk(junk);
jetCoreEnvironment.addToClasspath(jar);
InjectorForJavaSemanticServices injector = new InjectorForJavaSemanticServices(compilerDependencies, jetCoreEnvironment.getProject());
FileInputStream is = new FileInputStream(jar);
try {
ZipInputStream zip = new ZipInputStream(is);
for (;;) {
ZipEntry entry = zip.getNextEntry();
if (entry == null) {
break;
}
String entryName = entry.getName();
if (!entryName.endsWith(".class")) {
continue;
}
if (entryName.matches("(.*/|)package-info\\.class")) {
continue;
}
if (entryName.contains("$")) {
continue;
}
String className = entryName.substring(0, entryName.length() - ".class".length()).replace("/", ".");
ClassDescriptor clazz = injector.getJavaDescriptorResolver().resolveClass(new FqName(className), DescriptorSearchRule.ERROR_IF_FOUND_IN_KOTLIN);
if (clazz == null) {
throw new IllegalStateException("class not found by name " + className + " in " + lib);
}
clazz.getDefaultType().getMemberScope().getAllDescriptors();
}
} finally {
try {
is.close();
}
catch (Throwable e) {}
Disposer.dispose(junk);
}
System.out.println("Testing library " + lib + " done in " + TimeUtils.millisecondsToSecondsString(System.currentTimeMillis() - start) + "s");
}
@NotNull
private File getLibrary(@NotNull LibFromMaven lib) throws Exception {
File userHome = new File(System.getProperty("user.home"));
String fileName = lib.getModule() + "-" + lib.getRev() + ".jar";
File dir = new File(userHome, ".kotlin-project/resolve-libraries/" + lib.getOrg() + "/" + lib.getModule());
File file = new File(dir, fileName);
if (file.exists()) {
return file;
}
JetTestUtils.mkdirs(dir);
File tmp = new File(dir, fileName + "~");
String uri = url(lib);
GetMethod method = new GetMethod(uri);
FileOutputStream os = null;
System.out.println("Downloading library " + lib + " to " + file);
MultiThreadedHttpConnectionManager connectionManager = null;
try {
connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient httpClient = new HttpClient(connectionManager);
os = new FileOutputStream(tmp);
String userAgent = ResolveDescriptorsFromExternalLibraries.class.getName() + "/commons-httpclient";
method.getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
int code = httpClient.executeMethod(method);
if (code != 200) {
throw new RuntimeException("failed to execute GET " + uri + ", code is " + code);
}
InputStream responseBodyAsStream = method.getResponseBodyAsStream();
if (responseBodyAsStream == null) {
throw new RuntimeException("method is executed fine, but response is null");
}
ByteStreams.copy(responseBodyAsStream, os);
os.close();
if (!tmp.renameTo(file)) {
throw new RuntimeException("failed to rename file: " + tmp + " to " + file);
}
return file;
}
finally {
if (method != null) {
try {
method.releaseConnection();
}
catch (Throwable e) {}
}
if (connectionManager != null) {
try {
connectionManager.shutdown();
}
catch (Throwable e) {}
}
if (os != null) {
try {
os.close();
}
catch (Throwable e) {}
}
tmp.delete();
}
}
private static String url(LibFromMaven lib) {
return "http://repo1.maven.org/maven2/" + lib.getOrg().replace(".", "/") + "/" + lib.getModule() + "/" + lib.getRev()
+ "/" + lib.getModule() + "-" + lib.getRev() + ".jar";
}
}