113113 */
114114public 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 ()));
0 commit comments