Skip to content

Commit

Permalink
feat(#83): remove the puzzle for #83 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Sep 21, 2023
1 parent b72a285 commit 02c6a1b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
93 changes: 72 additions & 21 deletions src/test/java/it/JavaSourceClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
@SuppressWarnings("JTCOP.RuleCorrectTestName")
final class JavaSourceClass {

/**
* Name of Java class.
*/
private final String name;

/**
Expand All @@ -66,6 +69,7 @@ final class JavaSourceClass {

/**
* Constructor.
* @param filename Name of Java class.
* @param java Source of Java class.
*/
private JavaSourceClass(final String filename, final Input java) {
Expand All @@ -76,17 +80,11 @@ private JavaSourceClass(final String filename, final Input java) {
/**
* Compile the Java class.
* @return Bytecode of compiled class.
* @todo #81:30min Compile Java class dynamically.
* Currently, we don't compile Java classes dynamically.
* Instead, we pass the source code directly.
* This is a temporary solution. We need to compile the Java class
* dynamically and pass the bytecode to the test.
* When this is done, remove that puzzle from this method.
*/
byte[] compile() {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
BytecodeManager manager = new BytecodeManager(compiler);
boolean successful = compiler.getTask(
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final BytecodeManager manager = new BytecodeManager(compiler);
final boolean successful = compiler.getTask(
null,
manager,
null,
Expand All @@ -97,71 +95,120 @@ byte[] compile() {
if (successful) {
return manager.bytecode();
} else {
throw new IllegalStateException("Compilation failed.");
throw new IllegalStateException(
String.format("Compilation failed for class %s", this.name)
);
}
}

/**
* Name of Java class.
* @param resource Resource of Java class (full path in resources).
* @return Name of Java class.
*/
private static String filename(final String resource) {
return resource.substring(resource.lastIndexOf('/') + 1);
}

/**
* Compilation manager that stores the bytecode during compilation.
* @since 0.1.0
*/
private static final class BytecodeManager
extends ForwardingJavaFileManager<StandardJavaFileManager> {
private AtomicReference<Bytecode> output;

private BytecodeManager(JavaCompiler compiler) {
/**
* Bytecode.
*/
private final AtomicReference<Bytecode> output;

/**
* Constructor.
* @param compiler Java compiler.
*/
private BytecodeManager(final JavaCompiler compiler) {
this(
compiler.getStandardFileManager(
null,
Locale.getDefault(),
Charset.defaultCharset()
));
)
);
}

private BytecodeManager(StandardJavaFileManager manager) {
/**
* Constructor.
* @param manager Standard Java file manager.
*/
private BytecodeManager(final StandardJavaFileManager manager) {
super(manager);
this.output = new AtomicReference<>();
}

@Override
public JavaFileObject getJavaFileForOutput(
Location location,
String className,
JavaFileObject.Kind kind,
FileObject sibling
final Location location,
final String classname,
final JavaFileObject.Kind kind,
final FileObject sibling
) {
this.output.set(new Bytecode(className));
this.output.set(new Bytecode(classname));
return this.output.get();
}

/**
* Get the bytecode.
* @return Bytecode.
*/
byte[] bytecode() {
return this.output.get().bytecode();
}
}

/**
* Java source code.
* @since 0.1.0
*/
private static final class SourceCode extends SimpleJavaFileObject {

/**
* Source code.
*/
private final Input src;

/**
* Construct a SimpleJavaFileObject of the given kind and with the
* given URI.
* @param name Name of Java class.
* @param input Source of Java class.
*/
SourceCode(final String name, final Input input) {
super(URI.create(name), Kind.SOURCE);
this.src = input;
}

@Override
public CharSequence getCharContent(final boolean ignoreEncodingErrors) {
public CharSequence getCharContent(final boolean ignore) {
return new UncheckedText(new TextOf(this.src)).asString();
}
}

/**
* Bytecode of compiled class.
* @since 0.1.0
*/
private static final class Bytecode extends SimpleJavaFileObject {

/**
* Output stream.
*/
private final ByteArrayOutputStream output;

Bytecode(String name) {
/**
* Constructor.
* @param name Name of Java class.
*/
Bytecode(final String name) {
super(URI.create(String.format("%s%s", name, Kind.CLASS.extension)), Kind.CLASS);
this.output = new ByteArrayOutputStream();
}
Expand All @@ -171,6 +218,10 @@ public OutputStream openOutputStream() {
return this.output;
}

/**
* Get the bytecode.
* @return Bytecode.
*/
byte[] bytecode() {
return this.output.toByteArray();
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/it/JavaToEoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.cactoos.bytes.UncheckedBytes;
import org.cactoos.io.ResourceOf;
import org.eolang.jeo.representation.BytecodeRepresentation;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand Down

1 comment on commit 02c6a1b

@0pdd
Copy link

@0pdd 0pdd commented on 02c6a1b Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 81-8b72dde1 disappeared from src/test/java/it/JavaSourceClass.java), that's why I closed #83. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.