Skip to content

Commit de8e231

Browse files
committed
Improve naming of java files from jar
1 parent db01825 commit de8e231

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
*/
114114
public class JavacCompilationUnitResolver implements ICompilationUnitResolver {
115115

116-
public static final String MOCK_NAME_FOR_CLASSES = "whatever_InvalidNameWE_HOP3_n00ne_will_Ever_use_in_real_file.java";
116+
private static final String MOCK_NAME_FOR_CLASSES = "whatever_InvalidNameWE_HOP3_n00ne_will_Ever_use_in_real_file.java";
117117
public static final Key<Map<JavaFileObject, File>> FILE_OBJECTS_TO_JAR_KEY = new Key<>();
118118

119119
private final class ForwardDiagnosticsAsDOMProblems implements DiagnosticListener<JavaFileObject> {
@@ -712,6 +712,7 @@ public Void visitClass(ClassTree node, Void p) {
712712
unitFile = new File(new String(sourceUnit.getFileName()));
713713
}
714714
Path sourceUnitPath = null;
715+
boolean storeAsClassFromJar = false;
715716
if (!unitFile.getName().endsWith(".java") || sourceUnit.getFileName() == null || sourceUnit.getFileName().length == 0) {
716717
String uri1 = unitFile.toURI().toString().replaceAll("%7C", "/");
717718
if( uri1.endsWith(".class")) {
@@ -720,17 +721,24 @@ public Void visitClass(ClassTree node, Void p) {
720721
sourceUnitPath = Path.of(lastSegment);
721722
}
722723
if( sourceUnitPath == null ) {
723-
// This can cause trouble in case the name of the file is important
724-
// eg module-info.java.
725-
sourceUnitPath = Path.of(new File(System.identityHashCode(sourceUnit) + '/' + MOCK_NAME_FOR_CLASSES).toURI());
724+
storeAsClassFromJar = true;
725+
if (sourceUnit instanceof ICompilationUnit modelUnit) {
726+
sourceUnitPath = Path.of(new File(System.identityHashCode(sourceUnit) + "/" + modelUnit.getElementName()).toURI());
727+
} else {
728+
// This can cause trouble in case the name of the file is important
729+
// eg module-info.java.
730+
sourceUnitPath = Path.of(new File(System.identityHashCode(sourceUnit) + "/" + MOCK_NAME_FOR_CLASSES).toURI());
731+
}
726732
}
727733
} else if (unitFile.getName().endsWith(".jar")) {
728-
sourceUnitPath = Path.of(unitFile.toURI()).resolve(System.identityHashCode(sourceUnit) + '/' + MOCK_NAME_FOR_CLASSES);
734+
sourceUnitPath = Path.of(unitFile.toURI()).resolve(System.identityHashCode(sourceUnit) + "/" + MOCK_NAME_FOR_CLASSES);
735+
storeAsClassFromJar = true;
729736
} else {
730737
sourceUnitPath = Path.of(unitFile.toURI());
731738
}
739+
storeAsClassFromJar |= unitFile.getName().endsWith(".jar");
732740
var fileObject = fileManager.getJavaFileObject(sourceUnitPath);
733-
if (unitFile.getName().endsWith(".jar")) {
741+
if (storeAsClassFromJar) {
734742
fileObjectsToJars.put(fileObject, unitFile);
735743
}
736744
fileManager.cache(fileObject, CharBuffer.wrap(sourceUnit.getContents()));

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,7 @@ private IJavaElement computeJavaElement() {
279279
return resolved(ordinary.getType());
280280
}
281281
}
282-
}
283-
if (!jfo.getName().endsWith(JavacCompilationUnitResolver.MOCK_NAME_FOR_CLASSES)) {
282+
} else {
284283
var jfoFile = new File(jfo.getName());
285284
var jfoPath = new Path(jfo.getName());
286285
Stream<IFile> fileStream = jfoFile.isFile() ?
@@ -633,7 +632,7 @@ static void getKey(StringBuilder builder, Type typeToBuild, String n, boolean is
633632
} catch (IllegalArgumentException e) {
634633
// probably: uri is not a valid path
635634
}
636-
if (fileName != null && !fileName.startsWith(classSymbol.getSimpleName().toString()) && !fileName.endsWith(JavacCompilationUnitResolver.MOCK_NAME_FOR_CLASSES)) {
635+
if (fileName != null && !fileName.startsWith(classSymbol.getSimpleName().toString())) {
637636
// There are multiple top-level types in this file,
638637
// inject 'FileName~' before the type name to show that this type came from `FileName.java`
639638
// (eg. Lorg/eclipse/jdt/FileName~MyTopLevelType;)
@@ -1601,4 +1600,8 @@ public String toString() {
16011600
+ getQualifiedName();
16021601
}
16031602

1603+
private boolean isClassFromJar(String name) {
1604+
return this.resolver.context.get(JavacCompilationUnitResolver.FILE_OBJECTS_TO_JAR_KEY).containsValue(new File(name));
1605+
}
1606+
16041607
}

0 commit comments

Comments
 (0)