Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#779): Add j$ Prefix For Packages #781

Merged
merged 7 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/main/java/org/eolang/jeo/representation/PrefixedName.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.eolang.jeo.representation;

import java.util.Arrays;
import java.util.stream.Collectors;
import lombok.ToString;

/**
Expand Down Expand Up @@ -76,9 +78,9 @@ public String encode() {
if (this.origin.replace(" ", "").isEmpty()) {
throw new IllegalArgumentException(PrefixedName.BLANK);
} else if (this.origin.contains(PrefixedName.DELIMITER)) {
final String[] split = this.origin.split(PrefixedName.DELIMITER);
split[split.length - 1] = new PrefixedName(split[split.length - 1]).encode();
res = String.join(PrefixedName.DELIMITER, split);
res = Arrays.stream(this.origin.split(PrefixedName.DELIMITER))
.map(each -> String.format("%s%s", PrefixedName.PREFIX, each))
.collect(Collectors.joining(PrefixedName.DELIMITER));
} else {
res = String.format("%s%s", PrefixedName.PREFIX, this.origin);
}
Expand All @@ -90,18 +92,27 @@ public String encode() {
* @return Decoded name.
*/
public String decode() {
final String res;
if (this.origin.replace(" ", "").isEmpty()) {
throw new IllegalArgumentException(PrefixedName.BLANK);
} else if (this.origin.startsWith(PrefixedName.PREFIX)) {
res = this.origin.substring(PrefixedName.PREFIX.length());
} else if (this.origin.contains(PrefixedName.DELIMITER)) {
final String[] split = this.origin.split(PrefixedName.DELIMITER);
split[split.length - 1] = new PrefixedName(split[split.length - 1]).decode();
res = String.join(PrefixedName.DELIMITER, split);
}
return Arrays.stream(this.origin.split(PrefixedName.DELIMITER))
.map(PrefixedName::cut)
.collect(Collectors.joining(PrefixedName.DELIMITER));
}

/**
* Cut prefix from prefixed string.
* If the passed string doesn't contain prefix, nothing is removed.
* @param prefixed Prefixed string.
* @return String without prefix.
*/
private static String cut(final String prefixed) {
final String result;
if (prefixed.startsWith(PrefixedName.PREFIX)) {
result = prefixed.substring(PrefixedName.PREFIX.length());
} else {
res = this.origin;
result = prefixed;
}
return res;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eolang.jeo.representation.ClassName;
import org.eolang.jeo.representation.PrefixedName;
import org.xembly.Directive;
import org.xembly.Directives;

Expand Down Expand Up @@ -132,12 +133,29 @@ public Iterator<Directive> iterator() {
.add("metas")
.add("meta")
.add("head").set("package").up()
.add("tail").set(this.name.pckg()).up()
.add("part").set(this.name.pckg()).up()
.add("tail").set(this.pckg()).up()
.add("part").set(this.pckg()).up()
.up()
.append(opdirs)
.append(labeldirs)
.up()
.iterator();
}

/**
* Prefixed package name.
* We intentionally add prefix to the packages, because sometimes they can be really
* strange, <a href="https://github.com/objectionary/jeo-maven-plugin/issues/779">see</a>
* @return Prefixed package name.
*/
private String pckg() {
final String result;
final String original = this.name.pckg();
if (original.isEmpty()) {
result = "";
} else {
result = new PrefixedName(original).encode();
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import org.eolang.jeo.representation.ClassName;
import org.eolang.jeo.representation.PrefixedName;
import org.eolang.jeo.representation.bytecode.BytecodeProgram;
import org.eolang.jeo.representation.directives.DirectivesClass;
import org.eolang.jeo.representation.directives.DirectivesMetas;
Expand Down Expand Up @@ -140,6 +141,8 @@ private String pckg() {
.xpath("/program/metas/meta[head='package']/tail/text()")
.stream()
.findFirst()
.map(PrefixedName::new)
.map(PrefixedName::decode)
.orElse("");
}
}
3 changes: 2 additions & 1 deletion src/test/java/org/eolang/jeo/BachedTranslatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void savesXml(@TempDir final Path temp) throws IOException {
.getBytes(StandardCharsets.UTF_8)
);
new BachedTranslator(new Disassemble(temp))
.apply(Stream.of(new XmirRepresentation(clazz))).collect(Collectors.toList());
.apply(Stream.of(new XmirRepresentation(clazz)))
.collect(Collectors.toList());
MatcherAssert.assertThat(
"XML file was not saved",
temp.resolve(this.expected).toFile(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class PrefixedNameTest {
"ClassName, j$ClassName",
"someLongName, j$someLongName",
"j$j, j$j$j",
"jeo/xmir/Fake, jeo/xmir/j$Fake"
"jeo/xmir/Fake, j$jeo/j$xmir/j$Fake"
})
void encodesName(final String origin, final String encoded) {
MatcherAssert.assertThat(
Expand All @@ -59,7 +59,7 @@ void encodesName(final String origin, final String encoded) {
"j$someLongName, someLongName",
"j$j$j, j$j",
"someName, someName",
"jeo/xmir/j$Fake, jeo/xmir/Fake"
"j$jeo/j$xmir/j$Fake, jeo/xmir/Fake"
})
void decodesName(final String encoded, final String origin) {
MatcherAssert.assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class XmirRepresentationTest {
@Test
void retrievesName() {
final String name = "Math";
final String expected = "org/eolang/foo/j$Math";
final String expected = "j$org/eolang/foo/j$Math";
final String actual = new XmirRepresentation(
new BytecodeProgram(
"org/eolang/foo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ void createsMetasWithPackage() {
).xmlQuietly(),
Matchers.allOf(
XhtmlMatchers.hasXPath("/metas/meta/head[text()='package']"),
XhtmlMatchers.hasXPath("/metas/meta/tail[text()='path.to']"),
XhtmlMatchers.hasXPath("/metas/meta/part[text()='path.to']")
XhtmlMatchers.hasXPath("/metas/meta/tail[text()='j$path.to']"),
XhtmlMatchers.hasXPath("/metas/meta/part[text()='j$path.to']")
)
);
}
Expand Down Expand Up @@ -94,8 +94,8 @@ void addsNothingExceptPackage() {
).xmlQuietly(),
Matchers.allOf(
XhtmlMatchers.hasXPath("/metas/meta/head[text()='package']"),
XhtmlMatchers.hasXPath("/metas/meta/tail[text()='path.to']"),
XhtmlMatchers.hasXPath("/metas/meta/part[text()='path.to']"),
XhtmlMatchers.hasXPath("/metas/meta/tail[text()='j$path.to']"),
XhtmlMatchers.hasXPath("/metas/meta/part[text()='j$path.to']"),
Matchers.not(
XhtmlMatchers.hasXPath("/metas/meta/tail[text()='org.eolang.jeo.label']")
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,18 @@ public HasClass(final String name) {
*/
public HasClass inside(final String pckg) {
this.additional.add("/program/metas/meta/head[text()='package']/text()");
this.additional.add(String.format("/program/metas/meta/tail[text()='%s']/text()", pckg));
this.additional.add(String.format("/program/metas/meta/part[text()='%s']/text()", pckg));
this.additional.add(
String.format(
"/program/metas/meta/tail[text()='%s']/text()",
new PrefixedName(pckg).encode()
)
);
this.additional.add(
String.format(
"/program/metas/meta/part[text()='%s']/text()",
new PrefixedName(pckg).encode()
)
);
return this;
}

Expand Down
Loading