use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by sofastack.
the class AnnotationPlaceHolderTest method testReferenceAnnotationPlaceHolder.
@Test
@SuppressWarnings("unchecked")
public void testReferenceAnnotationPlaceHolder() throws Exception {
PlaceHolderBinder binder = new PlaceHolderBinder() {
@Override
public String bind(String origin) {
return environment.resolvePlaceholders(origin);
}
};
SofaReference sofaReference = AnnotationSampleService.class.getField("sampleService").getAnnotation(SofaReference.class);
PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder<SofaReference> builder = PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder.wrap(sofaReference).withBinder(binder);
SofaReference delegate = builder.build();
Assert.assertEquals("${annotation.sample.ref.uniqueId}", sofaReference.uniqueId());
Assert.assertEquals("sample-reference-uniqueId", delegate.uniqueId());
Assert.assertFalse(sofaReference.jvmFirst());
Assert.assertFalse(delegate.jvmFirst());
SofaReferenceBinding binding = sofaReference.binding();
SofaReferenceBinding delegateBinding = delegate.binding();
Assert.assertEquals("${annotation.sample.ref.bindingType}", binding.bindingType());
Assert.assertEquals("bolt", delegateBinding.bindingType());
Assert.assertEquals("${annotation.sample.ref.direct-url}", binding.directUrl());
Assert.assertEquals("127.0.0.1", delegateBinding.directUrl());
String[] filters = binding.filters();
String[] delegateFilters = delegateBinding.filters();
Assert.assertEquals(2, filters.length);
Assert.assertEquals(2, delegateFilters.length);
Assert.assertEquals("${annotation.sample.ref.filter-1}", filters[0]);
Assert.assertEquals("reference-filter-1", delegateFilters[0]);
Assert.assertEquals("filter-2", filters[1]);
Assert.assertEquals("filter-2", delegateFilters[1]);
Assert.assertTrue(delegate instanceof WrapperAnnotation);
Assert.assertTrue(delegateBinding instanceof WrapperAnnotation);
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by sofastack.
the class SofaParameterNameDiscoverer method transformParameterNames.
@SuppressWarnings("unchecked")
protected String[] transformParameterNames(String[] parameterNames, Class<?>[] parameterType, Annotation[][] annotations) {
for (int i = 0; i < annotations.length; ++i) {
for (Annotation annotation : annotations[i]) {
if (annotation instanceof SofaReference) {
AnnotationWrapperBuilder<SofaReference> wrapperBuilder = AnnotationWrapperBuilder.wrap(annotation).withBinder(binder);
SofaReference delegate = wrapperBuilder.build();
Class interfaceType = delegate.interfaceType();
if (interfaceType.equals(void.class)) {
interfaceType = parameterType[i];
}
String uniqueId = delegate.uniqueId();
parameterNames[i] = SofaBeanNameGenerator.generateSofaReferenceBeanName(interfaceType, uniqueId);
}
}
}
return parameterNames;
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by alipay.
the class ServiceBeanFactoryPostProcessor method doGenerateSofaReferenceDefinition.
@SuppressWarnings("unchecked")
private void doGenerateSofaReferenceDefinition(BeanDefinition beanDefinition, SofaReference sofaReference, Class<?> parameterType, ConfigurableListableBeanFactory beanFactory) {
Assert.isTrue(JvmBinding.JVM_BINDING_TYPE.getType().equals(sofaReference.binding().bindingType()), "Only jvm type of @SofaReference on parameter is supported.");
AnnotationWrapperBuilder<SofaReference> wrapperBuilder = AnnotationWrapperBuilder.wrap(sofaReference).withBinder(binder);
sofaReference = wrapperBuilder.build();
Class<?> interfaceType = sofaReference.interfaceType();
if (interfaceType.equals(void.class)) {
interfaceType = parameterType;
}
String uniqueId = sofaReference.uniqueId();
String referenceId = SofaBeanNameGenerator.generateSofaReferenceBeanName(interfaceType, uniqueId);
// build sofa reference definition
if (!beanFactory.containsBeanDefinition(referenceId)) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
builder.getRawBeanDefinition().setScope(beanDefinition.getScope());
builder.getRawBeanDefinition().setLazyInit(beanDefinition.isLazyInit());
builder.getRawBeanDefinition().setBeanClass(ReferenceFactoryBean.class);
builder.addAutowiredProperty(AbstractContractDefinitionParser.SOFA_RUNTIME_CONTEXT);
builder.addAutowiredProperty(AbstractContractDefinitionParser.BINDING_CONVERTER_FACTORY);
builder.addAutowiredProperty(AbstractContractDefinitionParser.BINDING_ADAPTER_FACTORY);
builder.addPropertyValue(AbstractContractDefinitionParser.UNIQUE_ID_PROPERTY, uniqueId);
builder.addPropertyValue(AbstractContractDefinitionParser.INTERFACE_CLASS_PROPERTY, interfaceType);
builder.addPropertyValue(AbstractContractDefinitionParser.BINDINGS, getSofaReferenceBinding(sofaReference, sofaReference.binding()));
builder.addPropertyValue(AbstractContractDefinitionParser.DEFINITION_BUILDING_API_TYPE, true);
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(referenceId, builder.getBeanDefinition());
}
// add bean dependency relationship
if (beanDefinition.getDependsOn() == null) {
beanDefinition.setDependsOn(referenceId);
} else {
String[] added = ObjectUtils.addObjectToArray(beanDefinition.getDependsOn(), referenceId);
beanDefinition.setDependsOn(added);
}
}
Aggregations