Skip to content

Commit

Permalink
Kotlin support :)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswhocodes committed Apr 22, 2015
1 parent cdd93a7 commit 9ad7d77
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 7 deletions.
Expand Up @@ -245,6 +245,7 @@ public static final Set<String> getAutoGeneratedClassPrefixes()
public static final String S_BYTECODE_MINOR_VERSION = "minor version:"; public static final String S_BYTECODE_MINOR_VERSION = "minor version:";
public static final String S_BYTECODE_MAJOR_VERSION = "major version:"; public static final String S_BYTECODE_MAJOR_VERSION = "major version:";
public static final String S_BYTECODE_SIGNATURE = "Signature:"; public static final String S_BYTECODE_SIGNATURE = "Signature:";
public static final String S_BYTECODE_SOURCE_FILE= "SourceFile:";


public static final String S_POLYMORPHIC_SIGNATURE = "PolymorphicSignature"; public static final String S_POLYMORPHIC_SIGNATURE = "PolymorphicSignature";


Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/adoptopenjdk/jitwatch/loader/BytecodeLoader.java
Expand Up @@ -21,14 +21,17 @@
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_MINOR_VERSION; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_MINOR_VERSION;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_RUNTIMEVISIBLEANNOTATIONS; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_RUNTIMEVISIBLEANNOTATIONS;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_SIGNATURE; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_SIGNATURE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_SOURCE_FILE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_STACKMAPTABLE; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_STACKMAPTABLE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_STATIC_INITIALISER_SIGNATURE; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_BYTECODE_STATIC_INITIALISER_SIGNATURE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_CLOSE_BRACE; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_CLOSE_BRACE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_COLON; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_COLON;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_COMMA; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_COMMA;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DEFAULT; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DEFAULT;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DOT; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DOT;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DOUBLE_QUOTE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DOUBLE_SLASH; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DOUBLE_SLASH;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_EMPTY;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_HASH; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_HASH;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_NEWLINE; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_NEWLINE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_SEMICOLON; import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_SEMICOLON;
Expand Down Expand Up @@ -289,6 +292,10 @@ else if (line.startsWith(S_BYTECODE_MAJOR_VERSION))
classBytecode.setMajorVersion(majorVersion); classBytecode.setMajorVersion(majorVersion);
} }
} }
else if (line.startsWith(S_BYTECODE_SOURCE_FILE))
{
classBytecode.setSourceFile(getSourceFile(line));
}
break; break;
case INNERCLASSES: case INNERCLASSES:
String innerClassName = getInnerClassNameOrNull(line); String innerClassName = getInnerClassNameOrNull(line);
Expand Down Expand Up @@ -633,6 +640,22 @@ private static int getVersionPart(final String line)


return version; return version;
} }

private static String getSourceFile(final String line)
{
String result = null;

int colonPos = line.indexOf(C_COLON);

if (colonPos != -1 && colonPos != line.length() - 1)
{
result = line.substring(colonPos + 1);

result = result.replace(S_DOUBLE_QUOTE, S_EMPTY).trim();
}

return result;
}


public static List<BytecodeInstruction> parseInstructions(final String bytecode) public static List<BytecodeInstruction> parseInstructions(final String bytecode)
{ {
Expand Down
30 changes: 27 additions & 3 deletions src/main/java/org/adoptopenjdk/jitwatch/loader/ResourceLoader.java
Expand Up @@ -13,9 +13,11 @@
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.List; import java.util.List;
import java.util.Properties;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;


Expand Down Expand Up @@ -62,7 +64,7 @@ public static String getSource(MetaClass metaClass, List<String> sourceLocations
return result; return result;
} }


private static String getSource(String fileName, List<String> locations) public static String getSource(String fileName, List<String> locations)
{ {
String source = null; String source = null;


Expand All @@ -76,14 +78,16 @@ private static String getSource(String fileName, List<String> locations)
{ {
source = readFileInDirectory(lf, fileName); source = readFileInDirectory(lf, fileName);


System.out.println(fileName + " found: " + source);

if (source != null) if (source != null)
{ {
break; break;
} }
} }
else else
{ {
source = searchFileInZip(lf, fileName); source = readFileFromZip(lf, fileName);


if (source != null) if (source != null)
{ {
Expand Down Expand Up @@ -123,7 +127,27 @@ public static String readFile(File sourceFile)
return result; return result;
} }


public static String searchFileInZip(File zipFile, String fileName) public static Properties readManifestFromZip(File zipFile)
{
Properties result = new Properties();

String manifestSource = readFileFromZip(zipFile, "META-INF/MANIFEST.MF");

if (manifestSource != null)
{
try
{
result.load(new StringReader(manifestSource));
}
catch (IOException e)
{
logger.error("Couldn't read manifest from {}", zipFile, e);
}
}
return result;
}

public static String readFileFromZip(File zipFile, String fileName)
{ {
String result = null; String result = null;


Expand Down
Expand Up @@ -111,7 +111,7 @@ public ClassBC getClassBytecode(List<String> classLocations)
return classBytecode; return classBytecode;
} }


public String toString2() public String toStringDetailed()
{ {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append(classPackage.getName()).append(S_DOT).append(className).append(C_SPACE).append(compiledMethodCount) builder.append(classPackage.getName()).append(S_DOT).append(className).append(C_SPACE).append(compiledMethodCount)
Expand Down
Expand Up @@ -25,13 +25,15 @@
import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerJRuby; import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerJRuby;
import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerJava; import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerJava;
import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerJavaScript; import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerJavaScript;
import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerKotlin;
import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerScala; import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerScala;
import org.adoptopenjdk.jitwatch.sandbox.compiler.ICompiler; import org.adoptopenjdk.jitwatch.sandbox.compiler.ICompiler;
import org.adoptopenjdk.jitwatch.sandbox.runtime.IRuntime; import org.adoptopenjdk.jitwatch.sandbox.runtime.IRuntime;
import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeGroovy; import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeGroovy;
import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeJRuby; import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeJRuby;
import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeJava; import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeJava;
import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeJavaScript; import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeJavaScript;
import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeKotlin;
import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeScala; import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeScala;


public class LanguageManager public class LanguageManager
Expand Down Expand Up @@ -72,6 +74,7 @@ public ICompiler getCompiler(String vmLanguage)
result = new CompilerJRuby(languageHomeDir); result = new CompilerJRuby(languageHomeDir);
break; break;
case VM_LANGUAGE_KOTLIN: case VM_LANGUAGE_KOTLIN:
result = new CompilerKotlin(languageHomeDir);
break; break;
case VM_LANGUAGE_SCALA: case VM_LANGUAGE_SCALA:
result = new CompilerScala(languageHomeDir); result = new CompilerScala(languageHomeDir);
Expand Down Expand Up @@ -114,6 +117,7 @@ public IRuntime getRuntime(String vmLanguage)
result = new RuntimeJRuby(languageHomeDir); result = new RuntimeJRuby(languageHomeDir);
break; break;
case VM_LANGUAGE_KOTLIN: case VM_LANGUAGE_KOTLIN:
result = new RuntimeKotlin(languageHomeDir);
break; break;
case VM_LANGUAGE_SCALA: case VM_LANGUAGE_SCALA:
result = new RuntimeScala(languageHomeDir); result = new RuntimeScala(languageHomeDir);
Expand Down Expand Up @@ -226,7 +230,7 @@ public static boolean isLanguageEnabled(String vmLanguage)
case VM_LANGUAGE_JRUBY: case VM_LANGUAGE_JRUBY:
return false; return false;
case VM_LANGUAGE_KOTLIN: case VM_LANGUAGE_KOTLIN:
return false; return true;
case VM_LANGUAGE_SCALA: case VM_LANGUAGE_SCALA:
return true; return true;
default: default:
Expand Down
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2013, 2014 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
package org.adoptopenjdk.jitwatch.sandbox.compiler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.adoptopenjdk.jitwatch.sandbox.AbstractProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;

public class CompilerKotlin extends AbstractProcess implements ICompiler
{
private Path compilerPath;

private final String COMPILER_NAME = "kotlinc-jvm";
public static final String KOTLIN_EXECUTABLE_JAR = "jitwatch-sandbox-kotlin.jar";

public CompilerKotlin(String languageHomeDir) throws FileNotFoundException
{
compilerPath = Paths.get(languageHomeDir, "bin", COMPILER_NAME);

if (!compilerPath.toFile().exists())
{
throw new FileNotFoundException("Could not find " + COMPILER_NAME);
}

compilerPath = compilerPath.normalize();
}

@Override
public boolean compile(List<File> sourceFiles, List<String> classpathEntries, File outputDir, ISandboxLogListener logListener)
throws IOException
{
List<String> commands = new ArrayList<>();

commands.add(compilerPath.toString());

String outputDirPath = outputDir.getAbsolutePath().toString();

List<String> compileOptions = Arrays.asList(new String[] {
"-include-runtime",
"-d",
outputDirPath + File.separator + KOTLIN_EXECUTABLE_JAR });

commands.addAll(compileOptions);

if (classpathEntries.size() > 0)
{
commands.add("-classpath");
commands.add(makeClassPath(classpathEntries));
}

for (File sourceFile : sourceFiles)
{
commands.add(sourceFile.getAbsolutePath());
}

return runCommands(commands, logListener);
}
}
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2013, 2014 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
package org.adoptopenjdk.jitwatch.sandbox.runtime;

import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_EMPTY;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.adoptopenjdk.jitwatch.loader.ResourceLoader;
import org.adoptopenjdk.jitwatch.sandbox.AbstractProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;
import org.adoptopenjdk.jitwatch.sandbox.Sandbox;
import org.adoptopenjdk.jitwatch.sandbox.compiler.CompilerKotlin;

public class RuntimeKotlin extends AbstractProcess implements IRuntime
{
private Path runtimePath;

private final Path pathToRuntimeJar = Paths.get(Sandbox.SANDBOX_CLASS_DIR.toString(), CompilerKotlin.KOTLIN_EXECUTABLE_JAR);

private final String RUNTIME_NAME = "java" + getExecutableSuffix();

public RuntimeKotlin(String languageHomeDir) throws FileNotFoundException
{
// Kotlin is executed on the current running Java VM
runtimePath = Paths.get(System.getProperty("java.home"), "bin", RUNTIME_NAME);

if (!runtimePath.toFile().exists())
{
throw new FileNotFoundException("Could not find " + RUNTIME_NAME);
}

runtimePath = runtimePath.normalize();
}

@Override
public boolean execute(String className, List<String> classpathEntries, List<String> vmOptions, ISandboxLogListener logListener)
{
List<String> commands = new ArrayList<>();

commands.add(runtimePath.toString());

if (vmOptions.size() > 0)
{
commands.addAll(vmOptions);
}

if (classpathEntries.size() > 0)
{
commands.add("-cp");
commands.add(makeClassPath(classpathEntries));
}

commands.add("-jar");
commands.add(pathToRuntimeJar.toString());

return runCommands(commands, logListener);
}

@Override
public String getClassToExecute(File fileToRun)
{
// Main class is in the jar manifest
return S_EMPTY;
}

@Override
public String getClassForTriView(File fileToRun)
{
Properties manifest = ResourceLoader.readManifestFromZip(pathToRuntimeJar.toFile());

String mainClass = manifest.getProperty("Main-Class");

return mainClass;
}
}
Expand Up @@ -174,6 +174,8 @@ public void handle(ActionEvent e)
selectedTab.setText(pane.getName()); selectedTab.setText(pane.getName());


setVMLanguage(pane); setVMLanguage(pane);

saveEditorPaneConfig();
} }
} }
}); });
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/org/adoptopenjdk/jitwatch/ui/triview/TriView.java
Expand Up @@ -503,10 +503,26 @@ public void setMember(IMetaMember member, boolean force, boolean jumpToSource)


memberInfo.setMember(member); memberInfo.setMember(member);


List<String> allClassLocations = config.getAllClassLocations();

ClassBC classBytecode = loadBytecodeForCurrentMember(allClassLocations);

if (!sameClass) if (!sameClass)
{ {
String source = ResourceLoader.getSource(memberClass, config.getSourceLocations()); String source = ResourceLoader.getSource(memberClass, config.getSourceLocations());


if (source == null)
{
logger.debug("Could not find source for {}. Trying to locate via bytecode source file attribute", memberClass);;

String sourceFileName = classBytecode.getSourceFile();

if (sourceFileName != null)
{
source = ResourceLoader.getSource(sourceFileName, config.getSourceLocations());
}
}

viewerSource.setContent(source, true); viewerSource.setContent(source, true);
} }


Expand All @@ -518,9 +534,7 @@ public void setMember(IMetaMember member, boolean force, boolean jumpToSource)


StringBuilder statusBarBuilder = new StringBuilder(); StringBuilder statusBarBuilder = new StringBuilder();


List<String> allClassLocations = config.getAllClassLocations();


ClassBC classBytecode = loadBytecodeForCurrentMember(allClassLocations);


updateStatusBarWithClassInformation(classBytecode, statusBarBuilder); updateStatusBarWithClassInformation(classBytecode, statusBarBuilder);
updateStatusBarIfCompiled(statusBarBuilder); updateStatusBarIfCompiled(statusBarBuilder);
Expand Down

0 comments on commit 9ad7d77

Please sign in to comment.