Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Oct 26, 2023
2 parents 58a442a + 90aa096 commit 53063c2
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ private static Representation replaceConstructors(
* @param clazz Class where to replace.
* @param target What should be replaced.
* @param replacement Replacement.
* @todo #161:90min Refactor replace method.
* Right now it's a big method with a lot of repetition and high complexity.
* Moreover, some constants are hardcoded and it's not good.
* We need to refactor it into a set of smaller methods and remove all linter warnings.
* @checkstyle ModifiedControlVariableCheck (200 lines)
*/
private static void replace(
Expand Down Expand Up @@ -178,17 +174,19 @@ private static void replace(
/**
* Replace arguments.
* @param clazz Class where to replace.
* @todo #157:90min Handle arguments correctly during inlining optimization.
* Right now we just replace all arguments with the new class name.
* It's not correct, because we need to handle arguments correctly.
*/
private static void replaceArguments(final XmlClass clazz) {
for (final XmlMethod method : clazz.methods()) {
for (final XmlInstruction instruction : method.instructions()) {
DecoratorPair.replaceArguments(
instruction.node(),
"org/eolang/jeo/B",
"org/eolang/jeo/A$B"
);
}
}
clazz.methods()
.stream()
.map(XmlMethod::instructions)
.flatMap(Collection::stream)
.forEach(
instruction ->
instruction.replaceArguementsValues("org/eolang/jeo/B", "org/eolang/jeo/A$B")
);
}

/**
Expand Down Expand Up @@ -245,33 +243,29 @@ private static class DecoratorPair {
* @return List of NEW instructions.
*/
private List<XmlInstruction> targetNew() {
final String firstname = this.decorated.name();
final Node first = new XMLDocument(
new StringBuilder()
.append("<o base=\"opcode\" name=\"NEW-187-50\">")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData(firstname).value())
.append("</o>")
.append("</o>")
.toString()
).node().getFirstChild();
final String secondname = this.decorator.name();
final Node second = new XMLDocument(
new StringBuilder()
.append("<o base=\"opcode\" name=\"NEW-187-50\">")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData(secondname).value())
.append("</o>")
.append("</o>")
.toString()
).node().getFirstChild();
final Node dup = new XMLDocument("<o base=\"opcode\" name=\"DUP-89-53\"/>")
.node()
.getFirstChild();
final String dup = "<o base='opcode' name='DUP-89-53'/>";
return Arrays.asList(
new XmlInstruction(second),
new XmlInstruction(
String.join(
"",
"<o base='opcode' name='NEW-187-50'>",
"<o base='string' data='bytes'>",
new HexData(this.decorator.name()).value(),
"</o>",
"</o>"
)
),
new XmlInstruction(dup),
new XmlInstruction(first),
new XmlInstruction(
String.join(
"",
"<o base='opcode' name='NEW-187-50'>",
"<o base='string' data='bytes'>",
new HexData(this.decorated.name()).value(),
"</o>",
"</o>"
)
),
new XmlInstruction(dup)
);
}
Expand All @@ -281,21 +275,18 @@ private List<XmlInstruction> targetNew() {
* @return Replacement.
*/
private List<XmlInstruction> replacementNew() {
final Node second = new XMLDocument(
new StringBuilder()
.append("<o base=\"opcode\" name=\"NEW-187-50\">")
.append("<o base=\"string\" data=\"bytes\">")
.append(new DecoratorCompositionName(this.decorated, this.decorator).hex())
.append("</o>")
.append("</o>")
.toString()
).node().getFirstChild();
final Node dup = new XMLDocument("<o base=\"opcode\" name=\"DUP-89-53\"/>")
.node()
.getFirstChild();
return Arrays.asList(
new XmlInstruction(second),
new XmlInstruction(dup)
new XmlInstruction(
String.join(
"",
"<o base='opcode' name='NEW-187-50'>",
"<o base='string' data='bytes'>",
new DecoratorCompositionName(this.decorated, this.decorator).hex(),
"</o>",
"</o>"
)
),
new XmlInstruction("<o base='opcode' name='DUP-89-53'/>")
);
}

Expand All @@ -306,38 +297,36 @@ private List<XmlInstruction> replacementNew() {
private List<XmlInstruction> targetSpecial() {
return Arrays.asList(
new XmlInstruction(
new XMLDocument(
new StringBuilder()
.append("<o base=\"opcode\" name=\"INVOKESPECIAL-183-55\">")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData(this.decorated.name()).value())
.append("</o>")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData("<init>").value())
.append("</o>")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData("(I)V").value())
.append("</o>")
.append("</o>")
.toString()
).node().getFirstChild()
String.join(
"",
"<o base='opcode' name='INVOKESPECIAL-183-55'>",
"<o base='string' data='bytes'>",
new HexData(this.decorated.name()).value(),
"</o>",
"<o base='string' data='bytes'>",
new HexData("<init>").value(),
"</o>",
"<o base='string' data='bytes'>",
new HexData("(I)V").value(),
"</o>",
"</o>"
)
),
new XmlInstruction(
new XMLDocument(
new StringBuilder()
.append("<o base=\"opcode\" name=\"INVOKESPECIAL-183-55\">")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData(this.decorator.name()).value())
.append("</o>")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData("<init>").value())
.append("</o>")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData("(Lorg/eolang/jeo/A;)V").value())
.append("</o>")
.append("</o>")
.toString()
).node().getFirstChild()
String.join(
"",
"<o base='opcode' name='INVOKESPECIAL-183-55'>",
"<o base='string' data='bytes'>",
new HexData(this.decorator.name()).value(),
"</o>",
"<o base='string' data='bytes'>",
new HexData("<init>").value(),
"</o>",
"<o base='string' data='bytes'>",
new HexData("(Lorg/eolang/jeo/A;)V").value(),
"</o>",
"</o>"
)
)
);
}
Expand All @@ -349,23 +338,20 @@ private List<XmlInstruction> targetSpecial() {
private List<XmlInstruction> replacementSpecial() {
return Collections.singletonList(
new XmlInstruction(
new XMLDocument(
new StringBuilder()
.append("<o base=\"opcode\" name=\"INVOKESPECIAL-183-55\">")
.append("<o base=\"string\" data=\"bytes\">")
.append(
new DecoratorCompositionName(this.decorated, this.decorator).hex()
)
.append("</o>")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData("<init>").value())
.append("</o>")
.append("<o base=\"string\" data=\"bytes\">")
.append(new HexData("(I)V").value())
.append("</o>")
.append("</o>")
.toString()
).node().getFirstChild()
String.join(
"",
"<o base='opcode' name='INVOKESPECIAL-183-55'>",
"<o base='string' data='bytes'>",
new DecoratorCompositionName(this.decorated, this.decorator).hex(),
"</o>",
"<o base='string' data='bytes'>",
new HexData("<init>").value(),
"</o>",
"<o base='string' data='bytes'>",
new HexData("(I)V").value(),
"</o>",
"</o>"
)
)
);
}
Expand All @@ -377,7 +363,7 @@ private List<XmlInstruction> replacementSpecial() {
private Representation combine() {
return new EoRepresentation(
new XMLDocument(
this.skeleton(
this.combine(
this.decorator.toEO(),
new DecoratorCompositionName(this.decorated, this.decorator).value()
).toString()
Expand All @@ -387,12 +373,12 @@ private Representation combine() {

/**
* Skeleton.
* @param decor Decorator.
* @param skeleton Decorator.
* @param name Class name.
* @return Combined XMIR representation.
*/
private XML skeleton(final XML decor, final String name) {
final List<XML> roots = decor.nodes("/program");
private XML combine(final XML skeleton, final String name) {
final List<XML> roots = skeleton.nodes("/program");
final Node root = roots.get(0).node();
final NamedNodeMap attributes = root.getAttributes();
attributes.getNamedItem("name").setNodeValue(name);
Expand Down Expand Up @@ -454,11 +440,9 @@ private void handleRootObject(final Node root, final String bytename) {
if (base.getNodeValue().equals("seq")) {
final NodeList instructions = item.getChildNodes();
for (int inst = 0; inst < instructions.getLength(); ++inst) {
DecoratorPair.replaceArguments(
instructions.item(inst),
this.decorated.name(),
bytename
);
new XmlInstruction(
instructions.item(inst)
).replaceArguementsValues(this.decorated.name(), bytename);
}
}
}
Expand Down Expand Up @@ -526,11 +510,7 @@ private void replaceMethodContent(
final Collection<XmlInstruction> filtered = new ArrayList<>(0);
for (final XmlInstruction xmlinstr : tadam) {
final int codee = xmlinstr.code();
DecoratorPair.replaceArguments(
xmlinstr.node(),
old,
bytename
);
xmlinstr.replaceArguementsValues(old, bytename);
if (codee != Opcodes.RETURN && codee != Opcodes.IRETURN
&& codee != Opcodes.ALOAD) {
filtered.add(xmlinstr);
Expand All @@ -545,32 +525,5 @@ private void replaceMethodContent(
candidate.setInstructions(res);
}
}

/**
* Replace arguments.
* @param node Instruction.
* @param oldname Old class name.
* @param newname New class name.
* @todo #157:90min Handle arguments correctly during inlining optimization.
* Right now we just replace all arguments with the new class name.
* It's not correct, because we need to handle arguments correctly.
*/
private static void replaceArguments(
final Node node,
final String oldname,
final String newname
) {
final NodeList children = node.getChildNodes();
for (int index = 0; index < children.getLength(); ++index) {
final Node child = children.item(index);
if (child.getNodeName().equals("o")) {
final String old = new HexData(oldname).value();
final String content = child.getTextContent();
if (old.equals(content)) {
child.setTextContent(new HexData(newname).value());
}
}
}
}
}
}
Loading

1 comment on commit 53063c2

@0pdd
Copy link

@0pdd 0pdd commented on 53063c2 Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 161-5e9acc3a disappeared from src/main/java/org/eolang/jeo/improvement/ImprovementDistilledObjects.java), that's why I closed #175. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.