|
| 1 | +/* |
| 2 | + * This file is part of SpongeAPI, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.obfuscation.util; |
| 26 | + |
| 27 | +import com.google.common.collect.Multimap; |
| 28 | +import org.spongepowered.despector.ast.SourceSet; |
| 29 | +import org.spongepowered.despector.ast.generic.ClassSignature; |
| 30 | +import org.spongepowered.despector.ast.generic.GenericClassTypeSignature; |
| 31 | +import org.spongepowered.despector.ast.generic.MethodSignature; |
| 32 | +import org.spongepowered.despector.ast.generic.TypeParameter; |
| 33 | +import org.spongepowered.despector.ast.generic.TypeSignature; |
| 34 | +import org.spongepowered.despector.ast.generic.TypeVariableSignature; |
| 35 | +import org.spongepowered.despector.ast.type.ClassEntry; |
| 36 | +import org.spongepowered.despector.ast.type.MethodEntry; |
| 37 | +import org.spongepowered.despector.ast.type.TypeEntry; |
| 38 | +import org.spongepowered.obfuscation.merge.data.MethodGroup; |
| 39 | + |
| 40 | +import java.util.HashSet; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.Set; |
| 43 | + |
| 44 | +public class MethodGroupBuilder { |
| 45 | + |
| 46 | + private final SourceSet set; |
| 47 | + private final Map<MethodEntry, MethodGroup> groups; |
| 48 | + private final Multimap<TypeEntry, TypeEntry> subtypes; |
| 49 | + |
| 50 | + private final Set<TypeEntry> handled = new HashSet<>(); |
| 51 | + |
| 52 | + public MethodGroupBuilder(SourceSet set, Map<MethodEntry, MethodGroup> groups, Multimap<TypeEntry, TypeEntry> subtypes) { |
| 53 | + this.set = set; |
| 54 | + this.groups = groups; |
| 55 | + this.subtypes = subtypes; |
| 56 | + } |
| 57 | + |
| 58 | + public void build() { |
| 59 | + for (TypeEntry type : this.set.getAllClasses()) { |
| 60 | + process(type); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void process(TypeEntry type) { |
| 65 | + if (this.handled.contains(type)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + this.handled.add(type); |
| 69 | + if (type instanceof ClassEntry) { |
| 70 | + TypeEntry supr = this.set.get(((ClassEntry) type).getSuperclassName()); |
| 71 | + if (supr != null) { |
| 72 | + process(supr); |
| 73 | + } |
| 74 | + } |
| 75 | + for (String intr : type.getInterfaces()) { |
| 76 | + TypeEntry intrface = this.set.get(intr); |
| 77 | + if (intrface != null) { |
| 78 | + process(intrface); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for (MethodEntry mth : type.getMethods()) { |
| 83 | + if (this.groups.get(mth) != null) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + MethodGroup group = new MethodGroup(mth); |
| 87 | + this.groups.put(mth, group); |
| 88 | + if (!mth.getName().equals("<init>") && !mth.getName().equals("<clinit>")) { |
| 89 | + for (TypeEntry sub : this.subtypes.get(type)) { |
| 90 | + discoverOverrides(sub, mth.getName(), mth.getDescription(), createSubtypeSignature(sub, type, mth.getMethodSignature()), group); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private MethodSignature createSubtypeSignature(TypeEntry sub, TypeEntry owner, MethodSignature sig) { |
| 97 | + GenericClassTypeSignature super_sig = null; |
| 98 | + ClassSignature clssig = sub.getSignature(); |
| 99 | + if (clssig.getSuperclassSignature().getName().equals(owner.getName())) { |
| 100 | + super_sig = clssig.getSuperclassSignature(); |
| 101 | + } else { |
| 102 | + for (GenericClassTypeSignature intr : clssig.getInterfaceSignatures()) { |
| 103 | + if (intr.getName().equals(owner.getName())) { |
| 104 | + super_sig = intr; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + if (super_sig == null) { |
| 109 | + throw new IllegalStateException(); |
| 110 | + } |
| 111 | + |
| 112 | + MethodSignature subsig = new MethodSignature(); |
| 113 | + subsig.setReturnType(sig.getReturnType()); |
| 114 | + subsig.getThrowsSignature().addAll(sig.getThrowsSignature()); |
| 115 | + subsig.getTypeParameters().addAll(sig.getTypeParameters()); |
| 116 | + outer: for (int i = 0; i < sig.getParameters().size(); i++) { |
| 117 | + TypeSignature n = sig.getParameters().get(i); |
| 118 | + if (n instanceof TypeVariableSignature) { |
| 119 | + String var = ((TypeVariableSignature) n).getIdentifier(); |
| 120 | + var = var.substring(1, var.length() - 1); |
| 121 | + int j = 0; |
| 122 | + for (; j < owner.getSignature().getParameters().size(); j++) { |
| 123 | + TypeParameter param = owner.getSignature().getParameters().get(j); |
| 124 | + if (param.getIdentifier().equals(var)) { |
| 125 | + subsig.getParameters().add(super_sig.getArguments().get(j).getSignature()); |
| 126 | + continue outer; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + subsig.getParameters().add(n); |
| 131 | + } |
| 132 | + return subsig; |
| 133 | + } |
| 134 | + |
| 135 | + private void discoverOverrides(TypeEntry next, String name, String desc, MethodSignature sig, MethodGroup group) { |
| 136 | + |
| 137 | + MethodEntry found = null; |
| 138 | + search: for (MethodEntry mth : next.getMethods()) { |
| 139 | + if (mth.getName().equals(name)) { |
| 140 | + if (mth.getDescription().equals(desc)) { |
| 141 | + MethodGroup current = this.groups.get(mth); |
| 142 | + if (current == null) { |
| 143 | + group.addMethod(mth); |
| 144 | + this.groups.put(mth, group); |
| 145 | + } else { |
| 146 | + mergeGroups(group, current); |
| 147 | + group.addMethod(mth); |
| 148 | + this.groups.put(mth, group); |
| 149 | + } |
| 150 | + found = mth; |
| 151 | + break; |
| 152 | + } |
| 153 | + if (mth.getMethodSignature().getParameters().size() != sig.getParameters().size()) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + for (int i = 0; i < mth.getMethodSignature().getParameters().size(); i++) { |
| 157 | + TypeSignature param = mth.getMethodSignature().getParameters().get(i); |
| 158 | + TypeSignature expected = sig.getParameters().get(i); |
| 159 | + if (!param.equals(expected)) { |
| 160 | + continue search; |
| 161 | + } |
| 162 | + } |
| 163 | + MethodGroup current = this.groups.get(mth); |
| 164 | + if (current == null) { |
| 165 | + group.addMethod(mth); |
| 166 | + this.groups.put(mth, group); |
| 167 | + } else { |
| 168 | + mergeGroups(group, current); |
| 169 | + } |
| 170 | + found = mth; |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if (found != null) { |
| 176 | + for (TypeEntry sub : this.subtypes.get(next)) { |
| 177 | + discoverOverrides(sub, found.getName(), found.getDescription(), createSubtypeSignature(sub, next, found.getMethodSignature()), group); |
| 178 | + } |
| 179 | + } else { |
| 180 | + for (TypeEntry sub : this.subtypes.get(next)) { |
| 181 | + discoverOverrides(sub, name, desc, createSubtypeSignature(sub, next, sig), group); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private void mergeGroups(MethodGroup a, MethodGroup b) { |
| 187 | + a.getMethods().addAll(b.getMethods()); |
| 188 | + for (MethodEntry mth : b.getMethods()) { |
| 189 | + this.groups.put(mth, a); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments