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

Fix keeping generics that are unavailable in the current context #322

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,32 @@ else if (!bounds.containsKey(from)) {
start = (newNode.access & CodeConstants.ACC_STATIC) == 0 ? 1 : 0;
}
}

Set<VarType> commonGenerics = new HashSet<>();
ClassNode currentNode = DecompilerContext.getContextProperty(DecompilerContext.CURRENT_CLASS_NODE);
MethodWrapper methodWrapper = DecompilerContext.getContextProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
if (methodWrapper != null) {
StructMethod currentMethod = DecompilerContext.getContextProperty(DecompilerContext.CURRENT_METHOD_WRAPPER).methodStruct;
if (newNode != null && currentNode != null && !desc.hasModifier(CodeConstants.ACC_STATIC) && !currentMethod.hasModifier(CodeConstants.ACC_STATIC)) {
List<ClassNode> parents = new ArrayList<>();
ClassNode search = currentNode;
while (search != null) {
parents.add(search);
search = (search.access & CodeConstants.ACC_STATIC) == 0 ? search.parent : null;
}

search = newNode;
while (search != null) {
if (parents.contains(search) && search.classStruct.getSignature() != null) {
commonGenerics.addAll(search.classStruct.getSignature().fparameters
.stream()
.map(generic -> GenericType.parse("T" + generic + ";"))
.collect(Collectors.toList()));
}
search = (search.access & CodeConstants.ACC_STATIC) == 0 ? search.parent : null;
}
}
}

int j = 0;
for (int i = start; i < lstParameters.size(); ++i) {
Expand Down Expand Up @@ -485,7 +511,7 @@ else if (!bounds.containsKey(from)) {
GenericType genArgType = (GenericType)argtype;

genParamType.mapGenVarsTo(genArgType, tempMap);
GenericType.cleanLoweredGenericTypes(tempMap, genParamType, genArgType);
GenericType.cleanLoweredGenericTypes(tempMap, genParamType, genArgType, commonGenerics);
tempMap.forEach((from, to) -> {
if (!excluded.contains(from)) {
paramGenerics.add(from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,14 +561,14 @@ public List<GenericType> getAllGenericVars() {
// In certain cases, we after we map generic variables we *don't* want to lower known generics to wildcards.
// This removes mappings where, for example, Type<T> is being mapped to Type<?>. This can't be done generally as there are
// cases where we *do* want to map to a wildcard- such as fields.
public static void cleanLoweredGenericTypes(Map<VarType, VarType> tempMap, GenericType type1, GenericType type2) {
public static void cleanLoweredGenericTypes(Map<VarType, VarType> tempMap, GenericType type1, GenericType type2, Set<VarType> canClean) {
if (type1.getArguments().size() == type2.getArguments().size()) {
for (int k = 0; k < type1.getArguments().size(); k++) {
VarType arg1 = type1.getArguments().get(k);
VarType arg2 = type2.getArguments().get(k);

// Don't lower the current type from a generic to a wildcard
if (arg1 != null && arg2 == null) {
if ((canClean == null || canClean.contains(arg1)) && arg1 != null && arg2 == null) {
tempMap.remove(arg1);
}
}
Expand Down Expand Up @@ -657,7 +657,7 @@ public static VarType getGenericSuperType(VarType derivedType, VarType superType

if (derivedType.isGeneric() && dcls.getSignature() != null) {
dcls.getSignature().genericType.mapGenVarsTo((GenericType)derivedType, tempMap);
cleanLoweredGenericTypes(tempMap, dcls.getSignature().genericType, (GenericType)derivedType);
cleanLoweredGenericTypes(tempMap, dcls.getSignature().genericType, (GenericType)derivedType, null);
// Given MyClass<T extends MyClass<T>> implements MyInterface<T>
// converting MyClass<?> to MyInterface should produce MyInterface<MyClass<?>> not MyInterface<?>
for (int i = 0; i < dcls.getSignature().fparameters.size(); ++i) {
Expand Down