Skip to content

Commit

Permalink
feat(objectionary#636): hide fields of XmlTryCatchEntry class
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Sep 6, 2024
1 parent fef0022 commit 4b03de3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.eolang.jeo.representation.bytecode;

import com.jcabi.log.Logger;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
Expand All @@ -33,6 +34,7 @@
* @since 0.1
*/
@ToString
@EqualsAndHashCode
public final class BytecodeTryCatchBlock implements BytecodeEntry {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,58 @@ public XmlTryCatchEntry(final XmlNode xmlnode) {
* @param node XML node
* @param labels Labels
*/
public XmlTryCatchEntry(final XmlNode node, final AllLabels labels) {
XmlTryCatchEntry(final XmlNode node, final AllLabels labels) {
this.xmlnode = node;
this.labels = labels;
}

@Override
public void writeTo(final BytecodeMethod method) {
method.trycatch(
new BytecodeTryCatchBlock(
this.start(),
this.end(),
this.handler(),
this.type()
)
method.trycatch(this.bytecode());
}

/**
* Converts XML to bytecode.
* @return Bytecode try-catch block.
*/
public BytecodeTryCatchBlock bytecode() {
return new BytecodeTryCatchBlock(
this.start(),
this.end(),
this.handler(),
this.type()
);
}

public Label start() {
/**
* Retrieves the start label.
* @return Start label.
*/
private Label start() {
return this.label(0).map(this.labels::label).orElse(null);
}

public Label end() {
/**
* Retrieves the end label.
* @return End label.
*/
private Label end() {
return this.label(1).map(this.labels::label).orElse(null);
}

public Label handler() {
/**
* Retrieves the handler label.
* @return Handler label.
*/
private Label handler() {
return this.label(2).map(this.labels::label).orElse(null);
}

/**
* Retrieves the exception type.
* @return Exception type.
*/
public String type() {
private String type() {
return Optional.ofNullable(this.xmlnode.children().collect(Collectors.toList()).get(3))
.map(XmlNode::text)
.map(HexString::new)
Expand All @@ -105,7 +123,7 @@ public String type() {
* @param id Label uid.
* @return Label.
*/
Optional<String> label(final int id) {
private Optional<String> label(final int id) {
return Optional.ofNullable(this.xmlnode.children().collect(Collectors.toList()).get(id))
.filter(node -> !node.hasAttribute("base", "nop"))
.map(XmlNode::text)
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlTuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ public final class XmlTuple {
* Constructor.
* @param lines XML Lines.
*/
public XmlTuple(final String... lines) {
XmlTuple(final String... lines) {
this(String.join("\n", lines));
}

/**
* Constructor.
* @param xml XML.
* @param node Xmir node.
*/
public XmlTuple(final String xml) {
this(new XmlNode(xml));
XmlTuple(final XmlNode node) {
this.node = node;
}

/**
* Constructor.
* @param node Xmir node.
* @param xml XML.
*/
public XmlTuple(final XmlNode node) {
this.node = node;
private XmlTuple(final String xml) {
this(new XmlNode(xml));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.eolang.jeo.representation.directives;

import java.util.stream.Stream;
import org.eolang.jeo.representation.bytecode.BytecodeTryCatchBlock;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.jeo.representation.xmir.XmlTryCatchEntry;
import org.hamcrest.MatcherAssert;
Expand All @@ -48,32 +49,21 @@ void convertsToXmir() throws ImpossibleModificationException {
final Label end = new Label();
final Label handler = new Label();
final String type = "java/lang/Exception";
final XmlTryCatchEntry entry = new XmlTryCatchEntry(
new XmlNode(
new Xembler(
new DirectivesTryCatch(start, end, handler, type)
).xml()
)
);
MatcherAssert.assertThat(
"We failed to convert try-catch directives to XML. Start label is incorrect.",
entry.start(),
Matchers.equalTo(start)
);
MatcherAssert.assertThat(
"We failed to convert try-catch directives to XML. End label is incorrect.",
entry.end(),
Matchers.equalTo(end)
);
MatcherAssert.assertThat(
"We failed to convert try-catch directives to XML. Handler label is incorrect.",
entry.handler(),
Matchers.equalTo(handler)
);
MatcherAssert.assertThat(
"We failed to convert try-catch directives to XML. Exception type is incorrect.",
entry.type(),
Matchers.equalTo(type)
"We expected to convert try-catch directives to correct bytecode.",
new XmlTryCatchEntry(
new XmlNode(
new Xembler(
new DirectivesTryCatch(start, end, handler, type)
).xml()
)
).bytecode(),
Matchers.equalTo(new BytecodeTryCatchBlock(
start,
end,
handler,
type
))
);
}

Expand All @@ -94,38 +84,29 @@ void convertsToXmirIfSomeLabelsAreAbsent(
final Label handler,
final String type
) throws ImpossibleModificationException {
final XmlTryCatchEntry entry = new XmlTryCatchEntry(
new XmlNode(
new Xembler(
new DirectivesTryCatch(start, end, handler, type)
).xml()
)
);
MatcherAssert.assertThat(
"We failed to convert try-catch directives to XML. Start label is incorrect.",
entry.start(),
Matchers.equalTo(start)
);
MatcherAssert.assertThat(
"We failed to convert try-catch directives to XML. End label is incorrect.",
entry.end(),
Matchers.equalTo(end)
);
MatcherAssert.assertThat(
"We failed to convert try-catch directives to XML. Handler label is incorrect.",
entry.handler(),
Matchers.equalTo(handler)
);
MatcherAssert.assertThat(
"We failed to convert try-catch directives to XML. Exception type is incorrect.",
entry.type(),
Matchers.equalTo(type)
"We failed to convert try-catch directives to correct XML.",
new XmlTryCatchEntry(
new XmlNode(
new Xembler(
new DirectivesTryCatch(start, end, handler, type)
).xml()
)
).bytecode(),
Matchers.equalTo(
new BytecodeTryCatchBlock(
start,
end,
handler,
type
)
)
);
}

/**
* Test cases.
* For {@link #correctlyConvertsToXmirIfSomeLabelsAreAbsent(Label, Label, Label, String)}.
* For {@link #convertsToXmirIfSomeLabelsAreAbsent(Label, Label, Label, String)}.
* @return Test cases.
*/
@SuppressWarnings("PMD.UnusedPrivateMethod")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.eolang.jeo.representation.xmir;

import org.eolang.jeo.representation.bytecode.BytecodeTryCatchBlock;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -34,9 +35,10 @@
final class XmlTryCatchEntryTest {

@Test
void findsType() {
void transformsToBytecode() {
AllLabels labels = new AllLabels();
MatcherAssert.assertThat(
"Can't find type",
"Can't convert XML try-catch entry to the correct bytecode",
new XmlTryCatchEntry(
new XmlNode(
String.join(
Expand All @@ -48,72 +50,29 @@ void findsType() {
" <o base='string' data='bytes' name='type'>6A 61 76 61 2F 69 6F 2F 49 4F 45 78 63 65 70 74 69 6F 6E</o>\n",
"</o>"
)
),
labels
).bytecode(),
Matchers.equalTo(
new BytecodeTryCatchBlock(
labels.label(
new HexString(
"30 65 65 66 66 62 37 37 2D 34 64 32 62 2D 34 63 31 38 2D 39 32 32 39 2D 36 32 65 39 66 61 66 39 34 61 34 34"
).decode()
),
labels.label(
new HexString(
"62 31 65 65 38 61 34 32 2D 37 63 39 63 2D 34 63 66 39 2D 61 63 63 65 2D 39 35 62 39 38 36 38 34 34 65 36 35"
).decode()
),
labels.label(
new HexString(
"62 31 65 65 38 61 34 32 2D 37 63 39 63 2D 34 63 66 39 2D 61 63 63 65 2D 39 35 62 39 38 36 38 34 34 65 36 35"
).decode()
),
"java/io/IOException"
)
).type(),
Matchers.equalTo("java/io/IOException")
);
}

@Test
void findsStartLabel() {
MatcherAssert.assertThat(
"Can't find 'start' label",
new XmlTryCatchEntry(
new XmlNode(
String.join(
"\n",
"<o base='trycatch'>\n",
" <o base='label' name='start' data='bytes'>30 65 65 66 66 62 37 37 2D 34 64 32 62 2D 34 63 31 38 2D 39 32 32 39 2D 36 32 65 39 66 61 66 39 34 61 34 34</o>\n",
" <o base='label' name='end' data='bytes'>62 31 65 65 38 61 34 32 2D 37 63 39 63 2D 34 63 66 39 2D 61 63 63 65 2D 39 35 62 39 38 36 38 34 34 65 36 35</o>\n",
" <o base='label' name='handler' data='bytes'>62 31 65 65 38 61 34 32 2D 37 63 39 63 2D 34 63 66 39 2D 61 63 63 65 2D 39 35 62 39 38 36 38 34 34 65 36 35</o>\n",
" <o base='string' data='bytes' name='type'>6A 61 76 61 2F 69 6F 2F 49 4F 45 78 63 65 70 74 69 6F 6E</o>\n",
"</o>"
)
)
).start(),
Matchers.notNullValue()
);
}

@Test
void findsEndLabel() {
MatcherAssert.assertThat(
"Can't find 'end' label",
new XmlTryCatchEntry(
new XmlNode(
String.join(
"\n",
"<o base='trycatch'>\n",
" <o base='label' name='start' data='bytes'>30 65 65 66 66 62 37 37 2D 34 64 32 62 2D 34 63 31 38 2D 39 32 32 39 2D 36 32 65 39 66 61 66 39 34 61 34 34</o>\n",
" <o base='label' name='end' data='bytes'>62 31 65 65 38 61 34 32 2D 37 63 39 63 2D 34 63 66 39 2D 61 63 63 65 2D 39 35 62 39 38 36 38 34 34 65 36 35</o>\n",
" <o base='label' name='handler' data='bytes'>62 31 65 65 38 61 34 32 2D 37 63 39 63 2D 34 63 66 39 2D 61 63 63 65 2D 39 35 62 39 38 36 38 34 34 65 36 35</o>\n",
" <o base='string' data='bytes' name='type'>6A 61 76 61 2F 69 6F 2F 49 4F 45 78 63 65 70 74 69 6F 6E</o>\n",
"</o>"
)
)
).end(),
Matchers.notNullValue()
);
}

@Test
void findsHandlerLabel() {
MatcherAssert.assertThat(
"Can't find 'handler' label",
new XmlTryCatchEntry(
new XmlNode(
String.join(
"\n",
"<o base='trycatch'>\n",
" <o base='label' name='start' data='bytes'>30 65 65 66 66 62 37 37 2D 34 64 32 62 2D 34 63 31 38 2D 39 32 32 39 2D 36 32 65 39 66 61 66 39 34 61 34 34</o>\n",
" <o base='label' name='end' data='bytes'>62 31 65 65 38 61 34 32 2D 37 63 39 63 2D 34 63 66 39 2D 61 63 63 65 2D 39 35 62 39 38 36 38 34 34 65 36 35</o>\n",
" <o base='label' name='handler' data='bytes'>62 31 65 65 38 61 34 32 2D 37 63 39 63 2D 34 63 66 39 2D 61 63 63 65 2D 39 35 62 39 38 36 38 34 34 65 36 35</o>\n",
" <o base='string' data='bytes' name='type'>6A 61 76 61 2F 69 6F 2F 49 4F 45 78 63 65 70 74 69 6F 6E</o>\n",
"</o>"
)
)
).handler(),
Matchers.notNullValue()
)
);
}
}

0 comments on commit 4b03de3

Please sign in to comment.