Skip to content

Commit

Permalink
Attempt to fix anonymous class classpath lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed May 17, 2024
1 parent 9c7d29a commit 6563621
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 26 deletions.
31 changes: 24 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ val extract = task<Copy>("extractJdt") {

include("org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java")
include("org/eclipse/jdt/internal/core/dom/rewrite/imports/*.java")
include("org/eclipse/jdt/internal/core/util/BindingKeyResolver*.java")
}
tasks["applyPatches"].inputs.files(extract)
tasks["resetSources"].inputs.files(extract)
Expand All @@ -95,8 +96,11 @@ val renames = listOf(
"org.eclipse.jdt.core.dom.rewrite" to "$og_group.$artifactId.jdt.rewrite.imports",
"org.eclipse.jdt.internal.core.dom.rewrite.imports" to "$og_group.$artifactId.jdt.internal.rewrite.imports"
)
val classRenames = listOf(
"org.eclipse.jdt.internal.core.util.BindingKeyResolver" to "$og_group.$artifactId.shadow.org.eclipse.jdt.internal.core.util.BindingKeyResolver",
)

fun createRenameTask(prefix: String, inputDir: File, outputDir: File, renames: List<Pair<String, String>>): Task
fun createRenameTask(prefix: String, inputDir: File, outputDir: File, renames: List<Pair<String, String>>, classRenames: List<Pair<String, String>>): Task
= task<Copy>("${prefix}renameJdt") {
destinationDir = file(outputDir)

Expand All @@ -105,14 +109,26 @@ fun createRenameTask(prefix: String, inputDir: File, outputDir: File, renames: L
into("${new.replace('.', '/')}/")
}
}
classRenames.forEach { (old, new) ->
val oldPkg = old.substringBeforeLast('.')
val newPkg = new.substringBeforeLast('.')
from("$inputDir/${oldPkg.replace('.', '/')}") {
into("${newPkg.replace('.', '/')}/")
filter {
if (it.startsWith("package ")) it.replace(oldPkg, newPkg)
else it
}
}
}

filter { renames.fold(it) { s, (from, to) -> s.replace(from, to) } }
filter { classRenames.fold(it) { s, (from, to) -> s.replace(from, to) } }
}

val renameTask = createRenameTask("", patches.target, jdtSrcDir, renames)
val renameTask = createRenameTask("", patches.target, jdtSrcDir, renames, classRenames)
renameTask.inputs.files(tasks["applyPatches"])

tasks["makePatches"].inputs.files(createRenameTask("un", jdtSrcDir, patches.target, renames.map { (a,b) -> b to a }))
tasks["makePatches"].inputs.files(createRenameTask("un", jdtSrcDir, patches.target, renames.map { (a,b) -> b to a }, classRenames.map { (a,b) -> b to a }))
sourceSets["main"].java.srcDirs(renameTask)

tasks.jar.configure {
Expand Down Expand Up @@ -141,6 +157,7 @@ tasks.shadowJar {
relocate("org.apache", "org.cadixdev.mercury.shadow.org.apache")
relocate("org.eclipse", "org.cadixdev.mercury.shadow.org.eclipse")
relocate("org.osgi", "org.cadixdev.mercury.shadow.org.osgi")
exclude("org/eclipse/jdt/internal/core/util/BindingKeyResolver.class")
}

val sourceJar = task<Jar>("sourceJar") {
Expand Down Expand Up @@ -233,11 +250,11 @@ publishing {
}

repositories {
if (System.getenv("MAVEN_PASS") != null) {
maven("https://deploy.shedaniel.me/") {
if (System.getenv("MAVEN_URL") != null) {
maven(System.getenv("MAVEN_URL")) {
credentials {
username = "shedaniel"
password = System.getenv("MAVEN_PASS")
username = System.getenv("MAVEN_USERNAME")
password = System.getenv("MAVEN_PASSWORD")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group = dev.architectury
group = org.sinytra
og_group = org.cadixdev
name = Mercury
base_version = 0.1.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@
return ImportRewriteContext.RES_NAME_FOUND;
}
+ }

if (this.astRoot != null) {
List<AbstractTypeDeclaration> types = this.astRoot.types();
@@ -1304,7 +1357,28 @@
@@ -1305,7 +1358,28 @@
* @throws CoreException the exception is thrown if the rewrite fails.
*/
public final TextEdit rewriteImports(IProgressMonitor monitor) throws CoreException {
Expand Down Expand Up @@ -179,7 +179,7 @@
SubMonitor subMonitor = SubMonitor.convert(monitor,
Messages.bind(Messages.importRewrite_processDescription), 2);
if (!hasRecordedChanges()) {
@@ -1325,7 +1399,7 @@
@@ -1326,7 +1400,7 @@
ImportRewriteConfiguration config= buildImportRewriteConfiguration();

ImportRewriteAnalyzer computer=
Expand All @@ -188,7 +188,7 @@

for (String addedImport : this.addedImports) {
boolean isStatic = STATIC_PREFIX == addedImport.charAt(0);
@@ -1368,11 +1442,17 @@
@@ -1369,11 +1443,17 @@
configBuilder.setTypeOnDemandThreshold(this.importOnDemandThreshold);
configBuilder.setStaticOnDemandThreshold(this.staticImportOnDemandThreshold);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- a/org/eclipse/jdt/internal/core/dom/rewrite/imports/ImportRewriteAnalyzer.java
+++ b/org/eclipse/jdt/internal/core/dom/rewrite/imports/ImportRewriteAnalyzer.java
@@ -46,8 +46,11 @@
@@ -47,8 +47,11 @@
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.dom.rewrite.imports.ConflictIdentifier.Conflicts;
import org.eclipse.jdt.internal.core.util.Util;
Expand All @@ -12,7 +12,7 @@
import org.eclipse.text.edits.TextEdit;

/**
@@ -261,10 +264,10 @@
@@ -262,10 +265,10 @@
}

private static RewriteSite determineRewriteSite(
Expand All @@ -25,7 +25,7 @@

boolean hasPrecedingElements = surroundingRegion.getOffset() != 0;

@@ -302,7 +305,7 @@
@@ -303,7 +306,7 @@
* Determines the region to be occupied by imports, their associated comments, and surrounding
* whitespace.
*/
Expand All @@ -34,7 +34,7 @@
NavigableMap<Integer, ASTNode> nodesTreeMap = mapTopLevelNodes(compilationUnit);

int surroundingStart;
@@ -331,12 +334,24 @@
@@ -344,12 +347,24 @@
}

int surroundingEnd = positionAfterImports;
Expand All @@ -59,15 +59,15 @@
return new Region(surroundingStart, surroundingEnd - surroundingStart);
}

@@ -432,6 +447,7 @@
@@ -445,6 +460,7 @@
public ImportRewriteAnalyzer(
ICompilationUnit cu,
CompilationUnit astRoot,
+ IDocument document,
ImportRewriteConfiguration configuration) throws JavaModelException {
this.originalImportEntries = Collections.unmodifiableList(readOriginalImports(astRoot));

@@ -460,7 +476,7 @@
@@ -473,7 +489,7 @@

ImportGroupComparator importGroupComparator = new ImportGroupComparator(configuration.importOrder);

Expand All @@ -76,7 +76,7 @@

this.importAdder = configuration.originalImportHandling.createImportAdder(new ImportComparator(
importGroupComparator,
@@ -468,31 +484,39 @@
@@ -481,31 +497,39 @@
configuration.staticContainerSorting.createContainerComparator(javaProject)));

this.implicitImportContainerNames =
Expand Down Expand Up @@ -120,7 +120,7 @@
this.importDeclarationWriter);
}

@@ -566,18 +590,18 @@
@@ -579,18 +603,18 @@

Set<ImportName> touchedContainers = determineTouchedContainers();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--- a/org/eclipse/jdt/internal/core/util/BindingKeyResolver.java
+++ b/org/eclipse/jdt/internal/core/util/BindingKeyResolver.java
@@ -57,6 +57,8 @@
import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
import org.eclipse.jdt.internal.compiler.lookup.WildcardBinding;
import org.eclipse.jdt.internal.compiler.util.HashtableOfObject;
+import org.eclipse.jdt.internal.core.util.BindingKeyParser;
+import org.eclipse.jdt.internal.core.util.Util;

@SuppressWarnings({"rawtypes", "unchecked"})
public class BindingKeyResolver extends BindingKeyParser {
@@ -354,7 +356,12 @@
@Override
public void consumeLocalType(char[] uniqueKey) {
if (this.parsedUnit == null) {
- this.typeBinding = null;
+ char[][] subtypeName = new char[this.compoundName.length][];
+ System.arraycopy(this.compoundName, 0, subtypeName, 0, this.compoundName.length);
+ String[] subTypeParts = String.copyValueOf(uniqueKey).split("/");
+ String subTypeClassName = subTypeParts[subTypeParts.length - 1];
+ subtypeName[subtypeName.length - 1] = subTypeClassName.substring(0, subTypeClassName.length() - 1).toCharArray();
+ this.typeBinding = this.environment.getType(subtypeName);
return;
}
for (LocalTypeBinding localTypeBinding : this.parsedUnit.localTypes.values()) {
12 changes: 6 additions & 6 deletions src/main/java/org/cadixdev/mercury/Mercury.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ public Map<Object, Object> getContext() {
}

public Optional<ITypeBinding> createTypeBinding(String className) {
if (isAnonymousOrLocalType(className)) {
// TODO: Anonymous or local types are currently not supported
// Eclipse uses source lines in their binding keys that are impossible
// to know in advance. Since it may return incorrect results, abort early.
return Optional.empty();
}
// if (isAnonymousOrLocalType(className)) {
// // TODO: Anonymous or local types are currently not supported
// // Eclipse uses source lines in their binding keys that are impossible
// // to know in advance. Since it may return incorrect results, abort early.
// return Optional.empty();
// }

IBinding binding = this.requestor.createBindings(new String[]{'L' + className.replace('.', '/') + ';'})[0];
return binding != null && binding.getKind() == IBinding.TYPE ? Optional.of((ITypeBinding) binding) : Optional.empty();
Expand Down

0 comments on commit 6563621

Please sign in to comment.