Fixed Fernflower decompiler output for generic nested types (IDEA-141230)

https://github.com/JetBrains/intellij-community/pull/273
master
Sebastian Zarnekow 10 years ago committed by Roman Shevchenko
parent 32d7924fe0
commit e4e72404dc
  1. 78
      src/org/jetbrains/java/decompiler/struct/gen/generics/GenericMain.java
  2. 1
      test/org/jetbrains/java/decompiler/SingleClassesTest.java
  3. BIN
      testData/classes/pkg/TestParameterizedTypes$Inner.class
  4. BIN
      testData/classes/pkg/TestParameterizedTypes.class
  5. 11
      testData/results/TestParameterizedTypes.dec
  6. 26
      testData/src/pkg/TestParameterizedTypes.java

@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 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.
@ -18,7 +18,6 @@ package org.jetbrains.java.decompiler.struct.gen.generics;
import org.jetbrains.java.decompiler.code.CodeConstants;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
import org.jetbrains.java.decompiler.struct.StructClass;
import java.util.ArrayList;
import java.util.List;
@ -193,25 +192,59 @@ public class GenericMain {
}
else if (tp == CodeConstants.TYPE_OBJECT) {
StringBuilder buffer = new StringBuilder();
buffer.append(DecompilerContext.getImportCollector().getShortName(buildJavaClassName(type)));
appendClassName(type, buffer);
return buffer.toString();
}
throw new RuntimeException("Invalid type: " + type);
}
private static void appendClassName(GenericType type, StringBuilder buffer) {
List<GenericType> enclosingClasses = type.getEnclosingClasses();
if (enclosingClasses.isEmpty()) {
String name = type.value.replace('/', '.');
buffer.append(DecompilerContext.getImportCollector().getShortName(name));
}
else {
for (GenericType tp : enclosingClasses) {
if (buffer.length() == 0) {
buffer.append(DecompilerContext.getImportCollector().getShortName(tp.value));
}
else {
buffer.append(tp.value);
}
appendTypeArguments(tp, buffer);
buffer.append('.');
}
buffer.append(type.value);
}
appendTypeArguments(type, buffer);
}
private static void appendTypeArguments(GenericType type, StringBuilder buffer) {
if (!type.getArguments().isEmpty()) {
buffer.append("<");
buffer.append('<');
for (int i = 0; i < type.getArguments().size(); i++) {
if (i > 0) {
buffer.append(", ");
}
int wildcard = type.getWildcards().get(i);
if (wildcard != GenericType.WILDCARD_NO) {
buffer.append("?");
int wildcard = type.getWildcards().get(i);
switch (wildcard) {
case GenericType.WILDCARD_UNBOUND:
buffer.append('?');
break;
case GenericType.WILDCARD_EXTENDS:
buffer.append(" extends ");
buffer.append("? extends ");
break;
case GenericType.WILDCARD_SUPER:
buffer.append(" super ");
}
buffer.append("? super ");
break;
}
GenericType genPar = type.getArguments().get(i);
@ -219,31 +252,8 @@ public class GenericMain {
buffer.append(getGenericCastTypeName(genPar));
}
}
buffer.append(">");
}
return buffer.toString();
}
throw new RuntimeException("Invalid type: " + type);
}
private static String buildJavaClassName(GenericType type) {
String name = "";
for (GenericType tp : type.getEnclosingClasses()) {
name += tp.value + "$";
}
name += type.value;
String res = name.replace('/', '.');
if (res.contains("$")) {
StructClass cl = DecompilerContext.getStructContext().getClass(name);
if (cl == null || !cl.isOwn()) {
res = res.replace('$', '.');
}
buffer.append(">");
}
return res;
}
}

@ -60,4 +60,5 @@ public class SingleClassesTest extends SingleClassesTestBase {
@Test public void testInnerLocal() { doTest("pkg/TestInnerLocal"); }
@Test public void testInnerLocalPkg() { doTest("pkg/TestInnerLocalPkg"); }
@Test public void testInnerSignature() { doTest("pkg/TestInnerSignature"); }
@Test public void testParameterizedTypes() { doTest("pkg/TestParameterizedTypes"); }
}

@ -0,0 +1,11 @@
package pkg;
public abstract class TestParameterizedTypes<P> {
abstract TestParameterizedTypes<P>.Inner<String> getUnspecificInner();
abstract TestParameterizedTypes<Number>.Inner<String> getSpecificInner();
public abstract class Inner<I> {
}
}

@ -0,0 +1,26 @@
/*
* Copyright 2000-2015 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;
public abstract class TestParameterizedTypes<P> {
public abstract class Inner<I> { }
abstract Inner<String> getUnspecificInner();
abstract TestParameterizedTypes<Number>.Inner<String> getSpecificInner();
}
Loading…
Cancel
Save