Skip to content

Commit

Permalink
1. update version 3.18.1 (#1192)
Browse files Browse the repository at this point in the history
* 1. update version 3.18.1
2. fix @SofaService Annotation parse

* format

* format

---------

Co-authored-by: 致节 <hzj266771@antgroup.com>
  • Loading branch information
HzjNeverStop and 致节 committed Jun 21, 2023
1 parent 9cc24a3 commit 3d2898c
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<description>SOFABoot Build</description>

<properties>
<revision>3.18.0</revision>
<revision>3.18.1</revision>
<sofa.boot.version>${revision}</sofa.boot.version>
<!--maven plugin-->
<maven.staging.plugin>1.6.7</maven.staging.plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alipay.sofa.boot.annotation.PlaceHolderBinder;
import com.alipay.sofa.boot.error.ErrorCode;
import com.alipay.sofa.boot.util.BeanDefinitionUtil;
import com.alipay.sofa.boot.util.SmartAnnotationUtils;
import com.alipay.sofa.runtime.api.ServiceRuntimeException;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
Expand Down Expand Up @@ -57,8 +58,6 @@
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.env.Environment;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.StandardMethodMetadata;
Expand All @@ -70,6 +69,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -180,17 +180,15 @@ private void generateSofaServiceDefinitionOnMethod(String beanId,

if (candidateMethods.size() == 1) {
Method method = candidateMethods.get(0);
MergedAnnotations annotations = MergedAnnotations.from(method, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
Collection<SofaService> sofaServiceList = SmartAnnotationUtils.getAnnotations(method, SofaService.class);
// use method @SofaService annotations
if (annotations.isPresent(SofaService.class)) {
annotations.stream(SofaService.class).map(MergedAnnotation::synthesize)
.forEach((annotation) -> generateSofaServiceDefinition(beanId, annotation, returnType, beanDefinition,
if (!sofaServiceList.isEmpty()) {
sofaServiceList.forEach((annotation) -> generateSofaServiceDefinition(beanId, annotation, returnType, beanDefinition,
registry));
} else {
// use returnType class @SofaService annotations
annotations = MergedAnnotations.from(returnType, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
annotations.stream(SofaService.class).map(MergedAnnotation::synthesize)
.forEach((annotation) -> generateSofaServiceDefinition(beanId, annotation, returnType, beanDefinition,
sofaServiceList = SmartAnnotationUtils.getAnnotations(returnType, SofaService.class);
sofaServiceList.forEach((annotation) -> generateSofaServiceDefinition(beanId, annotation, returnType, beanDefinition,
registry));
}
generateSofaReferenceDefinition(beanId, candidateMethods.get(0), registry);
Expand Down Expand Up @@ -280,8 +278,7 @@ private void generateSofaServiceDefinitionOnClass(String beanId, Class<?> beanCl
BeanDefinition beanDefinition,
BeanDefinitionRegistry registry) {
// See issue: https://github.com/sofastack/sofa-boot/issues/835
MergedAnnotations annotations = MergedAnnotations.from(beanClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
annotations.stream(SofaService.class).map(MergedAnnotation::synthesize)
SmartAnnotationUtils.getAnnotations(beanClass, SofaService.class)
.forEach((annotation) -> generateSofaServiceDefinition(beanId, annotation, beanClass, beanDefinition,
registry));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
@Import({ RepeatableSofaServiceTest.RepeatableClassA.class,
RepeatableSofaServiceTest.RepeatableClassB.class,
RepeatableSofaServiceTest.RepeatableClassD.class,
RepeatableSofaServiceTest.RepeatableClassE.class,
RepeatableSofaServiceTest.RepeatableConfiguration.class })
public class RepeatableSofaServiceTest {

Expand All @@ -69,6 +70,8 @@ public void checkSofaService() {
Assert.assertTrue(findServiceByUniqueId(components, "H"));
Assert.assertTrue(findServiceByUniqueId(components, "I"));
Assert.assertTrue(findServiceByUniqueId(components, "J"));
Assert.assertTrue(findServiceByUniqueId(components, "K"));
Assert.assertTrue(findServiceByUniqueId(components, "L"));
}

private boolean findServiceByUniqueId(Collection<ComponentInfo> componentInfos, String value) {
Expand Down Expand Up @@ -119,6 +122,16 @@ public String service() {
}
}

@SofaServices({ @SofaService(interfaceType = SampleService.class, uniqueId = "L"),
@SofaService(interfaceType = SampleService.class, uniqueId = "K") })
static class RepeatableClassE extends RepeatableClassC {

@Override
public String service() {
return null;
}
}

@Configuration
@Import(RuntimeConfiguration.class)
static class RepeatableConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package com.alipay.sofa.boot.util;

import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* Utils to find suitable annotations.
*
* @author huzijie
* @version SmartAnnotationUtils.java, v 0.1 2023年06月20日 5:44 PM huzijie Exp $
*/
public class SmartAnnotationUtils {

/**
* 使用 {@link MergedAnnotations.SearchStrategy#TYPE_HIERARCHY} 搜索策略获取最近的注解集合
* <p> 如果元素上无注解,返回空的集合
* <p> 如果元素上仅有一个注解,返回包含该注解的集合
* <p> 如果元素上有多个注解,通过 {@link MergedAnnotations#get(Class)} 方法拿到最近的注解,返回的集合仅包含最近的注解所在元素上的注解
*
* @param element 注解所在元素
* @param annotationType 注解类
* @param <T> 注解类型
* @return 注解集合,可能为空或者多个,如果存在多个注解,仅保留最高优先级元素上的注解
*/
public static <T extends Annotation> Collection<T> getAnnotations(AnnotatedElement element,
Class<T> annotationType) {
return getAnnotations(element, annotationType,
MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
}

/**
* 使用指定搜索策略获取最近的注解集合
* <p> 如果元素上无注解,返回空的集合
* <p> 如果元素上仅有一个注解,返回包含该注解的集合
* <p> 如果元素上有多个注解,通过 {@link MergedAnnotations#get(Class)} 方法拿到最近的注解,返回的集合仅包含最近的注解所在元素上的注解
*
* @param element 注解所在元素
* @param annotationType 注解类
* @param searchStrategy 搜索策略
* @param <T> 注解类型
* @return 注解集合,可能为空或者多个,如果存在多个注解,仅保留最高优先级元素上的注解
*/
public static <T extends Annotation> Collection<T> getAnnotations(AnnotatedElement element, Class<T> annotationType,
MergedAnnotations.SearchStrategy searchStrategy) {
MergedAnnotations annotations = MergedAnnotations.from(element, searchStrategy);
List<T> sofaServiceList = annotations.stream(annotationType).map(MergedAnnotation::synthesize).collect(Collectors.toList());
if (sofaServiceList.size() > 1) {
// 如果存在多个注解,先通过 get 方法拿到最高优先级的注解所在的 element
Object source = annotations.get(annotationType).getSource();
// 排除非最高优先级 element 以外的注解
return annotations.stream(annotationType)
.filter(annotation -> Objects.equals(annotation.getSource(), source))
.map(MergedAnnotation::synthesize).collect(Collectors.toList());
} else {
// 如果不存在注解或者只有一个注解,直接返回
return sofaServiceList;
}
}
}
Loading

0 comments on commit 3d2898c

Please sign in to comment.