disable constructor injection for CompileTaskBean

It is possible even if CompileTaskBean is project level extension point because of `execute(CompileContext context)` — CompileContext provides project.
This commit is contained in:
Vladimir Krivosheev
2019-01-31 19:20:20 +01:00
parent 85431d6d72
commit ce9a5ba4fe
7 changed files with 70 additions and 71 deletions
@@ -1,23 +1,9 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed 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.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.compiler;
import com.intellij.openapi.compiler.CompileTask;
import com.intellij.openapi.extensions.AbstractExtensionPointBean;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.extensions.ProjectExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.AtomicNotNullLazyValue;
import com.intellij.util.xmlb.annotations.Attribute;
@@ -26,35 +12,30 @@ import org.jetbrains.annotations.NotNull;
/**
* @author nik
*/
public class CompileTaskBean extends AbstractExtensionPointBean {
public static final ExtensionPointName<CompileTaskBean> EP_NAME = ExtensionPointName.create("com.intellij.compiler.task");
public final class CompileTaskBean extends AbstractExtensionPointBean {
public static final ProjectExtensionPointName<CompileTaskBean> EP_NAME = new ProjectExtensionPointName<>("com.intellij.compiler.task");
public enum CompileTaskExecutionPhase { BEFORE, AFTER }
private final Project myProject;
public CompileTaskBean(Project project) {
myProject = project;
}
@Attribute("execute")
public CompileTaskExecutionPhase myExecutionPhase = CompileTaskExecutionPhase.BEFORE;
@Attribute("implementation")
public String myImplementation;
private final AtomicNotNullLazyValue<CompileTask> myInstanceHolder = new AtomicNotNullLazyValue<CompileTask>() {
@NotNull
@Override
protected CompileTask compute() {
private final AtomicNotNullLazyValue<CompileTask> myInstanceHolder;
public CompileTaskBean(Project project) {
myInstanceHolder = AtomicNotNullLazyValue.createValue(() -> {
try {
return instantiate(myImplementation, myProject.getPicoContainer());
return instantiateWithPicoContainerOnlyIfNeeded(myImplementation, project.getPicoContainer());
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
};
});
}
@NotNull
public CompileTask getTaskInstance() {
return myInstanceHolder.getValue();
}
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.compiler;
import com.intellij.codeInspection.InspectionManager;
@@ -42,8 +42,7 @@ import org.jetbrains.jps.incremental.BinaryContent;
import org.jetbrains.jps.javac.*;
import org.jetbrains.jps.javac.ast.api.JavacFileData;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import javax.tools.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
@@ -198,23 +197,24 @@ public class CompilerManagerImpl extends CompilerManager {
@Override
@NotNull
public CompileTask[] getBeforeTasks() {
public List<CompileTask> getBeforeTasks() {
return getCompileTasks(myBeforeTasks, CompileTaskBean.CompileTaskExecutionPhase.BEFORE);
}
private CompileTask[] getCompileTasks(List<CompileTask> taskList, CompileTaskBean.CompileTaskExecutionPhase phase) {
@NotNull
private List<CompileTask> getCompileTasks(List<CompileTask> taskList, CompileTaskBean.CompileTaskExecutionPhase phase) {
List<CompileTask> beforeTasks = new ArrayList<>(taskList);
for (CompileTaskBean extension : CompileTaskBean.EP_NAME.getExtensions(myProject)) {
if (extension.myExecutionPhase == phase) {
beforeTasks.add(extension.getTaskInstance());
}
}
return beforeTasks.toArray(new CompileTask[0]);
return beforeTasks;
}
@Override
@NotNull
public CompileTask[] getAfterTasks() {
public List<CompileTask> getAfterTaskList() {
return getCompileTasks(myAfterTasks, CompileTaskBean.CompileTaskExecutionPhase.AFTER);
}
@@ -581,8 +581,8 @@ public class CompileDriver {
final ProgressIndicator progressIndicator = context.getProgressIndicator();
progressIndicator.pushState();
try {
CompileTask[] tasks = beforeTasks ? manager.getBeforeTasks() : manager.getAfterTasks();
if (tasks.length > 0) {
List<CompileTask> tasks = beforeTasks ? manager.getBeforeTasks() : manager.getAfterTaskList();
if (tasks.size() > 0) {
progressIndicator.setText(
CompilerBundle.message(beforeTasks ? "progress.executing.precompile.tasks" : "progress.executing.postcompile.tasks"));
for (CompileTask task : tasks) {
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.compiler;
import com.intellij.execution.configurations.RunConfiguration;
@@ -123,7 +123,16 @@ public abstract class CompilerManager {
* @return all tasks to be executed before compilation.
*/
@NotNull
public abstract CompileTask[] getBeforeTasks();
public abstract List<CompileTask> getBeforeTasks();
/**
* @deprecated Use {@link #getAfterTaskList}
*/
@Deprecated
@NotNull
public CompileTask[] getAfterTasks() {
return getAfterTaskList().toArray(new CompileTask[0]);
}
/**
* Returns the list of all tasks to be executed after compilation.
@@ -131,7 +140,7 @@ public abstract class CompilerManager {
* @return all tasks to be executed after compilation.
*/
@NotNull
public abstract CompileTask[] getAfterTasks();
public abstract List<CompileTask> getAfterTaskList();
/**
* Compile a set of files.
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.execution.scratch;
import com.intellij.compiler.options.CompileStepBeforeRun;
@@ -22,6 +22,7 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.java.JpsJavaSdkType;
@@ -33,8 +34,8 @@ import java.util.*;
* @author Eugene Zhuravlev
*/
public class JavaScratchCompilationSupport implements CompileTask {
public JavaScratchCompilationSupport(CompilerManager compileManager) {
compileManager.addAfterTask(this);
public JavaScratchCompilationSupport(@NotNull Project project) {
CompilerManager.getInstance(project).addAfterTask(this);
}
@Nullable
@@ -2,6 +2,7 @@
package com.intellij.openapi.extensions;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.pico.CachingConstructorInjectionComponentAdapter;
import com.intellij.util.xmlb.annotations.Transient;
import org.jetbrains.annotations.NotNull;
@@ -65,4 +66,30 @@ public abstract class AbstractExtensionPointBean implements PluginAware {
final boolean allowNonPublicClasses) {
return (T)new CachingConstructorInjectionComponentAdapter(aClass.getName(), aClass, null, allowNonPublicClasses).getComponentInstance(container);
}
@NotNull
protected <T> T instantiateWithPicoContainerOnlyIfNeeded(@Nullable String implementationClass, @NotNull PicoContainer picoContainer)
throws ClassNotFoundException {
if (implementationClass == null) {
throw new RuntimeException("implementation class is not specified, " +
"plugin id: " +
(myPluginDescriptor == null ? "<not available>" : myPluginDescriptor.getPluginId()) + ". " +
"Check if 'implementationClass' attribute is specified");
}
Class<T> clazz = findClass(implementationClass);
try {
return ReflectionUtil.newInstance(clazz);
}
catch (RuntimeException e) {
if (e.getCause() instanceof NoSuchMethodException) {
LOG.error("Bean extension class constructor must not have parameters: " + implementationClass);
return instantiate(clazz, picoContainer, true);
}
else {
throw e;
}
}
}
}
@@ -1,7 +1,6 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.extensions;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.xmlb.annotations.Attribute;
import org.jetbrains.annotations.NotNull;
import org.picocontainer.PicoContainer;
@@ -15,30 +14,12 @@ public class CustomLoadingExtensionPointBean extends AbstractExtensionPointBean
@NotNull
protected Object instantiateExtension(String implementationClass, @NotNull PicoContainer picoContainer) throws ClassNotFoundException {
if (factoryClass != null) {
if (factoryClass == null) {
return instantiateWithPicoContainerOnlyIfNeeded(implementationClass, picoContainer);
}
else {
ExtensionFactory factory = instantiate(factoryClass, picoContainer);
return factory.createInstance(factoryArgument, implementationClass);
}
else {
if (implementationClass == null) {
throw new RuntimeException("implementation class is not specified for unknown language extension point, " +
"plugin id: " +
(myPluginDescriptor == null ? "<not available>" : myPluginDescriptor.getPluginId()) + ". " +
"Check if 'implementationClass' attribute is specified");
}
Class<Object> clazz = findClass(implementationClass);
try {
return ReflectionUtil.newInstance(clazz);
}
catch (RuntimeException e) {
if (e.getCause() instanceof NoSuchMethodException) {
LOG.error("Bean extension class constructor must not have parameters: " + implementationClass);
return instantiate(clazz, picoContainer, true);
}
else {
throw e;
}
}
}
}
}