Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
bingoohuang committed May 14, 2018
1 parent 39b2dc5 commit 8edb5d8
Showing 1 changed file with 12 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ private static List<AnnotationAndRoot> filterForSupportedType(Annotation[] annot
List<AnnotationAndRoot> result = Lists.newArrayList();

for (val ann : annotations) {
if (supportType(ann, type)) {
result.add(new AnnotationAndRoot(ann));
}
if (supportType(ann, type)) result.add(new AnnotationAndRoot(ann));
}

return result;
Expand Down Expand Up @@ -101,11 +99,10 @@ private static boolean asmTypeValidateGeneratorSupport(AsmConstraint asmConstrai
}

public static Class<? extends MsaValidator> findMsaSupportType(AsmConstraint asmConstraint, Class<?> type) {
Class<?> wrap = Primitives.wrap(type);
for (val msa : asmConstraint.validateBy()) {
val arg = findSuperActualTypeArg(msa, MsaValidator.class, 1);
if (arg == null) continue;
if (arg.isAssignableFrom(wrap)) return msa;
if (arg.isAssignableFrom(Primitives.wrap(type))) return msa;
}

return null;
Expand All @@ -117,10 +114,9 @@ public static Class<?> findSuperActualTypeArg(Class<?> subClass, Class<?> superC
if (argType != null) return argType;
}

Class<?> parentClass = subClass.getSuperclass();
if (parentClass == Object.class) return null;
if (subClass.getSuperclass() == Object.class) return null;

return findSuperActualTypeArg(parentClass, superClass, argIndex);
return findSuperActualTypeArg(subClass.getSuperclass(), superClass, argIndex);
}

private static Class<?> findSuperActualTypeArg(Type type, Class<?> superClass, int argIndex) {
Expand Down Expand Up @@ -182,10 +178,8 @@ public static void searchConstraints(List<AnnotationAndRoot> asmConstraintAnns,
}

public static MethodVisitor startFieldValidatorMethod(ClassWriter cw, String fieldName, Class beanClass) {
val mv = cw.visitMethod(ACC_PRIVATE,
VALIDATE + StringUtils.capitalize(fieldName),
sig(void.class, beanClass, AsmValidateResult.class),
null, null);
val mv = cw.visitMethod(ACC_PRIVATE, VALIDATE + StringUtils.capitalize(fieldName),
sig(void.class, beanClass, AsmValidateResult.class), null, null);
mv.visitCode();
return mv;
}
Expand All @@ -197,33 +191,28 @@ public static void endFieldValidateMethod(MethodVisitor mv) {
}

public static boolean isAnnotationPresent(Annotation[] as, Class<?> ac) {
for (val a : as) {
if (ac.isInstance(a)) return true;
}
for (val a : as) if (ac.isInstance(a)) return true;

return false;
}

@SuppressWarnings("unchecked")
public static <T extends Annotation> T findAnn(Annotation[] as, Class<T> ac) {
for (val a : as) {
if (ac.isInstance(a)) return (T) a;
}
for (val a : as) if (ac.isInstance(a)) return (T) a;

return null;
}

public static void visitGetter(MethodVisitor mv, Field f) {
mv.visitVarInsn(ALOAD, 1);
val getterName = "get" + capitalize(f.getName());
val declaringClass = f.getDeclaringClass();
try {
declaringClass.getMethod(getterName);
f.getDeclaringClass().getMethod(getterName);
} catch (NoSuchMethodException e) {
throw new AsmValidateBadUsageException("there is no getter method for field " + f.getName());
}

mv.visitMethodInsn(INVOKEVIRTUAL, p(declaringClass), getterName, sig(f.getType()), false);
mv.visitMethodInsn(INVOKEVIRTUAL, p(f.getDeclaringClass()), getterName, sig(f.getType()), false);
}

public static void addIsNullLocal(LocalIndices localIndices, MethodVisitor mv) {
Expand Down Expand Up @@ -257,15 +246,12 @@ public static void visitValidateFieldMethod(MethodVisitor mv, String implName, S
mv.visitVarInsn(ALOAD, 0); // this
mv.visitVarInsn(ALOAD, 1); // field value
mv.visitVarInsn(ALOAD, 2); // AsmValidateResult
mv.visitMethodInsn(INVOKESPECIAL, p(implName),
MethodGeneratorUtils.VALIDATE + capitalize(fieldName),
mv.visitMethodInsn(INVOKESPECIAL, p(implName), MethodGeneratorUtils.VALIDATE + capitalize(fieldName),
sig(void.class, fieldClass, AsmValidateResult.class), false);
}

public static boolean hasBlankable(List<AnnotationAndRoot> annotations) {
for (val ar : annotations) {
if (ar.ann().annotationType() == AsmBlankable.class) return true;
}
for (val ar : annotations) if (ar.ann().annotationType() == AsmBlankable.class) return true;

return false;
}
Expand Down

0 comments on commit 8edb5d8

Please sign in to comment.