Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#344): Fix Several Problems With Constructors Transformation #362

Merged
merged 6 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Constructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ private String type(final AstNode node) {
result = this.type(((Reference) node).object());
} else if (node instanceof Duplicate) {
result = this.type(((Duplicate) node).origin());
} else if (node instanceof Labeled) {
result = this.type(((Labeled) node).origin());
} else {
throw new IllegalStateException(
String.format(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/eolang/opeo/ast/LocalVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class LocalVariable implements AstNode, Typed {
/**
* The prefix of the variable.
*/
private static final String PREFIX = "local";
private static final String PREFIX = "local-";

/**
* The identifier of the variable.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/eolang/opeo/compilation/XmirParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public AstNode parse(final XmlNode node) {
result = new FieldRetrieval(node, this);
} else if (".write-field".equals(base)) {
result = new FieldAssignment(node, this);
} else if (base.contains("local")) {
} else if (base.startsWith("local-")) {
result = new LocalVariable(node);
} else if (".new".equals(base)) {
result = new Constructor(node, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
import java.util.Collections;
import java.util.List;
import org.eolang.opeo.ast.AstNode;
import org.eolang.opeo.ast.Attributes;
import org.eolang.opeo.ast.Constructor;
import org.eolang.opeo.ast.Duplicate;
import org.eolang.opeo.ast.Labeled;
import org.eolang.opeo.ast.NewAddress;
import org.eolang.opeo.ast.Super;
import org.eolang.opeo.ast.This;
import org.eolang.opeo.decompilation.DecompilerState;
Expand Down Expand Up @@ -84,13 +88,43 @@ public void handle(final DecompilerState state) {
state.stack().push(
new Super(target, args, descriptor, type, name)
);
} else if (this.isNewAddress(target)) {
state.stack().push(
new Constructor(
target,
new Attributes().descriptor(descriptor).interfaced(interfaced),
args
)
);
} else {
state.stack().push(
new Super(target, args, descriptor, type, name)
);
}
}

/**
* Check if the target is a new address.
* @param target Where to check if it is a new address.
* @return True if the target is a new address.
* @todo #344:90min Remove ugly {{@link #isNewAddress(AstNode)}} method.
* We need to find a better algorithm to decompile Constructors and Super calls.
* The usage of 'instanceof' tells us about pure design decisions made before.
*/
private boolean isNewAddress(final AstNode target) {
final boolean result;
if (target instanceof NewAddress) {
result = true;
} else if (target instanceof Duplicate) {
result = this.isNewAddress(((Duplicate) target).origin());
} else if (target instanceof Labeled) {
result = this.isNewAddress(((Labeled) target).origin());
} else {
result = false;
}
return result;
}

/**
* Check if the node is "this".
* @param candidate Node to check
Expand All @@ -102,6 +136,8 @@ private static boolean isThis(final AstNode candidate) {
result = true;
} else if (candidate instanceof Labeled) {
result = InvokespecialHandler.isThis(((Labeled) candidate).origin());
} else if (candidate instanceof Duplicate) {
result = InvokespecialHandler.isThis(((Duplicate) candidate).origin());
} else {
result = false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/it/JeoAndOpeoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ void compilesDecompiled(final String path) {
"xmir/disassembled/Main.xmir",
"xmir/disassembled/UndertowWebServerFactoryCustomizer$ServerOptions.xmir",
"xmir/disassembled/MutableCoercionConfig.xmir",
"xmir/disassembled/WebProperties$Resources$Chain$Strategy$Content.xmir"
"xmir/disassembled/WebProperties$Resources$Chain$Strategy$Content.xmir",
"xmir/disassembled/OAuth2ClientRegistrationRepositoryConfiguration.xmir",
"xmir/disassembled/DefaultRouterFunctionSpec.xmir",
"xmir/disassembled/SpringBootExceptionHandler$LoggedExceptionHandlerThreadLocal.xmir"
})
void decompilesCompilesAndKeepsTheSameInstructions(final String path) throws Exception {
final XMLDocument original = new XMLDocument(new BytesOf(new ResourceOf(path)).asBytes());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/eolang/opeo/ast/LocalVariableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void convertsToXmir() throws ImpossibleModificationException {
MatcherAssert.assertThat(
"We expect the xmir variable to be equal to <o base='local1'/>, but it wasn't",
new Xembler(new LocalVariable(1, Type.INT_TYPE).toXmir()).xml(),
XhtmlMatchers.hasXPath("./o[@base='local1']")
XhtmlMatchers.hasXPath("./o[@base='local-1']")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class VariableAssignmentTest {
private static final String XMIR = String.join(
"\n",
"<o base='.write-local-var'>",
" <o base='local1'>",
" <o base='local-1'>",
" <o base='string' data='bytes'>64 65 73 63 72 69 70 74 6F 72 3D 44 7C 74 79 70 65 3D 6C 6F 63 61 6C</o>",
" </o>",
" <o base='int' data='bytes'>00 00 00 00 00 00 00 02</o>",
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/xmir/decompiled/Enhancer$3.xmir
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 6F 72 67 2E 73 70 72 69 6E 67 66 72 61 6D 65 77 6F 72 6B 2E 63 67 6C 69 62 2E 70 72 6F 78 79 2E 45 6E 68 61 6E 63 65 72 24 33</o>
</o>
</o>
<o base="local1" line="999">
<o base="local-1" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 4C 6F 72 67 2F 73 70 72 69 6E 67 66 72 61 6D 65 77 6F 72 6B 2F 63 67 6C 69 62 2F 70 72 6F 78 79 2F 45 6E 68 61 6E 63 65 72 3B 7C 74 79 70 65 3D 6C 6F 63 61 6C</o>
</o>
</o>
Expand All @@ -91,7 +91,7 @@
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 6F 72 67 2E 73 70 72 69 6E 67 66 72 61 6D 65 77 6F 72 6B 2E 63 67 6C 69 62 2E 70 72 6F 78 79 2E 45 6E 68 61 6E 63 65 72 24 33</o>
</o>
</o>
<o base="local2" line="999">
<o base="local-2" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 4C 6F 72 67 2F 73 70 72 69 6E 67 66 72 61 6D 65 77 6F 72 6B 2F 63 67 6C 69 62 2F 63 6F 72 65 2F 43 6F 64 65 45 6D 69 74 74 65 72 3B 7C 74 79 70 65 3D 6C 6F 63 61 6C</o>
</o>
</o>
Expand Down Expand Up @@ -140,7 +140,7 @@
<o base=".access$100" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 28 49 29 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 7C 69 6E 74 65 72 66 61 63 65 64 3D 66 61 6C 73 65 7C 6E 61 6D 65 3D 61 63 63 65 73 73 24 31 30 30 7C 6F 77 6E 65 72 3D 6F 72 67 2F 73 70 72 69 6E 67 66 72 61 6D 65 77 6F 72 6B 2F 63 67 6C 69 62 2F 70 72 6F 78 79 2F 45 6E 68 61 6E 63 65 72 7C 74 79 70 65 3D 73 74 61 74 69 63</o>
<o base="org.springframework.cglib.proxy.Enhancer" line="999"/>
<o base="local1" line="999">
<o base="local-1" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 49 7C 74 79 70 65 3D 6C 6F 63 61 6C</o>
</o>
</o>
Expand All @@ -158,7 +158,7 @@
</o>
</o>
</o>
<o base="local2" line="999">
<o base="local-2" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 4C 6F 72 67 2F 73 70 72 69 6E 67 66 72 61 6D 65 77 6F 72 6B 2F 61 73 6D 2F 4C 61 62 65 6C 3B 7C 74 79 70 65 3D 6C 6F 63 61 6C</o>
</o>
</o>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<o base="$" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 6F 72 67 2E 73 70 72 69 6E 67 66 72 61 6D 65 77 6F 72 6B 2E 75 74 69 6C 2E 53 74 72 65 61 6D 55 74 69 6C 73 24 4E 6F 6E 43 6C 6F 73 69 6E 67 4F 75 74 70 75 74 53 74 72 65 61 6D</o>
</o>
<o base="local1" line="999">
<o base="local-1" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 4C 6A 61 76 61 2F 69 6F 2F 4F 75 74 70 75 74 53 74 72 65 61 6D 3B 7C 74 79 70 65 3D 6C 6F 63 61 6C</o>
</o>
</o>
Expand Down Expand Up @@ -98,13 +98,13 @@
</o>
</o>
</o>
<o base="local1" line="999">
<o base="local-1" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 5B 42 7C 74 79 70 65 3D 6C 6F 63 61 6C</o>
</o>
<o base="local2" line="999">
<o base="local-2" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 49 7C 74 79 70 65 3D 6C 6F 63 61 6C</o>
</o>
<o base="local3" line="999">
<o base="local-3" line="999">
<o base="string" data="bytes" line="999">64 65 73 63 72 69 70 74 6F 72 3D 49 7C 74 79 70 65 3D 6C 6F 63 61 6C</o>
</o>
</o>
Expand Down
Loading
Loading