Skip to content

Commit

Permalink
feat(objectionary#37): Save resulting bytecode into filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Sep 7, 2023
1 parent 82af672 commit 6219418
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/main/java/org/eolang/jeo/Optimization.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -68,12 +70,26 @@ final class Optimization {
* @throws IOException If some I/O problem arises.
*/
void apply() throws IOException {
final Collection<IR> apply = this.boosts.apply(
this.boosts.apply(
this.bytecode()
.stream()
.map(BytecodeIr::new)
.collect(Collectors.toList())
);
).stream().forEach(this::recompile);
}

private void recompile(final IR ir) {
final String name = ir.name();
try {
final byte[] bytecode = ir.toBytecode();
final String[] subpath = name.split("\\.");
subpath[subpath.length - 1] = String.format("%s.class", subpath[subpath.length - 1]);
final Path path = Paths.get(this.classes.toString(), subpath);
Files.createDirectories(path.getParent());
Files.write(path, bytecode, StandardOpenOption.CREATE_NEW);
} catch (final IOException exception) {
throw new IllegalStateException(String.format("Can't recompile '%s'", name), exception);
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/eolang/jeo/OptimizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.cactoos.io.ResourceOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.io.FileMatchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand Down Expand Up @@ -65,5 +66,14 @@ void appliesAllBoosts(@TempDir final Path classes) throws IOException {
boost.isApplied(),
Matchers.equalTo(true)
);
MatcherAssert.assertThat(
"Optimization should save final bytecode into appropriate directory",
classes.resolve("org")
.resolve("eolang")
.resolve("jeo")
.resolve("MethodByte.class")
.toFile(),
FileMatchers.anExistingFile()
);
}
}

0 comments on commit 6219418

Please sign in to comment.