mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-03-13 11:20:32 +08:00
fix unsafe access warning
This commit is contained in:
parent
da3eca3d49
commit
df63bc5c74
@ -130,7 +130,8 @@ public class TestClassHandler extends BaseClassHandler {
|
||||
|
||||
private boolean isMockForConstructor(MethodNode mn) {
|
||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||
String method = AnnotationUtil.getAnnotationParameter(an, ConstPool.FIELD_TARGET_METHOD, null);
|
||||
String method = AnnotationUtil.getAnnotationParameter
|
||||
(an, ConstPool.FIELD_TARGET_METHOD, null, String.class);
|
||||
if (ConstPool.CONSTRUCTOR.equals(method)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -90,7 +90,8 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||
if (toDotSeparateFullClassName(an.desc).equals(ConstPool.TESTABLE_MOCK)) {
|
||||
String targetClass = ClassUtil.toSlashSeparateFullClassName(methodDescPair.left);
|
||||
String targetMethod = AnnotationUtil.getAnnotationParameter(an, ConstPool.FIELD_TARGET_METHOD, mn.name);
|
||||
String targetMethod = AnnotationUtil.getAnnotationParameter(
|
||||
an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class);
|
||||
if (targetMethod.equals(ConstPool.CONSTRUCTOR)) {
|
||||
String sourceClassName = ClassUtil.getSourceClassName(cn.name);
|
||||
methodInfos.add(new MethodInfo(sourceClassName, targetMethod, mn.name, mn.desc));
|
||||
|
@ -10,11 +10,11 @@ public class AnnotationUtil {
|
||||
/**
|
||||
* Read value of annotation parameter
|
||||
*/
|
||||
public static <T> T getAnnotationParameter(AnnotationNode an, String key, T defaultValue) {
|
||||
public static <T> T getAnnotationParameter(AnnotationNode an, String key, T defaultValue, Class<T> clazz) {
|
||||
if (an.values != null) {
|
||||
for (int i = 0; i < an.values.size(); i += 2) {
|
||||
if (an.values.get(i).equals(key)) {
|
||||
return (T)(an.values.get(i + 1));
|
||||
return clazz.cast(an.values.get(i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,9 +12,9 @@ class AnnotationUtilTest {
|
||||
void should_get_annotation_parameter() {
|
||||
AnnotationNode an = new AnnotationNode("");
|
||||
an.values = listOf((Object)"testKey", "testValue", "demoKey", "demoValue");
|
||||
assertEquals("testValue", AnnotationUtil.getAnnotationParameter(an, "testKey", "none"));
|
||||
assertEquals("demoValue", AnnotationUtil.getAnnotationParameter(an, "demoKey", "none"));
|
||||
assertEquals("none", AnnotationUtil.getAnnotationParameter(an, "testValue", "none"));
|
||||
assertEquals("testValue", AnnotationUtil.getAnnotationParameter(an, "testKey", "none", String.class));
|
||||
assertEquals("demoValue", AnnotationUtil.getAnnotationParameter(an, "demoKey", "none", String.class));
|
||||
assertEquals("none", AnnotationUtil.getAnnotationParameter(an, "testValue", "none", String.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user