Skip to content

Commit

Permalink
feat(objectionary#112): save class atributes to EO object attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Oct 4, 2023
1 parent 47aaaa8 commit a9dc6dc
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public void visit(
.add("objects");
this.directives.add("o")
.attr("abstract", "")
.attr("name", DirectivesClass.className(access, name));
.attr("name", name)
.append(new DirectivesClassProperties(access, signature, supername, interfaces));
super.visit(version, access, name, signature, supername, interfaces);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ public class DirectivesClassProperties implements Iterable<Directive> {

@Override
public Iterator<Directive> iterator() {
return new Directives()
.append(new XmlData(this.access, "access").directives())
.append(new XmlData(this.signature, "signature").directives())
.append(new XmlData(this.supername, "supername").directives())
.iterator();
final Directives directives = new Directives()
.append(new XmlData(this.access, "access").directives());
if (this.signature != null) {
directives.append(new XmlData(this.signature, "signature").directives());
}
if (this.supername != null) {
directives.append(new XmlData(this.supername, "supername").directives());
}
return directives.iterator();
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/eolang/jeo/representation/asm/HexString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.eolang.jeo.representation.asm;

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

public class HexString {

private final String hex;

public HexString(final String hex) {
this.hex = hex;
}

/**
* Convert hex string to human-readable string.
* Example:
* "48 65 6C 6C 6F 20 57 6F 72 6C 64 21" -> "Hello World!"
* @return Human-readable string.
*/
String decode() {
return Arrays.stream(hex.split(" "))
.map(ch -> (char) Integer.parseInt(ch, 16))
.map(String::valueOf)
.collect(Collectors.joining());
}

int decodeAsInt() {
return Arrays.stream(this.hex.split(" "))
.mapToInt(ch -> Integer.parseInt(ch, 16))
.sum();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public XmlBytecode(final XML xml) {
*/
public Bytecode bytecode() {
final XmlClass clazz = new XmlClass(this.xml);
final BytecodeClass bytecode = new BytecodeClass(clazz.name(), clazz.access());
final BytecodeClass bytecode = new BytecodeClass(clazz.name(), clazz.properties().access());
for (final XmlMethod xmlmethod : clazz.methods()) {
final BytecodeMethod method = bytecode.withMethod(
xmlmethod.name(),
Expand Down
31 changes: 11 additions & 20 deletions src/main/java/org/eolang/jeo/representation/asm/XmlClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

Expand Down Expand Up @@ -58,19 +59,19 @@ private XmlClass(final Node xml) {
}

/**
* Class access modifiers.
* @return Access modifiers.
* Class name.
* @return Name.
*/
int access() {
return Integer.parseInt(this.nameAttribute()[0]);
String name() {
return String.valueOf(this.node.getAttributes().getNamedItem("name").getTextContent());
}

/**
* Class name.
* @return Name.
* Class properties.
* @return Class properties.
*/
String name() {
return String.valueOf(this.nameAttribute()[1]);
XmlClassProperties properties() {
return new XmlClassProperties(this.node);
}

/**
Expand All @@ -82,23 +83,14 @@ List<XmlMethod> methods() {
final NodeList children = this.node.getChildNodes();
for (int child = 0; child < children.getLength(); ++child) {
final Node item = children.item(child);
if (item.getNodeName().equals("o")) {
final NamedNodeMap attrs = item.getAttributes();
if (item.getNodeName().equals("o") && attrs.getNamedItem("base") == null) {
res.add(new XmlMethod(item));
}
}
return res;
}

/**
* Name attribute.
* @return Name attribute.
*/
private String[] nameAttribute() {
final Node name = this.node.getAttributes().getNamedItem("name");
final String content = name.getTextContent();
return content.split("__");
}

/**
* Find class node in entire XML.
* @param xml Entire XML.
Expand Down Expand Up @@ -153,5 +145,4 @@ private static boolean isClass(final Node node) {
return node.getNodeName().equals("o")
&& node.getParentNode().getNodeName().equals("objects");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.eolang.jeo.representation.asm;

import com.jcabi.xml.XMLDocument;
import org.w3c.dom.Node;

public class XmlClassProperties {
private final XMLDocument clazz;

public XmlClassProperties(final Node clazz) {
this.clazz = new XMLDocument(clazz);
}

public int access() {
return new HexString(this.clazz.xpath("//o[@name='access']/text()").get(0)).decodeAsInt();
}

public String signature() {
return this.clazz.xpath("//o[@name='signature']/text()").get(0);
}

public String supername() {
return this.clazz.xpath("//o[@name='supername']/text()").get(0);

}


}

Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package org.eolang.jeo.representation.asm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

Expand Down Expand Up @@ -80,23 +78,9 @@ private static Object[] arguments(final Node node) {
for (int index = 0; index < children.getLength(); ++index) {
final Node child = children.item(index);
if (child.getNodeName().equals("o")) {
res.add(decodeHexString(child.getTextContent()));
res.add(new HexString(child.getTextContent()).decode());
}
}
return res.toArray();
}

/**
* Convert hex string to human-readable string.
* Example:
* "48 65 6C 6C 6F 20 57 6F 72 6C 64 21" -> "Hello World!"
* @param hex Hex string.
* @return Human-readable string.
*/
private static String decodeHexString(final String hex) {
return Arrays.stream(hex.split(" "))
.map(ch -> (char) Integer.parseInt(ch, 16))
.map(String::valueOf)
.collect(Collectors.joining());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.eolang.jeo.representation;

import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.XML;
import org.eolang.jeo.representation.asm.Bytecode;
import org.eolang.jeo.representation.asm.generation.BytecodeClass;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -65,7 +66,9 @@ void returnsXmlRepresentationOfEo() {
void returnsBytecodeRepresentationOfEo() {
final BytecodeClass clazz = new BytecodeClass("Bar");
final Bytecode expected = clazz.bytecode();
final Bytecode actual = new EoRepresentation(clazz.xml()).toBytecode();
final XML xml = clazz.xml();
System.out.println(xml);
final Bytecode actual = new EoRepresentation(xml).toBytecode();
MatcherAssert.assertThat(
String.format(
"The bytecode representation of the EO object is not correct,%nexpected:%n%s%nbut got:%n%s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void parsesSimpleClassWithoutConstructor() throws ImpossibleModificationExceptio
MatcherAssert.assertThat(
"Can't parse simple class without constructor",
new XMLDocument(new Xembler(directives).xml()),
XhtmlMatchers.hasXPath("/program/objects/o[@name='1__Simple']")
XhtmlMatchers.hasXPath("/program/objects/o[@name='Simple']")
);
}

Expand All @@ -65,10 +65,10 @@ void parsesSimpleClassWithMethod() throws ImpossibleModificationException {
new XMLDocument(new Xembler(directives).xml()),
Matchers.allOf(
XhtmlMatchers.hasXPath(
"/program/objects/o[@name='1__WithMethod']/o[contains(@name,'main')]"
"/program/objects/o[@name='WithMethod']/o[contains(@name,'main')]"
),
XhtmlMatchers.hasXPath(
"/program/objects/o[@name='1__WithMethod']/o[contains(@name,'main')]/o[@base='seq']"
"/program/objects/o[@name='WithMethod']/o[contains(@name,'main')]/o[@base='seq']"
)
)
);
Expand Down

0 comments on commit a9dc6dc

Please sign in to comment.