-
-
Notifications
You must be signed in to change notification settings - Fork 93
Bytecode command #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Bytecode command #207
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
application/src/main/java/org/togetherjava/tjbot/commands/code/BytecodeAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.togetherjava.tjbot.commands.code; | ||
|
||
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.entities.MessageEmbed; | ||
|
||
import org.togetherjava.tjbot.commands.utils.CodeFence; | ||
import org.togetherjava.tjbot.imc.CompilationResult; | ||
import org.togetherjava.tjbot.imc.InMemoryCompiler; | ||
import org.togetherjava.tjbot.imc.JavacOption; | ||
import org.togetherjava.tjbot.javap.Javap; | ||
import org.togetherjava.tjbot.javap.JavapOption; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.util.StringJoiner; | ||
|
||
// TODO Javadoc | ||
final class BytecodeAction implements CodeAction { | ||
@Override | ||
public String getLabel() { | ||
return "Bytecode"; | ||
} | ||
|
||
@Override | ||
public MessageEmbed apply(CodeFence codeFence) { | ||
if (!isLanguageSupported(codeFence.language())) { | ||
return createResponse("Sorry, only Java is supported."); | ||
} | ||
|
||
String result = compile(codeFence.code()); | ||
return createResponse(result); | ||
} | ||
|
||
private static boolean isLanguageSupported(@Nullable String language) { | ||
// Assume that absence indicates Java code | ||
return language == null || "java".equals(language); | ||
} | ||
|
||
private static MessageEmbed createResponse(String content) { | ||
// Rust highlighting looks decent for bytecode | ||
CodeFence resultCodeFence = new CodeFence("rust", content); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe extract There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
|
||
return new EmbedBuilder().setTitle("Bytecode") | ||
.setDescription(resultCodeFence.toMarkdown()) | ||
.setColor(CodeMessageHandler.AMBIENT_COLOR) | ||
.build(); | ||
} | ||
|
||
private static String compile(String code) { | ||
// Raw compilation | ||
CompilationResult compilationResult; | ||
try { | ||
compilationResult = InMemoryCompiler.compile(code, JavacOption.DEBUG_ALL); | ||
} catch (RuntimeException e) { | ||
// TODO logging | ||
return "A fatal error has occurred during compilation: " + e.getMessage(); | ||
} | ||
|
||
if (!compilationResult.success()) { | ||
StringJoiner failureMessage = new StringJoiner("\n", "Compilation failed.\n", ""); | ||
compilationResult.compileInfos() | ||
.forEach(info -> failureMessage.add(info.diagnostic().toString())); | ||
return failureMessage.toString(); | ||
} | ||
|
||
// Disassembling | ||
String disassembledResult; | ||
try { | ||
disassembledResult = Javap.disassemble(compilationResult.bytes(), JavapOption.VERBOSE); | ||
} catch (RuntimeException e) { | ||
// TODO logging | ||
return "A fatal error has occurred during disassembly: " + e.getMessage(); | ||
} | ||
|
||
// TODO max length of embeds | ||
return disassembledResult; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
dependencies { | ||
implementation 'com.google.code.findbugs:jsr305:3.0.2' | ||
|
||
implementation project(':utils') | ||
} |
37 changes: 37 additions & 0 deletions
37
inmemorycompiler/src/main/java/org/togetherjava/tjbot/imc/ByteJavaFileObjectLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.togetherjava.tjbot.imc; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
class ByteJavaFileObjectLoader extends ClassLoader { | ||
private final Map<String, InMemoryByteJavaFileObject> nameToClassJFO = new LinkedHashMap<>(); | ||
|
||
public ByteJavaFileObjectLoader(ClassLoader parent) { | ||
super(parent); | ||
} | ||
|
||
public InMemoryByteJavaFileObject registerJFO(InMemoryByteJavaFileObject jfo) { | ||
nameToClassJFO.put(jfo.getName(), jfo); | ||
|
||
return jfo; | ||
} | ||
|
||
/** | ||
* Only meant to be used for registering and getting, not finding. There are no plans to | ||
* implement this, and it is also not needed. | ||
* | ||
* @throws UnsupportedOperationException always | ||
*/ | ||
@Override | ||
protected Class<?> findClass(String name) throws UnsupportedOperationException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public byte[] getLastBytes() { | ||
return last(nameToClassJFO.values().toArray(InMemoryByteJavaFileObject[]::new)).getBytes(); | ||
} | ||
|
||
private <E> E last(E[] col) { | ||
return col[col.length - 1]; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
inmemorycompiler/src/main/java/org/togetherjava/tjbot/imc/CompilationResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.togetherjava.tjbot.imc; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
/** | ||
* Class representing a compilation result of {@link InMemoryCompiler} | ||
*/ | ||
public record CompilationResult(boolean success, byte[] bytes, Iterable<CompileInfo> compileInfos) { | ||
|
||
private static final CompilationResult EMPTY_RESULT = | ||
new CompilationResult(false, new byte[0], Collections.emptyList()); | ||
|
||
/** | ||
* @return an empty compilation result | ||
*/ | ||
public static CompilationResult empty() { | ||
return EMPTY_RESULT; | ||
} | ||
|
||
/** | ||
* Creates an unsuccessful compilation result | ||
* | ||
* @param compileInfos compilation infos | ||
* @return the generated compilation result | ||
*/ | ||
public static CompilationResult fail(Iterable<CompileInfo> compileInfos) { | ||
return new CompilationResult(false, new byte[0], compileInfos); | ||
} | ||
|
||
/** | ||
* Creates a successful compilation result | ||
* | ||
* @param bytes classfile bytecode | ||
* @param compileInfos compilation infos | ||
* @return the generated compilation result | ||
*/ | ||
public static CompilationResult success(byte[] bytes, Iterable<CompileInfo> compileInfos) { | ||
return new CompilationResult(true, bytes, compileInfos); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
|
||
if (!(o instanceof CompilationResult that) || success != that.success | ||
|| !Arrays.equals(bytes, that.bytes)) { | ||
return false; | ||
} | ||
|
||
return compileInfos.equals(that.compileInfos); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = (success ? 1 : 0); | ||
|
||
result = 31 * result + Arrays.hashCode(bytes); | ||
result = 31 * result + compileInfos.hashCode(); | ||
|
||
return result; | ||
} | ||
RealYusufIsmail marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public String toString() { | ||
return "CompilationResult{" + "success=" + success + ", bytes=" + Arrays.toString(bytes) | ||
+ ", compileInfos=" + compileInfos + '}'; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
inmemorycompiler/src/main/java/org/togetherjava/tjbot/imc/CompileInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.togetherjava.tjbot.imc; | ||
|
||
import javax.tools.Diagnostic; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Wrapper class for a {@link Diagnostic} of unknown type | ||
* | ||
* @see javax.tools.Diagnostic | ||
*/ | ||
public record CompileInfo(Diagnostic<?> diagnostic) { | ||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (!(o instanceof CompileInfo that)) | ||
return false; | ||
return diagnostic.equals(that.diagnostic); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(diagnostic); | ||
} | ||
illuminator3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public String toString() { | ||
return "CompileInfo{" + "diagnostic=" + diagnostic + '}'; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
inmemorycompiler/src/main/java/org/togetherjava/tjbot/imc/InMemoryByteJavaFileObject.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.togetherjava.tjbot.imc; | ||
|
||
import javax.tools.SimpleJavaFileObject; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.OutputStream; | ||
import java.net.URI; | ||
|
||
/** | ||
* Represents a {@link SimpleJavaFileObject} that exists purely in memory. | ||
* | ||
* @see javax.tools.SimpleJavaFileObject | ||
*/ | ||
class InMemoryByteJavaFileObject extends SimpleJavaFileObject { | ||
private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
|
||
public InMemoryByteJavaFileObject(String className) { | ||
super(URI.create(className), Kind.CLASS); | ||
} | ||
|
||
@Override | ||
public OutputStream openOutputStream() { | ||
return baos; | ||
} | ||
|
||
public byte[] getBytes() { | ||
return baos.toByteArray(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.