use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by sofastack.
the class ReferenceAnnotationBeanPostProcessor method createReferenceProxy.
private Object createReferenceProxy(SofaReference sofaReferenceAnnotation, Class<?> interfaceType) {
Reference reference = new ReferenceImpl(sofaReferenceAnnotation.uniqueId(), interfaceType, InterfaceMode.annotation, sofaReferenceAnnotation.jvmFirst());
BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(new BindingType(sofaReferenceAnnotation.binding().bindingType()));
if (bindingConverter == null) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00200", sofaReferenceAnnotation.binding().bindingType()));
}
BindingConverterContext bindingConverterContext = new BindingConverterContext();
bindingConverterContext.setInBinding(true);
bindingConverterContext.setApplicationContext(applicationContext);
bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
Binding binding = bindingConverter.convert(sofaReferenceAnnotation, sofaReferenceAnnotation.binding(), bindingConverterContext);
reference.addBinding(binding);
return ReferenceRegisterHelper.registerReference(reference, bindingAdapterFactory, sofaRuntimeContext, applicationContext);
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by sofastack.
the class ServiceBeanFactoryPostProcessor method generateSofaServiceDefinitionOnMethod.
private void generateSofaServiceDefinitionOnMethod(String beanId, AnnotatedBeanDefinition beanDefinition, BeanDefinitionRegistry registry) {
Class<?> returnType;
Class<?> declaringClass;
List<Method> candidateMethods = new ArrayList<>();
MethodMetadata methodMetadata = beanDefinition.getFactoryMethodMetadata();
try {
returnType = ClassUtils.forName(methodMetadata.getReturnTypeName(), null);
declaringClass = ClassUtils.forName(methodMetadata.getDeclaringClassName(), null);
} catch (Throwable throwable) {
// it's impossible to catch throwable here
SofaLogger.error(ErrorCode.convert("01-02001", beanId), throwable);
return;
}
if (methodMetadata instanceof StandardMethodMetadata) {
candidateMethods.add(((StandardMethodMetadata) methodMetadata).getIntrospectedMethod());
} else {
for (Method m : declaringClass.getDeclaredMethods()) {
// check methodName and return type
if (!m.getName().equals(methodMetadata.getMethodName()) || !m.getReturnType().getTypeName().equals(methodMetadata.getReturnTypeName())) {
continue;
}
// check bean method
if (!AnnotatedElementUtils.hasAnnotation(m, Bean.class)) {
continue;
}
Bean bean = m.getAnnotation(Bean.class);
Set<String> beanNames = new HashSet<>();
beanNames.add(m.getName());
if (bean != null) {
beanNames.addAll(Arrays.asList(bean.name()));
beanNames.addAll(Arrays.asList(bean.value()));
}
// check bean name
if (!beanNames.contains(beanId)) {
continue;
}
candidateMethods.add(m);
}
}
if (candidateMethods.size() == 1) {
SofaService sofaServiceAnnotation = candidateMethods.get(0).getAnnotation(SofaService.class);
if (sofaServiceAnnotation == null) {
sofaServiceAnnotation = returnType.getAnnotation(SofaService.class);
}
generateSofaServiceDefinition(beanId, sofaServiceAnnotation, returnType, beanDefinition, registry);
generateSofaReferenceDefinition(beanId, candidateMethods.get(0), registry);
} else if (candidateMethods.size() > 1) {
for (Method m : candidateMethods) {
if (AnnotatedElementUtils.hasAnnotation(m, SofaService.class) || AnnotatedElementUtils.hasAnnotation(returnType, SofaService.class)) {
throw new FatalBeanException(ErrorCode.convert("01-02002", declaringClass.getCanonicalName()));
}
if (Stream.of(m.getParameterAnnotations()).flatMap(Stream::of).anyMatch(annotation -> annotation instanceof SofaReference)) {
throw new FatalBeanException(ErrorCode.convert("01-02003", declaringClass.getCanonicalName()));
}
}
}
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by alipay.
the class ServiceBeanFactoryPostProcessor method generateSofaServiceDefinitionOnMethod.
private void generateSofaServiceDefinitionOnMethod(String beanId, AnnotatedBeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
Class<?> returnType;
Class<?> declaringClass;
List<Method> candidateMethods = new ArrayList<>();
MethodMetadata methodMetadata = beanDefinition.getFactoryMethodMetadata();
try {
returnType = ClassUtils.forName(methodMetadata.getReturnTypeName(), null);
declaringClass = ClassUtils.forName(methodMetadata.getDeclaringClassName(), null);
} catch (Throwable throwable) {
// it's impossible to catch throwable here
SofaLogger.error(ErrorCode.convert("01-02001", beanId), throwable);
return;
}
if (methodMetadata instanceof StandardMethodMetadata) {
candidateMethods.add(((StandardMethodMetadata) methodMetadata).getIntrospectedMethod());
} else {
for (Method m : declaringClass.getDeclaredMethods()) {
// check methodName and return type
if (!m.getName().equals(methodMetadata.getMethodName()) || !m.getReturnType().getTypeName().equals(methodMetadata.getReturnTypeName())) {
continue;
}
// check bean method
if (!AnnotatedElementUtils.hasAnnotation(m, Bean.class)) {
continue;
}
Bean bean = m.getAnnotation(Bean.class);
Set<String> beanNames = new HashSet<>();
beanNames.add(m.getName());
if (bean != null) {
beanNames.addAll(Arrays.asList(bean.name()));
beanNames.addAll(Arrays.asList(bean.value()));
}
// check bean name
if (!beanNames.contains(beanId)) {
continue;
}
candidateMethods.add(m);
}
}
if (candidateMethods.size() == 1) {
SofaService sofaServiceAnnotation = candidateMethods.get(0).getAnnotation(SofaService.class);
if (sofaServiceAnnotation == null) {
sofaServiceAnnotation = returnType.getAnnotation(SofaService.class);
}
generateSofaServiceDefinition(beanId, sofaServiceAnnotation, returnType, beanDefinition, beanFactory);
generateSofaReferenceDefinition(beanId, candidateMethods.get(0), beanFactory);
} else if (candidateMethods.size() > 1) {
for (Method m : candidateMethods) {
if (AnnotatedElementUtils.hasAnnotation(m, SofaService.class) || AnnotatedElementUtils.hasAnnotation(returnType, SofaService.class)) {
throw new FatalBeanException(ErrorCode.convert("01-02002", declaringClass.getCanonicalName()));
}
if (Stream.of(m.getParameterAnnotations()).flatMap(Stream::of).anyMatch(annotation -> annotation instanceof SofaReference)) {
throw new FatalBeanException(ErrorCode.convert("01-02003", declaringClass.getCanonicalName()));
}
}
}
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by alipay.
the class ServiceBeanFactoryPostProcessor method generateSofaReferenceDefinition.
private void generateSofaReferenceDefinition(String beanId, Method method, ConfigurableListableBeanFactory beanFactory) {
Class<?>[] parameterTypes = method.getParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterAnnotations.length; ++i) {
for (Annotation annotation : parameterAnnotations[i]) {
if (annotation instanceof SofaReference) {
doGenerateSofaReferenceDefinition(beanFactory.getBeanDefinition(beanId), (SofaReference) annotation, parameterTypes[i], beanFactory);
}
}
}
}
use of com.alipay.sofa.runtime.api.annotation.SofaReference in project sofa-boot by alipay.
the class RpcBindingConverterTest method parseSofaMethods.
@Test
public void parseSofaMethods() {
RpcBindingConverter rpcBindingConverter = new BoltBindingConverter();
SofaReference reference = null;
try {
reference = RpcBindingConverterTest.class.getDeclaredField("testAnnotation").getAnnotation(SofaReference.class);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
List<RpcBindingMethodInfo> result = rpcBindingConverter.parseSofaMethods(reference.binding().methodInfos());
Assert.assertEquals(1, result.size());
final RpcBindingMethodInfo rpcBindingMethodInfo = result.get(0);
Assert.assertEquals("test", rpcBindingMethodInfo.getName());
Assert.assertEquals(1, rpcBindingMethodInfo.getRetries().intValue());
Assert.assertEquals("callback", rpcBindingMethodInfo.getType());
Assert.assertEquals("class", rpcBindingMethodInfo.getCallbackClass());
Assert.assertEquals("ref", rpcBindingMethodInfo.getCallbackRef());
Assert.assertEquals(2000, rpcBindingMethodInfo.getTimeout().intValue());
}
Aggregations