mirror of https://github.com/FabricMC/yarn.git
Skip synthetic params by guess (no attribute available unfortunately) (#4)
* Skip synthetic params by guess (no attribute available unfortunately) Signed-off-by: liach <liach@users.noreply.github.com> * Fix code style issue Signed-off-by: liach <liach@users.noreply.github.com> Co-authored-by: liach <liach@users.noreply.github.com>
This commit is contained in:
parent
b79be3aea4
commit
50625c80a7
|
@ -42,6 +42,7 @@ public class ClassBuilder {
|
|||
|
||||
private Signatures.ClassSignature signature;
|
||||
private boolean enumClass;
|
||||
private boolean instanceInner = false;
|
||||
|
||||
public ClassBuilder(MappingsStore mappings, ClassNode classNode, Function<String, Collection<String>> superGetter) {
|
||||
this.mappings = mappings;
|
||||
|
@ -50,9 +51,12 @@ public class ClassBuilder {
|
|||
this.builder = setupBuilder();
|
||||
addInterfaces();
|
||||
addDirectAnnotations();
|
||||
addJavaDoc();
|
||||
}
|
||||
|
||||
public void addMembers() {
|
||||
addMethods();
|
||||
addFields();
|
||||
addJavaDoc();
|
||||
}
|
||||
|
||||
public static ClassName parseInternalName(String internalName) {
|
||||
|
@ -150,6 +154,7 @@ public class ClassBuilder {
|
|||
if (method.name.equals("<clinit>")) {
|
||||
continue;
|
||||
}
|
||||
int formalParamStartIndex = 0;
|
||||
if (enumClass) {
|
||||
// Skip enum sugar methods
|
||||
if (method.name.equals("values") && method.desc.equals("()[L" + classNode.name + ";")) {
|
||||
|
@ -158,8 +163,16 @@ public class ClassBuilder {
|
|||
if (method.name.equals("valueOf") && method.desc.equals("(Ljava/lang/String;)L" + classNode.name + ";")) {
|
||||
continue;
|
||||
}
|
||||
if (method.name.equals("<init>")) {
|
||||
formalParamStartIndex = 2; // 0 String 1 int
|
||||
}
|
||||
}
|
||||
builder.addMethod(new MethodBuilder(mappings, classNode, method, superGetter).build());
|
||||
if (instanceInner) {
|
||||
if (method.name.equals("<init>")) {
|
||||
formalParamStartIndex = 1; // 0 this$0
|
||||
}
|
||||
}
|
||||
builder.addMethod(new MethodBuilder(mappings, classNode, method, superGetter, formalParamStartIndex).build());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -202,6 +215,7 @@ public class ClassBuilder {
|
|||
classBuilder.builder.addModifiers(javax.lang.model.element.Modifier.STATIC);
|
||||
} else {
|
||||
classBuilder.builder.addModifiers(new ModifierBuilder(innerClassNode.access).getModifiers(classBuilder.enumClass ? ModifierBuilder.Type.ENUM : ModifierBuilder.Type.CLASS));
|
||||
if (!Modifier.isStatic(innerClassNode.access)) classBuilder.instanceInner = true;
|
||||
}
|
||||
innerClasses.add(classBuilder);
|
||||
}
|
||||
|
|
|
@ -16,11 +16,6 @@
|
|||
|
||||
package net.fabricmc.mappingpoet;
|
||||
|
||||
import com.squareup.javapoet.JavaFile;
|
||||
import com.squareup.javapoet.TypeSpec;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
@ -36,11 +31,15 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
import com.squareup.javapoet.JavaFile;
|
||||
import com.squareup.javapoet.TypeSpec;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -111,7 +110,7 @@ public class Main {
|
|||
continue;
|
||||
}
|
||||
|
||||
try(InputStream is = jarFile.getInputStream(entry)) {
|
||||
try (InputStream is = jarFile.getInputStream(entry)) {
|
||||
ClassReader reader = new ClassReader(is);
|
||||
ClassNode classNode = new ClassNode();
|
||||
reader.accept(classNode, 0);
|
||||
|
@ -139,7 +138,7 @@ public class Main {
|
|||
Function<String, Collection<String>> superGetter = k -> supers.getOrDefault(k, Collections.emptyList());
|
||||
classes.forEach(node -> classNodeConsumer.accept(superGetter, node));
|
||||
}
|
||||
|
||||
|
||||
private static boolean isDigit(char ch) {
|
||||
return ch >= '0' && ch <= '9';
|
||||
}
|
||||
|
@ -151,8 +150,10 @@ public class Main {
|
|||
int lastSearch = name.length();
|
||||
while (lastSearch != -1) {
|
||||
lastSearch = name.lastIndexOf('$', lastSearch - 1);
|
||||
if (isDigit(name.charAt(lastSearch + 1))) // names starting with digit is illegal java
|
||||
// names starting with digit is illegal java
|
||||
if (isDigit(name.charAt(lastSearch + 1))) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,6 +167,7 @@ public class Main {
|
|||
existingClasses.get(parentClass).addInnerClass(classBuilder);
|
||||
}
|
||||
|
||||
classBuilder.addMembers();
|
||||
existingClasses.put(name, classBuilder);
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.util.Arrays;
|
|||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
@ -50,14 +51,16 @@ public class MethodBuilder {
|
|||
private final MethodNode methodNode;
|
||||
private final MethodSpec.Builder builder;
|
||||
private final Function<String, Collection<String>> superGetter;
|
||||
private final int formalParamStartIndex;
|
||||
|
||||
private Signatures.MethodSignature signature;
|
||||
|
||||
public MethodBuilder(MappingsStore mappings, ClassNode classNode, MethodNode methodNode, Function<String, Collection<String>> superGetter) {
|
||||
public MethodBuilder(MappingsStore mappings, ClassNode classNode, MethodNode methodNode, Function<String, Collection<String>> superGetter, int formalParamStartIndex) {
|
||||
this.mappings = mappings;
|
||||
this.classNode = classNode;
|
||||
this.methodNode = methodNode;
|
||||
this.superGetter = superGetter;
|
||||
this.formalParamStartIndex = formalParamStartIndex;
|
||||
this.builder = createBuilder();
|
||||
addJavaDoc();
|
||||
addDirectAnnotations();
|
||||
|
@ -184,11 +187,7 @@ public class MethodBuilder {
|
|||
List<ParamType> paramTypes = new ArrayList<>();
|
||||
boolean instanceMethod = !builder.modifiers.contains(Modifier.STATIC);
|
||||
Set<String> usedParamNames = new HashSet<>(RESERVED_KEYWORDS);
|
||||
if (signature != null) {
|
||||
getGenericParams(paramTypes, instanceMethod, usedParamNames);
|
||||
} else {
|
||||
getDescParams(paramTypes, instanceMethod, usedParamNames);
|
||||
}
|
||||
getParams(paramTypes, instanceMethod, usedParamNames);
|
||||
|
||||
List<AnnotationNode>[] visibleParameterAnnotations = methodNode.visibleParameterAnnotations;
|
||||
List<AnnotationNode>[] invisibleParameterAnnotations = methodNode.invisibleParameterAnnotations;
|
||||
|
@ -206,20 +205,10 @@ public class MethodBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
private void getGenericParams(List<ParamType> paramTypes, boolean instance, Set<String> usedParamNames) {
|
||||
int slot = instance ? 1 : 0;
|
||||
for (TypeName each : signature.parameters) {
|
||||
paramTypes.add(new ParamType(mappings.getParamNameAndDoc(superGetter, new EntryTriple(classNode.name, methodNode.name, methodNode.desc), slot), each, usedParamNames, slot));
|
||||
slot++;
|
||||
if (each.equals(TypeName.DOUBLE) || each.equals(TypeName.LONG)) {
|
||||
slot++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void getDescParams(List<ParamType> paramTypes, boolean instance, Set<String> usedParamNames) {
|
||||
private void getParams(List<ParamType> paramTypes, boolean instance, Set<String> usedParamNames) {
|
||||
int slot = instance ? 1 : 0;
|
||||
final String desc = methodNode.desc;
|
||||
int paramIndex = 0;
|
||||
int index = 0;
|
||||
|
||||
if (desc.charAt(index) != '(') {
|
||||
|
@ -227,16 +216,23 @@ public class MethodBuilder {
|
|||
}
|
||||
index++; // consume '('
|
||||
|
||||
Iterator<TypeName> signatureParamIterator = signature == null ? Collections.emptyIterator() : signature.parameters.iterator();
|
||||
while (desc.charAt(index) != ')') {
|
||||
Map.Entry<Integer, TypeName> parsedParam = FieldBuilder.parseType(desc, index);
|
||||
index = parsedParam.getKey();
|
||||
TypeName paramType = parsedParam.getValue();
|
||||
|
||||
paramTypes.add(new ParamType(mappings.getParamNameAndDoc(superGetter, new EntryTriple(classNode.name, methodNode.name, methodNode.desc), slot), paramType, usedParamNames, slot));
|
||||
if (paramIndex >= formalParamStartIndex) { // skip guessed synthetic/implicit params
|
||||
if (signatureParamIterator.hasNext()) {
|
||||
paramType = signatureParamIterator.next();
|
||||
}
|
||||
paramTypes.add(new ParamType(mappings.getParamNameAndDoc(superGetter, new EntryTriple(classNode.name, methodNode.name, methodNode.desc), slot), paramType, usedParamNames, slot));
|
||||
}
|
||||
slot++;
|
||||
if (paramType.equals(TypeName.DOUBLE) || paramType.equals(TypeName.LONG)) {
|
||||
slot++;
|
||||
}
|
||||
paramIndex++;
|
||||
}
|
||||
/* bruh, we don't care about return type
|
||||
index++; // consume ')'
|
||||
|
|
Loading…
Reference in New Issue