From c1040f60de27aee6a0d13ebb5313e13397a073c4 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Thu, 7 Sep 2023 17:51:47 +0300 Subject: [PATCH] feat(#37): remove the puzzle for the #37 issue --- src/it/successful-path/verify.groovy | 9 +-- .../java/org/eolang/jeo/Base64Bytecode.java | 57 +++++++++++++++++-- .../java/org/eolang/jeo/Optimization.java | 18 ++++-- .../java/org/eolang/jeo/OptimizationTest.java | 2 - 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/it/successful-path/verify.groovy b/src/it/successful-path/verify.groovy index 72547011e..a3ba2d670 100644 --- a/src/it/successful-path/verify.groovy +++ b/src/it/successful-path/verify.groovy @@ -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 \ No newline at end of file diff --git a/src/main/java/org/eolang/jeo/Base64Bytecode.java b/src/main/java/org/eolang/jeo/Base64Bytecode.java index 80dc38cb1..451d006a1 100644 --- a/src/main/java/org/eolang/jeo/Base64Bytecode.java +++ b/src/main/java/org/eolang/jeo/Base64Bytecode.java @@ -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); } } diff --git a/src/main/java/org/eolang/jeo/Optimization.java b/src/main/java/org/eolang/jeo/Optimization.java index f39d8807f..f58477e1e 100644 --- a/src/main/java/org/eolang/jeo/Optimization.java +++ b/src/main/java/org/eolang/jeo/Optimization.java @@ -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; @@ -79,10 +78,14 @@ 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); @@ -90,11 +93,16 @@ private void recompile(final IR ir) { 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); } diff --git a/src/test/java/org/eolang/jeo/OptimizationTest.java b/src/test/java/org/eolang/jeo/OptimizationTest.java index bbd4bc2b4..035c4cf69 100644 --- a/src/test/java/org/eolang/jeo/OptimizationTest.java +++ b/src/test/java/org/eolang/jeo/OptimizationTest.java @@ -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; @@ -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;