Skip to content

Commit

Permalink
devkit: more reliable openapi.jar version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
trespasserw committed May 5, 2015
1 parent 5e0e849 commit fd00042
Showing 1 changed file with 32 additions and 35 deletions.
67 changes: 32 additions & 35 deletions plugins/devkit/src/projectRoots/IdeaJdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.ZipFileCache;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiManager;
import com.intellij.psi.impl.compiled.ClsParsingUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.io.zip.JBZipEntry;
import com.intellij.util.io.zip.JBZipFile;
import icons.DevkitIcons;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
Expand All @@ -41,15 +45,12 @@
import org.jetbrains.idea.devkit.DevKitBundle;

import javax.swing.*;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
* @author anna
Expand Down Expand Up @@ -264,47 +265,43 @@ public static boolean isValidInternalJdk(Sdk ideaSdk, Sdk sdk) {
return false;
}

private static int getIdeaClassFileVersion(Sdk ideaSdk) {
@Nullable
private static JavaSdkVersion getRequiredJdkVersion(Sdk ideaSdk) {
File apiJar = getOpenApiJar(ideaSdk.getHomePath());
if (apiJar != null) {
int classFileVersion = getIdeaClassFileVersion(apiJar);
if (classFileVersion > 0) {
LanguageLevel languageLevel = ClsParsingUtil.getLanguageLevelByVersion(classFileVersion);
if (languageLevel != null) {
return JavaSdkVersion.fromLanguageLevel(languageLevel);
}
}
}

return null;
}

private static int getIdeaClassFileVersion(File apiJar) {
try {
File apiJar = getOpenApiJar(ideaSdk.getHomePath());
if (apiJar != null) {
ZipFile zipFile = ZipFileCache.acquire(apiJar.getPath());
try {
ZipEntry entry = zipFile.getEntry("com/intellij/psi/PsiManager.class");
if (entry != null) {
DataInputStream stream = new DataInputStream(zipFile.getInputStream(entry));
try {
if (stream.skip(6) == 6) {
return stream.readUnsignedShort();
}
}
finally {
stream.close();
}
JBZipFile zipFile = new JBZipFile(apiJar);
try {
JBZipEntry entry = zipFile.getEntry(PsiManager.class.getName().replace('.', '/') + ".class");
if (entry != null) {
byte[] bytes = entry.getData();
if (bytes != null && bytes.length > 8) {
return ((int)bytes[6] << 8) + (int)bytes[7];
}
}
finally {
ZipFileCache.release(zipFile);
}
}
finally {
zipFile.close();
}
}
catch (IOException ignored) { }

return -1;
}

@Nullable
private static JavaSdkVersion getRequiredJdkVersion(final Sdk ideaSdk) {
int classFileVersion = getIdeaClassFileVersion(ideaSdk);
switch(classFileVersion) {
case 48: return JavaSdkVersion.JDK_1_4;
case 49: return JavaSdkVersion.JDK_1_5;
case 50: return JavaSdkVersion.JDK_1_6;
case 51: return JavaSdkVersion.JDK_1_7;
}
return null;
}

public static void setupSdkPaths(final SdkModificator sdkModificator, final String sdkHome, final Sdk internalJava) {
//roots from internal jre
addClasses(sdkModificator, internalJava);
Expand Down

0 comments on commit fd00042

Please sign in to comment.