fix unsafe access warning

This commit is contained in:
金戟 2020-11-01 07:53:04 +08:00
parent da3eca3d49
commit df63bc5c74
4 changed files with 9 additions and 7 deletions

View File

@ -130,7 +130,8 @@ public class TestClassHandler extends BaseClassHandler {
private boolean isMockForConstructor(MethodNode mn) { private boolean isMockForConstructor(MethodNode mn) {
for (AnnotationNode an : mn.visibleAnnotations) { 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)) { if (ConstPool.CONSTRUCTOR.equals(method)) {
return true; return true;
} }

View File

@ -90,7 +90,8 @@ public class TestableClassTransformer implements ClassFileTransformer {
for (AnnotationNode an : mn.visibleAnnotations) { for (AnnotationNode an : mn.visibleAnnotations) {
if (toDotSeparateFullClassName(an.desc).equals(ConstPool.TESTABLE_MOCK)) { if (toDotSeparateFullClassName(an.desc).equals(ConstPool.TESTABLE_MOCK)) {
String targetClass = ClassUtil.toSlashSeparateFullClassName(methodDescPair.left); 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)) { if (targetMethod.equals(ConstPool.CONSTRUCTOR)) {
String sourceClassName = ClassUtil.getSourceClassName(cn.name); String sourceClassName = ClassUtil.getSourceClassName(cn.name);
methodInfos.add(new MethodInfo(sourceClassName, targetMethod, mn.name, mn.desc)); methodInfos.add(new MethodInfo(sourceClassName, targetMethod, mn.name, mn.desc));

View File

@ -10,11 +10,11 @@ public class AnnotationUtil {
/** /**
* Read value of annotation parameter * 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) { if (an.values != null) {
for (int i = 0; i < an.values.size(); i += 2) { for (int i = 0; i < an.values.size(); i += 2) {
if (an.values.get(i).equals(key)) { if (an.values.get(i).equals(key)) {
return (T)(an.values.get(i + 1)); return clazz.cast(an.values.get(i + 1));
} }
} }
} }

View File

@ -12,9 +12,9 @@ class AnnotationUtilTest {
void should_get_annotation_parameter() { void should_get_annotation_parameter() {
AnnotationNode an = new AnnotationNode(""); AnnotationNode an = new AnnotationNode("");
an.values = listOf((Object)"testKey", "testValue", "demoKey", "demoValue"); an.values = listOf((Object)"testKey", "testValue", "demoKey", "demoValue");
assertEquals("testValue", AnnotationUtil.getAnnotationParameter(an, "testKey", "none")); assertEquals("testValue", AnnotationUtil.getAnnotationParameter(an, "testKey", "none", String.class));
assertEquals("demoValue", AnnotationUtil.getAnnotationParameter(an, "demoKey", "none")); assertEquals("demoValue", AnnotationUtil.getAnnotationParameter(an, "demoKey", "none", String.class));
assertEquals("none", AnnotationUtil.getAnnotationParameter(an, "testValue", "none")); assertEquals("none", AnnotationUtil.getAnnotationParameter(an, "testValue", "none", String.class));
} }
} }