Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Nov 14, 2024
2 parents 7fb7f2c + 78141cd commit d9b4e62
Show file tree
Hide file tree
Showing 10 changed files with 638 additions and 87 deletions.
22 changes: 5 additions & 17 deletions src/main/java/org/eolang/jeo/Assemble.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@
*/
package org.eolang.jeo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.eolang.jeo.representation.BytecodeRepresentation;
import org.eolang.jeo.representation.PrefixedName;
import org.eolang.jeo.representation.xmir.AllLabels;

/**
Expand All @@ -53,18 +49,10 @@ public Assemble(final Path classes) {
@Override
public Representation apply(final Representation representation) {
new AllLabels().clearCache();
final Details details = representation.details();
final String name = new PrefixedName(details.name()).decode();
try {
final byte[] bytecode = representation.toBytecode().bytes();
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);
return new BytecodeRepresentation(path);
} catch (final IOException exception) {
throw new IllegalStateException(String.format("Can't recompile '%s'", name), exception);
}
final Transformation trans = new Caching(
new Assembling(this.classes, representation)
);
trans.transform();
return new BytecodeRepresentation(trans.target());
}
}
80 changes: 80 additions & 0 deletions src/main/java/org/eolang/jeo/Assembling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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.nio.file.Path;
import java.nio.file.Paths;
import org.eolang.jeo.representation.PrefixedName;

/**
* Assembling transformation.
* @since 0.6
*/
public final class Assembling implements Transformation {

/**
* Target folder where to save the assembled class.
*/
private final Path folder;

/**
* Representation to assemble.
*/
private final Representation repr;

/**
* Constructor.
* @param target Target folder.
* @param representation Representation to assemble.
*/
Assembling(final Path target, final Representation representation) {
this.folder = target;
this.repr = representation;
}

@Override
public Path source() {
return this.repr.details().source().orElseThrow(
() -> new IllegalStateException(
String.format(
"Source is not defined for assembling '%s'",
this.repr.details()
)
)
);
}

@Override
public Path target() {
final String name = new PrefixedName(this.repr.details().name()).decode();
final String[] subpath = name.split("\\.");
subpath[subpath.length - 1] = String.format("%s.class", subpath[subpath.length - 1]);
return Paths.get(this.folder.toString(), subpath);
}

@Override
public byte[] transform() {
return this.repr.toBytecode().bytes();
}
}
113 changes: 113 additions & 0 deletions src/main/java/org/eolang/jeo/Caching.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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 com.jcabi.log.Logger;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Cached transformation.
* @since 0.6
*/
public final class Caching implements Transformation {

/**
* Original transformation.
*/
private final Transformation origin;

/**
* Constructor.
* @param origin Original transformation.
*/
Caching(final Transformation origin) {
this.origin = origin;
}

@Override
public Path source() {
return this.origin.source();
}

@Override
public Path target() {
return this.origin.target();
}

@Override
public byte[] transform() {
try {
return this.tryTransform();
} catch (final IOException exception) {
throw new IllegalStateException(
String.format(
"Failed to transform of '%s' to '%s'",
this.source(),
this.target()
),
exception
);
}
}

/**
* Try to transform the file.
* @return The transformed file content.
* @throws IOException If something goes wrong.
*/
private byte[] tryTransform() throws IOException {
final byte[] result;
final Path target = this.target();
if (this.alreadyTransformed()) {
Logger.info(
this,
"The file '%s' is already transformed to '%s'. Skipping.",
this.source(),
target
);
result = Files.readAllBytes(target);
} else {
final byte[] transform = this.origin.transform();
Files.createDirectories(target.getParent());
Files.write(target, transform);
result = transform;
}
return result;
}

/**
* Check if the file has already been transformed.
* @return True if the file has already been transformed.
* @throws IOException If something goes wrong.
*/
private boolean alreadyTransformed() throws IOException {
final Path source = this.source();
final Path target = this.target();
return Files.exists(target)
&& Files.exists(source)
&& Files.getLastModifiedTime(target).compareTo(Files.getLastModifiedTime(source)) >= 0;
}
}
23 changes: 5 additions & 18 deletions src/main/java/org/eolang/jeo/Disassemble.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@
*/
package org.eolang.jeo;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eolang.jeo.representation.PrefixedName;
import org.eolang.jeo.representation.XmirRepresentation;
import org.eolang.jeo.representation.xmir.AllLabels;

Expand Down Expand Up @@ -61,18 +56,10 @@ public Representation apply(final Representation representation) {
// This is dangerous and should be removed as soon as possible.
// Moreover, we have the same solution in {@link Assemble} class.
new AllLabels().clearCache();
final String name = new PrefixedName(representation.details().name()).decode();
final Path path = this.target
.resolve(String.format("%s.xmir", name.replace('/', File.separatorChar)));
try {
Files.createDirectories(path.getParent());
Files.write(path, representation.toEO().toString().getBytes(StandardCharsets.UTF_8));
return new XmirRepresentation(path);
} catch (final IOException exception) {
throw new IllegalStateException(
String.format("Can't save XML to %s", path),
exception
);
}
final Transformation trans = new Caching(
new Disassembling(this.target, representation)
);
trans.transform();
return new XmirRepresentation(trans.target());
}
}
85 changes: 85 additions & 0 deletions src/main/java/org/eolang/jeo/Disassembling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import org.eolang.jeo.representation.PrefixedName;

/**
* Disassembling transformation.
* @since 0.6
*/
public final class Disassembling implements Transformation {

/**
* Target folder where to save the disassembled class.
*/
private final Path folder;

/**
* Representation to disassemble.
*/
private final Representation repr;

/**
* Constructor.
* @param target Target folder.
* @param representation Representation to disassemble.
*/
Disassembling(final Path target, final Representation representation) {
this.folder = target;
this.repr = representation;
}

@Override
public Path source() {
return this.repr.details().source().orElseThrow(
() -> new IllegalStateException(
String.format(
"Source is not defined for disassembling '%s'",
this.repr.details()
)
)
);
}

@Override
public Path target() {
return this.folder.resolve(
String.format(
"%s.xmir",
new PrefixedName(
this.repr.details().name()
).decode().replace('/', File.separatorChar)
)
);
}

@Override
public byte[] transform() {
return this.repr.toEO().toString().getBytes(StandardCharsets.UTF_8);
}
}
Loading

1 comment on commit d9b4e62

@0pdd
Copy link

@0pdd 0pdd commented on d9b4e62 Nov 14, 2024

Choose a reason for hiding this comment

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

Puzzle 784-069c94bc discovered in src/main/java/org/eolang/jeo/Transformation.java) and submitted as #869. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.