From 7cf7bec4b68a70b7da6aed3cd41a375e67364626 Mon Sep 17 00:00:00 2001 From: Alexandru-Constantin Bledea Date: Mon, 7 Mar 2016 21:00:21 +0200 Subject: [PATCH] prevent static field name clashes in local methods --- .../modules/decompiler/exps/FieldExprent.java | 8 ++- .../java/decompiler/util/StructUtils.java | 65 ++++++++++++++++++ .../java/decompiler/SingleClassesTest.java | 3 +- .../classes/pkg/TestStaticNameClash.class | Bin 0 -> 443 bytes testData/results/TestStaticNameClash.dec | 20 ++++++ testData/src/pkg/TestStaticNameClash.java | 15 ++++ 6 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 src/org/jetbrains/java/decompiler/util/StructUtils.java create mode 100644 testData/classes/pkg/TestStaticNameClash.class create mode 100644 testData/results/TestStaticNameClash.dec create mode 100644 testData/src/pkg/TestStaticNameClash.java diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java index 4ac8123..28002ff 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java @@ -28,9 +28,9 @@ import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor; import org.jetbrains.java.decompiler.struct.gen.VarType; import org.jetbrains.java.decompiler.struct.match.MatchEngine; import org.jetbrains.java.decompiler.struct.match.MatchNode; -import org.jetbrains.java.decompiler.struct.match.IMatchable.MatchProperties; import org.jetbrains.java.decompiler.struct.match.MatchNode.RuleValue; import org.jetbrains.java.decompiler.util.InterpreterUtil; +import org.jetbrains.java.decompiler.util.StructUtils; import org.jetbrains.java.decompiler.util.TextUtil; import java.util.ArrayList; @@ -84,13 +84,17 @@ public class FieldExprent extends Exprent { return new FieldExprent(name, classname, isStatic, instance == null ? null : instance.copy(), descriptor, bytecode); } + private boolean isAmbiguous() { + return StructUtils.getCurrentMethodLocalVariableNames().contains(name); + } + @Override public TextBuffer toJava(int indent, BytecodeMappingTracer tracer) { TextBuffer buf = new TextBuffer(); if (isStatic) { ClassNode node = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE); - if (node == null || !classname.equals(node.classStruct.qualifiedName)) { + if (node == null || !classname.equals(node.classStruct.qualifiedName) || isAmbiguous()) { buf.append(DecompilerContext.getImportCollector().getShortName(ExprProcessor.buildJavaClassName(classname))); buf.append("."); } diff --git a/src/org/jetbrains/java/decompiler/util/StructUtils.java b/src/org/jetbrains/java/decompiler/util/StructUtils.java new file mode 100644 index 0000000..d7dad68 --- /dev/null +++ b/src/org/jetbrains/java/decompiler/util/StructUtils.java @@ -0,0 +1,65 @@ +/* + * Copyright 2000-2016 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 org.jetbrains.java.decompiler.util; + +import org.jetbrains.java.decompiler.main.DecompilerContext; +import org.jetbrains.java.decompiler.main.rels.MethodWrapper; +import org.jetbrains.java.decompiler.struct.StructMember; +import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute; +import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTableAttribute; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.jetbrains.java.decompiler.main.DecompilerContext.CURRENT_METHOD_WRAPPER; +import static org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute.ATTRIBUTE_LOCAL_VARIABLE_TABLE; + +/** + * @author Alexandru-Constantin Bledea + * @since March 07, 2016 + */ +public final class StructUtils { + + private StructUtils() { + } + + /** + * @return the local variables of the current method + */ + public static List getCurrentMethodLocalVariableNames() { + final MethodWrapper method = (MethodWrapper) DecompilerContext.getProperty(CURRENT_METHOD_WRAPPER); + if (null == method) { + return Collections.emptyList(); + } + return getLocalVariables(method.methodStruct); + } + + /** + * @param structMember the struct member from which to extract the local variables + * @return the local variables of the struct member + */ + public static List getLocalVariables(final StructMember structMember) { + final VBStyleCollection methodStruct = structMember.getAttributes(); + final StructGeneralAttribute generalAttribute = methodStruct.getWithKey(ATTRIBUTE_LOCAL_VARIABLE_TABLE); + if (generalAttribute instanceof StructLocalVariableTableAttribute) { + final StructLocalVariableTableAttribute table = (StructLocalVariableTableAttribute) generalAttribute; + return Collections.unmodifiableList(new ArrayList<>(table.getMapVarNames().values())); + } + return Collections.emptyList(); + } + +} diff --git a/test/org/jetbrains/java/decompiler/SingleClassesTest.java b/test/org/jetbrains/java/decompiler/SingleClassesTest.java index 5647ab8..e39bb3a 100644 --- a/test/org/jetbrains/java/decompiler/SingleClassesTest.java +++ b/test/org/jetbrains/java/decompiler/SingleClassesTest.java @@ -82,6 +82,7 @@ public class SingleClassesTest { @Test public void testMethodReferenceSameName() { doTest("pkg/TestMethodReferenceSameName"); } @Test public void testMethodReferenceLetterClass() { doTest("pkg/TestMethodReferenceLetterClass"); } @Test public void testMemberAnnotations() { doTest("pkg/TestMemberAnnotations"); } + @Test public void testStaticNameClash() { doTest("pkg/TestStaticNameClash"); } protected void doTest(String testFile, String... companionFiles) { ConsoleDecompiler decompiler = fixture.getDecompiler(); @@ -128,4 +129,4 @@ public class SingleClassesTest { return files; } -} \ No newline at end of file +} diff --git a/testData/classes/pkg/TestStaticNameClash.class b/testData/classes/pkg/TestStaticNameClash.class new file mode 100644 index 0000000000000000000000000000000000000000..16a1da3eaa4b6d04a9ca9552b68470b096425ceb GIT binary patch literal 443 zcmZ`#%T9wp6g@*J6pB{S`dGTFE@YuQjhiN}NHwA9e!vNxN`b)C#LqG=OkDT@ew6VJ z7;B<#=H7emoX5=j$Lkw_bL_b=;XCMH$HuOWAw#dMic(bSk--la{DB7<&(k1MmCVx% z2J2GhQe820M#nP-b6ngBhCwKEFI)i%BY7iix$_VYAynsD5YnM)|0d4fD_u30oH)wLOi2)%F c^=DLD@-vJz*(DE6Gl}q1p!KJev^B8(3F 7 +13 <-> 8 diff --git a/testData/src/pkg/TestStaticNameClash.java b/testData/src/pkg/TestStaticNameClash.java new file mode 100644 index 0000000..ced8f12 --- /dev/null +++ b/testData/src/pkg/TestStaticNameClash.java @@ -0,0 +1,15 @@ +package pkg; + +/** + * @author Alexandru-Constantin Bledea + * @since March 07, 2016 + */ +public class TestStaticNameClash { + + public static String property; + + public static void setProperty(final String property) { + TestStaticNameClash.property = property; + } + +}