Skip to content

Commit

Permalink
feat(objectionary#112): remove the puzzle for objectionary#112 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Oct 4, 2023
1 parent a9dc6dc commit 954faba
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.cactoos.io.InputOf;
import org.eolang.jeo.Representation;
import org.eolang.jeo.representation.asm.Bytecode;
import org.eolang.jeo.representation.asm.DirectivesClass;
import org.eolang.jeo.representation.asm.ClassName;
import org.eolang.jeo.representation.asm.DirectivesClass;
import org.objectweb.asm.ClassReader;
import org.xembly.ImpossibleModificationException;
import org.xembly.Xembler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,6 @@ public Iterator<Directive> iterator() {
return this.directives.iterator();
}

/**
* Class name.
* @param access Access.
* @param name Name.
* @return Class name.
* @todo #108:90min Implement different way to generate class name.
* Right now we use class name and access to generate class name and insert it as a name
* attribute of the class. This is not a good way to do it. At least it looks ugly.
* We should find a better way to generate class name and place it somewhere in the XML
* representation of the class.
*/
private static String className(final int access, final String name) {
return String.format("%d__%s", access, name);
}

/**
* Method name.
* @param access Access.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@
*/
package org.eolang.jeo.representation.asm;

import com.jcabi.log.Logger;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Class properties as Xembly directives.
*
* @since 0.1.0.
* @since 0.1.0
* @todo #112:60min Convert array into data or tuple object.
* Right now we just skip array of interfaces. We should convert it into
* data or tuple object. When the method is ready remove that puzzle.
*/
public class DirectivesClassProperties implements Iterable<Directive> {
final class DirectivesClassProperties implements Iterable<Directive> {

/**
* Access modifiers.
Expand All @@ -51,19 +52,20 @@ public class DirectivesClassProperties implements Iterable<Directive> {
/**
* Class supername.
*/
final String supername;
private final String supername;

/**
* Class interfaces.
*/
final String[] interfaces;
private final String[] interfaces;

/**
* Constructor.
* @param access Access modifiers.
* @param signature Class Signature.
* @param supername Class supername.
* @param interfaces Class interfaces.
* @checkstyle ParameterNumberCheck (5 lines)
*/
DirectivesClassProperties(
final int access,
Expand All @@ -87,6 +89,13 @@ public Iterator<Directive> iterator() {
if (this.supername != null) {
directives.append(new XmlData(this.supername, "supername").directives());
}
Logger.warn(
this,
String.format(
"Interfaces mapping is not implemented yet. Interfaces '%s' was skipped",
Arrays.toString(this.interfaces)
)
);
return directives.iterator();
}
}
55 changes: 50 additions & 5 deletions src/main/java/org/eolang/jeo/representation/asm/HexString.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 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.representation.asm;

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

public class HexString {
/**
* Hex string.
* @since 0.1.0
*/
final class HexString {

/**
* Hex radix.
*/
private static final int RADIX = 16;

/**
* Hex string.
* Example:
* - "48 65 6C 6C 6F 20 57 6F 72 6C 64 21"
*/
private final String hex;

public HexString(final String hex) {
/**
* Constructor.
* @param hex Hex string.
*/
HexString(final String hex) {
this.hex = hex;
}

Expand All @@ -18,15 +59,19 @@ public HexString(final String hex) {
* @return Human-readable string.
*/
String decode() {
return Arrays.stream(hex.split(" "))
.map(ch -> (char) Integer.parseInt(ch, 16))
return Arrays.stream(this.hex.split(" "))
.map(ch -> (char) Integer.parseInt(ch, HexString.RADIX))
.map(String::valueOf)
.collect(Collectors.joining());
}

/**
* Convert hex string to integer.
* @return Integer.
*/
int decodeAsInt() {
return Arrays.stream(this.hex.split(" "))
.mapToInt(ch -> Integer.parseInt(ch, 16))
.mapToInt(ch -> Integer.parseInt(ch, HexString.RADIX))
.sum();
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 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.representation.asm;

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

public class XmlClassProperties {
/**
* XML representation of a class.
*
* @since 0.1.0
*/
final class XmlClassProperties {

/**
* XML representation of a class.
*/
private final XMLDocument clazz;

public XmlClassProperties(final Node clazz) {
/**
* Constructor.
* @param clazz XMl representation of a class.
*/
XmlClassProperties(final Node clazz) {
this.clazz = new XMLDocument(clazz);
}

public int access() {
/**
* Retrieve 'access' modifiers of a class.
* @return Access modifiers.
*/
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,7 +24,6 @@
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 @@ -66,9 +65,7 @@ void returnsXmlRepresentationOfEo() {
void returnsBytecodeRepresentationOfEo() {
final BytecodeClass clazz = new BytecodeClass("Bar");
final Bytecode expected = clazz.bytecode();
final XML xml = clazz.xml();
System.out.println(xml);
final Bytecode actual = new EoRepresentation(xml).toBytecode();
final Bytecode actual = new EoRepresentation(clazz.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
@@ -1,3 +1,26 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 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.representation.asm;

import org.hamcrest.MatcherAssert;
Expand All @@ -7,7 +30,12 @@
import org.xembly.ImpossibleModificationException;
import org.xembly.Xembler;

class DirectivesClassPropertiesTest {
/**
* Test case for {@link DirectivesClassProperties}.
*
* @since 0.1.0
*/
final class DirectivesClassPropertiesTest {

@Test
void createsDirectives() throws ImpossibleModificationException {
Expand All @@ -26,7 +54,8 @@ void createsDirectives() throws ImpossibleModificationException {
).up()
).xml(),
Matchers.equalTo(
String.join("",
String.join(
"",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<o>\n",
" <o base=\"int\" data=\"bytes\" name=\"access\">00 00 00 00 00 00 00 01</o>\n",
Expand All @@ -37,6 +66,4 @@ void createsDirectives() throws ImpossibleModificationException {
)
);
}


}
}

0 comments on commit 954faba

Please sign in to comment.