Skip to content

Commit

Permalink
Don't remap usages of the var language feature
Browse files Browse the repository at this point in the history
This prevents code like `var example = get();` from being remapped to
code like `FetchedValue example = get();`.
  • Loading branch information
jamierocks committed May 31, 2021
1 parent 0277125 commit 409123d
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void remapType(SimpleName node, ITypeBinding binding) {
String qualifiedName = (mapping != null ? mapping.getFullDeobfuscatedName().replace('/', '.') : binding.getBinaryName()).replace('$', '.');
String newName = this.importRewrite.addImport(qualifiedName, this.importStack.peek());

if (!node.getIdentifier().equals(newName)) {
if (!node.getIdentifier().equals(newName) && !node.isVar()) {
if (newName.indexOf('.') == -1) {
this.context.createASTRewrite().set(node, SimpleName.IDENTIFIER_PROPERTY, newName, null);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SimpleRemapperVisitor extends ASTVisitor {
}

final void updateIdentifier(SimpleName node, String newName) {
if (!node.getIdentifier().equals(newName)) {
if (!node.getIdentifier().equals(newName) && !node.isVar()) {
this.context.createASTRewrite().set(node, SimpleName.IDENTIFIER_PROPERTY, newName, null);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/cadixdev/mercury/test/RemappingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.cadixdev.lorenz.io.MappingsReader;
import org.cadixdev.mercury.Mercury;
import org.cadixdev.mercury.remapper.MercuryRemapper;
import org.eclipse.jdt.core.JavaCore;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -97,6 +98,7 @@ void remap() throws Exception {

// Run Mercury
final Mercury mercury = new Mercury();
mercury.setSourceCompatibility(JavaCore.VERSION_11);
mercury.getProcessors().add(MercuryRemapper.create(mappings));
mercury.setFlexibleAnonymousClassMemberLookups(true);
mercury.rewrite(in, out);
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/a/OverrideChild.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class OverrideChild extends OverrideParent<String> {

@Override
public String abc() {
return "Hello, World!";
var result = "Hello, World!";
return result;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/a/com/example/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
public class ImportTest {

public void test() {
OtherClass otherClass = new OtherClass();
AnotherClass anotherClass = new AnotherClass();
var otherClass = new OtherClass();
var anotherClass = new AnotherClass();
}

}
3 changes: 2 additions & 1 deletion src/test/resources/b/OverrideChild.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class OverrideChild extends OverrideParent<String> {

@Override
public String get() {
return "Hello, World!";
var result = "Hello, World!";
return result;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/b/net/example/ImportTestNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
public class ImportTestNew {

public void test() {
OtherClass otherClass = new OtherClass();
AnotherClass anotherClass = new AnotherClass();
var otherClass = new OtherClass();
var anotherClass = new AnotherClass();
}

}

0 comments on commit 409123d

Please sign in to comment.