Commits

Anonymous committed 9eb0bf4 Draft

#244554:IllegalArgumentException: Makefile source expected, got org.netbeans.modules.parsing.api.Source@2d5608a2[mimeType=text/plain, fileObject=/home/apepin/NetBeansProjects/JavaApplication8/cplusassign/Make

Comments (0)

Files changed (2)

parsing.api/src/org/netbeans/modules/parsing/api/Source.java

         this.fileObject =   fileObject;
     }
 
-    private static Source _get(String mimeType, FileObject fileObject) {
+    @NonNull
+    private static Source _get(
+            @NonNull final String mimeType,
+            @NonNull final FileObject fileObject) {
         assert mimeType != null;
         assert fileObject != null;
-        
+
         synchronized (Source.class) {
-            Reference<Source> sourceRef = instances.get(fileObject);
+            final Reference<Source> sourceRef = instances.get(fileObject);
             Source source = sourceRef == null ? null : sourceRef.get();
-
-            if (source == null) {
+            if (source == null || !mimeType.equals(source.getMimeType())) {
                 source = new Source(mimeType, null, fileObject);
-                instances.put(fileObject, new WeakReference<Source>(source));
+                instances.put(fileObject, new WeakReference<>(source));
             }
-            // XXX: we may want to update the mime type to the one from the document,
-            // but I'm not sure what would that mean for the rest of the infrastructure.
-            // It would probably need to throw everything (?) away and start from scratch.
-
             return source;
         }
     }

parsing.api/test/unit/src/org/netbeans/modules/parsing/api/SourceTest.java

 import org.netbeans.modules.parsing.impl.SourceAccessor;
 import org.netbeans.modules.parsing.impl.TaskProcessor;
 import org.openide.cookies.EditorCookie;
+import org.openide.filesystems.FileLock;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
 import org.openide.loaders.DataFolder;
         super(name);
     }
 
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        FileUtil.setMIMEType("txt", "text/plain");  //NOI18N
+    }
+
     public void testCreateSnapshotEOLConversions() throws IOException {
         String documentContent = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
 
         assertSame("Wrong FileObject", file, source.getFileObject());
     }
 
+    public void testMimeTypeChange() throws IOException {
+        final FileObject file = createFileObject("empty.foo", "", "\n");
+        final Source source = Source.create(file);
+        assertNotNull("No Source for " + file, source);
+        assertSame("Wrong FileObject", file, source.getFileObject());
+        assertSame("Inconsistent Source.create(FileObject)", source, Source.create(file));
+        final FileLock lock = file.lock();
+        try {
+            file.rename(lock, "empty", "txt");  //NOI18N
+        } finally {
+            lock.releaseLock();
+        }
+        final Source source2 = Source.create(file);
+        assertNotNull("No Source for " + file, source2);
+        assertSame("Wrong FileObject", file, source2.getFileObject());
+        assertNotSame("Inconsistent Source.create(FileObject)", source, source2);
+    }
+
     public void testConsistencySourceForFilelessDocument() {
         Document doc = createDocument("text/plain", "");
         Source source = Source.create(doc);