Skip to content

Commit

Permalink
feat(objectionary#329): add javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jul 11, 2024
1 parent 5100308 commit a1ee4f2
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 72 deletions.
21 changes: 21 additions & 0 deletions src/main/java/org/eolang/opeo/ast/CheckCast.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ public final class CheckCast implements AstNode, Typed {
*/
private final AstNode value;

/**
* Constructor.
* @param node XMIR node to parse.
* @param parser Parser to use.
*/
public CheckCast(final XmlNode node, final Parser parser) {
this(CheckCast.xtype(node), CheckCast.xvalue(node, parser));
}

/**
* Constructor.
* @param type Type to check.
* @param value Value to cast.
*/
public CheckCast(final Type type, final AstNode value) {
this.type = type;
this.value = value;
Expand All @@ -81,10 +91,21 @@ public Type type() {
return this.type;
}

/**
* Parse value from XMIR node.
* @param node XMIR node to parse.
* @param parser Parser to use.
* @return Parsed value.
*/
private static AstNode xvalue(final XmlNode node, final Parser parser) {
return parser.parse(node.children().collect(Collectors.toList()).get(1));
}

/**
* Parse checkcast type from XMIR node.
* @param node XMIR node to parse.
* @return Parsed type.
*/
private static Type xtype(final XmlNode node) {
return Type.getType(
new HexString(
Expand Down
129 changes: 91 additions & 38 deletions src/main/java/org/eolang/opeo/ast/DynamicInvocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,40 @@
*/
public final class DynamicInvocation implements AstNode, Typed {

/**
* Name of the method.
*/
private final String name;

/**
* Factory method reference.
*/
private final Handle factory;

/**
* Method attributes.
*/
private final Attributes attributes;

/**
* Factory method arguments.
*/
private final List<Object> arguments;

/**
* Constructor.
* @param root XMIR node to parse.
*/
public DynamicInvocation(final XmlNode root) {
this(root, root.children().collect(Collectors.toList()));
}

/**
* Constructor.
* Added for efficiency to receive children nodes only once.
* @param root XMIR node to parse.
* @param chldren XMIR node children.
*/
public DynamicInvocation(final XmlNode root, final List<XmlNode> chldren) {
this(
DynamicInvocation.xname(root),
Expand All @@ -61,28 +85,13 @@ public DynamicInvocation(final XmlNode root, final List<XmlNode> chldren) {
);
}

private static List<Object> xarguments(final List<XmlNode> children) {
List<Object> res = new ArrayList<>(3);
res.add(Type.getType(new HexString(children.get(2).text()).decode()));
res.add(new Handle(children.get(3)).toAsm());
res.add(Type.getType(new HexString(children.get(4).text()).decode()));
return res;
}

private static String xdescriptor(final List<XmlNode> root) {
return new Attributes(root.get(0)).descriptor();
}

private static Handle xfactory(final List<XmlNode> root) {
return new Handle(root.get(1));
}

private static String xname(final XmlNode root) {
return root.attribute("base")
.map(s -> s.substring(1))
.orElseThrow(() -> new IllegalArgumentException("Name is required"));
}

/**
* Constructor.
* @param name Name of the method.
* @param factory Factory method reference.
* @param descriptor Method descriptor.
* @param arguments Factory method arguments.
*/
public DynamicInvocation(
final String name,
final Handle factory,
Expand All @@ -97,28 +106,13 @@ public DynamicInvocation(
this.arguments = arguments;
}

private List<Xmir> toXmlArg(final List<Object> arguments) {
return arguments.stream().map(
node -> {
if (node instanceof org.objectweb.asm.Handle) {
return new Handle((org.objectweb.asm.Handle) node);
} else {
return (Xmir) () -> new DirectivesData(node);
}
}
).collect(Collectors.toList());
}

@Override
public Iterable<Directive> toXmir() {
final Directives directives = new Directives().add("o")
.attr("base", String.format(".%s", this.name))
.append(this.attributes.toXmir())
.append(this.factory.toXmir());
final List<Xmir> xmlArg = this.toXmlArg(this.arguments);
xmlArg.stream()
.map(Xmir::toXmir)
.forEach(directives::append);
this.xmirArgs(this.arguments).stream().map(Xmir::toXmir).forEach(directives::append);
return directives.up();
}

Expand All @@ -143,4 +137,63 @@ public List<AstNode> opcodes() {
public Type type() {
return Type.getReturnType(this.attributes.descriptor());
}

/**
* Parse the dynamic method name.
* @param root XMIR root node.
* @return Name.
*/
private static String xname(final XmlNode root) {
return root.attribute("base")
.map(s -> s.substring(1))
.orElseThrow(() -> new IllegalArgumentException("Name is required"));
}

/**
* Parse a dynamic invocation descriptor.
* @param children XMIR root node children which we parse.
* @return Descriptor.
*/
private static String xdescriptor(final List<XmlNode> children) {
return new Attributes(children.get(0)).descriptor();
}

/**
* Parse a factory method reference.
* @param children XMIR root node children which we parse.
* @return Factory method reference.
*/
private static Handle xfactory(final List<XmlNode> children) {
return new Handle(children.get(1));
}

/**
* Parse a factory method arguments.
* @param children XMIR children.
* @return Arguments.
*/
private static List<Object> xarguments(final List<XmlNode> children) {
final List<Object> res = new ArrayList<>(3);
res.add(Type.getType(new HexString(children.get(2).text()).decode()));
res.add(new Handle(children.get(3)).toAsm());
res.add(Type.getType(new HexString(children.get(4).text()).decode()));
return res;
}

/**
* Convert the factory method arguments to XMIR.
* @param arguments Arguments.
* @return XMIR arguments.
*/
private List<Xmir> xmirArgs(final List<Object> arguments) {
return arguments.stream().map(
node -> {
if (node instanceof org.objectweb.asm.Handle) {
return new Handle((org.objectweb.asm.Handle) node);
} else {
return (Xmir) () -> new DirectivesData(node);
}
}
).collect(Collectors.toList());
}
}
Loading

0 comments on commit a1ee4f2

Please sign in to comment.