vendredi 13 avril 2018

Is using java reflection in aws lambda a bad practice?

First of all, I know it's plenty of already asked (and generically answered) questions around here about how good of a practice it is to use Java Reflection, but I'm specifically wondering how bad it is in a specific context that is AWS Lambda.

I'm thrilled and amazed on the power of Java Reflection and I wrote some utility methods to more easily and (code length) efficiently execute some tasks in my serverless architecture, in particular when I have to deal with Dynamo DB queries.

I think that AWS Dynamo DB sdk lacks of some powerful facilities and that the DynamoDBMapper class is far from the perfection, but it still might be because of some lack of knowledge by myself.

Going straight to the point, and thinking on a sentence I've heard yesterday about lambda, "you don't pay for the code storage", I'd like to know, from your experience, if I'd rather write tons of boilerplate code or using reflection is good enough.

Here's an explanatory enough example, that I use to get the real attributeName of a field or a method from a class mapping a DynamoDBTable:

private static <T extends Bean, A extends Annotation> String getBeanKeyAttributeName(Class<T> tClass,
        Class<A> annClass) {
    Field field = Arrays.stream(tClass.getDeclaredFields()).filter(f -> f.isAnnotationPresent(annClass)).findAny()
            .orElse(null);
    if (field != null) {
        String s = getAttributeName(annClass, field.getAnnotation(annClass));
        if (s != null)
            return s;
        return field.getName();
    }
    Method method = Arrays.stream(tClass.getDeclaredMethods()).filter(f -> f.isAnnotationPresent(annClass))
            .findAny().orElse(null);
    if (method == null)
        throw new IllegalArgumentException();
    String key = method.getName();
    if (key.startsWith("get")) {
        key = key.replaceFirst("get", "");
        key = (key.charAt(0) + "").toLowerCase() + key.substring(1);
    }
    String s = getAttributeName(annClass, method.getAnnotation(annClass));
    if (s != null)
        return s;
    return key;
}

private static <A extends Annotation> String getAttributeName(Class<A> annClass, A annotation) {
    try {
        Method attributeNameMethod = annClass.getMethod("attributeName");
        attributeNameMethod.setAccessible(true);
        String s = (String) attributeNameMethod.invoke(annotation);
        if (!s.isEmpty())
            return s;
    } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}

`





Aucun commentaire:

Enregistrer un commentaire