mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
Created CancelService implementation
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* This file is part of AndroidIDE.
|
||||
*
|
||||
* AndroidIDE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* AndroidIDE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.itsaky.lsp.java.compiler
|
||||
|
||||
import com.itsaky.androidide.utils.ILogger
|
||||
import java.util.concurrent.atomic.*
|
||||
import org.netbeans.lib.nbjavac.services.CancelService
|
||||
|
||||
/**
|
||||
* Cancel service implementation for the java compiler.
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
class CancelServiceImpl : CancelService() {
|
||||
private val log = ILogger.newInstance(javaClass.simpleName)
|
||||
val cancelled = AtomicBoolean(false)
|
||||
|
||||
/**
|
||||
* Sets the cancellation flag.
|
||||
*
|
||||
* @return `true` if compilation process was running and it was set to be cancelled, `false`
|
||||
* otherwise.
|
||||
*/
|
||||
fun cancel(): Boolean {
|
||||
log.info("...cancelling compilation")
|
||||
if (cancelled.getAndSet(true)) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun isCanceled(): Boolean = cancelled.get()
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* This file is part of AndroidIDE.
|
||||
*
|
||||
* AndroidIDE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* AndroidIDE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.itsaky.lsp.java.compiler
|
||||
|
||||
/**
|
||||
* Thrown when a compilation process is cancelled.
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
class CompilationCancellationException : java.lang.RuntimeException {
|
||||
constructor() : super()
|
||||
constructor(message: String?) : super(message)
|
||||
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
constructor(cause: Throwable?) : super(cause)
|
||||
constructor(
|
||||
message: String?,
|
||||
cause: Throwable?,
|
||||
enableSuppression: Boolean,
|
||||
writableStackTrace: Boolean
|
||||
) : super(message, cause, enableSuppression, writableStackTrace)
|
||||
}
|
||||
@@ -44,6 +44,7 @@ import com.sun.tools.javac.util.DefinedBy;
|
||||
import com.sun.tools.javac.util.DefinedBy.Api;
|
||||
import com.sun.tools.javac.util.Log;
|
||||
|
||||
import org.netbeans.lib.nbjavac.services.CancelService;
|
||||
import org.netbeans.lib.nbjavac.services.NBAttr;
|
||||
import org.netbeans.lib.nbjavac.services.NBClassFinder;
|
||||
import org.netbeans.lib.nbjavac.services.NBEnter;
|
||||
@@ -101,6 +102,9 @@ public class ReusableCompiler {
|
||||
private ReusableContext currentContext;
|
||||
private boolean checkedOut;
|
||||
|
||||
// TODO This is not used currently
|
||||
private static final CancelService cancelService = new CancelServiceImpl();
|
||||
|
||||
/**
|
||||
* Creates a new task as if by {@link javax.tools.JavaCompiler#getTask} and runs the provided
|
||||
* worker with it. The task is only valid while the worker is running. The internal structures may
|
||||
@@ -137,12 +141,12 @@ public class ReusableCompiler {
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
if (!opts.equals(currentOptions)) {
|
||||
final ArrayList<String> newOpts = new ArrayList<>(currentOptions);
|
||||
newOpts.removeAll(opts);
|
||||
newOpts.removeAll(currentOptions);
|
||||
LOG.debug("New compiler options:", newOpts);
|
||||
|
||||
currentOptions.clear();
|
||||
currentOptions.addAll(opts);
|
||||
currentContext = new ReusableContext(new ArrayList<>(opts));
|
||||
currentContext = new ReusableContext(new ArrayList<>(opts), cancelService);
|
||||
}
|
||||
JavacTaskImpl task =
|
||||
(JavacTaskImpl)
|
||||
@@ -186,17 +190,17 @@ public class ReusableCompiler {
|
||||
|
||||
static class ReusableContext extends Context implements TaskListener {
|
||||
|
||||
List<String> arguments;
|
||||
final List<String> arguments;
|
||||
|
||||
ReusableContext(List<String> arguments) {
|
||||
ReusableContext(List<String> arguments, final CancelService cancelService) {
|
||||
super();
|
||||
this.arguments = arguments;
|
||||
put(Log.logKey, ReusableLog.factory);
|
||||
put(JavaCompiler.compilerKey, ReusableJavaCompiler.factory);
|
||||
registerNBServices();
|
||||
registerNBServices(cancelService);
|
||||
}
|
||||
|
||||
private void registerNBServices() {
|
||||
private void registerNBServices(final CancelService cancelService) {
|
||||
NBAttr.preRegister(this);
|
||||
NBParserFactory.preRegister(this);
|
||||
NBTreeMaker.preRegister(this);
|
||||
@@ -205,6 +209,7 @@ public class ReusableCompiler {
|
||||
NBEnter.preRegister(this);
|
||||
NBMemberEnter.preRegister(this, false);
|
||||
NBClassFinder.preRegister(this);
|
||||
CancelService.preRegister(this, cancelService);
|
||||
|
||||
if (!TestUtils.isTestEnvironment()) {
|
||||
put(LazyTreeLoader.lazyTreeLoaderKey, new IDELazyTreeLoader());
|
||||
|
||||
@@ -38,6 +38,8 @@ import androidx.annotation.NonNull;
|
||||
|
||||
import com.itsaky.androidide.utils.ILogger;
|
||||
|
||||
import org.netbeans.lib.nbjavac.services.CancelAbort;
|
||||
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@@ -47,7 +49,6 @@ public class SynchronizedTask {
|
||||
|
||||
private static final ILogger LOG = ILogger.newInstance("SynchronizedTask");
|
||||
private final Semaphore semaphore = new Semaphore(1);
|
||||
private volatile boolean isWriting;
|
||||
private CompileTask task;
|
||||
|
||||
public void run(@NonNull Consumer<CompileTask> taskConsumer) {
|
||||
@@ -75,16 +76,16 @@ public class SynchronizedTask {
|
||||
|
||||
void doCompile(@NonNull Runnable run) {
|
||||
semaphore.acquireUninterruptibly();
|
||||
isWriting = true;
|
||||
try {
|
||||
if (this.task != null) {
|
||||
this.task.close();
|
||||
}
|
||||
run.run();
|
||||
} catch (Throwable th) {
|
||||
LOG.error("An error occurred in SynchronizedTask.doCompile()", th);
|
||||
if (!(th instanceof CancelAbort)) {
|
||||
LOG.error("An error occurred in SynchronizedTask.doCompile()", th);
|
||||
}
|
||||
} finally {
|
||||
isWriting = false;
|
||||
semaphore.release();
|
||||
}
|
||||
}
|
||||
@@ -92,8 +93,4 @@ public class SynchronizedTask {
|
||||
void setTask(CompileTask task) {
|
||||
this.task = task;
|
||||
}
|
||||
|
||||
public synchronized boolean isWriting() {
|
||||
return isWriting || semaphore.hasQueuedThreads();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,5 @@ import com.sun.tools.javac.util.Abort;
|
||||
* @author Tomas Zezula
|
||||
*/
|
||||
public final class CancelAbort extends Abort {
|
||||
|
||||
CancelAbort() {}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
package org.netbeans.lib.nbjavac.services;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.sun.tools.javac.util.Context;
|
||||
|
||||
/**
|
||||
@@ -27,10 +29,10 @@ import com.sun.tools.javac.util.Context;
|
||||
public class CancelService {
|
||||
|
||||
/** The context key for the parameter name resolver. */
|
||||
protected static final Context.Key<CancelService> cancelServiceKey =
|
||||
new Context.Key<CancelService>();
|
||||
protected static final Context.Key<CancelService> cancelServiceKey = new Context.Key<>();
|
||||
|
||||
public static CancelService instance(Context context) {
|
||||
@NonNull
|
||||
public static CancelService instance(@NonNull Context context) {
|
||||
CancelService instance = context.get(cancelServiceKey);
|
||||
if (instance == null) {
|
||||
instance = new CancelService();
|
||||
@@ -39,6 +41,10 @@ public class CancelService {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void preRegister(@NonNull Context context, @NonNull CancelService impl) {
|
||||
context.put(CancelService.cancelServiceKey, impl);
|
||||
}
|
||||
|
||||
protected CancelService() {}
|
||||
|
||||
public boolean isCanceled() {
|
||||
|
||||
@@ -45,11 +45,7 @@ public class NBAttr extends Attr {
|
||||
public static void preRegister(Context context) {
|
||||
context.put(
|
||||
attrKey,
|
||||
new Context.Factory<Attr>() {
|
||||
public Attr make(Context c) {
|
||||
return new NBAttr(c);
|
||||
}
|
||||
});
|
||||
(Context.Factory<Attr>) NBAttr::new);
|
||||
}
|
||||
|
||||
private final CancelService cancelService;
|
||||
|
||||
@@ -46,11 +46,7 @@ public class NBClassFinder extends ClassFinder {
|
||||
public static void preRegister(Context context) {
|
||||
context.put(
|
||||
classFinderKey,
|
||||
new Context.Factory<ClassFinder>() {
|
||||
public ClassFinder make(Context c) {
|
||||
return new NBClassFinder(c);
|
||||
}
|
||||
});
|
||||
(Context.Factory<ClassFinder>) NBClassFinder::new);
|
||||
}
|
||||
|
||||
private final Context context;
|
||||
|
||||
@@ -41,11 +41,7 @@ public class NBClassWriter extends ClassWriter {
|
||||
public static void preRegister(Context context) {
|
||||
context.put(
|
||||
classWriterKey,
|
||||
new Context.Factory<ClassWriter>() {
|
||||
public ClassWriter make(Context c) {
|
||||
return new NBClassWriter(c);
|
||||
}
|
||||
});
|
||||
(Context.Factory<ClassWriter>) NBClassWriter::new);
|
||||
}
|
||||
|
||||
private final NBNames nbNames;
|
||||
|
||||
@@ -38,11 +38,7 @@ public class NBEnter extends Enter {
|
||||
public static void preRegister(Context context) {
|
||||
context.put(
|
||||
enterKey,
|
||||
new Context.Factory<Enter>() {
|
||||
public Enter make(Context c) {
|
||||
return new NBEnter(c);
|
||||
}
|
||||
});
|
||||
(Context.Factory<Enter>) NBEnter::new);
|
||||
}
|
||||
|
||||
private final CancelService cancelService;
|
||||
|
||||
@@ -38,11 +38,7 @@ public class NBJavaCompiler extends JavaCompiler {
|
||||
public static void preRegister(Context context) {
|
||||
context.put(
|
||||
compilerKey,
|
||||
new Context.Factory<JavaCompiler>() {
|
||||
public JavaCompiler make(Context c) {
|
||||
return new NBJavaCompiler(c);
|
||||
}
|
||||
});
|
||||
(Context.Factory<JavaCompiler>) NBJavaCompiler::new);
|
||||
}
|
||||
|
||||
private final CancelService cancelService;
|
||||
|
||||
@@ -64,23 +64,13 @@ public class NBLog extends Log {
|
||||
final PrintWriter noticeWriter) {
|
||||
context.put(
|
||||
logKey,
|
||||
new Context.Factory<Log>() {
|
||||
@Override
|
||||
public Log make(Context c) {
|
||||
return new NBLog(c, errWriter);
|
||||
}
|
||||
});
|
||||
(Context.Factory<Log>) c -> new NBLog(c, errWriter));
|
||||
}
|
||||
|
||||
public static void preRegister(Context context, final PrintWriter output) {
|
||||
context.put(
|
||||
logKey,
|
||||
new Context.Factory<Log>() {
|
||||
@Override
|
||||
public Log make(Context c) {
|
||||
return new NBLog(c, output);
|
||||
}
|
||||
});
|
||||
(Context.Factory<Log>) c -> new NBLog(c, output));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -35,11 +35,7 @@ public class NBMemberEnter extends MemberEnter {
|
||||
public static void preRegister(Context context, boolean backgroundScan) {
|
||||
context.put(
|
||||
MemberEnter.class,
|
||||
new Context.Factory<MemberEnter>() {
|
||||
public MemberEnter make(Context c) {
|
||||
return new NBMemberEnter(c, backgroundScan);
|
||||
}
|
||||
});
|
||||
(Context.Factory<MemberEnter>) c -> new NBMemberEnter(c, backgroundScan));
|
||||
}
|
||||
|
||||
private final CancelService cancelService;
|
||||
|
||||
@@ -45,12 +45,7 @@ public class NBParserFactory extends ParserFactory {
|
||||
public static void preRegister(Context context) {
|
||||
context.put(
|
||||
parserFactoryKey,
|
||||
new Context.Factory<ParserFactory>() {
|
||||
@Override
|
||||
public ParserFactory make(Context c) {
|
||||
return new NBParserFactory(c);
|
||||
}
|
||||
});
|
||||
(Context.Factory<ParserFactory>) NBParserFactory::new);
|
||||
}
|
||||
|
||||
private final ScannerFactory scannerFactory;
|
||||
|
||||
@@ -39,12 +39,7 @@ public class NBResolve extends Resolve {
|
||||
public static void preRegister(Context context) {
|
||||
context.put(
|
||||
resolveKey,
|
||||
new Context.Factory<Resolve>() {
|
||||
@Override
|
||||
public Resolve make(Context c) {
|
||||
return new NBResolve(c);
|
||||
}
|
||||
});
|
||||
(Context.Factory<Resolve>) NBResolve::new);
|
||||
}
|
||||
|
||||
protected NBResolve(Context ctx) {
|
||||
|
||||
@@ -18,19 +18,11 @@
|
||||
*/
|
||||
package org.netbeans.lib.nbjavac.services;
|
||||
|
||||
import com.sun.tools.javac.code.Symbol.ClassSymbol;
|
||||
import com.sun.tools.javac.code.Symtab;
|
||||
import com.sun.tools.javac.code.Types;
|
||||
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.JCTree.JCExpression;
|
||||
import com.sun.tools.javac.tree.JCTree.JCModifiers;
|
||||
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
|
||||
import com.sun.tools.javac.tree.TreeMaker;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.Name;
|
||||
import com.sun.tools.javac.util.Names;
|
||||
|
||||
/**
|
||||
@@ -39,13 +31,7 @@ import com.sun.tools.javac.util.Names;
|
||||
public class NBTreeMaker extends TreeMaker {
|
||||
|
||||
public static void preRegister(Context context) {
|
||||
context.put(
|
||||
treeMakerKey,
|
||||
new Context.Factory<TreeMaker>() {
|
||||
public TreeMaker make(Context c) {
|
||||
return new NBTreeMaker(c);
|
||||
}
|
||||
});
|
||||
context.put(treeMakerKey, (Context.Factory<TreeMaker>) NBTreeMaker::new);
|
||||
}
|
||||
|
||||
private final Names names;
|
||||
|
||||
Reference in New Issue
Block a user