-
Notifications
You must be signed in to change notification settings - Fork 147
/
InterceptDeclarer.java
138 lines (127 loc) · 4.78 KB
/
InterceptDeclarer.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
133
134
135
136
137
138
/*
* Copyright (C) 2021-2022 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed 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 io.sermant.core.plugin.agent.declarer;
import io.sermant.core.classloader.ClassLoaderManager;
import io.sermant.core.common.LoggerFactory;
import io.sermant.core.plugin.agent.interceptor.Interceptor;
import io.sermant.core.plugin.agent.matcher.MethodMatcher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* InterceptDeclarer
*
* @author HapThorin
* @version 1.0.0
* @since 2022-01-27
*/
public abstract class InterceptDeclarer {
/**
* 日志
*/
private static final Logger LOGGER = LoggerFactory.getLogger();
/**
* constructor
*
* @param methodMatcher method matcher
* @param interceptors interceptors
* @return InterceptDeclarer
* @throws IllegalArgumentException IllegalArgumentException
*/
public static InterceptDeclarer build(MethodMatcher methodMatcher, Interceptor... interceptors) {
if (methodMatcher == null || interceptors == null || interceptors.length == 0) {
throw new IllegalArgumentException("Matcher cannot be null and interceptor array cannot be empty. ");
}
return new InterceptDeclarer() {
@Override
public MethodMatcher getMethodMatcher() {
return methodMatcher;
}
@Override
public Interceptor[] getInterceptors(ClassLoader classLoader) {
return interceptors;
}
};
}
/**
* build InterceptDeclarer
*
* @param methodMatcher method matcher
* @param interceptors interceptors
* @return InterceptDeclarer
* @throws IllegalArgumentException IllegalArgumentException
* @deprecated Deprecated
*/
@Deprecated
public static InterceptDeclarer build(MethodMatcher methodMatcher, String... interceptors) {
if (methodMatcher == null || interceptors == null || interceptors.length == 0) {
throw new IllegalArgumentException("Matcher cannot be null and interceptor array cannot be empty. ");
}
return new InterceptDeclarer() {
@Override
public MethodMatcher getMethodMatcher() {
return methodMatcher;
}
@Override
public Interceptor[] getInterceptors(ClassLoader classLoader) {
try {
return createInterceptors(interceptors);
} catch (IllegalAccessException | ClassNotFoundException | InstantiationException e) {
LOGGER.log(Level.SEVERE,
"Unable to create instance of interceptors: " + Arrays.toString(interceptors), e);
}
return new Interceptor[0];
}
};
}
/**
* Create all interceptor objects using the classLoader of the class being enhanced
*
* @param interceptors Interceptor fully qualified name set
* @return Interceptor set
* @throws ClassNotFoundException Class not found
* @throws IllegalAccessException The addURL method or defineClass method cannot be accessed
* @throws InstantiationException Instantiation failure
* @deprecated Deprecated
*/
@Deprecated
private static Interceptor[] createInterceptors(String[] interceptors)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
final ArrayList<Interceptor> interceptorList = new ArrayList<>();
for (String interceptor : interceptors) {
final Object instance = ClassLoaderManager.getPluginClassFinder().loadSermantClass(interceptor)
.newInstance();
if (instance instanceof Interceptor) {
interceptorList.add((Interceptor) instance);
}
}
return interceptorList.toArray(new Interceptor[0]);
}
/**
* Get method matcher
*
* @return method matcher
*/
public abstract MethodMatcher getMethodMatcher();
/**
* Gets the interceptor set
*
* @param classLoader The classLoader of the enhanced class
* @return Interceptor set
*/
public abstract Interceptor[] getInterceptors(ClassLoader classLoader);
}