Skip to content

Commit

Permalink
feat(#165): leave decorator fields
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Nov 8, 2023
1 parent 1cda7a3 commit a1750bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.eolang.jeo.representation.xmir.XmlInstruction;
import org.eolang.jeo.representation.xmir.XmlMethod;
import org.eolang.jeo.representation.xmir.XmlProgram;
import org.objectweb.asm.Opcodes;
import org.w3c.dom.Node;

/**
Expand Down Expand Up @@ -379,7 +380,8 @@ private Representation combine() {
*/
private void handleClass(final XmlClass decor) {
final Node root = decor.node();
DecoratorPair.removeOldFields(root);
this.removeOldFields(root);
decor.replaceArguments(this.decorator.name(), this.combinedName());
DecoratorPair.removeOldConstructors(root);
final XmlClass clazz = new XmlProgram(this.decorated.toEO()).top();
clazz.fields().forEach(decor::withField);
Expand Down Expand Up @@ -415,11 +417,31 @@ private static void removeOldConstructors(final Node root) {
* It's not correct, because we need to handle fields correctly and probably remove only
* those fields that are used in the decorator.
*/
private static void removeOldFields(final Node root) {
private void removeOldFields(final Node root) {
new XmlClass(root).fields()
.stream()
.map(XmlField::node)
.forEach(root::removeChild);
.forEach(field -> this.handleField(root, field));
}

private void handleField(final Node root, final XmlField field) {
if (this.isInlinedField(field)) {
root.removeChild(field.node());
}
}

private boolean isInlinedField(final XmlField field) {
boolean inner = field.access() == (Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL);
final String descriptor = field.descriptor();
final String name = String.format("L%s;", this.decorated.name());
Logger.info(
this,
"Compare %s and %s, access modifier %d",
descriptor,
name,
field.access()
);
boolean same = descriptor.equals(name);
return inner && same;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void replaceArguments(final String old, final String replacement) {
* Class name.
* @return Name.
*/
String name() {
public String name() {
return String.valueOf(this.node.getAttributes().getNamedItem("name").getTextContent());
}

Expand Down

0 comments on commit a1750bd

Please sign in to comment.