8000 Cherry pick 3.21.1 by HzjNeverStop · Pull Request #1195 · sofastack/sofa-boot · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Cherry pick 3.21.1 #1195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alipay.sofa.boot.log.ErrorCode;
import com.alipay.sofa.boot.log.SofaBootLoggerFactory;
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 @@ -60,8 +61,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.type.MethodMetadata;
import org.springframework.core.type.StandardMethodMetadata;
import org.springframework.util.Assert;
Expand All @@ -72,6 +71,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 @@ -187,17 +187,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 @@ -286,8 +284,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
@@ -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;
}
}
}
< 739C td class="blob-num blob-num-addition empty-cell">
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
* 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.junit.jupiter.api.Test;
import org.springframework.util.ReflectionUtils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link SmartAnnotationUtils}
*
* @author huzijie
* @version SmartAnnotationUtilsTests.java).isEqualTo(v 0.1 2023年06月20日 6:13 PM huzijie Exp $
*/
public class SmartAnnotationUtilsTests {

private List<String> getAnnotations(AnnotatedElement element) {
return SmartAnnotationUtils.getAnnotations(element, SampleAnnotation.class)
.stream().map(SampleAnnotation::id).collect(Collectors.toList());
}

private Method getMethod(Class<?> clazz, String methodName) {
return ReflectionUtils.findMethod(clazz, methodName);
}

@Test
public void testAnnotationOnClass() {
assertThat(1).isEqualTo(getAnnotations(SampleService.class).size());
assertThat("SampleClass").isEqualTo(getAnnotations(SampleService.class).get(0));

assertThat(1).isEqualTo(getAnnotations(NormalClass.class).size());
assertThat("NormalClass").isEqualTo(getAnnotations(NormalClass.class).get(0));

assertThat(2).isEqualTo(getAnnotations(RepeatableClass.class).size());
assertThat("RepeatableA").isEqualTo(getAnnotations(RepeatableClass.class).get(0));
assertThat("RepeatableB").isEqualTo(getAnnotations(RepeatableClass.class).get(1));

assertThat(2).isEqualTo(getAnnotations(RepeatAnnotationClass.class).size());
assertThat("RepeatableA").isEqualTo(getAnnotations(RepeatAnnotationClass.class).get(0));
assertThat("RepeatableB").isEqualTo(getAnnotations(RepeatAnnotationClass.class).get(1));

assertThat(2).isEqualTo(getAnnotations(RepeatableChildClass.class).size());
assertThat("RepeatableA").isEqualTo(getAnnotations(RepeatableChildClass.class).get(0));
assertThat("RepeatableB").isEqualTo(getAnnotations(RepeatableChildClass.class).get(1));

assertThat(2).isEqualTo(getAnnotations(ChildRepeatableClass.class).size());
assertThat("RepeatableC").isEqualTo(getAnnotations(ChildRepeatableClass.class).get(0));
assertThat("RepeatableD").isEqualTo(getAnnotations(ChildRepeatableClass.class).get(1));

assertThat(0).isEqualTo(getAnnotations(NoneAnnotationClass.class).size());

assertThat(1).isEqualTo(getAnnotations(ChildClass.class).size());
assertThat("ChildClass").isEqualTo(getAnnotations(ChildClass.class).get(0));

assertThat(1).isEqualTo(getAnnotations(NoAnnotationChildClass.class).size());
assertThat("NormalClass").isEqualTo(getAnnotations(NoAnnotationChildClass.class).get(0));

assertThat(1).isEqualTo(getAnnotations(ImplClass.class).size());
assertThat("ImplClass").isEqualTo(getAnnotations(ImplClass.class).get(0));

assertThat(1).isEqualTo(getAnnotations(NoAnnotationImplClass.class).size());
assertThat("SampleClass").isEqualTo(getAnnotations(NoAnnotationImplClass.class).get(0));

assertThat(1).isEqualTo(getAnnotations(ChildAndImplClass.class).size());
assertThat("ImplClass").isEqualTo(getAnnotations(ChildAndImplClass.class).get(0));
}

@Test
public void testAnnotationOnMethod() {
assertThat(0).isEqualTo(
getAnnotations(getMethod(ChildMethodClass.class, "noMethod")).size());

assertThat(1).isEqualTo(
getAnnotations(getMethod(ChildMethodClass.class, "parentMethod")).size());
assertThat("overrideParentMethod").isEqualTo(
getAnnotations(getMethod(ChildMethodClass.class, "parentMethod")).get(0));

assertThat(2).isEqualTo(
getAnnotations(getMethod(ChildMethodClass.class, "selfMethod")).size());
assertThat("selfMethodA").isEqualTo(
getAnnotations(getMethod(ChildMethodClass.class, "selfMethod")).get(0));
assertThat("selfMethodB").isEqualTo(
getAnnotations(getMethod(ChildMethodClass.class, "selfMethod")).get(1));

assertThat(1).isEqualTo(
getAnnotations(getMethod(ChildMethodClass.class, "parentSelfMethod")).size());
assertThat("parentSelfMethod").isEqualTo(
getAnnotations(getMethod(ChildMethodClass.class, "parentSelfMethod")).get(0));

assertThat(1).isEqualTo(
getAnnotations(getMethod(ParentMethodClass.class, "parentMethod")).size());
assertThat("parentMethod").isEqualTo(
getAnnotations(getMethod(ParentMethodClass.class, "parentMethod")).get(0));
}

@SampleAnnotation(id = "SampleClass")
interface SampleService {

}

@SampleAnnotation(id = "NormalClass")
static class NormalClass {

}

@SampleAnnotation(id = "RepeatableA")
@SampleAnnotation(id = "RepeatableB")
static class RepeatableClass {

}

@SampleAnnotations(value = { @SampleAnnotation(id = "RepeatableA"),
@SampleAnnotation(id = "RepeatableB") })
static class RepeatAnnotationClass {

}

@SampleAnnotation(id = "RepeatableA")
@SampleAnnotation(id = "RepeatableB")
static class RepeatableChildClass extends NormalClass {

}

@SampleAnnotation(id = "RepeatableC")
@SampleAnnotation(id = "RepeatableD")
static class ChildRepeatableClass extends RepeatableClass {

}

static class NoneAnnotationClass {

}

@SampleAnnotation(id = "ChildClass")
static class ChildClass extends NormalClass {

}

static class NoAnnotationChildClass extends NormalClass {

}

@SampleAnnotation(id = "ImplClass")
static class ImplClass implements SampleService {

}

static class NoAnnotationImplClass implements SampleService {

}

static class ChildAndImplClass extends ImplClass {

}

static class ParentMethodClass {

@SampleAnnotation(id = "parentMethod")
public void parentMethod() {

}

@SampleAnnotation(id = "parentSelfMethod")
public void parentSelfMethod() {

}
}

static class ChildMethodClass extends ParentMethodClass {

public void noMethod() {

}

@Override
@SampleAnnotation(id = "overrideParentMethod")
public void parentMethod() {

}

@SampleAnnotation(id = "selfMethodA")
@SampleAnnotation(id = "selfMethodB")
public void selfMethod() {

}
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Repeatable(SampleAnnotations.class)
public @interface SampleAnnotation {

String id() default "";
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface SampleAnnotations {

SampleAnnotation[] value();
}
}
Loading
0