This is an automated email from the ASF dual-hosted git repository.
jlahoda pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new b5c4e36 [NETBEANS-893] Reuse JavaFileObject from CompilatioInfo
instead of creating a new one when creating JavacTask, so that embeddings work
correctly.
b5c4e36 is described below
commit b5c4e365578abb5f62a2b43780a969bb4d3cea3d
Author: Jan Lahoda <[email protected]>
AuthorDate: Sun Sep 30 21:31:39 2018 +0200
[NETBEANS-893] Reuse JavaFileObject from CompilatioInfo instead of creating
a new one when creating JavacTask, so that embeddings work correctly.
---
.../java/source/parsing/CompilationInfoImpl.java | 2 +-
.../modules/java/source/parsing/JavacParser.java | 16 +--
.../java/source/parsing/JavacParserTest.java | 114 ++++++++++++++++++++-
.../java/source/parsing/ModuleOraculumTest.java | 60 +++++++----
4 files changed, 154 insertions(+), 38 deletions(-)
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 0ce7612..93ef66d 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 final class CompilationInfoImpl {
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 b63703e..1a499eb 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 @@ public class JavacParser extends Parser {
static JavacTaskImpl createJavacTask(
final FileObject file,
+ final JavaFileObject jfo,
final FileObject root,
final ClasspathInfo cpInfo,
final JavacParser parser,
@@ -727,19 +728,6 @@ public class JavacParser extends Parser {
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 @@ public class JavacParser extends Parser {
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 b159166..bfadaae 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.File;
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.Objects;
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.loaders.DataObject;
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 @@ public class JavacParserTest extends NbTestCase {
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 63eda2d..8610a18 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.Level;
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.classpath.support.ClassPathSupport;
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 @@ public class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
.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 class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
assertEquals(1, roots.size());
assertEquals(root1, roots.get(0));
h.reset();
- impl = JavacParser.createJavacTask(
+ impl = createJavacTask(
javaFile1,
null,
cpInfo,
@@ -222,7 +225,7 @@ public class ModuleOraculumTest extends NbTestCase {
assertPatchModules(impl, "Test");
roots = h.getRoots();
assertEquals(0, roots.size());
- impl = JavacParser.createJavacTask(
+ impl = createJavacTask(
javaFile2,
null,
cpInfo,
@@ -248,7 +251,7 @@ public class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
assertPatchModules(impl, "Test");
names = h.getModuleNames();
assertEquals(0, names.size());
- impl = JavacParser.createJavacTask(
+ impl = createJavacTask(
javaFile2,
root2,
cpInfo,
@@ -296,7 +299,7 @@ public class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
assertEquals("Test", names.get(0)); //NOI18N
h.reset();
createModule(root1, "TestUpdated");
- impl = JavacParser.createJavacTask(
+ impl = createJavacTask(
javaFile1,
root1,
cpInfo,
@@ -321,7 +324,7 @@ public class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
assertEquals("Test", names.get(0)); //NOI18N
h.reset();
moduleFile1.delete();
- impl = JavacParser.createJavacTask(
+ impl = createJavacTask(
javaFile1,
root1,
cpInfo,
@@ -371,7 +374,7 @@ public class ModuleOraculumTest extends NbTestCase {
assertEquals(1, names.size());
assertEquals(null, names.get(0));
h.reset();
- impl = JavacParser.createJavacTask(
+ impl = createJavacTask(
javaFile1,
root1,
cpInfo,
@@ -397,7 +400,7 @@ public class ModuleOraculumTest extends NbTestCase {
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 class ModuleOraculumTest extends NbTestCase {
assertNull(names.get(0));
h.reset();
createModule(root1, "TestNew");
- impl = JavacParser.createJavacTask(
+ impl = createJavacTask(
javaFile1,
root1,
cpInfo,
@@ -422,7 +425,7 @@ public class ModuleOraculumTest extends NbTestCase {
assertEquals(1, names.size());
assertEquals("TestNew", names.get(0)); //NOI18N
h.reset();
- impl = JavacParser.createJavacTask(
+ impl = createJavacTask(
javaFile1,
root1,
cpInfo,
@@ -450,6 +453,21 @@ public class ModuleOraculumTest extends NbTestCase {
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));
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists