fixed lambda parameter names already used in the context

master
Egor.Ushakov 8 years ago
parent d40f673689
commit 6a7763922b
  1. 5
      src/org/jetbrains/java/decompiler/main/collectors/VarNamesCollector.java
  2. 14
      src/org/jetbrains/java/decompiler/main/rels/NestedClassProcessor.java
  3. 4
      src/org/jetbrains/java/decompiler/modules/decompiler/vars/VarProcessor.java
  4. 1
      test/org/jetbrains/java/decompiler/SingleClassesTest.java
  5. BIN
      testData/classes/pkg/TestLambdaParams.class
  6. 48
      testData/results/TestLambdaParams.dec
  7. 28
      testData/src/pkg/TestLambdaParams.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2000-2015 JetBrains s.r.o. * Copyright 2000-2017 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,6 +15,7 @@
*/ */
package org.jetbrains.java.decompiler.main.collectors; package org.jetbrains.java.decompiler.main.collectors;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -24,7 +25,7 @@ public class VarNamesCollector {
public VarNamesCollector() { } public VarNamesCollector() { }
public VarNamesCollector(Set<String> setNames) { public VarNamesCollector(Collection<String> setNames) {
usedNames.addAll(setNames); usedNames.addAll(setNames);
} }

@ -142,19 +142,25 @@ public class NestedClassProcessor {
if (expr.type == Exprent.EXPRENT_NEW) { if (expr.type == Exprent.EXPRENT_NEW) {
NewExprent new_expr = (NewExprent)expr; NewExprent new_expr = (NewExprent)expr;
VarNamesCollector enclosingCollector = new VarNamesCollector(enclosingMethod.varproc.getVarNames());
if (new_expr.isLambda() && lambda_class_type.equals(new_expr.getNewType())) { if (new_expr.isLambda() && lambda_class_type.equals(new_expr.getNewType())) {
InvocationExprent inv_dynamic = new_expr.getConstructor(); InvocationExprent inv_dynamic = new_expr.getConstructor();
int param_index = is_static_lambda_content ? 0 : 1; int param_index = is_static_lambda_content ? 0 : 1;
int varIndex = is_static_lambda_content ? 0 : 1; int varIndex = is_static_lambda_content ? 0 : 1;
for (int i = 0; i < vars_count; ++i) { for (int i = 0; i < md_content.params.length; ++i) {
VarVersionPair varVersion = new VarVersionPair(varIndex, 0);
if (i < vars_count) {
Exprent param = inv_dynamic.getLstParameters().get(param_index + i); Exprent param = inv_dynamic.getLstParameters().get(param_index + i);
if (param.type == Exprent.EXPRENT_VAR) { if (param.type == Exprent.EXPRENT_VAR) {
VarVersionPair pair = new VarVersionPair((VarExprent)param); mapNewNames.put(varVersion, enclosingMethod.varproc.getVarName(new VarVersionPair((VarExprent)param)));
String name = enclosingMethod.varproc.getVarName(pair); }
mapNewNames.put(new VarVersionPair(varIndex, 0), name); }
else {
mapNewNames.put(varVersion, enclosingCollector.getFreeName(method.varproc.getVarName(varVersion)));
} }
varIndex += md_content.params[i].stackSize; varIndex += md_content.params[i].stackSize;

@ -115,6 +115,10 @@ public class VarProcessor {
mapVarNames.put(pair, name); mapVarNames.put(pair, name);
} }
public Collection<String> getVarNames() {
return mapVarNames != null ? mapVarNames.values() : Collections.emptySet();
}
public int getVarFinal(VarVersionPair pair) { public int getVarFinal(VarVersionPair pair) {
return varVersions == null ? VarTypeProcessor.VAR_FINAL : varVersions.getVarFinal(pair); return varVersions == null ? VarTypeProcessor.VAR_FINAL : varVersions.getVarFinal(pair);
} }

@ -110,6 +110,7 @@ public class SingleClassesTest {
@Test public void testSwitchOnEnum() { doTest("pkg/TestSwitchOnEnum");} @Test public void testSwitchOnEnum() { doTest("pkg/TestSwitchOnEnum");}
//@Test public void TestSwitchOnStrings() { doTest("pkg/TestSwitchOnStrings");} //@Test public void TestSwitchOnStrings() { doTest("pkg/TestSwitchOnStrings");}
@Test public void testVarArgCalls() { doTest("pkg/TestVarArgCalls"); } @Test public void testVarArgCalls() { doTest("pkg/TestVarArgCalls"); }
@Test public void testLambdaParams() { doTest("pkg/TestLambdaParams"); }
private void doTest(String testFile, String... companionFiles) { private void doTest(String testFile, String... companionFiles) {
ConsoleDecompiler decompiler = fixture.getDecompiler(); ConsoleDecompiler decompiler = fixture.getDecompiler();

@ -0,0 +1,48 @@
package pkg;
import java.util.function.Function;
public class TestLambdaParams {
public static void toCollection(Object var0) {
Object var1 = null;// 23
Function var2 = (var1x) -> {
return var0;
};// 24
Function var3 = (var1x) -> {
return var1;
};// 25
Function var4 = (var0x) -> {
return var0x;
};// 26
}// 27
}
class 'pkg/TestLambdaParams' {
method 'lambda$toCollection$0 (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;' {
1 8
}
method 'lambda$toCollection$1 (Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object;' {
1 11
}
method 'lambda$toCollection$2 (Ljava/lang/Object;)Ljava/lang/Object;' {
1 14
}
method 'toCollection (Ljava/lang/Object;)V' {
0 6
1 6
8 9
f 12
15 15
17 16
}
}
Lines mapping:
23 <-> 7
24 <-> 10
25 <-> 13
26 <-> 16
27 <-> 17

@ -0,0 +1,28 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pkg;
import java.util.function.BinaryOperator;
import java.util.function.Function;
public class TestLambdaParams {
public static void toCollection(Object collectionFactory) {
Class a = null;
Function f = r1 -> collectionFactory;
Function f1 = r1 -> a;
Function f2 = r1 -> r1;
}
}
Loading…
Cancel
Save