Java Reflection Field Set setFieldForAnnotation(Object target, Class annotation, Object value)

Here you can find the source of setFieldForAnnotation(Object target, Class annotation, Object value)

Description

set Field For Annotation

License

Open Source License

Declaration

public static void setFieldForAnnotation(Object target, Class<? extends Annotation> annotation, Object value) 

Method Source Code

//package com.java2s;
/**************************************************************************************
 * Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
 * http://esper.codehaus.org                                                          *
 * http://www.espertech.com                                                           *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the GPL license       *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/

import java.lang.annotation.Annotation;
import java.lang.reflect.*;

import java.util.*;

public class Main {
    public static void setFieldForAnnotation(Object target, Class<? extends Annotation> annotation, Object value) {
        boolean found = setFieldForAnnotation(target, annotation, value, target.getClass());
        if (!found) {

            Class superClass = target.getClass().getSuperclass();
            while (!found) {
                found = setFieldForAnnotation(target, annotation, value, superClass);
                if (!found) {
                    superClass = superClass.getSuperclass();
                }/*from   www .  j  a va 2 s  .c  o  m*/
                if (superClass == Object.class || superClass == null) {
                    break;
                }
            }
        }
    }

    private static boolean setFieldForAnnotation(Object target, Class<? extends Annotation> annotation,
            Object value, Class currentClass) {
        boolean found = false;
        for (Field field : currentClass.getDeclaredFields()) {
            if (isAnnotationListed(annotation, field.getDeclaredAnnotations())) {
                field.setAccessible(true);
                try {
                    field.set(target, value);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to set field " + field + " on class "
                            + currentClass.getName() + ": " + e.getMessage(), e);
                }
                return true;
            }
        }
        return found;
    }

    public static boolean isAnnotationListed(Class<? extends Annotation> annotationClass,
            Annotation[] annotations) {
        return !getAnnotations(annotationClass, annotations).isEmpty();
    }

    public static List<Annotation> getAnnotations(Class<? extends Annotation> annotationClass,
            Annotation[] annotations) {
        List<Annotation> result = null;
        for (Annotation annotation : annotations) {
            if (annotation.annotationType() == annotationClass) {
                if (result == null) {
                    result = new ArrayList<Annotation>();
                }
                result.add(annotation);
            }
        }
        if (result == null) {
            return Collections.emptyList();
        }
        return result;
    }
}

Related

  1. setFieldAccessible(Class clazz, String... names)
  2. setFieldAccessible(Field field)
  3. setFieldBySomeMethod(List setMethods, Object object, Object value)
  4. setFieldContent(final Object obj, final String name, final Object value)
  5. setFieldEditable(Class clazz, String fieldName)
  6. setFieldHelper(Object o, String name, Object v)
  7. setFieldIfExists(final Object instance, final String fieldName, final Object value)
  8. setFieldObject(Class clazz, String field, Object object, Object newObj)
  9. setFieldObject(Class target, Object targetObject, String fieldName, Object object)