WIP: Port to compilation on 1.21.4

This commit is contained in:
Linnea Gräf
2024-12-07 13:26:03 +01:00
parent cdb5e60f52
commit bf7795df22
77 changed files with 780 additions and 1446 deletions

View File

@@ -59,6 +59,14 @@ public class InitReplacer extends TreeScanner<Void, Void> {
var target = plugin.utils.getAnnotationValue(jcAnnotation, "value");
var targetClass = plugin.utils.resolveClassLiteralExpression(target).tsym.flatName().toString();
var intermediaryClass = mappingTree.resolveClassToIntermediary(targetClass);
if (intermediaryClass == null){
plugin.utils.reportError(
compilationUnitTree.getSourceFile(),
jcNode.init,
"Unknown class name " + targetClass
);
return super.visitVariable(node, unused);
}
var remapper = treeMaker.Select(treeMaker.This(classTree.type), names.fromString("remapper"));
var remappingCall = treeMaker.Apply(
List.nil(),

View File

@@ -57,7 +57,12 @@ public class IntermediaryMethodReplacer extends TreeScanner<Void, Void> {
}
var head = node.typeargs.head;
var resolved = plugin.utils.resolveClassName(head, compilationUnit);
var mappedName = mappings.resolveClassToIntermediary(resolved.tsym.flatName().toString());
var sourceName = resolved.tsym.flatName().toString();
var mappedName = mappings.resolveClassToIntermediary(sourceName);
if (mappedName == null) {
plugin.utils.reportError(sourceFile, node, "Unknown class name " + sourceName);
return;
}
fieldAccess.name = plugin.names.fromString("id");
node.typeargs = List.nil();
node.args = List.of(plugin.treeMaker.Literal(mappedName));

View File

@@ -9,40 +9,43 @@ import java.util.stream.Collectors;
public class MappingTree {
private final Map<String, TinyClass> classLookup;
private final int targetIndex;
private final int sourceIndex;
private final Map<String, TinyClass> classLookup;
private final int targetIndex;
private final int sourceIndex;
public MappingTree(TinyFile tinyV2File, String sourceNamespace, String targetNamespace) {
sourceIndex = tinyV2File.getHeader().getNamespaces().indexOf(sourceNamespace);
if (sourceIndex < 0)
throw new RuntimeException("Could not find source namespace " + sourceNamespace + " in mappings file.");
this.classLookup = tinyV2File
.getClassEntries()
.stream()
.collect(Collectors.toMap(it -> it.getClassNames().get(sourceIndex), it -> it));
targetIndex = tinyV2File.getHeader().getNamespaces().indexOf(targetNamespace);
if (targetIndex < 0)
throw new RuntimeException("Could not find target namespace " + targetNamespace + " in mappings file.");
}
public MappingTree(TinyFile tinyV2File, String sourceNamespace, String targetNamespace) {
sourceIndex = tinyV2File.getHeader().getNamespaces().indexOf(sourceNamespace);
if (sourceIndex < 0)
throw new RuntimeException("Could not find source namespace " + sourceNamespace + " in mappings file.");
this.classLookup = tinyV2File
.getClassEntries()
.stream()
.collect(Collectors.toMap(it -> it.getClassNames().get(sourceIndex), it -> it));
targetIndex = tinyV2File.getHeader().getNamespaces().indexOf(targetNamespace);
if (targetIndex < 0)
throw new RuntimeException("Could not find target namespace " + targetNamespace + " in mappings file.");
}
public String resolveMethodToIntermediary(String className, String methodName) {
var classData = classLookup.get(className.replace(".", "/"));
TinyMethod candidate = null;
for (TinyMethod method : classData.getMethods()) {
if (method.getMethodNames().get(sourceIndex).equals(methodName)) {
if (candidate != null) {
throw new RuntimeException("Found two candidates for method " + className + "." + methodName);
}
candidate = method;
}
}
return candidate.getMethodNames().get(targetIndex);
}
public String resolveMethodToIntermediary(String className, String methodName) {
var classData = classLookup.get(className.replace(".", "/"));
TinyMethod candidate = null;
for (TinyMethod method : classData.getMethods()) {
if (method.getMethodNames().get(sourceIndex).equals(methodName)) {
if (candidate != null) {
throw new RuntimeException("Found two candidates for method " + className + "." + methodName);
}
candidate = method;
}
}
return candidate.getMethodNames().get(targetIndex);
}
public String resolveClassToIntermediary(String className) {
return classLookup.get(className.replace(".", "/"))
.getClassNames().get(targetIndex)
.replace("/", ".");
}
public String resolveClassToIntermediary(String className) {
var cls = classLookup.get(className.replace(".", "/"));
if (cls == null) {
return null;
}
return cls.getClassNames().get(targetIndex)
.replace("/", ".");
}
}

View File

@@ -111,7 +111,7 @@ public class Utils {
var error = diagnostics.error(
JCDiagnostic.DiagnosticFlag.API,
log.currentSource(),
((JCTree) node).pos(),
node == null ? null : ((JCTree) node).pos(),
"firmament.generic",
message
);