Skip to content

Commit

Permalink
minor fix-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Oct 27, 2020
1 parent af3c944 commit f0d0bde
Show file tree
Hide file tree
Showing 49 changed files with 950 additions and 1,028 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5150,16 +5150,16 @@ public void testCompileStatic9771() {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"class Main {\n" +
" private final Map<String, Boolean> map = [:]\n" +
" void test() {\n" +
" { ->\n" +
" map['key'] = true\n" +
" }.call()\n" +
" print map\n" +
" }\n" +
" static main(args) {\n" +
" newInstance().test()\n" +
" }\n" +
" private final Map<String, Boolean> map = [:]\n" +
" void test() {\n" +
" { ->\n" +
" map['key'] = true\n" +
" }.call()\n" +
" print map\n" +
" }\n" +
" static main(args) {\n" +
" newInstance().test()\n" +
" }\n" +
"}\n",
};
//@formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package org.codehaus.jdt.groovy.integration.internal;

import java.util.Collections;
import java.util.Optional;

import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration;
import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyParser;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.groovy.core.util.ReflectionUtils;
import org.eclipse.jdt.internal.compiler.CompilationResult;
Expand All @@ -32,15 +32,16 @@
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.core.search.indexing.IndexingParser;
import org.eclipse.jdt.internal.core.util.Util;
import org.eclipse.jdt.internal.core.util.Messages;

class MultiplexingIndexingParser extends IndexingParser {

private final SourceElementNotifier notifier;
private final boolean reportReferenceInfo;
private ISourceElementRequestor requestor;

MultiplexingIndexingParser(final ISourceElementRequestor requestor, final IProblemFactory problemFactory, final CompilerOptions options, final boolean reportLocalDeclarations, final boolean optimizeStringLiterals, final boolean useSourceJavadocParser) {
MultiplexingIndexingParser(final ISourceElementRequestor requestor, final IProblemFactory problemFactory, final CompilerOptions options,
final boolean reportLocalDeclarations, final boolean optimizeStringLiterals, final boolean useSourceJavadocParser) {
super(requestor, problemFactory, options, reportLocalDeclarations, optimizeStringLiterals, useSourceJavadocParser);
this.notifier = ReflectionUtils.getPrivateField(SourceElementParser.class, "notifier", this);
this.reportReferenceInfo = reportLocalDeclarations;
Expand All @@ -56,25 +57,17 @@ public void setRequestor(final ISourceElementRequestor requestor) {
@Override
public CompilationUnitDeclaration parseCompilationUnit(final ICompilationUnit compilationUnit, final boolean fullParse, final IProgressMonitor pm) {
if (GroovyParser.isGroovyParserEligible(compilationUnit, readManager)) {
// ASSUMPTIONS:
// 1) parsing is for the entire CU (ie- from character 0 to compilationUnit.getContents().length)
// 2) nodesToCategories map is not necessary. I think it has something to do with JavaDoc, but not sure
// 3) there is no difference between a diet and full parse in the groovy works, so can ignore the fullParse parameter

char[] contents = GroovyParser.getContents(compilationUnit, readManager);
String fileName = CharOperation.charToString(compilationUnit.getFileName());
CompilationResult compilationResult = new CompilationResult(compilationUnit, 0, 0, options.maxProblemsPerUnit);
CompilationResult compilationResult = new CompilationResult(compilationUnit, 0, 1, options.maxProblemsPerUnit);
GroovyCompilationUnitDeclaration gcud = new GroovyParser(options, problemReporter, false, true).dietParse(contents, fileName, compilationResult);

Optional.ofNullable(gcud.getModuleNode()).ifPresent(module -> {
try {
new GroovyIndexingVisitor(requestor).visitModule(module);
} catch (RuntimeException e) {
Util.log(e);
}
});
if (pm != null && pm.isCanceled())
throw new OperationCanceledException(Messages.operation_cancelled);

new GroovyIndexingVisitor(requestor).visitModule(gcud.getModuleNode());

notifier.notifySourceElementRequestor(gcud, 0, contents.length, reportReferenceInfo, gcud.sourceEnds, Collections.EMPTY_MAP);
notifier.notifySourceElementRequestor(gcud, 0, contents.length, reportReferenceInfo, gcud.sourceEnds, Collections.emptyMap());

return gcud;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public CompilationUnitDeclaration parseCompilationUnit(final ICompilationUnit co
// FIXASC Is it ok to use a new parser here everytime? If we don't we sometimes recurse back into the first one.
// FIXASC ought to reuse to ensure types end up in same groovy CU
GroovyParser groovyParser = new GroovyParser(this.groovyParser.requestor, options, problemReporter, !disableGlobalXforms, true);
CompilationResult compilationResult = new CompilationResult(compilationUnit, 0, 0, options.maxProblemsPerUnit);
CompilationResult compilationResult = new CompilationResult(compilationUnit, 0, 1, options.maxProblemsPerUnit);
GroovyCompilationUnitDeclaration compUnitDecl = groovyParser.dietParse(contents, fileName, compilationResult);

scanner.setSource(contents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import org.codehaus.jdt.groovy.core.dom.GroovyCompilationUnit;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.Flags;
Expand Down Expand Up @@ -251,7 +252,9 @@ public boolean processToPhase(int phase) {
GroovyLogManager.manager.log(TraceCategory.COMPILER, e.getBugText());
}

if (e.getCause() instanceof AbortCompilation) {
if (e.getCause() instanceof OperationCanceledException) {
throw (OperationCanceledException) e.getCause();
} else if (e.getCause() instanceof AbortCompilation) {
AbortCompilation abort = (AbortCompilation) e.getCause();
if (!abort.isSilent) {
if (abort.problem != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,6 @@

public class GroovyCompilationUnit extends CompilationUnit {

private class GroovyErrorHandlingPolicy implements IErrorHandlingPolicy {

final boolean stopOnFirst;

GroovyErrorHandlingPolicy(boolean stopOnFirst) {
this.stopOnFirst = stopOnFirst;
}

@Override
public boolean proceedOnErrors() {
return !stopOnFirst;
}

@Override
public boolean stopOnFirstError() {
return stopOnFirst;
}

@Override
public boolean ignoreAllErrors() {
// TODO is this the right decision here? New method with java8 support
return false;
}

}

public GroovyCompilationUnit(PackageFragment parent, String name, WorkingCopyOwner owner) {
super(parent, name, owner);
}
Expand Down Expand Up @@ -183,28 +157,13 @@ public void discardWorkingCopy() throws JavaModelException {
* working copy info is about to be discared if useCount <= 1
*/
private boolean workingCopyInfoWillBeDiscarded(JavaModelManager.PerWorkingCopyInfo info) {
return info != null && ((Integer) ReflectionUtils.getPrivateField(JavaModelManager.PerWorkingCopyInfo.class, "useCount", info)).intValue() <= 1;
return (info != null && ((Integer) ReflectionUtils.getPrivateField(JavaModelManager.PerWorkingCopyInfo.class, "useCount", info)).intValue() <= 1);
}

/**
* Tracks how deep we are in recursive calls to {@link #buildStructure}.
*/
private static final ThreadLocalAtomicInteger depth = new ThreadLocalAtomicInteger();
private static class ThreadLocalAtomicInteger extends ThreadLocal<AtomicInteger> {
@Override
protected AtomicInteger initialValue() {
return new AtomicInteger();
}
int intValue() {
return get().get();
}
void increment() {
get().incrementAndGet();
}
void decrement() {
get().decrementAndGet();
}
}

@Override
protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource)
Expand Down Expand Up @@ -405,26 +364,24 @@ protected void maybeCacheModuleNode(JavaModelManager.PerWorkingCopyInfo perWorki
ModuleNodeMapper.getInstance().maybeCacheModuleNode(perWorkingCopyInfo, compilationUnitDeclaration);
}

/*
* Copied from super class, but changed so that a custom ReconcileWorkingCopyOperation can be run
*/
@Override
public org.eclipse.jdt.core.dom.CompilationUnit reconcile(int astLevel, int reconcileFlags, WorkingCopyOwner workingCopyOwner, IProgressMonitor monitor)
throws JavaModelException {
if (!isWorkingCopy())
if (!isWorkingCopy() || isCanceled(monitor))
return null; // reconciling is not supported on non-working copies
if (workingCopyOwner == null)
workingCopyOwner = DefaultWorkingCopyOwner.PRIMARY;

PerformanceStats stats = null;
if (ReconcileWorkingCopyOperation.PERF) {
stats = PerformanceStats.getStats(JavaModelManager.RECONCILE_PERF, this);
stats.startRun(String.valueOf(getFileName()));
}
ReconcileWorkingCopyOperation op = new GroovyReconcileWorkingCopyOperation(this, astLevel, reconcileFlags, workingCopyOwner);
ReconcileWorkingCopyOperation op = new GroovyReconcileWorkingCopyOperation(this, astLevel,
reconcileFlags, workingCopyOwner != null ? workingCopyOwner : DefaultWorkingCopyOwner.PRIMARY);
JavaModelManager manager = JavaModelManager.getJavaModelManager();
try {
manager.cacheZipFiles(this); // cache zip files for performance (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=134172)
op.runOperation(monitor);
if (!isCanceled(monitor))
op.runOperation(monitor);
} finally {
manager.flushZipFiles(this);
}
Expand All @@ -434,7 +391,12 @@ public org.eclipse.jdt.core.dom.CompilationUnit reconcile(int astLevel, int reco
return op.ast;
}

@Override @SuppressWarnings("unchecked")
private boolean isCanceled(IProgressMonitor monitor) {
return (monitor != null && monitor.isCanceled());
}

@Override
@SuppressWarnings("unchecked")
public <T> T getAdapter(Class<T> adapter) {
if (GroovyCompilationUnit.class.equals(adapter)) {
return (T) this;
Expand All @@ -445,36 +407,6 @@ public <T> T getAdapter(Class<T> adapter) {
return super.getAdapter(adapter);
}

class CompilationUnitClone extends GroovyCompilationUnit {
private char[] cachedContents;

CompilationUnitClone(char[] cachedContents) {
this();
this.cachedContents = cachedContents;
}

CompilationUnitClone() {
super((PackageFragment) GroovyCompilationUnit.this.parent, GroovyCompilationUnit.this.name, GroovyCompilationUnit.this.owner);
}

@Override
public char[] getContents() {
if (this.cachedContents == null)
this.cachedContents = GroovyCompilationUnit.this.getContents();
return this.cachedContents;
}

@Override
public CompilationUnit originalFromClone() {
return GroovyCompilationUnit.this;
}

@Override
public char[] getFileName() {
return GroovyCompilationUnit.this.getFileName();
}
}

public GroovyCompilationUnit cloneCachingContents(char[] newContents) {
return new CompilationUnitClone(newContents);
}
Expand Down Expand Up @@ -593,4 +525,81 @@ protected void codeComplete(org.eclipse.jdt.internal.compiler.env.ICompilationUn
super.codeComplete(cu, unitToSkip, position, requestor, owner, typeRoot, monitor);
}
}

//--------------------------------------------------------------------------

private class CompilationUnitClone extends GroovyCompilationUnit {

private char[] cachedContents;

CompilationUnitClone(char[] cachedContents) {
this();
this.cachedContents = cachedContents;
}

CompilationUnitClone() {
super((PackageFragment) GroovyCompilationUnit.this.parent, GroovyCompilationUnit.this.name, GroovyCompilationUnit.this.owner);
}

@Override
public char[] getContents() {
if (this.cachedContents == null)
this.cachedContents = GroovyCompilationUnit.this.getContents();
return this.cachedContents;
}

@Override
public CompilationUnit originalFromClone() {
return GroovyCompilationUnit.this;
}

@Override
public char[] getFileName() {
return GroovyCompilationUnit.this.getFileName();
}
}

private static class GroovyErrorHandlingPolicy implements IErrorHandlingPolicy {

private final boolean stopOnFirst;

GroovyErrorHandlingPolicy(final boolean stopOnFirst) {
this.stopOnFirst = stopOnFirst;
}

@Override
public boolean stopOnFirstError() {
return stopOnFirst;
}

@Override
public boolean proceedOnErrors() {
return !stopOnFirst;
}

@Override
public boolean ignoreAllErrors() {
return false;
}
}

private static class ThreadLocalAtomicInteger extends ThreadLocal<AtomicInteger> {

@Override
protected AtomicInteger initialValue() {
return new AtomicInteger();
}

int intValue() {
return get().get();
}

void increment() {
get().incrementAndGet();
}

void decrement() {
get().decrementAndGet();
}
}
}
Loading

0 comments on commit f0d0bde

Please sign in to comment.