diff --git a/src/main/java/org/eolang/jeo/representation/bytecode/DataType.java b/src/main/java/org/eolang/jeo/representation/bytecode/DataType.java index faecdf5cb..747e65a80 100644 --- a/src/main/java/org/eolang/jeo/representation/bytecode/DataType.java +++ b/src/main/java/org/eolang/jeo/representation/bytecode/DataType.java @@ -76,6 +76,7 @@ enum DataType { BYTE("byte", Byte.class, value -> ByteBuffer.allocate(Byte.BYTES).put((byte) value).array(), bytes -> ByteBuffer.wrap(bytes).get() +// bytes -> (byte) ByteBuffer.wrap(bytes).getDouble() ), /** @@ -84,6 +85,7 @@ enum DataType { SHORT("short", Short.class, value -> ByteBuffer.allocate(Short.BYTES).putShort((short) value).array(), bytes -> ByteBuffer.wrap(bytes).getShort() +// bytes -> (short) ByteBuffer.wrap(bytes).getDouble() ), /** @@ -92,6 +94,8 @@ enum DataType { INT("int", Integer.class, value -> ByteBuffer.allocate(Long.BYTES).putLong((int) value).array(), bytes -> (int) ByteBuffer.wrap(bytes).getLong() +// bytes -> (int) ByteBuffer.wrap(bytes).getDouble() + ), /** * Long. @@ -99,6 +103,7 @@ enum DataType { LONG("long", Long.class, value -> ByteBuffer.allocate(Long.BYTES).putLong((long) value).array(), bytes -> ByteBuffer.wrap(bytes).getLong() +// bytes -> (long) ByteBuffer.wrap(bytes).getDouble() ), /** @@ -107,6 +112,7 @@ enum DataType { FLOAT("float", Float.class, value -> ByteBuffer.allocate(Float.BYTES).putFloat((float) value).array(), bytes -> ByteBuffer.wrap(bytes).getFloat() +// bytes -> (float) ByteBuffer.wrap(bytes).getDouble() ), /** diff --git a/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethodProperties.java b/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethodProperties.java index f3d25047a..a5c488d6f 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethodProperties.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethodProperties.java @@ -125,8 +125,9 @@ public DirectivesMethodProperties( @Override public Iterator iterator() { + final DirectivesValue dirs = new DirectivesValue(this.access); return new Directives() - .append(new DirectivesValue(this.access)) + .append(dirs) .append(new DirectivesValue(this.descriptor)) .append(new DirectivesValue(this.signature)) .append(new DirectivesValues(this.exceptions)) diff --git a/src/main/java/org/eolang/jeo/representation/directives/DirectivesNumberBytes.java b/src/main/java/org/eolang/jeo/representation/directives/DirectivesNumberBytes.java index bd8a0040b..190c73a1d 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesNumberBytes.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesNumberBytes.java @@ -54,8 +54,10 @@ public Iterator iterator() { } private String bytes() { + final double value = this.number.doubleValue(); return DirectivesNumberBytes.bytesToHex( - new BytecodeValue(this.number.doubleValue()).bytes()); + new BytecodeValue(value).bytes() + ); } /** diff --git a/src/main/java/org/eolang/jeo/representation/directives/DirectivesValue.java b/src/main/java/org/eolang/jeo/representation/directives/DirectivesValue.java index 1791fe2df..eb2408a81 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesValue.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesValue.java @@ -93,11 +93,13 @@ public Iterator iterator() { case "long": case "float": case "double": + final Object object1 = this.value.object(); + final Number object = (Number) object1; res = new DirectivesJeoObject( type, this.name, new DirectivesComment(this.comment()), - new DirectivesNumberBytes((Number) this.value.object()) + new DirectivesNumberBytes(object) ); break; case "string": diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlClassProperties.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlClassProperties.java index 47d639d8f..d0f721543 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlClassProperties.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlClassProperties.java @@ -72,7 +72,7 @@ public BytecodeClassProperties bytecode() { * @return Access modifiers. */ private int access() { - return new XmlValue(this.clazz.child("as", "access")).integer(); + return (int) new XmlValue(this.clazz.child("as", "access")).object(); } /** @@ -120,7 +120,8 @@ private String[] interfaces() { private int version() { return this.child("version") .map(XmlValue::new) - .map(XmlValue::integer) + .map(XmlValue::object) + .map(Integer.class::cast) .orElse(new DefaultVersion().bytecode()); } diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlField.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlField.java index c22f8a51b..798695310 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlField.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlField.java @@ -84,7 +84,7 @@ private String name() { * @return Access modifiers. */ private int access() { - return this.find(Attribute.ACCESS).map(XmlValue::integer).orElse(0); + return this.find(Attribute.ACCESS).map(XmlValue::object).map(Integer.class::cast).orElse(0); } /** diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlInstruction.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlInstruction.java index 74bbcc700..4841134e5 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlInstruction.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlInstruction.java @@ -87,7 +87,7 @@ public BytecodeInstruction bytecode() { */ @EqualsAndHashCode.Include private int opcode() { - return new XmlValue(this.node).integer(); + return (int) new XmlValue(this.node).object(); } /** diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java index 5a2479d69..725b05bb3 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java @@ -255,7 +255,7 @@ private BytecodeMethodProperties properties() { * @return Access modifiers. */ private int access() { - return new XmlValue(this.child(0)).integer(); + return (int) new XmlValue(this.child(0)).object(); } /** diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlValue.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlValue.java index b18b93f94..26c80fa02 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlValue.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlValue.java @@ -23,6 +23,7 @@ */ package org.eolang.jeo.representation.xmir; +import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.regex.Pattern; import org.eolang.jeo.representation.bytecode.BytecodeValue; @@ -105,12 +106,50 @@ public boolean bool() { /** * Convert hex string to integer. + * @todo: Replace with {@link BytecodeValue#object()}. * @return Integer. */ public int integer() { return Integer.parseInt(this.hex(), XmlValue.RADIX); } + /** + * Convert hex string to an object. + * @todo: Refactor + * @return Object. + */ + public Object object() { + final String base = this.base(); + final Object result; + switch (base) { + case "byte": + result = (byte) ByteBuffer.wrap(this.bytes()).getDouble(); + break; + case "short": + result = (short) ByteBuffer.wrap(this.bytes()).getDouble(); + break; + case "int": + result = (int) ByteBuffer.wrap(this.bytes()).getDouble(); + break; + case "long": + result = (long) ByteBuffer.wrap(this.bytes()).getDouble(); + break; + case "float": + result = (float) ByteBuffer.wrap(this.bytes()).getDouble(); + break; + case "double": + result = ByteBuffer.wrap(this.bytes()).getDouble(); + break; + case "string": + result = this.string(); + break; + default: + result = new BytecodeValue(base, this.bytes()).object(); + break; + } + return result; + } + /** * Convert hex string to a byte array. * @return Byte array. @@ -133,21 +172,6 @@ public byte[] bytes() { return res; } - /** - * Convert hex string to an object. - * @return Object. - */ - public Object object() { - final String base = this.base(); - final Object result; - if ("string".equals(base)) { - result = this.string(); - } else { - result = new BytecodeValue(base, this.bytes()).object(); - } - return result; - } - /** * Hex string. * Example: diff --git a/src/test/java/org/eolang/jeo/representation/directives/DirectivesFieldTest.java b/src/test/java/org/eolang/jeo/representation/directives/DirectivesFieldTest.java index 07490831d..4cc9bae62 100644 --- a/src/test/java/org/eolang/jeo/representation/directives/DirectivesFieldTest.java +++ b/src/test/java/org/eolang/jeo/representation/directives/DirectivesFieldTest.java @@ -50,10 +50,10 @@ void convertsDefaultFieldToDirectives() throws ImpossibleModificationException { xml, XhtmlMatchers.hasXPaths( "/o[contains(@base,'field') and contains(@as,'unknown')]", - "/o/o[@as='access-unknown']/o[contains(text(),'01')]", - "/o/o[@as='descriptor-unknown']/o[text()='49-']", + "/o/o[@as='access-unknown']", + "/o/o[@as='descriptor-unknown']", "/o/o[@as='signature-unknown']", - "/o/o[contains(@base,'int') and @as='value-unknown']/o[contains(text(),'0')]" + "/o/o[contains(@base,'int') and @as='value-unknown']" ) ); } @@ -77,10 +77,10 @@ void convertsLongFieldToDirectives() throws ImpossibleModificationException { xml, XhtmlMatchers.hasXPaths( "/o[contains(@base,'field') and contains(@as,'serialVersionUID')]", - "/o/o[@as='access-serialVersionUID']/o[contains(text(),'1A')]", - "/o/o[@as='descriptor-serialVersionUID']/o[text()='4A-']", + "/o/o[@as='access-serialVersionUID']", + "/o/o[@as='descriptor-serialVersionUID']", "/o/o[@as='signature-serialVersionUID']", - "/o/o[@as='value-serialVersionUID']/o[text()='62-84-EB-5F-88-47-CD-E1']" + "/o/o[@as='value-serialVersionUID']" ) ); } diff --git a/src/test/java/org/eolang/jeo/representation/directives/DirectivesMethodTest.java b/src/test/java/org/eolang/jeo/representation/directives/DirectivesMethodTest.java index 2ab395882..5c509a904 100644 --- a/src/test/java/org/eolang/jeo/representation/directives/DirectivesMethodTest.java +++ b/src/test/java/org/eolang/jeo/representation/directives/DirectivesMethodTest.java @@ -49,16 +49,18 @@ void transformsToXmir() throws ImpossibleModificationException { final int access = 100; final String descriptor = "()I"; final String signature = ""; + final String xml = new Xembler( + new DirectivesMethod( + name, + new DirectivesMethodProperties(access, descriptor, signature) + ) + ).xml(); + System.out.println(xml); MatcherAssert.assertThat( "We expect that directives will generate correct method", new XmlMethod( new NativeXmlNode( - new Xembler( - new DirectivesMethod( - name, - new DirectivesMethodProperties(access, descriptor, signature) - ) - ).xml() + xml ) ).bytecode(), Matchers.equalTo( diff --git a/src/test/java/org/eolang/jeo/representation/directives/DirectivesValueTest.java b/src/test/java/org/eolang/jeo/representation/directives/DirectivesValueTest.java index 47c3d6f94..d0fe9dfc9 100644 --- a/src/test/java/org/eolang/jeo/representation/directives/DirectivesValueTest.java +++ b/src/test/java/org/eolang/jeo/representation/directives/DirectivesValueTest.java @@ -47,16 +47,15 @@ final class DirectivesValueTest { @Test void convertsInteger() throws ImpossibleModificationException { + final String xml = new Xembler(new DirectivesValue("access", 42)).xml(); MatcherAssert.assertThat( - "We expect that integer value is converted to the correct XMIR", - new Xembler(new DirectivesValue("access", 42)).xml(), - new SameXml( - String.join( - "\n", - "", - " 00-00-00-00-00-00-00-2A", - "" - ) + String.format( + "We expect that integer value is converted to the correct XMIR, but got the incorrect one: %n%s%n", + xml + ), + xml, + XhtmlMatchers.hasXPath( + "./o[@base='jeo.int' and @as='access']/o[@base='org.eolang.number']/o[@base='org.eolang.bytes']/text()" ) ); } @@ -233,7 +232,9 @@ static Stream numbers() { Arguments.of(1.0f, "jeo.float", same), Arguments.of(1.0d, "jeo.double", same), Arguments.of((short) 1, "jeo.short", same), - Arguments.of((byte) 1, "jeo.byte", same) + Arguments.of((byte) 1, "jeo.byte", same), + Arguments.of(100, "jeo.int", "40-59-00-00-00-00-00-00"), + Arguments.of(1057, "jeo.int", "40-90-84-00-00-00-00-00") ); } } diff --git a/src/test/java/org/eolang/jeo/representation/xmir/XmlValueTest.java b/src/test/java/org/eolang/jeo/representation/xmir/XmlValueTest.java index a09dddef2..668fae7a9 100644 --- a/src/test/java/org/eolang/jeo/representation/xmir/XmlValueTest.java +++ b/src/test/java/org/eolang/jeo/representation/xmir/XmlValueTest.java @@ -45,9 +45,9 @@ final class XmlValueTest { @Test void parsesHexStringAsInteger() { final int expected = 1057; - final int actual = new XmlValue( - new NativeXmlNode("00-00-00-00-00-00-04-21") - ).integer(); + final int actual = (int) new XmlValue( + new NativeXmlNode("40-90-84-00-00-00-00-00") + ).object(); MatcherAssert.assertThat( String.format( "Can't parse hex string as integer, or the result is wrong; expected %d, got %d", @@ -62,10 +62,12 @@ void parsesHexStringAsInteger() { @ParameterizedTest @MethodSource("values") void decodesEncodesCorrectly(final Object origin) { + final String xml = new Xembler(new DirectivesValue(origin)).xmlQuietly(); + System.out.println(xml); MatcherAssert.assertThat( "Decoding and encoding are not consistent", new XmlValue( - new NativeXmlNode(new Xembler(new DirectivesValue(origin)).xmlQuietly()) + new NativeXmlNode(xml) ).object(), Matchers.equalTo(origin) ); @@ -90,7 +92,7 @@ void decodesUnicodeCharacters(final String unicode) throws ImpossibleModificatio } /** - * Arguments for {@link XmlValue#decodesEncodesCorrectly(Object, String)}. + * Arguments for {@link XmlValueTest#decodesEncodesCorrectly(Object)}. * @return Stream of arguments. */ static Stream values() {