Skip to content

Commit

Permalink
feat(objectionary#636): refactor XmlClass a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Sep 9, 2024
1 parent 32b83c6 commit 4f2e23d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,53 @@ public BytecodeClass(
final String name,
final Collection<BytecodeMethod> methods,
final BytecodeClassProperties properties
) {
this(name, methods, new ArrayList<>(0), new ArrayList<>(0), new ArrayList<>(0), properties);
}

/**
* Constructor.
* @param name Class name.
* @param fields Fields.
* @param annotations Annotations.
* @param attributes Attributes.
* @param props Class properties.
* @checkstyle ParameterNumberCheck (5 lines)
*/
public BytecodeClass(
final String name,
final Collection<BytecodeField> fields,
final Collection<BytecodeAnnotation> annotations,
final Collection<BytecodeAttribute> attributes,
final BytecodeClassProperties props
) {
this(name, new ArrayList<>(0), fields, annotations, attributes, props);
}

/**
* Constructor.
* @param name Class name.
* @param methods Methods.
* @param fields Fields.
* @param annotations Annotations.
* @param attributes Attributes.
* @param props Class properties.
* @checkstyle ParameterNumberCheck (5 lines)
*/
public BytecodeClass(
final String name,
final Collection<BytecodeMethod> methods,
final Collection<BytecodeField> fields,
final Collection<BytecodeAnnotation> annotations,
final Collection<BytecodeAttribute> attributes,
final BytecodeClassProperties props
) {
this.name = name;
this.methods = methods;
this.props = properties;
this.fields = new ArrayList<>(0);
this.annotations = new ArrayList<>(0);
this.attributes = new ArrayList<>(0);
this.fields = fields;
this.annotations = annotations;
this.attributes = attributes;
this.props = props;
}

/**
Expand All @@ -143,6 +183,10 @@ public String name() {
return this.name;
}

/**
* Constructor.
* @param visitor Writer.
*/
void writeTo(final CustomClassWriter visitor) {
try {
visitor.visit(
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ public final class XmlAttribute {
}

/**
* Write to bytecode.
* @param bytecode Bytecode.
* Get attribute.
* @return Attribute.
*/
public void writeTo(final BytecodeClass bytecode) {
public Optional<BytecodeAttribute> attribute() {
final String base = this.node.attribute("base")
.orElseThrow(
() -> new IllegalArgumentException(
String.format("Attribute base is missing in XML node %s", this.node)
)
);
if ("inner-class".equals(base)) {
bytecode.withAttribute(
return Optional.of(
new BytecodeAttribute.InnerClass(
Optional.ofNullable(this.node.children().collect(Collectors.toList()).get(0))
.map(XmlOperand::new)
Expand All @@ -89,8 +89,16 @@ public void writeTo(final BytecodeClass bytecode) {
.map(XmlOperand::asObject)
.map(Integer.class::cast)
.orElse(0)
)
);
));
}
return Optional.empty();
}

/**
* Write to bytecode.
* @param bytecode Bytecode.
*/
public void writeTo(final BytecodeClass bytecode) {
this.attribute().ifPresent(bytecode::withAttribute);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
*/
package org.eolang.jeo.representation.xmir;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.eolang.jeo.representation.bytecode.BytecodeAttribute;
import org.eolang.jeo.representation.bytecode.BytecodeClass;

/**
Expand All @@ -48,6 +52,17 @@ public final class XmlAttributes {
this.node = node;
}

/**
* Get attributes.
* @return Attributes.
*/
public List<BytecodeAttribute> attributes() {
return this.node.children().map(XmlAttribute::new).map(XmlAttribute::attribute)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}

/**
* Write to bytecode.
* @param bytecode Bytecode where to write.
Expand Down
50 changes: 25 additions & 25 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
*/
package org.eolang.jeo.representation.xmir;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eolang.jeo.representation.ClassName;
import org.eolang.jeo.representation.PrefixedName;
import org.eolang.jeo.representation.bytecode.BytecodeAnnotations;
import org.eolang.jeo.representation.bytecode.BytecodeClass;
import org.eolang.jeo.representation.directives.DirectivesClass;
import org.eolang.jeo.representation.directives.DirectivesClassProperties;
Expand All @@ -56,16 +57,14 @@ public final class XmlClass {

/**
* Constructor.
*
* @param xmlnode XML node.
* @param node Class node.
*/
XmlClass(final XML xmlnode) {
this(xmlnode.node().getFirstChild());
public XmlClass(final XmlNode node) {
this.node = node;
}

/**
* Constructor.
*
* @param classname Class name.
*/
XmlClass(final String classname) {
Expand All @@ -74,7 +73,6 @@ public final class XmlClass {

/**
* Constructor.
*
* @param classname Class name.
* @param properties Class properties.
*/
Expand All @@ -84,16 +82,6 @@ public final class XmlClass {

/**
* Constructor.
*
* @param node Class node.
*/
public XmlClass(final XmlNode node) {
this.node = node;
}

/**
* Constructor.
*
* @param xml Class node.
*/
XmlClass(final Node xml) {
Expand All @@ -107,16 +95,28 @@ public XmlClass(final XmlNode node) {
public BytecodeClass bytecode(final String pckg) {
final BytecodeClass bytecode = new BytecodeClass(
new ClassName(pckg, new PrefixedName(this.name()).decode()).full(),
this.fields().stream()
.map(XmlField::bytecode)
.collect(Collectors.toList()),
this.annotations()
.map(XmlAnnotations::bytecode)
.orElse(new BytecodeAnnotations())
.annotations(),
this.attributes()
.map(XmlAttributes::attributes)
.orElse(new ArrayList<>(0)),
this.properties().bytecode()
);
this.annotations().ifPresent(bytecode::withAnnotations);
for (final XmlField field : this.fields()) {
bytecode.withField(field.bytecode());
}
for (final XmlMethod xmlmethod : this.methods()) {
bytecode.withMethod(xmlmethod.bytecode(bytecode));
}
this.attributes().ifPresent(attrs -> attrs.writeTo(bytecode));
this.methods().stream().map(m -> m.bytecode(bytecode))
.forEach(bytecode::withMethod);
// this.annotations().ifPresent(bytecode::withAnnotations);
// for (final XmlField field : this.fields()) {
// bytecode.withField(field.bytecode());
// }
// for (final XmlMethod xmlmethod : this.methods()) {
// bytecode.withMethod(xmlmethod.bytecode(bytecode));
// }
// this.attributes().ifPresent(attrs -> attrs.writeTo(bytecode));
return bytecode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.objectweb.asm.Opcodes;

/**
* Test case for {@link org.eolang.jeo.representation.xmir.XmlClassProperties}.
* Test case for {@link XmlClassProperties}.
* @since 0.1
*/
final class XmlClassPropertiesTest {
Expand Down

0 comments on commit 4f2e23d

Please sign in to comment.