Skip to content

Commit

Permalink
feat(objectionary#2080): Cleanup the code
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed May 22, 2023
1 parent 2e72d3e commit ac9ba24
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
66 changes: 47 additions & 19 deletions eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -101,6 +102,11 @@ public final class TranspileMojo extends SafeMojo {
)
);

/**
* Java extension.
*/
private static final Pattern JAVA_EXT = Pattern.compile(".java", Pattern.LITERAL);

/**
* The directory where to put pre-transpile files.
*/
Expand Down Expand Up @@ -178,7 +184,7 @@ public void exec() throws IOException {
* Transpile.
* @param tojo Tojo that should be transpiled.
* @return Number of transpiled files.
* @throws IOException If any issues with I/O
* @throws java.io.IOException If any issues with I/O
*/
private int transpile(final ForeignTojo tojo) throws IOException {
final int saved;
Expand Down Expand Up @@ -215,7 +221,7 @@ private int transpile(final ForeignTojo tojo) throws IOException {
* @param input The .xmir file
* @param target The path to transpiled .xmir file
* @return List of Paths to generated java file
* @throws IOException If any issues with I/O
* @throws java.io.IOException If any issues with I/O
*/
private List<Path> transpile(
final Path src,
Expand Down Expand Up @@ -249,31 +255,53 @@ removed, new Rel(src)
return javas;
}

private void cleanUpClasses(final List<Path> save) {
System.out.println(save);

final Set<Path> collect = save.stream().map(
p -> this.generatedDir.toPath().relativize(p))
.map(p -> getResolve(p))
/**
* Clean up dirty classes.
* The method is trying to fix problem produced by dirty libraries:
* <a href="https://github.com/objectionary/eo-strings/issues/147"> eo-strings example </a>
* Some libraries by mistake can put ALL their compiled classes right into the final library
* jar, instead of only adding atoms. This can cause different runtime errors since the
* classpath will contain two classes with the same name:
* - The first class file will be added from dirty library
* - The second class with the same name will be compiled from the transpiled java file
* In order to prevent this, we remove all classes that have the java analog in the
* generated sources. In other words, if generated-sources (or generated-test-sources) folder
* has java classes, we expect that they will be only compiled from that folder.
* @param java The list of java files.
*/
private void cleanUpClasses(final Collection<? extends Path> java) {
final Set<Path> unexpected = java.stream()
.map(path -> this.generatedDir.toPath().relativize(path))
.map(TranspileMojo::classExtension)
.map(binary -> this.outputDir.toPath().resolve(binary))
.collect(Collectors.toSet());

for (final Path path : collect) {
for (final Path binary : unexpected) {
try {
Files.deleteIfExists(outputDir.toPath().resolve(path));
} catch (final IOException e) {
throw new IllegalStateException(e);
Files.deleteIfExists(binary);
} catch (final IOException cause) {
throw new IllegalStateException(
String.format("Can't delete file %s", binary),
cause
);
}
}
System.out.println(collect);
}

private static Path getResolve(final Path p) {
final String filename = p.getFileName().toString().replace(".java", ".class");
if (p.getParent() != null) {
return p.getParent().resolve(filename);
/**
* Rename java to class.
* @param java The java file
* @return The class file with the same (java) content.
*/
private static Path classExtension(final Path java) {
final Path result;
final String filename = TranspileMojo.JAVA_EXT.matcher(java.getFileName().toString())
.replaceAll(".class");
if (java.getParent() == null) {
result = Paths.get(filename);
} else {
return Paths.get(filename);
result = java.getParent().resolve(filename);
}
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -49,7 +48,7 @@
* @since 0.1
* @checkstyle LocalFinalVariableNameCheck (100 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
final class UnplaceMojoTest {

/**
Expand Down

0 comments on commit ac9ba24

Please sign in to comment.