You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by jl...@apache.org on 2017/10/04 19:41:21 UTC
[09/13] incubator-netbeans git commit: Work in progress: an
experiment on using javac from JDK for source code modeling.
http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/ffc0de5a/java.source.base/src/org/netbeans/modules/java/source/indexing/MultiPassCompileWorker.java
----------------------------------------------------------------------
diff --git a/java.source.base/src/org/netbeans/modules/java/source/indexing/MultiPassCompileWorker.java b/java.source.base/src/org/netbeans/modules/java/source/indexing/MultiPassCompileWorker.java
deleted file mode 100644
index b832607..0000000
--- a/java.source.base/src/org/netbeans/modules/java/source/indexing/MultiPassCompileWorker.java
+++ /dev/null
@@ -1,518 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.netbeans.modules.java.source.indexing;
-
-import com.sun.source.tree.CompilationUnitTree;
-import com.sun.tools.javac.api.JavacTaskImpl;
-import com.sun.tools.javac.code.Symbol.ClassSymbol;
-import com.sun.tools.javac.code.Symtab;
-import com.sun.tools.javac.code.Type;
-import com.sun.tools.javac.code.TypeTag;
-import com.sun.tools.javac.code.Types;
-import com.sun.tools.javac.comp.AttrContext;
-import com.sun.tools.javac.comp.Enter;
-import com.sun.tools.javac.comp.Env;
-import com.sun.tools.javac.comp.Modules;
-import com.sun.tools.javac.tree.JCTree;
-import com.sun.tools.javac.tree.JCTree.JCClassDecl;
-import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
-import com.sun.tools.javac.tree.TreeScanner;
-import org.netbeans.lib.nbjavac.services.CancelAbort;
-import org.netbeans.lib.nbjavac.services.CancelService;
-import com.sun.tools.javac.util.CouplingAbort;
-import com.sun.tools.javac.util.FatalError;
-import com.sun.tools.javac.util.Log;
-import com.sun.tools.javac.util.MissingPlatformError;
-import java.io.File;
-import java.io.IOException;
-import java.util.*;
-import java.util.logging.Level;
-import java.util.stream.Collectors;
-import java.util.stream.StreamSupport;
-import javax.annotation.processing.Processor;
-import javax.lang.model.element.Element;
-import javax.lang.model.element.ModuleElement;
-import javax.lang.model.element.TypeElement;
-import javax.tools.JavaFileManager;
-import javax.tools.JavaFileObject;
-import javax.tools.StandardLocation;
-import org.netbeans.api.annotations.common.NonNull;
-import org.netbeans.api.annotations.common.NullAllowed;
-import org.netbeans.api.java.classpath.ClassPath;
-import org.netbeans.api.java.queries.CompilerOptionsQuery;
-import org.netbeans.api.java.source.ClasspathInfo;
-import org.netbeans.api.java.source.ElementHandle;
-import org.netbeans.modules.java.source.TreeLoader;
-import org.netbeans.modules.java.source.indexing.JavaCustomIndexer.CompileTuple;
-import org.netbeans.modules.java.source.parsing.FileObjects;
-import org.netbeans.modules.java.source.parsing.JavacParser;
-import org.netbeans.modules.java.source.parsing.OutputFileManager;
-import org.netbeans.modules.java.source.usages.ClassNamesForFileOraculumImpl;
-import org.netbeans.modules.java.source.usages.ExecutableFilesIndex;
-import org.netbeans.modules.parsing.spi.indexing.Context;
-import org.netbeans.modules.parsing.spi.indexing.Indexable;
-import org.openide.filesystems.FileUtil;
-
-/**
- *
- * @author Dusan Balek
- */
-final class MultiPassCompileWorker extends CompileWorker {
-
- private static final int MEMORY_LOW = 1;
- private static final int ERR = 2;
- private boolean checkForMemLow = true;
-
- @Override
- ParsingOutput compile(
- final ParsingOutput previous,
- final Context context,
- final JavaParsingContext javaContext,
- final Collection<? extends CompileTuple> files) {
- final LinkedList<CompileTuple> toProcess = new LinkedList<>();
- final HashMap<JavaFileObject, CompileTuple> jfo2tuples = new HashMap<>();
- final ModuleName moduleName = new ModuleName(javaContext.getModuleName());
- for (CompileTuple i : files) {
- if (!previous.finishedFiles.contains(i.indexable)) {
- toProcess.add(i);
- jfo2tuples.put(i.jfo, i);
- }
- }
- if (toProcess.isEmpty()) {
- return ParsingOutput.success(
- moduleName.name,
- previous.file2FQNs,
- previous.addedTypes,
- previous.addedModules,
- previous.createdFiles,
- previous.finishedFiles,
- previous.modifiedTypes,
- previous.aptGenerated);
- }
- final ClassNamesForFileOraculumImpl cnffOraculum = new ClassNamesForFileOraculumImpl(previous.file2FQNs);
- final DiagnosticListenerImpl diagnosticListener = new DiagnosticListenerImpl();
- final LinkedList<CompileTuple> bigFiles = new LinkedList<CompileTuple>();
- JavacTaskImpl jt = null;
- CompileTuple active = null;
- boolean aptEnabled = false;
- int state = 0;
- boolean isBigFile = false;
- boolean[] flm = null;
- while (!toProcess.isEmpty() || !bigFiles.isEmpty() || active != null) {
- if (context.isCancelled()) {
- return null;
- }
- try {
- context.getSuspendStatus().parkWhileSuspended();
- } catch (InterruptedException ex) {
- //NOP - safe to ignore
- }
- try {
- try {
- if (isLowMemory(flm)) {
- dumpSymFiles(jt, previous.createdFiles, context);
- jt = null;
- diagnosticListener.cleanDiagnostics();
- if ((state & MEMORY_LOW) != 0) {
- break;
- } else {
- state |= MEMORY_LOW;
- }
- freeMemory(true);
- continue;
- }
- if (active == null) {
- if (!toProcess.isEmpty()) {
- active = toProcess.removeFirst();
- if (active == null || previous.finishedFiles.contains(active.indexable))
- continue;
- isBigFile = false;
- } else {
- active = bigFiles.removeFirst();
- isBigFile = true;
- if (flm == null) {
- flm = new boolean[] {true};
- }
- }
- }
- if (jt == null) {
- jt = JavacParser.createJavacTask(
- javaContext.getClasspathInfo(),
- diagnosticListener,
- javaContext.getSourceLevel(),
- javaContext.getProfile(),
- cnffOraculum,
- javaContext.getFQNs(),
- new CancelService() {
- public @Override boolean isCanceled() {
- return context.isCancelled() || (checkForMemLow && isLowMemory(null));
- }
- },
- active.aptGenerated ? null : APTUtils.get(context.getRoot()),
- CompilerOptionsQuery.getOptions(context.getRoot()));
- Iterable<? extends Processor> processors = jt.getProcessors();
- aptEnabled = processors != null && processors.iterator().hasNext();
- if (JavaIndex.LOG.isLoggable(Level.FINER)) {
- JavaIndex.LOG.finer("Created new JavacTask for: " + FileUtil.getFileDisplayName(context.getRoot()) + " " + javaContext.getClasspathInfo().toString()); //NOI18N
- }
- }
- Iterable<? extends CompilationUnitTree> trees = jt.parse(new JavaFileObject[]{active.jfo});
- if (isLowMemory(flm)) {
- dumpSymFiles(jt, previous.createdFiles, context);
- jt = null;
- diagnosticListener.cleanDiagnostics();
- trees = null;
- if ((state & MEMORY_LOW) != 0) {
- if (isBigFile) {
- break;
- } else {
- bigFiles.add(active);
- active = null;
- state &= ~MEMORY_LOW;
- }
- } else {
- state |= MEMORY_LOW;
- }
- freeMemory(true);
- continue;
- }
- Iterable<? extends Element> types;
- types = jt.enterTrees(trees);
- if (jfo2tuples.remove(active.jfo) != null) {
- final Types ts = Types.instance(jt.getContext());
- final Indexable activeIndexable = active.indexable;
- class ScanNested extends TreeScanner {
- Set<CompileTuple> dependencies = new LinkedHashSet<CompileTuple>();
- @Override
- public void visitClassDef(JCClassDecl node) {
- if (node.sym != null) {
- Type st = ts.supertype(node.sym.type);
- boolean envForSuperTypeFound = false;
- while (!envForSuperTypeFound && st != null && st.hasTag(TypeTag.CLASS)) {
- ClassSymbol c = st.tsym.outermostClass();
- CompileTuple u = jfo2tuples.get(c.sourcefile);
- if (u != null && !previous.finishedFiles.contains(u.indexable) && !u.indexable.equals(activeIndexable)) {
- dependencies.add(u);
- envForSuperTypeFound = true;
- }
- st = ts.supertype(st);
- }
- }
- super.visitClassDef(node);
- }
- }
- ScanNested scanner = new ScanNested();
- for (CompilationUnitTree cut : trees) {
- scanner.scan((JCCompilationUnit)cut);
- }
- if (!scanner.dependencies.isEmpty()) {
- toProcess.addFirst(active);
- for (CompileTuple tuple : scanner.dependencies) {
- toProcess.addFirst(tuple);
- }
- active = null;
- continue;
- }
- }
- if (isLowMemory(flm)) {
- dumpSymFiles(jt, previous.createdFiles, context);
- jt = null;
- diagnosticListener.cleanDiagnostics();
- trees = null;
- types = null;
- if ((state & MEMORY_LOW) != 0) {
- if (isBigFile) {
- break;
- } else {
- bigFiles.add(active);
- active = null;
- state &= ~MEMORY_LOW;
- }
- } else {
- state |= MEMORY_LOW;
- }
- freeMemory(true);
- continue;
- }
- jt.analyze(types);
- if (aptEnabled) {
- JavaCustomIndexer.addAptGenerated(context, javaContext, active, previous.aptGenerated);
- }
- if (isLowMemory(flm)) {
- dumpSymFiles(jt, previous.createdFiles, context);
- jt = null;
- diagnosticListener.cleanDiagnostics();
- trees = null;
- types = null;
- if ((state & MEMORY_LOW) != 0) {
- if (isBigFile) {
- break;
- } else {
- bigFiles.add(active);
- active = null;
- state &= ~MEMORY_LOW;
- }
- } else {
- state |= MEMORY_LOW;
- }
- freeMemory(true);
- continue;
- }
- javaContext.getFQNs().set(types, active.indexable.getURL());
- boolean[] main = new boolean[1];
- if (javaContext.getCheckSums().checkAndSet(
- active.indexable.getURL(),
- StreamSupport.stream(types.spliterator(), false)
- .filter((e) -> e.getKind().isClass() || e.getKind().isInterface())
- .map ((e) -> (TypeElement)e)
- .collect(Collectors.toList()),
- jt.getElements()) || context.isSupplementaryFilesIndexing()) {
- javaContext.analyze(trees, jt, active, previous.addedTypes, previous.addedModules, main);
- } else {
- final Set<ElementHandle<TypeElement>> aTypes = new HashSet<>();
- javaContext.analyze(trees, jt, active, aTypes, previous.addedModules, main);
- previous.addedTypes.addAll(aTypes);
- previous.modifiedTypes.addAll(aTypes);
- }
- ExecutableFilesIndex.DEFAULT.setMainClass(context.getRoot().toURL(), active.indexable.getURL(), main[0]);
- JavaCustomIndexer.setErrors(context, active, diagnosticListener);
- Iterable<? extends JavaFileObject> generatedFiles = jt.generate(types);
- if (!active.virtual) {
- for (JavaFileObject generated : generatedFiles) {
- if (generated instanceof FileObjects.FileBase) {
- previous.createdFiles.add(((FileObjects.FileBase) generated).getFile());
- } else {
- // presumably should not happen
- }
- }
- }
- if (!moduleName.assigned) {
- ModuleElement module = trees.iterator().hasNext() ?
- ((JCTree.JCCompilationUnit)trees.iterator().next()).modle :
- null;
- if (module == null) {
- module = module = Modules.instance(jt.getContext()).getDefaultModule();
- }
- moduleName.name = module == null || module.isUnnamed() ?
- null :
- module.getQualifiedName().toString();
- moduleName.assigned = true;
- }
- Log.instance(jt.getContext()).nerrors = 0;
- previous.finishedFiles.add(active.indexable);
- active = null;
- state = 0;
- } catch (CancelAbort ca) {
- if (isLowMemory(flm)) {
- dumpSymFiles(jt, previous.createdFiles, context);
- jt = null;
- diagnosticListener.cleanDiagnostics();
- if ((state & MEMORY_LOW) != 0) {
- if (isBigFile) {
- break;
- } else {
- bigFiles.add(active);
- active = null;
- state &= ~MEMORY_LOW;
- }
- } else {
- state |= MEMORY_LOW;
- }
- freeMemory(true);
- } else if (JavaIndex.LOG.isLoggable(Level.FINEST)) {
- JavaIndex.LOG.log(Level.FINEST, "OnePassCompileWorker was canceled in root: " + FileUtil.getFileDisplayName(context.getRoot()), ca); //NOI18N
- }
- }
- } catch (CouplingAbort ca) {
- //Coupling error
- TreeLoader.dumpCouplingAbort(ca, null);
- jt = null;
- diagnosticListener.cleanDiagnostics();
- if ((state & ERR) != 0) {
- //When a javac failed with the Exception mark a file
- //causing this exceptin as compiled
- if (active != null)
- previous.finishedFiles.add(active.indexable);
- active = null;
- state = 0;
- } else {
- state |= ERR;
- }
- } catch (OutputFileManager.InvalidSourcePath isp) {
- //Deleted project - log & ignore
- if (JavaIndex.LOG.isLoggable(Level.FINEST)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("MultiPassCompileWorker caused an exception\nFile: %s\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- active.jfo.toUri().toString(),
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(Level.FINEST, message, isp);
- }
- return ParsingOutput.failure(moduleName.name, previous.file2FQNs,
- previous.addedTypes, previous.addedModules, previous.createdFiles, previous.finishedFiles,
- previous.modifiedTypes, previous.aptGenerated);
- } catch (MissingPlatformError mpe) {
- //No platform - log & mark files as errornous
- if (JavaIndex.LOG.isLoggable(Level.FINEST)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("MultiPassCompileWorker caused an exception\nFile: %s\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- active.jfo.toUri().toString(),
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(Level.FINEST, message, mpe);
- }
- JavaCustomIndexer.brokenPlatform(context, files, mpe.getDiagnostic());
- return ParsingOutput.failure(moduleName.name, previous.file2FQNs,
- previous.addedTypes, previous.addedModules, previous.createdFiles, previous.finishedFiles,
- previous.modifiedTypes, previous.aptGenerated);
- } catch (Throwable t) {
- if (t instanceof ThreadDeath) {
- throw (ThreadDeath) t;
- }
- else {
- Level level = t instanceof FatalError ? Level.FINEST : Level.WARNING;
- if (JavaIndex.LOG.isLoggable(level)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("MultiPassCompileWorker caused an exception\nFile: %s\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- active == null ? null : active.jfo.toUri().toString(),
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(level, message, t); //NOI18N
- }
- jt = null;
- diagnosticListener.cleanDiagnostics();
- if ((state & ERR) != 0) {
- //When a javac failed with the Exception mark a file
- //causing this exceptin as compiled
- if (active != null)
- previous.finishedFiles.add(active.indexable);
- active = null;
- state = 0;
- } else {
- state |= ERR;
- }
- }
- }
- }
- return (state & MEMORY_LOW) == 0?
- ParsingOutput.success(moduleName.name, previous.file2FQNs,
- previous.addedTypes, previous.addedModules, previous.createdFiles, previous.finishedFiles,
- previous.modifiedTypes, previous.aptGenerated):
- ParsingOutput.lowMemory(moduleName.name, previous.file2FQNs,
- previous.addedTypes, previous.addedModules, previous.createdFiles, previous.finishedFiles,
- previous.modifiedTypes, previous.aptGenerated);
- }
-
- private void dumpSymFiles(
- final JavacTaskImpl jti,
- final Set<File> alreadyCreated,
- final Context ctx) throws IOException {
- if (jti != null) {
- final JavaFileManager jfm = jti.getContext().get(JavaFileManager.class);
- checkForMemLow = false;
- try {
- final Types types = Types.instance(jti.getContext());
- final Enter enter = Enter.instance(jti.getContext());
- final Symtab syms = Symtab.instance(jti.getContext());
- final HashMap<ClassSymbol, JCClassDecl> syms2trees = new HashMap<>();
- class ScanNested extends TreeScanner {
- private Env<AttrContext> env;
- private Set<Env<AttrContext>> checked = new HashSet<Env<AttrContext>>();
- private List<Env<AttrContext>> dependencies = new LinkedList<Env<AttrContext>>();
- public ScanNested(Env<AttrContext> env) {
- this.env = env;
- }
- @Override
- public void visitClassDef(JCClassDecl node) {
- if (node.sym != null) {
- Type st = types.supertype(node.sym.type);
- boolean envForSuperTypeFound = false;
- while (!envForSuperTypeFound && st != null && st.hasTag(TypeTag.CLASS)) {
- ClassSymbol c = st.tsym.outermostClass();
- Env<AttrContext> stEnv = enter.getEnv(c);
- if (stEnv != null && env != stEnv) {
- if (checked.add(stEnv)) {
- scan(stEnv.tree);
- if (TreeLoader.pruneTree(stEnv.tree, syms, syms2trees))
- dependencies.add(stEnv);
- }
- envForSuperTypeFound = true;
- }
- st = types.supertype(st);
- }
- }
- super.visitClassDef(node);
- }
- }
- final Set<Env<AttrContext>> processedEnvs = new HashSet<Env<AttrContext>>();
- File classes = JavaIndex.getClassFolder(ctx);
- for (Env<AttrContext> env : jti.getTodo()) {
- if (processedEnvs.add(env)) {
- ScanNested scanner = new ScanNested(env);
- scanner.scan(env.tree);
- for (Env<AttrContext> dep: scanner.dependencies) {
- if (processedEnvs.add(dep)) {
- dumpSymFile(jfm, jti, dep.enclClass.sym, alreadyCreated, classes, syms2trees);
- }
- }
- if (TreeLoader.pruneTree(env.tree, syms, syms2trees))
- dumpSymFile(jfm, jti, env.enclClass.sym, alreadyCreated, classes, syms2trees);
- }
- }
- } finally {
- checkForMemLow = true;
- }
- }
- }
-
- private void dumpSymFile(
- @NonNull final JavaFileManager jfm,
- @NonNull final JavacTaskImpl jti,
- @NullAllowed final ClassSymbol cs,
- @NonNull final Set<File> alreadyCreated,
- @NonNull final File classes,
- @NonNull final HashMap<ClassSymbol, JCClassDecl> syms2trees) throws IOException {
- if (cs == null) {
- //ClassDecl has no symbol because compilation was cancelled
- //by low memory before ENTER done.
- return;
- }
- JavaFileObject file = jfm.getJavaFileForOutput(StandardLocation.CLASS_OUTPUT,
- cs.flatname.toString(), JavaFileObject.Kind.CLASS, cs.sourcefile);
- if (file instanceof FileObjects.FileBase && !alreadyCreated.contains(((FileObjects.FileBase)file).getFile())) {
- TreeLoader.dumpSymFile(jfm, jti, cs, classes, syms2trees);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/ffc0de5a/java.source.base/src/org/netbeans/modules/java/source/indexing/OnePassCompileWorker.java
----------------------------------------------------------------------
diff --git a/java.source.base/src/org/netbeans/modules/java/source/indexing/OnePassCompileWorker.java b/java.source.base/src/org/netbeans/modules/java/source/indexing/OnePassCompileWorker.java
deleted file mode 100644
index f89b4cf..0000000
--- a/java.source.base/src/org/netbeans/modules/java/source/indexing/OnePassCompileWorker.java
+++ /dev/null
@@ -1,370 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.netbeans.modules.java.source.indexing;
-
-import com.sun.source.tree.CompilationUnitTree;
-import com.sun.tools.javac.api.JavacTaskImpl;
-import com.sun.tools.javac.code.Symbol.ClassSymbol;
-import com.sun.tools.javac.code.Type;
-import com.sun.tools.javac.code.TypeTag;
-import com.sun.tools.javac.code.Types;
-import com.sun.tools.javac.comp.Modules;
-import com.sun.tools.javac.tree.JCTree;
-import com.sun.tools.javac.tree.JCTree.JCClassDecl;
-import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
-import com.sun.tools.javac.tree.TreeScanner;
-import com.sun.tools.javac.util.CouplingAbort;
-import com.sun.tools.javac.util.FatalError;
-import com.sun.tools.javac.util.Log;
-import com.sun.tools.javac.util.MissingPlatformError;
-import java.io.File;
-import java.io.IOException;
-import java.util.*;
-import java.util.concurrent.Future;
-import java.util.logging.Level;
-import java.util.stream.Collectors;
-import java.util.stream.StreamSupport;
-import javax.annotation.processing.Processor;
-import javax.lang.model.element.Element;
-import javax.lang.model.element.ModuleElement;
-import javax.lang.model.element.TypeElement;
-import javax.tools.JavaFileObject;
-import org.netbeans.api.java.classpath.ClassPath;
-import org.netbeans.api.java.queries.CompilerOptionsQuery;
-import org.netbeans.api.java.source.ClasspathInfo;
-import org.netbeans.api.java.source.ElementHandle;
-import org.netbeans.lib.nbjavac.services.CancelAbort;
-import org.netbeans.lib.nbjavac.services.CancelService;
-import org.netbeans.modules.java.source.TreeLoader;
-import org.netbeans.modules.java.source.indexing.JavaCustomIndexer.CompileTuple;
-import org.netbeans.modules.java.source.parsing.FileManagerTransaction;
-import org.netbeans.modules.java.source.parsing.FileObjects;
-import org.netbeans.modules.java.source.parsing.JavacParser;
-import org.netbeans.modules.java.source.parsing.OutputFileManager;
-import org.netbeans.modules.java.source.usages.ClassNamesForFileOraculumImpl;
-import org.netbeans.modules.java.source.usages.ExecutableFilesIndex;
-import org.netbeans.modules.parsing.spi.indexing.Context;
-import org.netbeans.modules.parsing.spi.indexing.Indexable;
-import org.netbeans.modules.parsing.spi.indexing.SuspendStatus;
-import org.openide.filesystems.FileSystem;
-import org.openide.filesystems.FileUtil;
-import org.openide.util.Exceptions;
-import org.openide.util.Pair;
-
-/**
- *
- * @author Jan Lahoda, Dusan Balek
- */
-final class OnePassCompileWorker extends CompileWorker {
-
- @Override
- ParsingOutput compile(
- final ParsingOutput previous,
- final Context context,
- final JavaParsingContext javaContext,
- final Collection<? extends CompileTuple> files) {
- final Map<JavaFileObject, List<String>> file2FQNs = previous != null ? previous.file2FQNs : new HashMap<>();
- final Set<ElementHandle<TypeElement>> addedTypes = previous != null ? previous.addedTypes : new HashSet<>();
- final Set<ElementHandle<ModuleElement>> addedModules = previous != null ? previous.addedModules : new HashSet<>();
- final Set<File> createdFiles = previous != null ? previous.createdFiles : new HashSet<>();
- final Set<Indexable> finished = previous != null ? previous.finishedFiles : new HashSet<>();
- final Set<ElementHandle<TypeElement>> modifiedTypes = previous != null ? previous.modifiedTypes : new HashSet<>();
- final Set<javax.tools.FileObject> aptGenerated = previous != null ? previous.aptGenerated : new HashSet<>();
- final ClassNamesForFileOraculumImpl cnffOraculum = new ClassNamesForFileOraculumImpl(file2FQNs);
-
- final DiagnosticListenerImpl dc = new DiagnosticListenerImpl();
- final HashMap<JavaFileObject, Pair<CompilationUnitTree, CompileTuple>> jfo2units = new HashMap<JavaFileObject, Pair<CompilationUnitTree, CompileTuple>>();
- LinkedList<Pair<CompilationUnitTree, CompileTuple>> units = new LinkedList<Pair<CompilationUnitTree, CompileTuple>>();
- JavacTaskImpl jt = null;
-
- boolean nop = true;
- final SuspendStatus suspendStatus = context.getSuspendStatus();
- final SourcePrefetcher sourcePrefetcher = SourcePrefetcher.create(files, suspendStatus);
- try {
- while (sourcePrefetcher.hasNext()) {
- final CompileTuple tuple = sourcePrefetcher.next();
- try {
- if (tuple != null) {
- nop = false;
- if (context.isCancelled()) {
- return null;
- }
- try {
- if (isLowMemory(null)) {
- jt = null;
- units = null;
- jfo2units.clear();
- dc.cleanDiagnostics();
- freeMemory(false);
- }
- if (jt == null) {
- jt = JavacParser.createJavacTask(
- javaContext.getClasspathInfo(),
- dc,
- javaContext.getSourceLevel(),
- javaContext.getProfile(),
- cnffOraculum,
- javaContext.getFQNs(),
- new CancelService() {
- public @Override boolean isCanceled() {
- return context.isCancelled() || isLowMemory(null);
- }
- },
- tuple.aptGenerated ? null : APTUtils.get(context.getRoot()),
- CompilerOptionsQuery.getOptions(context.getRoot()));
- }
- for (CompilationUnitTree cut : jt.parse(tuple.jfo)) { //TODO: should be exactly one
- if (units != null) {
- Pair<CompilationUnitTree, CompileTuple> unit = Pair.<CompilationUnitTree, CompileTuple>of(cut, tuple);
- units.add(unit);
- jfo2units.put(tuple.jfo, unit);
- }
- computeFQNs(file2FQNs, cut, tuple);
- }
- Log.instance(jt.getContext()).nerrors = 0;
- } catch (CancelAbort ca) {
- if (context.isCancelled() && JavaIndex.LOG.isLoggable(Level.FINEST)) {
- JavaIndex.LOG.log(Level.FINEST, "OnePassCompileWorker was canceled in root: " + FileUtil.getFileDisplayName(context.getRoot()), ca); //NOI18N
- }
- } catch (Throwable t) {
- if (JavaIndex.LOG.isLoggable(Level.WARNING)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("OnePassCompileWorker caused an exception\nFile: %s\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- tuple.indexable.getURL().toString(),
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(Level.WARNING, message, t); //NOI18N
- }
- if (t instanceof ThreadDeath) {
- throw (ThreadDeath) t;
- } else {
- jt = null;
- units = null;
- dc.cleanDiagnostics();
- freeMemory(false);
- }
- }
- }
- } finally {
- sourcePrefetcher.remove();
- }
- }
- } finally {
- try {
- sourcePrefetcher.close();
- } catch (IOException ioe) {
- Exceptions.printStackTrace(ioe);
- }
- }
- final ModuleName moduleName = new ModuleName(javaContext.getModuleName());
- if (nop) {
- return ParsingOutput.success(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
-
- if (units == null || JavaCustomIndexer.NO_ONE_PASS_COMPILE_WORKER) {
- return ParsingOutput.failure(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
-
- CompileTuple active = null;
- final boolean aptEnabled = Optional.ofNullable(jt)
- .map((jtask) -> jtask.getProcessors())
- .map((it) -> it.iterator().hasNext())
- .orElse(Boolean.FALSE);
- final boolean[] flm = {true};
- try {
- final Queue<Future<Void>> barriers = new ArrayDeque<>();
- while(!units.isEmpty()) {
- if (context.isCancelled()) {
- return null;
- }
- final Pair<CompilationUnitTree, CompileTuple> unit = units.removeFirst();
- active = unit.second();
- if (finished.contains(active.indexable)) {
- continue;
- }
- if (isLowMemory(flm)) {
- return ParsingOutput.lowMemory(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
- final Iterable<? extends Element> types = jt.enterTrees(Collections.singletonList(unit.first()));
- if (jfo2units.remove(active.jfo) != null) {
- final Types ts = Types.instance(jt.getContext());
- final Indexable activeIndexable = active.indexable;
- class ScanNested extends TreeScanner {
- Set<Pair<CompilationUnitTree, CompileTuple>> dependencies = new LinkedHashSet<Pair<CompilationUnitTree, CompileTuple>>();
- @Override
- public void visitClassDef(JCClassDecl node) {
- if (node.sym != null) {
- Type st = ts.supertype(node.sym.type);
- boolean envForSuperTypeFound = false;
- while (!envForSuperTypeFound && st != null && st.hasTag(TypeTag.CLASS)) {
- ClassSymbol c = st.tsym.outermostClass();
- Pair<CompilationUnitTree, CompileTuple> u = jfo2units.remove(c.sourcefile);
- if (u != null && !finished.contains(u.second().indexable) && !u.second().indexable.equals(activeIndexable)) {
- if (dependencies.add(u)) {
- scan((JCCompilationUnit)u.first());
- }
- envForSuperTypeFound = true;
- }
- st = ts.supertype(st);
- }
- }
- super.visitClassDef(node);
- }
- }
- ScanNested scanner = new ScanNested();
- scanner.scan((JCCompilationUnit)unit.first());
- if (!scanner.dependencies.isEmpty()) {
- units.addFirst(unit);
- for (Pair<CompilationUnitTree, CompileTuple> pair : scanner.dependencies) {
- units.addFirst(pair);
- }
- continue;
- }
- }
- if (isLowMemory(flm)) {
- return ParsingOutput.lowMemory(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
- jt.analyze(types);
- if (aptEnabled) {
- JavaCustomIndexer.addAptGenerated(context, javaContext, active, aptGenerated);
- }
- if (isLowMemory(flm)) {
- return ParsingOutput.lowMemory(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
- javaContext.getFQNs().set(types, active.indexable.getURL());
- boolean[] main = new boolean[1];
- if (javaContext.getCheckSums().checkAndSet(
- active.indexable.getURL(),
- StreamSupport.stream(types.spliterator(), false)
- .filter((e) -> e.getKind().isClass() || e.getKind().isInterface())
- .map ((e) -> (TypeElement)e)
- .collect(Collectors.toList()),
- jt.getElements()) || context.isSupplementaryFilesIndexing()) {
- javaContext.analyze(Collections.singleton(unit.first()), jt, unit.second(), addedTypes, addedModules, main);
- } else {
- final Set<ElementHandle<TypeElement>> aTypes = new HashSet<>();
- javaContext.analyze(Collections.singleton(unit.first()), jt, unit.second(), aTypes, addedModules, main);
- addedTypes.addAll(aTypes);
- modifiedTypes.addAll(aTypes);
- }
- ExecutableFilesIndex.DEFAULT.setMainClass(context.getRoot().toURL(), active.indexable.getURL(), main[0]);
- JavaCustomIndexer.setErrors(context, active, dc);
- final boolean virtual = active.virtual;
- final JavacTaskImpl jtFin = jt;
- barriers.offer(FileManagerTransaction.runConcurrent(new FileSystem.AtomicAction(){
- @Override
- public void run() throws IOException {
- Iterable<? extends JavaFileObject> generatedFiles = jtFin.generate(types);
- if (!virtual) {
- for (JavaFileObject generated : generatedFiles) {
- if (generated instanceof FileObjects.FileBase) {
- createdFiles.add(((FileObjects.FileBase) generated).getFile());
- } else {
- // presumably should not happen
- }
- }
- }
- if (!moduleName.assigned) {
- ModuleElement module = ((JCTree.JCCompilationUnit)unit.first()).modle;
- if (module == null) {
- module = Modules.instance(jtFin.getContext()).getDefaultModule();
- }
- moduleName.name = module == null || module.isUnnamed() ?
- null :
- module.getQualifiedName().toString();
- moduleName.assigned = true;
- }
- }
- }));
- Log.instance(jt.getContext()).nerrors = 0;
- finished.add(active.indexable);
- }
- for (Future<Void> barrier : barriers) {
- barrier.get();
- }
- return ParsingOutput.success(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- } catch (CouplingAbort ca) {
- //Coupling error
- TreeLoader.dumpCouplingAbort(ca, null);
- } catch (OutputFileManager.InvalidSourcePath isp) {
- //Deleted project - log & ignore
- if (JavaIndex.LOG.isLoggable(Level.FINEST)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("OnePassCompileWorker caused an exception\nFile: %s\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- active.jfo.toUri().toString(),
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(Level.FINEST, message, isp);
- }
- } catch (MissingPlatformError mpe) {
- //No platform - log & mark files as errornous
- if (JavaIndex.LOG.isLoggable(Level.FINEST)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("OnePassCompileWorker caused an exception\nFile: %s\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- active.jfo.toUri().toString(),
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(Level.FINEST, message, mpe);
- }
- JavaCustomIndexer.brokenPlatform(context, files, mpe.getDiagnostic());
- } catch (CancelAbort ca) {
- if (isLowMemory(flm)) {
- return ParsingOutput.lowMemory(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- } else if (JavaIndex.LOG.isLoggable(Level.FINEST)) {
- JavaIndex.LOG.log(Level.FINEST, "OnePassCompileWorker was canceled in root: " + FileUtil.getFileDisplayName(context.getRoot()), ca); //NOI18N
- }
- } catch (Throwable t) {
- if (t instanceof ThreadDeath) {
- throw (ThreadDeath) t;
- } else {
- Level level = t instanceof FatalError ? Level.FINEST : Level.WARNING;
- if (JavaIndex.LOG.isLoggable(level)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("OnePassCompileWorker caused an exception\nFile: %s\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- active.jfo.toUri().toString(),
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(level, message, t); //NOI18N
- }
- }
- }
- return ParsingOutput.failure(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/ffc0de5a/java.source.base/src/org/netbeans/modules/java/source/indexing/SourcePrefetcher.java
----------------------------------------------------------------------
diff --git a/java.source.base/src/org/netbeans/modules/java/source/indexing/SourcePrefetcher.java b/java.source.base/src/org/netbeans/modules/java/source/indexing/SourcePrefetcher.java
index ab1343a..2cdb35b 100644
--- a/java.source.base/src/org/netbeans/modules/java/source/indexing/SourcePrefetcher.java
+++ b/java.source.base/src/org/netbeans/modules/java/source/indexing/SourcePrefetcher.java
@@ -31,7 +31,7 @@ import org.netbeans.modules.parsing.spi.indexing.SuspendStatus;
*
* @author Tomas Zezula
*/
-class SourcePrefetcher implements Iterator<CompileTuple>, /*Auto*/Closeable {
+public class SourcePrefetcher implements Iterator<CompileTuple>, /*Auto*/Closeable {
private final Iterator<? extends CompileTuple> iterator;
//@NotThreadSafe
@@ -79,7 +79,7 @@ class SourcePrefetcher implements Iterator<CompileTuple>, /*Auto*/Closeable {
}
- static SourcePrefetcher create(
+ public static SourcePrefetcher create(
@NonNull final Collection<? extends CompileTuple> files,
@NonNull final SuspendStatus suspendStatus) {
return new SourcePrefetcher(JavaIndexerWorker.getCompileTupleIterator(files, suspendStatus));
http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/ffc0de5a/java.source.base/src/org/netbeans/modules/java/source/indexing/SuperOnePassCompileWorker.java
----------------------------------------------------------------------
diff --git a/java.source.base/src/org/netbeans/modules/java/source/indexing/SuperOnePassCompileWorker.java b/java.source.base/src/org/netbeans/modules/java/source/indexing/SuperOnePassCompileWorker.java
deleted file mode 100644
index 870a01b..0000000
--- a/java.source.base/src/org/netbeans/modules/java/source/indexing/SuperOnePassCompileWorker.java
+++ /dev/null
@@ -1,375 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.netbeans.modules.java.source.indexing;
-
-import com.sun.source.tree.CompilationUnitTree;
-import com.sun.source.tree.PackageTree;
-import com.sun.source.tree.Tree;
-import com.sun.tools.javac.api.JavacTaskImpl;
-import com.sun.tools.javac.code.Symbol.ClassSymbol;
-import com.sun.tools.javac.code.Symbol.TypeSymbol;
-import com.sun.tools.javac.comp.AttrContext;
-import com.sun.tools.javac.comp.Enter;
-import com.sun.tools.javac.comp.Env;
-import com.sun.tools.javac.comp.Modules;
-import com.sun.tools.javac.tree.JCTree;
-import com.sun.tools.javac.tree.JCTree.JCClassDecl;
-import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
-import com.sun.tools.javac.tree.JCTree.JCPackageDecl;
-import org.netbeans.lib.nbjavac.services.CancelAbort;
-import org.netbeans.lib.nbjavac.services.CancelService;
-import com.sun.tools.javac.util.CouplingAbort;
-import com.sun.tools.javac.util.FatalError;
-import com.sun.tools.javac.util.Log;
-import com.sun.tools.javac.util.MissingPlatformError;
-import java.io.File;
-import java.io.IOException;
-import java.util.*;
-import java.util.Map.Entry;
-import java.util.concurrent.Future;
-import java.util.logging.Level;
-import java.util.stream.Collectors;
-import javax.annotation.processing.Processor;
-import javax.lang.model.element.Element;
-import javax.lang.model.element.ElementKind;
-import javax.lang.model.element.ModuleElement;
-import javax.lang.model.element.TypeElement;
-import javax.tools.JavaFileObject;
-import org.netbeans.api.java.classpath.ClassPath;
-import org.netbeans.api.java.queries.CompilerOptionsQuery;
-import org.netbeans.api.java.source.ClasspathInfo;
-import org.netbeans.api.java.source.ElementHandle;
-import org.netbeans.modules.java.source.TreeLoader;
-import org.netbeans.modules.java.source.indexing.JavaCustomIndexer.CompileTuple;
-import org.netbeans.modules.java.source.parsing.FileManagerTransaction;
-import org.netbeans.modules.java.source.parsing.FileObjects;
-import org.netbeans.modules.java.source.parsing.JavacParser;
-import org.netbeans.modules.java.source.parsing.OutputFileManager;
-import org.netbeans.modules.java.source.usages.ClassNamesForFileOraculumImpl;
-import org.netbeans.modules.java.source.usages.ExecutableFilesIndex;
-import org.netbeans.modules.parsing.spi.indexing.Context;
-import org.netbeans.modules.parsing.spi.indexing.Indexable;
-import org.netbeans.modules.parsing.spi.indexing.SuspendStatus;
-import org.openide.filesystems.FileSystem;
-import org.openide.filesystems.FileUtil;
-import org.openide.util.Exceptions;
-
-/**
- *
- * @author Dusan Balek
- */
-final class SuperOnePassCompileWorker extends CompileWorker {
-
- @Override
- ParsingOutput compile(
- final ParsingOutput previous,
- final Context context,
- final JavaParsingContext javaContext,
- final Collection<? extends CompileTuple> files) {
- final Map<JavaFileObject, List<String>> file2FQNs = previous != null ? previous.file2FQNs : new HashMap<>();
- final Set<ElementHandle<TypeElement>> addedTypes = previous != null ? previous.addedTypes : new HashSet<>();
- final Set<ElementHandle<ModuleElement>> addedModules = previous != null ? previous.addedModules : new HashSet<>();
- final Set<File> createdFiles = previous != null ? previous.createdFiles : new HashSet<>();
- final Set<Indexable> finished = previous != null ? previous.finishedFiles : new HashSet<>();
- final Set<ElementHandle<TypeElement>> modifiedTypes = previous != null ? previous.modifiedTypes : new HashSet<>();
- final Set<javax.tools.FileObject> aptGenerated = previous != null ? previous.aptGenerated : new HashSet<>();
- final ClassNamesForFileOraculumImpl cnffOraculum = new ClassNamesForFileOraculumImpl(file2FQNs);
-
- final DiagnosticListenerImpl dc = new DiagnosticListenerImpl();
- final LinkedList<CompilationUnitTree> trees = new LinkedList<CompilationUnitTree>();
- Map<CompilationUnitTree, CompileTuple> units = new IdentityHashMap<CompilationUnitTree, CompileTuple>();
- JavacTaskImpl jt = null;
-
- boolean nop = true;
- final SuspendStatus suspendStatus = context.getSuspendStatus();
- final SourcePrefetcher sourcePrefetcher = SourcePrefetcher.create(files, suspendStatus);
- try {
- final boolean flm[] = {true};
- while (sourcePrefetcher.hasNext()) {
- final CompileTuple tuple = sourcePrefetcher.next();
- try {
- if (tuple != null) {
- nop = false;
- if (context.isCancelled()) {
- return null;
- }
- try {
- if (isLowMemory(flm)) {
- jt = null;
- units = null;
- trees.clear();
- dc.cleanDiagnostics();
- freeMemory(false);
- }
- if (jt == null) {
- jt = JavacParser.createJavacTask(
- javaContext.getClasspathInfo(),
- dc,
- javaContext.getSourceLevel(),
- javaContext.getProfile(),
- cnffOraculum,
- javaContext.getFQNs(),
- new CancelService() {
- public @Override boolean isCanceled() {
- return context.isCancelled() || isLowMemory(null);
- }
- },
- tuple.aptGenerated ? null : APTUtils.get(context.getRoot()),
- CompilerOptionsQuery.getOptions(context.getRoot()));
- }
- for (CompilationUnitTree cut : jt.parse(tuple.jfo)) { //TODO: should be exactly one
- if (units != null) {
- trees.add(cut);
- units.put(cut, tuple);
- }
- computeFQNs(file2FQNs, cut, tuple);
- }
- Log.instance(jt.getContext()).nerrors = 0;
- } catch (CancelAbort ca) {
- if (context.isCancelled() && JavaIndex.LOG.isLoggable(Level.FINEST)) {
- JavaIndex.LOG.log(Level.FINEST, "SuperOnePassCompileWorker was canceled in root: " + FileUtil.getFileDisplayName(context.getRoot()), ca); //NOI18N
- }
- } catch (Throwable t) {
- if (JavaIndex.LOG.isLoggable(Level.WARNING)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("SuperOnePassCompileWorker caused an exception\nFile: %s\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- tuple.indexable.getURL().toString(),
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(Level.WARNING, message, t); //NOI18N
- }
- if (t instanceof ThreadDeath) {
- throw (ThreadDeath) t;
- } else {
- jt = null;
- units = null;
- dc.cleanDiagnostics();
- freeMemory(false);
- }
- }
- }
- } finally {
- sourcePrefetcher.remove();
- }
- }
- } finally {
- try {
- sourcePrefetcher.close();
- } catch (IOException ex) {
- Exceptions.printStackTrace(ex);
- }
- }
- ModuleName moduleName = new ModuleName(javaContext.getModuleName());
- if (nop) {
- return ParsingOutput.success(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
- if (jt == null || units == null || JavaCustomIndexer.NO_ONE_PASS_COMPILE_WORKER) {
- return ParsingOutput.failure(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
- if (context.isCancelled()) {
- return null;
- }
- if (isLowMemory(null)) {
- return ParsingOutput.lowMemory(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
- Iterable<? extends Processor> processors = jt.getProcessors();
- boolean aptEnabled = processors != null && processors.iterator().hasNext();
- try {
- final Iterable<? extends Element> types = jt.enter(trees);
- if (context.isCancelled()) {
- return null;
- }
- if (isLowMemory(null)) {
- return ParsingOutput.lowMemory(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
- final Map<Element, CompileTuple> clazz2Tuple = new IdentityHashMap<Element, CompileTuple>();
- Enter enter = Enter.instance(jt.getContext());
- for (Element type : types) {
- if (type.getKind().isClass() || type.getKind().isInterface() || type.getKind() == ElementKind.MODULE) {
- Env<AttrContext> typeEnv = enter.getEnv((TypeSymbol) type);
- if (typeEnv == null) {
- JavaIndex.LOG.log(Level.FINE, "No Env for: {0}", ((TypeSymbol) type).getQualifiedName());
- continue;
- }
- clazz2Tuple.put(type, units.get(typeEnv.toplevel));
- }
- }
- jt.analyze(types);
- if (context.isCancelled()) {
- return null;
- }
- if (isLowMemory(null)) {
- return ParsingOutput.lowMemory(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
- for (Entry<CompilationUnitTree, CompileTuple> unit : units.entrySet()) {
- CompileTuple active = unit.getValue();
- if (aptEnabled) {
- JavaCustomIndexer.addAptGenerated(context, javaContext, active, aptGenerated);
- }
- List<Element> activeTypes = new ArrayList<>();
- if (unit.getValue().jfo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE)) {
- final PackageTree pt = unit.getKey().getPackage();
- if (pt instanceof JCPackageDecl) {
- final Element sym = ((JCPackageDecl)pt).packge;
- if (sym != null)
- activeTypes.add(sym);
- }
- } else {
- for (Tree tree : unit.getKey().getTypeDecls()) {
- if (tree instanceof JCTree) {
- final JCTree jct = (JCTree)tree;
- if (jct.getTag() == JCTree.Tag.CLASSDEF) {
- final Element sym = ((JCClassDecl)tree).sym;
- if (sym != null)
- activeTypes.add(sym);
- } else if (jct.getTag() == JCTree.Tag.MODULEDEF) {
- final Element sym = ((JCModuleDecl)tree).sym;
- if (sym != null)
- activeTypes.add(sym);
- }
- }
- }
- }
- javaContext.getFQNs().set(activeTypes, active.indexable.getURL());
- boolean[] main = new boolean[1];
- if (javaContext.getCheckSums().checkAndSet(
- active.indexable.getURL(),
- activeTypes.stream()
- .filter((e) -> e.getKind().isClass() || e.getKind().isInterface())
- .map ((e) -> (TypeElement)e)
- .collect(Collectors.toList()),
- jt.getElements()) || context.isSupplementaryFilesIndexing()) {
- javaContext.analyze(Collections.singleton(unit.getKey()), jt, active, addedTypes, addedModules, main);
- } else {
- final Set<ElementHandle<TypeElement>> aTypes = new HashSet<>();
- javaContext.analyze(Collections.singleton(unit.getKey()), jt, active, aTypes, addedModules, main);
- addedTypes.addAll(aTypes);
- modifiedTypes.addAll(aTypes);
- }
- ExecutableFilesIndex.DEFAULT.setMainClass(context.getRoot().toURL(), active.indexable.getURL(), main[0]);
- JavaCustomIndexer.setErrors(context, active, dc);
- }
- if (context.isCancelled()) {
- return null;
- }
- if (isLowMemory(null)) {
- return ParsingOutput.lowMemory(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
- final JavacTaskImpl jtFin = jt;
- final Future<Void> done = FileManagerTransaction.runConcurrent(new FileSystem.AtomicAction() {
- @Override
- public void run() throws IOException {
- for (Element type : types) {
- Iterable<? extends JavaFileObject> generatedFiles = jtFin.generate(Collections.singletonList(type));
- CompileTuple unit = clazz2Tuple.get(type);
- if (unit == null || !unit.virtual) {
- for (JavaFileObject generated : generatedFiles) {
- if (generated instanceof FileObjects.FileBase) {
- createdFiles.add(((FileObjects.FileBase) generated).getFile());
- } else {
- // presumably should not happen
- }
- }
- }
- }
- if (!moduleName.assigned) {
- ModuleElement module = !trees.isEmpty() ?
- ((JCTree.JCCompilationUnit)trees.getFirst()).modle :
- null;
- if (module == null) {
- module = Modules.instance(jtFin.getContext()).getDefaultModule();
- }
- moduleName.name = module == null || module.isUnnamed() ?
- null :
- module.getQualifiedName().toString();
- moduleName.assigned = true;
- }
- }
- });
- for (Entry<CompilationUnitTree, CompileTuple> unit : units.entrySet()) {
- finished.add(unit.getValue().indexable);
- }
- done.get();
- return ParsingOutput.success(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- } catch (CouplingAbort ca) {
- //Coupling error
- TreeLoader.dumpCouplingAbort(ca, null);
- } catch (OutputFileManager.InvalidSourcePath isp) {
- //Deleted project - log & ignore
- if (JavaIndex.LOG.isLoggable(Level.FINEST)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("SuperOnePassCompileWorker caused an exception\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(Level.FINEST, message, isp);
- }
- } catch (MissingPlatformError mpe) {
- //No platform - log & mark files as errornous
- if (JavaIndex.LOG.isLoggable(Level.FINEST)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("SuperOnePassCompileWorker caused an exception\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(Level.FINEST, message, mpe);
- }
- JavaCustomIndexer.brokenPlatform(context, files, mpe.getDiagnostic());
- } catch (CancelAbort ca) {
- if (isLowMemory(null)) {
- return ParsingOutput.lowMemory(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- } else if (JavaIndex.LOG.isLoggable(Level.FINEST)) {
- JavaIndex.LOG.log(Level.FINEST, "SuperOnePassCompileWorker was canceled in root: " + FileUtil.getFileDisplayName(context.getRoot()), ca); //NOI18N
- }
- } catch (Throwable t) {
- if (t instanceof ThreadDeath) {
- throw (ThreadDeath) t;
- } else {
- Level level = t instanceof FatalError ? Level.FINEST : Level.WARNING;
- if (JavaIndex.LOG.isLoggable(level)) {
- final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT);
- final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE);
- final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
- final String message = String.format("SuperOnePassCompileWorker caused an exception\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N
- FileUtil.getFileDisplayName(context.getRoot()),
- bootPath == null ? null : bootPath.toString(),
- classPath == null ? null : classPath.toString(),
- sourcePath == null ? null : sourcePath.toString()
- );
- JavaIndex.LOG.log(level, message, t); //NOI18N
- }
- }
- }
- return ParsingOutput.failure(moduleName.name, file2FQNs, addedTypes, addedModules, createdFiles, finished, modifiedTypes, aptGenerated);
- }
-}