return ofInstance(component, LOG);
}
+ public String ofClass(Class<?> clazz) {
+ return clazz.getClassLoader() + "-" + clazz.getCanonicalName();
+ }
+
String ofInstance(Object component, Logger log) {
String key = component.toString();
if (IDENTITY_HASH_PATTERN.matcher(key).matches()) {
}
key += Uuids.create();
}
- return component.getClass().getCanonicalName() + "-" + key;
+ return ofClass(component.getClass()) + "-" + key;
}
}
ComponentKeys keys = new ComponentKeys();
@Test
- public void generate_key_of_class() {
+ public void generate_key_of_object() {
assertThat(keys.of(FakeComponent.class)).isEqualTo(FakeComponent.class);
}
@Test
- public void generate_key_of_object() {
- assertThat(keys.of(new FakeComponent())).isEqualTo("org.sonar.core.platform.ComponentKeysTest.FakeComponent-fake");
+ public void generate_key_of_instance() {
+ assertThat((String) keys.of(new FakeComponent())).endsWith("-org.sonar.core.platform.ComponentKeysTest.FakeComponent-fake");
+ }
+
+ @Test
+ public void generate_key_of_class() {
+ assertThat(keys.ofClass(FakeComponent.class)).endsWith("-org.sonar.core.platform.ComponentKeysTest.FakeComponent");
}
@Test
package org.sonar.scanner.bootstrap;
import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import org.sonar.core.platform.ExtensionContainer;
import org.sonar.core.platform.PluginInfo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-import org.springframework.context.annotation.FullyQualifiedAnnotationBeanNameGenerator;
import static java.util.Collections.emptyList;
import static java.util.Optional.ofNullable;
this.parent = parent;
this.propertyDefinitions = propertyDefinitions;
this.context = new AnnotationConfigApplicationContext(new PriorityBeanFactory());
- // it won't set the name of beans created with @Bean annotated methods
- this.context.setBeanNameGenerator(new FullyQualifiedAnnotationBeanNameGenerator());
if (parent != null) {
context.setParent(parent.context);
}
/**
* Beans need to have a unique name, otherwise they'll override each other.
* The strategy is:
- * - For classes, use the fully qualified class name as the name of the bean
- * - For instances, use the FQCN + toString()
+ * - For classes, use the classloader + fully qualified class name as the name of the bean
+ * - For instances, use the Classloader + FQCN + toString()
* - If the object is a collection, iterate through the elements and apply the same strategy for each of them
*/
@Override
for (Object o : objects) {
if (o instanceof Class) {
Class<?> clazz = (Class<?>) o;
- context.registerBean(clazz);
+ context.registerBean(componentKeys.ofClass(clazz), clazz);
} else if (o instanceof Iterable) {
add(Iterables.toArray((Iterable<?>) o, Object.class));
} else {
if (o instanceof Class) {
Class<?> clazz = (Class<?>) o;
ClassDerivedBeanDefinition bd = new ClassDerivedBeanDefinition(clazz);
- context.registerBeanDefinition(clazz.getName(), bd);
+ context.registerBeanDefinition(componentKeys.ofClass(clazz), bd);
} else if (o instanceof Iterable) {
((Iterable<?>) o).forEach(this::addExtension);
} else {
SpringComponentContainer container = new SimpleContainer(new ToString("a"), new ToString("b"));
container.startComponents();
assertThat(container.context.getBeanDefinitionNames())
- .contains("org.sonar.scanner.bootstrap.SpringComponentContainerTest.ToString-a", "org.sonar.scanner.bootstrap.SpringComponentContainerTest.ToString-b");
+ .contains(
+ this.getClass().getClassLoader() + "-org.sonar.scanner.bootstrap.SpringComponentContainerTest.ToString-a",
+ this.getClass().getClassLoader() + "-org.sonar.scanner.bootstrap.SpringComponentContainerTest.ToString-b");
assertThat(container.getComponentsByType(ToString.class)).hasSize(2);
}
@Test
- public void register_class_with_fqcn() {
+ public void register_class_with_classloader_and_fqcn() {
SpringComponentContainer container = new SimpleContainer(A.class, B.class);
container.startComponents();
assertThat(container.context.getBeanDefinitionNames())
- .contains("org.sonar.scanner.bootstrap.SpringComponentContainerTest$A", "org.sonar.scanner.bootstrap.SpringComponentContainerTest$B");
+ .contains(
+ this.getClass().getClassLoader() + "-org.sonar.scanner.bootstrap.SpringComponentContainerTest.A",
+ this.getClass().getClassLoader() + "-org.sonar.scanner.bootstrap.SpringComponentContainerTest.B");
assertThat(container.getComponentByType(A.class)).isNotNull();
assertThat(container.getComponentByType(B.class)).isNotNull();
}