Skip to content

Commit

Permalink
feat(objectionary#37): remove the puzzle for the objectionary#37 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Sep 7, 2023
1 parent b76d4e2 commit c1040f6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
9 changes: 1 addition & 8 deletions src/it/successful-path/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,5 @@ assert log.contains("Hello, World!")
//Check that we have generated XMIR object file.
assert new File(basedir, 'target/jeo/xmir/org/eolang/jeo/Application.xmir').exists()
//Check that class file was changed
/**
* @todo #34:90min Implement class recompilation.
* Class recompilation is not implemented yet, so we can't check it.
* Invert the followed assertion when class recompilation will be implemented.
* Under recompilation we mean that class file should be compiled from
* XMIR object file.
*/
assert !log.contains("Application.class was recompiled successfully.")
assert log.contains("Application.class was recompiled successfully.")
true
57 changes: 53 additions & 4 deletions src/main/java/org/eolang/jeo/Base64Bytecode.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,72 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.jeo;

import java.util.Arrays;
import java.util.Base64;
import org.cactoos.Input;
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.UncheckedBytes;

/**
* Base64 bytecode.
* Converts bytecode to Base64 string.
*
* @since 0.1.0
* @todo #37:90min Add unit test for Base64Bytecode class.
* The test should check all the methods of the {@link Base64Bytecode} class.
* Don't forget to test corner cases.
* When the test is ready, remove this puzzle.
*/
public class Base64Bytecode {

/**
* Bytecode.
*/
private final byte[] bytes;

public Base64Bytecode(final Input input) {
/**
* Constructor.
* @param input Input.
*/
Base64Bytecode(final Input input) {
this(new UncheckedBytes(new BytesOf(input)).asBytes());
}

public Base64Bytecode(final byte[] bytes) {
this.bytes = bytes;
/**
* Constructor.
* @param bytes Bytecode.
*/
private Base64Bytecode(final byte[] bytes) {
this.bytes = Arrays.copyOf(bytes, bytes.length);
}

public String asString() {
/**
* Convert to string.
* @return String.
*/
String asString() {
return Base64.getEncoder().encodeToString(this.bytes);
}
}
18 changes: 13 additions & 5 deletions src/main/java/org/eolang/jeo/Optimization.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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 @@ -79,22 +78,31 @@ void apply() throws IOException {
).forEach(this::recompile);
}

private void recompile(final IR ir) {
final String name = ir.name();
/**
* Recompile the Intermediate Representation.
* @param representation Intermediate Representation to recompile.
*/
private void recompile(final IR representation) {
final String name = representation.name();
try {
final byte[] bytecode = ir.toBytecode();
final byte[] bytecode = representation.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);
Logger.info(
this,
"Recompiling '%s', bytecode instance '%s', bytes to save '%s'",
path,
ir.getClass(),
representation.getClass(),
bytecode.length
);
Files.createDirectories(path.getParent());
Files.write(path, bytecode);
Logger.info(
this,
"%s was recompiled successfully.",
path.getFileName().toString()
);
} catch (final IOException exception) {
throw new IllegalStateException(String.format("Can't recompile '%s'", name), exception);
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/eolang/jeo/OptimizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.eolang.jeo;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -35,7 +34,6 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.io.FileMatchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand Down

0 comments on commit c1040f6

Please sign in to comment.