// 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.

package com.intellij.openapi.components;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.extensions.PluginAware;
import com.intellij.openapi.extensions.PluginDescriptor;
import com.intellij.util.xmlb.annotations.Attribute;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mike
 */
public class ServiceBean implements PluginAware {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.components.ServiceBean");

  @Attribute("serviceInterface")
  public String serviceInterface;
  private PluginDescriptor myPluginDescriptor;

  public static <T> List<T> loadServicesFromBeans(final ExtensionPointName<? extends ServiceBean> epName, Class<T> componentClass) {
    final List<T> components = new ArrayList<>();
    for (ServiceBean exportableBean : epName.getExtensionList()) {
      final String serviceClass = exportableBean.serviceInterface;
      if (serviceClass == null) {
        LOG.error("Service interface not specified in " + epName);
        continue;
      }
      try {
        final Class<?> aClass = Class.forName(serviceClass, true, exportableBean.getPluginDescriptor().getPluginClassLoader());
        final Object service = ServiceManager.getService(aClass);
        if (service == null) {
          LOG.error("Can't find service: " + serviceClass);
          continue;
        }
        if (!componentClass.isInstance(service)) {
          LOG.error("Service " + serviceClass + " is registered in " + epName.getName() + " EP, but doesn't implement " + componentClass.getName());
          continue;
        }

        components.add((T)service);
      }
      catch (ClassNotFoundException e) {
        LOG.error(e);
      }
    }
    return components;
  }

  @Override
  public void setPluginDescriptor(final PluginDescriptor pluginDescriptor) {
    myPluginDescriptor = pluginDescriptor;
  }

  public PluginDescriptor getPluginDescriptor() {
    return myPluginDescriptor;
  }
}

@@ -18,9 +18,9 @@
 -201
-8
+9
  Jet
@@ -134,17 +134,16 @@
 E file.%0A
-%0A
 package 
@@ -443,16 +443,111 @@
 tribute;
+%0Aimport com.intellij.util.xmlb.annotations.Transient;%0Aimport org.jetbrains.annotations.NotNull;
 %0A%0Aimport
@@ -596,38 +596,20 @@
 t;%0A%0A
-/**%0A * @author mike%0A */%0A
 public
+ final
  cla
@@ -707,42 +707,8 @@
 nce(
-%22#com.intellij.openapi.components.
 Serv
@@ -714,17 +714,22 @@
 viceBean
-%22
+.class
 );%0A%0A  @A
@@ -740,28 +740,8 @@
 bute
-(%22serviceInterface%22)
 %0A  p
@@ -869,20 +869,23 @@
 omBeans(
-fina
+@NotNul
 l Extens
@@ -901,18 +901,8 @@
 ame%3C
-? extends 
 Serv
@@ -917,16 +917,25 @@
  epName,
+ @NotNull
  Class%3CT
@@ -1979,16 +1979,17 @@
 %0A  @
-Override
+Transient
 %0A  p
@@ -1998,16 +1998,8 @@
 lic 
-void set
 Plug
@@ -2014,33 +2014,13 @@
 ptor
-(final PluginDescriptor p
+ getP
 lugi
@@ -2030,24 +2030,25 @@
 scriptor
+(
 ) %7B%0A    
 myPlugin
@@ -2043,30 +2043,18 @@
     
-myPluginDescriptor = p
+return myP
 lugi
@@ -2077,36 +2077,70 @@
 %0A%0A  
-public PluginDescriptor getP
+@Override%0A  public void setPluginDescriptor(PluginDescriptor p
 lugi
@@ -2146,33 +2146,32 @@
 inDescriptor
-(
 ) %7B%0A    
 return myPlu
@@ -2154,34 +2154,46 @@
 ptor) %7B%0A    
-return myP
+myPluginDescriptor = p
 luginDescrip

mark as transient