-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
638 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
d9b4e62
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
784-069c94bc
discovered insrc/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.