Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling partial reparse; improving and detection of mistakes. #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void createAnnotations(CompilationInfo info, StyledDocument doc, Map<Ele

@Override
public int getPriority() {
return Integer.MAX_VALUE;
return Integer.MAX_VALUE - 1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ public enum CacheClearPolicy {
public JavacTask getJavacTask(CompilationInfo info) {
return info.impl.getJavacTask();
}
@Override
public CompilationInfoImpl getCompilationInfoImpl(CompilationInfo info) {
return info.impl;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.sun.source.util.JavacTask;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.modules.java.source.parsing.CompilationInfoImpl;

/**
*
Expand Down Expand Up @@ -61,5 +62,6 @@ protected CompilationInfoAccessor() {
}

public abstract JavacTask getJavacTask(CompilationInfo info);
public abstract CompilationInfoImpl getCompilationInfoImpl(CompilationInfo info);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.sun.tools.javac.util.Log;

import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -84,6 +86,7 @@ public final class CompilationInfoImpl {
final AbstractSourceFileObject jfo;
//@NotThreadSafe //accessed under parser lock
private Snapshot snapshot;
private Reference<Snapshot> partialReparseLastGoodSnapshot;
private final JavacParser parser;
private final boolean isClassFile;
private final boolean isDetached;
Expand All @@ -102,7 +105,7 @@ public final class CompilationInfoImpl {
* @param detached true if the CompilationInfoImpl is detached from parsing infrastructure.
* @throws java.io.IOException
*/
CompilationInfoImpl (final JavacParser parser,
public CompilationInfoImpl (final JavacParser parser,
final FileObject file,
final FileObject root,
final JavacTaskImpl javacTask,
Expand All @@ -116,6 +119,7 @@ public final class CompilationInfoImpl {
this.file = file;
this.root = root;
this.snapshot = snapshot;
this.partialReparseLastGoodSnapshot = new SoftReference<>(snapshot);
assert file == null || snapshot != null;
this.jfo = file != null ?
FileObjects.sourceFileObject(file, root, JavaFileFilterQuery.getFilter(file), snapshot.getText()) :
Expand Down Expand Up @@ -175,7 +179,16 @@ public void update (final Snapshot snapshot) throws IOException {
public Snapshot getSnapshot () {
return this.snapshot;
}


public Snapshot getPartialReparseLastGoodSnapshot() {
return partialReparseLastGoodSnapshot != null ? partialReparseLastGoodSnapshot.get()
: null;
}

public void setPartialReparseLastGoodSnapshot(Snapshot snapshot) {
this.partialReparseLastGoodSnapshot = new SoftReference<>(snapshot);
}

/**
* Returns the current phase of the {@link JavaSource}.
* @return {@link JavaSource.Phase} the state which was reached by the {@link JavaSource}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacTaskImpl;
import com.sun.tools.javac.api.JavacTool;
Expand Down Expand Up @@ -60,7 +57,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -76,7 +72,6 @@
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.Document;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
Expand Down Expand Up @@ -159,7 +154,6 @@ public class JavacParser extends Parser {
//Command line switch disabling partial reparse
private static final boolean DISABLE_PARTIAL_REPARSE = Boolean.getBoolean("org.netbeans.modules.java.source.parsing.JavacParser.no_reparse"); //NOI18N
private static final boolean DISABLE_PARAMETER_NAMES_READING = Boolean.getBoolean("org.netbeans.modules.java.source.parsing.JavacParser.no_parameter_names"); //NOI18N
private static final boolean VERIFY_PARTIAL_REPARSE = Boolean.getBoolean("org.netbeans.modules.java.source.parsing.JavacParser.verify_partial_reparse"); //NOI18N
public static final String LOMBOK_DETECTED = "lombokDetected";

/**
Expand Down Expand Up @@ -425,9 +419,6 @@ private void parseImpl(
positions.clear();
ciImpl = createCurrentInfo(this, file, root, snapshot, null, null, oldParsedTrees);
LOGGER.fine("\t:created new javac"); //NOI18N
} else if (VERIFY_PARTIAL_REPARSE) {
CompilationInfoImpl verifyInfo = new CompilationInfoImpl(this, file, root, null, null, snapshot, true);
verifyCompilationInfos(ciImpl, verifyInfo);
}
break;
default:
Expand Down Expand Up @@ -567,6 +558,10 @@ public void cancel (final @NonNull CancelReason reason, final @NonNull SourceMod
}
}

public void cancelParse() {
parserCanceled.set(true);
}

public void resultFinished (boolean isCancelable) {
if (isCancelable) {
ClassIndexImpl.cancel.remove();
Expand Down Expand Up @@ -885,66 +880,6 @@ public static JavacTaskImpl createJavacTask (
files);
}

private void verifyCompilationInfos(CompilationInfoImpl reparsed, CompilationInfoImpl verifyInfo) throws IOException {
String failInfo = "";
//move to phase, and verify
if (verifyInfo.toPhase(reparsed.getPhase()) != reparsed.getPhase()) {
failInfo += "Expected phase: " + reparsed.getPhase() + ", actual phase: " + verifyInfo.getPhase();
}
//verify diagnostics:
Set<String> reparsedDiags = (Set<String>) reparsed.getDiagnostics().stream().map(this::diagnosticToString).collect(Collectors.toSet());
Set<String> verifyDiags = (Set<String>) verifyInfo.getDiagnostics().stream().map(this::diagnosticToString).collect(Collectors.toSet());
System.err.println("reparsedDiags=" + reparsedDiags);
System.err.println("verifyDiags=" + verifyDiags);
if (!Objects.equals(reparsedDiags, verifyDiags)) {
failInfo += "Expected diags: " + reparsedDiags + ", actual diags: " + verifyDiags;
}
String reparsedTree = treeToString(reparsed, reparsed.getCompilationUnit());
String verifyTree = treeToString(verifyInfo, verifyInfo.getCompilationUnit());
System.err.println("reparsedTree=" + reparsedTree);
System.err.println("verifyTree=" + verifyTree);
if (!Objects.equals(reparsedTree, verifyTree)) {
failInfo += "Expected tree: " + reparsedTree + ", actual tree: " + verifyTree;
}
if (!failInfo.isEmpty()) {
throw new IllegalStateException("Partial reparse didn't work properly; original text: " + ciImpl.getText() + ", new text: " + verifyInfo.getText() + "; failInfo=" + failInfo);
}
}

private String diagnosticToString(Diagnostic<JavaFileObject> d) {
return d.getSource().toUri().toString() + ":" +
d.getKind() + ":" +
d.getStartPosition() + ":" +
d.getPosition() + ":" +
d.getEndPosition() + ":" +
d.getLineNumber() + ":" +
d.getColumnNumber() + ":" +
d.getCode() + ":" +
d.getMessage(null);
}

private String treeToString(CompilationInfoImpl info, CompilationUnitTree cut) {
StringBuilder dump = new StringBuilder();
new TreePathScanner<Void, Void>() {
@Override
public Void scan(Tree tree, Void p) {
if (tree == null) {
dump.append("null,");
} else {
TreePath tp = new TreePath(getCurrentPath(), tree);
dump.append(tree.getKind()).append(":");
dump.append(Trees.instance(info.getJavacTask()).getSourcePositions().getStartPosition(tp.getCompilationUnit(), tree)).append(":");
dump.append(Trees.instance(info.getJavacTask()).getSourcePositions().getEndPosition(tp.getCompilationUnit(), tree)).append(":");
dump.append(String.valueOf(Trees.instance(info.getJavacTask()).getElement(tp))).append(":");
dump.append(String.valueOf(Trees.instance(info.getJavacTask()).getTypeMirror(tp))).append(":");
dump.append(",");
}
return super.scan(tree, p);
}
}.scan(cut, null);
return dump.toString();
}

private static enum ConfigFlags {
BACKGROUND_COMPILATION,
MULTI_SOURCE,
Expand Down Expand Up @@ -1292,25 +1227,15 @@ public static void logTime (FileObject source, Phase phase, long time) {
TIME_LOGGER.log(Level.FINE, message, new Object[] {source, time});
}

/**
* Dumps the source code to the file. Used for parser debugging. Only a limited number
* of dump files is used. If the last file exists, this method doesn't dump anything.
*
* @param info CompilationInfo for which the error occurred.
* @param exc exception to write to the end of dump file
*/
public static void dumpSource(CompilationInfoImpl info, Throwable exc) {
public static File createDumpFile(CompilationInfoImpl info) {
String userDir = System.getProperty("netbeans.user");
if (userDir == null) {
return;
return null;
}
String dumpDir = userDir + "/var/log/"; //NOI18N
String src = info.getText();
FileObject file = info.getFileObject();
String fileName = FileUtil.getFileDisplayName(file);
String origName = file.getName();
File f = new File(dumpDir + origName + ".dump"); // NOI18N
boolean dumpSucceeded = false;
int i = 1;
while (i < MAX_DUMPS) {
if (!f.exists()) {
Expand All @@ -1319,7 +1244,23 @@ public static void dumpSource(CompilationInfoImpl info, Throwable exc) {
f = new File(dumpDir + origName + '_' + i + ".dump"); // NOI18N
i++;
}
if (!f.exists()) {
return !f.exists() ? f : null;
}

/**
* Dumps the source code to the file. Used for parser debugging. Only a limited number
* of dump files is used. If the last file exists, this method doesn't dump anything.
*
* @param info CompilationInfo for which the error occurred.
* @param exc exception to write to the end of dump file
*/
public static void dumpSource(CompilationInfoImpl info, Throwable exc) {
String src = info.getText();
FileObject file = info.getFileObject();
String fileName = FileUtil.getFileDisplayName(file);
File f = createDumpFile(info);
boolean dumpSucceeded = false;
if (f != null) {
try {
OutputStream os = new FileOutputStream(f);
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF-8"))) { //NOI18N
Expand Down Expand Up @@ -1357,7 +1298,7 @@ public static void dumpSource(CompilationInfoImpl info, Throwable exc) {
LOGGER.log(Level.WARNING,
"Dump could not be written. Either dump file could not " + // NOI18N
"be created or all dump files were already used. Please " + // NOI18N
"check that you have write permission to '" + dumpDir + "' and " + // NOI18N
"check that you have write permission to '" + (f != null ? f.getParent() : "var/log") + "' and " + // NOI18N
"clean all *.dump files in that directory."); // NOI18N
}
}
Expand Down
Loading