You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2018/09/30 19:31:41 UTC

[GitHub] jlahoda closed pull request #570: [NETBEANS-893] Reuse JavaFileObject from CompilatioInfo instead of cr…

jlahoda closed pull request #570: [NETBEANS-893] Reuse JavaFileObject from CompilatioInfo instead of cr…
URL: https://github.com/apache/incubator-netbeans/pull/570
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/parsing/CompilationInfoImpl.java b/java/java.source.base/src/org/netbeans/modules/java/source/parsing/CompilationInfoImpl.java
index 0ce7612289..93ef66d981 100644
--- a/java/java.source.base/src/org/netbeans/modules/java/source/parsing/CompilationInfoImpl.java
+++ b/java/java.source.base/src/org/netbeans/modules/java/source/parsing/CompilationInfoImpl.java
@@ -371,7 +371,7 @@ public Document getDocument() {
     public synchronized JavacTaskImpl getJavacTask() {
         if (javacTask == null) {
             diagnosticListener = new DiagnosticListenerImpl(this.root, this.jfo, this.cpInfo);
-            javacTask = JavacParser.createJavacTask(this.file, this.root, this.cpInfo,
+            javacTask = JavacParser.createJavacTask(this.file, this.jfo, this.root, this.cpInfo,
                     this.parser, diagnosticListener, isDetached);
         }
 	return javacTask;
diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/parsing/JavacParser.java b/java/java.source.base/src/org/netbeans/modules/java/source/parsing/JavacParser.java
index b63703e329..1a499eb768 100644
--- a/java/java.source.base/src/org/netbeans/modules/java/source/parsing/JavacParser.java
+++ b/java/java.source.base/src/org/netbeans/modules/java/source/parsing/JavacParser.java
@@ -670,6 +670,7 @@ private static CompilationInfoImpl createCurrentInfo (final JavacParser parser,
 
     static JavacTaskImpl createJavacTask(
             final FileObject file,
+            final JavaFileObject jfo,
             final FileObject root,
             final ClasspathInfo cpInfo,
             final JavacParser parser,
@@ -727,19 +728,6 @@ static JavacTaskImpl createJavacTask(
                 compilerOptions = null;
                 sourceLevel = null;
             }
-            AbstractSourceFileObject source = null;
-            if (file != null) {
-                try {
-                    source = FileObjects.sourceFileObject(file, root, null, false);
-                    if (source.getKind() != Kind.SOURCE) {
-                        source = null;
-                    }
-                } catch (FileObjects.InvalidFileException ife) {
-                    //ignore, it will be handled again later, see #parse.
-                } catch (IOException ex) {
-                    throw new IllegalStateException(ex);
-                }
-            }
             final JavacTaskImpl javacTask = createJavacTask(cpInfo,
                     diagnosticListener,
                     sourceLevel != null ? sourceLevel.getSourceLevel() : null,
@@ -750,7 +738,7 @@ static JavacTaskImpl createJavacTask(
                     APTUtils.get(root),
                     compilerOptions,
                     additionalModules,
-                    source != null ? Arrays.asList(source) : Collections.emptyList());
+                    jfo != null ? Arrays.asList(jfo) : Collections.emptyList());
             Lookup.getDefault()
                   .lookupAll(TreeLoaderRegistry.class)
                   .stream()
diff --git a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java
index b1591661a4..bfadaaeafa 100644
--- a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java
+++ b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java
@@ -28,8 +28,10 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStreamWriter;
 import java.net.URL;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
@@ -37,10 +39,13 @@
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 import javax.lang.model.element.TypeElement;
+import javax.swing.event.ChangeListener;
 import javax.swing.text.Document;
 import javax.tools.Diagnostic;
-import static junit.framework.Assert.assertEquals;
-import org.netbeans.api.java.classpath.ClassPath;
+import javax.tools.JavaFileObject.Kind;
+import static junit.framework.TestCase.assertEquals;
+import org.netbeans.api.editor.mimelookup.MimePath;
+import org.netbeans.api.editor.mimelookup.test.MockMimeLookup;
 import org.netbeans.api.java.lexer.JavaTokenId;
 import org.netbeans.api.java.source.ClasspathInfo;
 import org.netbeans.api.java.source.CompilationController;
@@ -63,6 +68,18 @@
 import org.openide.util.Lookup;
 import org.openide.util.lookup.Lookups;
 import org.netbeans.modules.java.source.base.SourceLevelUtils;
+import org.netbeans.modules.parsing.api.Embedding;
+import org.netbeans.modules.parsing.api.ParserManager;
+import org.netbeans.modules.parsing.api.ResultIterator;
+import org.netbeans.modules.parsing.api.Snapshot;
+import org.netbeans.modules.parsing.api.UserTask;
+import org.netbeans.modules.parsing.spi.EmbeddingProvider;
+import org.netbeans.modules.parsing.spi.ParseException;
+import org.netbeans.modules.parsing.spi.Parser;
+import org.netbeans.modules.parsing.spi.ParserFactory;
+import org.netbeans.modules.parsing.spi.SchedulerTask;
+import org.netbeans.modules.parsing.spi.SourceModificationEvent;
+import org.netbeans.modules.parsing.spi.TaskFactory;
 
 /**
  *
@@ -395,6 +412,99 @@ private Source guessSourceLevel(boolean objectOnBCP, boolean sbOnCP, boolean acO
         
         return JavacParser.validateSourceLevel("1.7", info, false);
     }
+    
+    public void testEmbeddedJava() throws Exception {
+        Lookups.executeWith(Lookups.exclude(Lookup.getDefault(), JavacParser.SequentialParsing.class), () -> {
+            try {
+                //set-up taken from EmbeddingTest:
+                MockMimeLookup.setInstances (
+                    MimePath.get ("text/jsh"),
+                    new ParserFactory () {
+                        public Parser createParser (Collection<Snapshot> snapshots2) {
+                            return new Parser () {
+                                private Snapshot last;
+
+                                public void parse (Snapshot snapshot, org.netbeans.modules.parsing.api.Task task, SourceModificationEvent event) throws ParseException {
+                                    last = snapshot;
+                                }
+
+                                public Parser.Result getResult (org.netbeans.modules.parsing.api.Task task) throws ParseException {
+                                    return new Parser.Result(last) {
+                                        public void invalidate () {
+                                        }
+                                    };
+                                }
+
+                                public void cancel () {
+                                }
+
+                                public void addChangeListener (ChangeListener changeListener) {
+                                }
+
+                                public void removeChangeListener (ChangeListener changeListener) {
+                                }
+
+                                @Override
+                                public String toString () {
+                                    return "TestParser";
+                                }
+                            };
+                        }
+                    },
+                    new TaskFactory () {
+                        public Collection<SchedulerTask> create (Snapshot snapshot) {
+                            return Arrays.asList (new SchedulerTask[] {
+                                new EmbeddingProvider() {
+                                    public List<Embedding> getEmbeddings (Snapshot snapshot) {
+                                        Embedding embedding = snapshot.create("JAVA".length(), snapshot.getText().length() - "JAVA".length(), "text/x-java");
+                                        return Arrays.asList (new Embedding[] {
+                                            embedding
+                                        });
+                                    }
+
+                                    public int getPriority () {
+                                        return 10;
+                                    }
+
+                                    public void cancel () {
+                                    }
+
+                                    @Override
+                                    public String toString () {
+                                        return "TestEmbeddingProvider " + getPriority ();
+                                    }
+                                },
+                            });
+                        }
+                    }
+
+                );
+
+                clearWorkDir ();
+                FileObject workDir = FileUtil.toFileObject (getWorkDir ());
+                FileObject testFile = FileUtil.createData (workDir, "bla.jsh");
+                FileUtil.setMIMEType ("jsh", "text/jsh");
+                try (OutputStreamWriter writer = new OutputStreamWriter (testFile.getOutputStream ())) {
+                    writer.append ("JAVAclass T {}");
+                }
+                assertEquals(Kind.SOURCE, FileObjects.sourceFileObject(testFile, workDir).getKind());
+                org.netbeans.modules.parsing.api.Source source = org.netbeans.modules.parsing.api.Source.create(testFile);
+                ParserManager.parse(Collections.singletonList(source), new UserTask() {
+                    @Override
+                    public void run(ResultIterator ri) throws Exception {
+                        Embedding emb = ri.getEmbeddings().iterator().next();
+                        assertEquals("text/x-java", emb.getMimeType());
+                        CompilationController cc = CompilationController.get(ri.getResultIterator(emb).getParserResult());
+                        cc.toPhase(Phase.RESOLVED);
+                        assertEquals(Collections.emptyList(), cc.getDiagnostics());
+                        assertEquals("T", cc.getTopLevelElements().get(0).getQualifiedName().toString());
+                    }
+                });
+            } catch (Exception ex) {
+                throw new IllegalStateException(ex);
+            }
+        });
+    }
 
     public void testValidateCompilerOptions() {
         List<String> input = Arrays.asList("--add-exports", "foo/bar=foobar",
diff --git a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/ModuleOraculumTest.java b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/ModuleOraculumTest.java
index 63eda2da22..8610a18575 100644
--- a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/ModuleOraculumTest.java
+++ b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/parsing/ModuleOraculumTest.java
@@ -37,8 +37,10 @@
 import java.util.logging.LogRecord;
 import java.util.logging.Logger;
 import javax.swing.event.ChangeListener;
+import javax.tools.DiagnosticListener;
 import javax.tools.JavaFileManager;
 import javax.tools.JavaFileManager.Location;
+import javax.tools.JavaFileObject;
 import javax.tools.StandardLocation;
 import org.netbeans.api.annotations.common.NonNull;
 import org.netbeans.api.java.classpath.ClassPath;
@@ -54,6 +56,7 @@
 import org.netbeans.spi.java.queries.CompilerOptionsQueryImplementation;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
+import org.openide.util.Exceptions;
 import org.openide.util.Lookup;
 
 /**
@@ -91,7 +94,7 @@ protected void setUp() throws Exception {
     public void testOraculumLibrarySourceWithRoot() throws IOException {
         final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
         final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-        final JavacTaskImpl impl = JavacParser.createJavacTask(
+        final JavacTaskImpl impl = createJavacTask(
                 javaFile1,
                 root1,
                 cpInfo,
@@ -109,7 +112,7 @@ public void testOraculumLibrarySourceWithoutRootWithSourcePath() throws IOExcept
                 ClassPathSupport.createClassPath(root1));
         final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
         final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-        final JavacTaskImpl impl = JavacParser.createJavacTask(
+        final JavacTaskImpl impl = createJavacTask(
                 javaFile1,
                 null,
                 cpInfo,
@@ -123,7 +126,7 @@ public void testOraculumLibrarySourceWithoutRootWithSourcePath() throws IOExcept
     public void testOraculumLibrarySourceWithoutRootWithoutSourcePath() throws IOException {
         final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
         final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-        final JavacTaskImpl impl = JavacParser.createJavacTask(
+        final JavacTaskImpl impl = createJavacTask(
                 javaFile1,
                 null,
                 cpInfo,
@@ -138,7 +141,7 @@ public void testOraculumLibrarySourceNoModuleInfo() throws IOException {
         moduleFile1.delete();
         final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
         final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-        final JavacTaskImpl impl = JavacParser.createJavacTask(
+        final JavacTaskImpl impl = createJavacTask(
                 javaFile1,
                 root1,
                 cpInfo,
@@ -153,7 +156,7 @@ public void testOraculumProjectSource() throws IOException {
         scan(root1);
         final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
         final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-        final JavacTaskImpl impl = JavacParser.createJavacTask(
+        final JavacTaskImpl impl = createJavacTask(
                 javaFile1,
                 root1,
                 cpInfo,
@@ -170,7 +173,7 @@ public void testOraculumLibrarySourceWithRootExpliciteXModule() throws IOExcepti
                 .apply("-XD-Xmodule:SomeModule");  //NOI18N
         final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
         final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-        final JavacTaskImpl impl = JavacParser.createJavacTask(
+        final JavacTaskImpl impl = createJavacTask(
                 javaFile1,
                 root1,
                 cpInfo,
@@ -200,7 +203,7 @@ public void testRootCache() throws IOException {
         try {
             final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
             final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-            JavacTaskImpl impl = JavacParser.createJavacTask(
+            JavacTaskImpl impl = createJavacTask(
                     javaFile1,
                     null,
                     cpInfo,
@@ -212,7 +215,7 @@ public void testRootCache() throws IOException {
             assertEquals(1, roots.size());
             assertEquals(root1, roots.get(0));
             h.reset();
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile1,
                     null,
                     cpInfo,
@@ -222,7 +225,7 @@ public void testRootCache() throws IOException {
             assertPatchModules(impl, "Test");
             roots = h.getRoots();
             assertEquals(0, roots.size());
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile2,
                     null,
                     cpInfo,
@@ -248,7 +251,7 @@ public void testModuleNameCache() throws IOException {
         try {
             final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
             final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-            JavacTaskImpl impl = JavacParser.createJavacTask(
+            JavacTaskImpl impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -260,7 +263,7 @@ public void testModuleNameCache() throws IOException {
             assertEquals(1, names.size());
             assertEquals("Test", names.get(0)); //NOI18N
             h.reset();
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -270,7 +273,7 @@ public void testModuleNameCache() throws IOException {
             assertPatchModules(impl, "Test");
             names = h.getModuleNames();
             assertEquals(0, names.size());
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile2,
                     root2,
                     cpInfo,
@@ -296,7 +299,7 @@ public void testModuleNameCache_ModuleInfoUpdated() throws IOException {
         try {
             final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
             final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-            JavacTaskImpl impl = JavacParser.createJavacTask(
+            JavacTaskImpl impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -309,7 +312,7 @@ public void testModuleNameCache_ModuleInfoUpdated() throws IOException {
             assertEquals("Test", names.get(0)); //NOI18N
             h.reset();
             createModule(root1, "TestUpdated");
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -321,7 +324,7 @@ public void testModuleNameCache_ModuleInfoUpdated() throws IOException {
             assertEquals(1, names.size());
             assertEquals("TestUpdated", names.get(0)); //NOI18N
             h.reset();
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -346,7 +349,7 @@ public void testModuleNameCache_ModuleInfoDeleted() throws IOException {
         try {
             final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
             final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-            JavacTaskImpl impl = JavacParser.createJavacTask(
+            JavacTaskImpl impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -359,7 +362,7 @@ public void testModuleNameCache_ModuleInfoDeleted() throws IOException {
             assertEquals("Test", names.get(0)); //NOI18N
             h.reset();
             moduleFile1.delete();
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -371,7 +374,7 @@ public void testModuleNameCache_ModuleInfoDeleted() throws IOException {
             assertEquals(1, names.size());
             assertEquals(null, names.get(0));
             h.reset();
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -397,7 +400,7 @@ public void testModuleNameCache_ModuleInfoCreated() throws IOException {
             moduleFile1.delete();
             final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
             final JavacParser parser = new JavacParser(Collections.emptyList(), true);
-            JavacTaskImpl impl = JavacParser.createJavacTask(
+            JavacTaskImpl impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -410,7 +413,7 @@ public void testModuleNameCache_ModuleInfoCreated() throws IOException {
             assertNull(names.get(0));
             h.reset();
             createModule(root1, "TestNew");
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -422,7 +425,7 @@ public void testModuleNameCache_ModuleInfoCreated() throws IOException {
             assertEquals(1, names.size());
             assertEquals("TestNew", names.get(0));  //NOI18N
             h.reset();
-            impl = JavacParser.createJavacTask(
+            impl = createJavacTask(
                     javaFile1,
                     root1,
                     cpInfo,
@@ -450,6 +453,21 @@ private void assertPatchModules(JavacTaskImpl impl, String... expectedPatches) t
         assertEquals(new HashSet<>(Arrays.asList(expectedPatches)), actualNames);
     }
 
+    private static JavacTaskImpl createJavacTask(
+            final FileObject file,
+            final FileObject root,
+            final ClasspathInfo cpInfo,
+            final JavacParser parser,
+            final DiagnosticListener<? super JavaFileObject> diagnosticListener,
+            final boolean detached) {
+        try {
+            JavaFileObject jfo = file != null ? FileObjects.sourceFileObject(file, root, null, false) : null;
+            return JavacParser.createJavacTask(file, jfo, root, cpInfo, parser, diagnosticListener, detached);
+        } catch (IOException ex) {
+            throw new IllegalStateException(ex);
+        }
+    }
+
     private static void scan(@NonNull final FileObject root) throws IOException {
         final TransactionContext ctx = TransactionContext.beginTrans()
                 .register(ClassIndexEventsTransaction.class, ClassIndexEventsTransaction.create(true, ()->true));


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists