forked from apache/dubbo
/
ServiceAnnotationResolver.java
132 lines (108 loc) · 4.45 KB
/
ServiceAnnotationResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* 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 org.apache.dubbo.common.utils;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
import java.lang.annotation.Annotation;
import java.util.List;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
import static org.apache.dubbo.common.utils.AnnotationUtils.getAttribute;
import static org.apache.dubbo.common.utils.ArrayUtils.isNotEmpty;
import static org.apache.dubbo.common.utils.ClassUtils.isGenericClass;
import static org.apache.dubbo.common.utils.ClassUtils.resolveClass;
import static org.apache.dubbo.common.utils.StringUtils.isEmpty;
/**
* The resolver class for {@link Service @Service}
*
* @see Service
* @see com.alibaba.dubbo.config.annotation.Service
* @since 2.7.6
*/
public class ServiceAnnotationResolver {
/**
* The annotation {@link Class classes} of Dubbo Service (read-only)
*
* @since 2.7.9
*/
public static List<Class<? extends Annotation>> SERVICE_ANNOTATION_CLASSES = unmodifiableList(asList(DubboService.class, Service.class, com.alibaba.dubbo.config.annotation.Service.class));
private final Annotation serviceAnnotation;
private final Class<?> serviceType;
public ServiceAnnotationResolver(Class<?> serviceType) throws IllegalArgumentException {
this.serviceType = serviceType;
this.serviceAnnotation = getServiceAnnotation(serviceType);
}
private Annotation getServiceAnnotation(Class<?> serviceType) {
Annotation serviceAnnotation = null;
for (Class<? extends Annotation> serviceAnnotationClass : SERVICE_ANNOTATION_CLASSES) {
serviceAnnotation = serviceType.getAnnotation(serviceAnnotationClass);
if (serviceAnnotation != null) {
break;
}
}
if (serviceAnnotation == null) {
throw new IllegalArgumentException(format("Any annotation of [%s] can't be annotated in the service type[%s].",
SERVICE_ANNOTATION_CLASSES,
serviceType.getName()
));
}
return serviceAnnotation;
}
/**
* Resolve the class name of interface
*
* @return if not found, return <code>null</code>
*/
public String resolveInterfaceClassName() {
Class interfaceClass = null;
// first, try to get the value from "interfaceName" attribute
String interfaceName = resolveAttribute("interfaceName");
if (isEmpty(interfaceName)) { // If not found, try "interfaceClass"
interfaceClass = resolveAttribute("interfaceClass");
} else {
interfaceClass = resolveClass(interfaceName, getClass().getClassLoader());
}
if (isGenericClass(interfaceClass)) {
interfaceName = interfaceClass.getName();
} else {
interfaceName = null;
}
if (isEmpty(interfaceName)) { // If not fund, try to get the first interface from the service type
Class[] interfaces = serviceType.getInterfaces();
if (isNotEmpty(interfaces)) {
interfaceName = interfaces[0].getName();
}
}
return interfaceName;
}
public String resolveVersion() {
return resolveAttribute("version");
}
public String resolveGroup() {
return resolveAttribute("group");
}
private <T> T resolveAttribute(String attributeName) {
return getAttribute(serviceAnnotation, attributeName);
}
public Annotation getServiceAnnotation() {
return serviceAnnotation;
}
public Class<?> getServiceType() {
return serviceType;
}
}