Example usage for java.lang.reflect Field getDeclaringClass

List of usage examples for java.lang.reflect Field getDeclaringClass

Introduction

In this page you can find the example usage for java.lang.reflect Field getDeclaringClass.

Prototype

@Override
public Class<?> getDeclaringClass() 

Source Link

Document

Returns the Class object representing the class or interface that declares the field represented by this Field object.

Usage

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Affirms if the original declaration the given field is annotated
 * for reflection. //from w ww  .ja  va 2 s . c o m
 */
static boolean canReflect(Field field) {
    Class cls = field.getDeclaringClass();
    return canReflect((Reflectable) cls.getAnnotation(Reflectable.class),
            field.getAnnotation(Reflectable.class));
}

From source file:adalid.core.XS1.java

static void logDuplicateAnnotation(Field field, Class<? extends Annotation> annotation, Field previous) {
    String name = field.getName();
    Class<?> type = field.getDeclaringClass();
    String string = previous + " has the same annotation";
    logFieldAnnotationErrorMessage(name, type, annotation, string);
}

From source file:gov.nih.nci.system.web.util.RESTUtil.java

public static Object getAttribute(Field field, String attributeName, Class rootClass) throws Exception {
    Object attribute = null;/* w  ww  .  j  av  a  2  s.  c  o  m*/

    String fieldName = field.getDeclaringClass().getName() + "." + field.getName();
    log.debug("attributeName " + attributeName);
    log.debug("fieldName " + fieldName);
    if (attributeName.equals(fieldName)) {
        attribute = Class.forName(fieldName).newInstance();
        log.debug("Equal returning " + attribute);
        return attribute;
    } else if (attributeName.indexOf(fieldName) == -1)
        throw new Exception("Invalid field and attribute combination.  Field: " + fieldName + " ; attribute:"
                + attributeName);

    String subAttributeName = attributeName.substring(attributeName.indexOf(fieldName) + 1,
            attributeName.length());
    log.debug("subAttributeName " + subAttributeName);

    return attribute;
}

From source file:adalid.core.XS1.java

static boolean checkFieldAnnotation(boolean log, Field field, Class<? extends Annotation> annotation,
        Class<?>[] validTypes) {
    String name = field.getName();
    Class<?> type = field.getDeclaringClass();
    String string;/*from w  ww.  j a va2  s. com*/
    int modifiers = field.getModifiers();
    if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) {
        string = "field " + name + " has static and/or final modifier";
        if (log) {
            logFieldAnnotationErrorMessage(name, type, annotation, string);
        }
        return false;
    }
    int length = validTypes == null ? 0 : validTypes.length;
    if (length < 1) {
        return true;
    }
    Class<?> ft = getTrueType(field.getType());
    String[] strings = new String[length];
    int i = 0;
    for (Class<?> vt : validTypes) {
        if (vt.isAssignableFrom(ft)) {
            return true;
        }
        strings[i++] = vt.getSimpleName();
    }
    string = "type of " + name + " is not " + StringUtils.join(strings, " or ");
    if (log) {
        logFieldAnnotationErrorMessage(name, type, annotation, string);
    }
    return false;
}

From source file:org.thelq.stackexchange.api.model.types.EntryFormatTest.java

@Test(dataProvider = "enumsInFieldsAreNotFromAnotherClassDataProvider")
public void enumsInFieldsAreNotFromAnotherClass(Field curField) {
    assertEquals(curField.getDeclaringClass(), curField.getType().getDeclaringClass(), "Classes do not match");
}

From source file:jease.cmf.service.Serializer.java

public boolean isNodeDeclaringClass(Field field) {
    return field.getDeclaringClass() == Node.class;
}

From source file:jease.cmf.service.Serializer.java

public void omitField(Field field) {
    xstream.omitField(field.getDeclaringClass(), field.getName());
}

From source file:ch.rasc.extclassgenerator.ModelGenerator.java

private static void createModelBean(ModelBean model, AccessibleObject accessibleObject,
        OutputConfig outputConfig) {//  ww w  .  j a  v a 2 s .c o m
    Class<?> javaType = null;
    String name = null;
    Class<?> declaringClass = null;

    if (accessibleObject instanceof Field) {
        Field field = (Field) accessibleObject;
        javaType = field.getType();
        name = field.getName();
        declaringClass = field.getDeclaringClass();
    } else if (accessibleObject instanceof Method) {
        Method method = (Method) accessibleObject;

        javaType = method.getReturnType();
        if (javaType.equals(Void.TYPE)) {
            return;
        }

        if (method.getName().startsWith("get")) {
            name = StringUtils.uncapitalize(method.getName().substring(3));
        } else if (method.getName().startsWith("is")) {
            name = StringUtils.uncapitalize(method.getName().substring(2));
        } else {
            name = method.getName();
        }

        declaringClass = method.getDeclaringClass();
    }

    ModelType modelType = null;
    if (model.isAutodetectTypes()) {
        for (ModelType mt : ModelType.values()) {
            if (mt.supports(javaType)) {
                modelType = mt;
                break;
            }
        }
    } else {
        modelType = ModelType.AUTO;
    }

    ModelFieldBean modelFieldBean = null;

    ModelField modelFieldAnnotation = accessibleObject.getAnnotation(ModelField.class);
    if (modelFieldAnnotation != null) {

        if (StringUtils.hasText(modelFieldAnnotation.value())) {
            name = modelFieldAnnotation.value();
        }

        if (StringUtils.hasText(modelFieldAnnotation.customType())) {
            modelFieldBean = new ModelFieldBean(name, modelFieldAnnotation.customType());
        } else {
            ModelType type = null;
            if (modelFieldAnnotation.type() != ModelType.NOT_SPECIFIED) {
                type = modelFieldAnnotation.type();
            } else {
                type = modelType;
            }

            modelFieldBean = new ModelFieldBean(name, type);
        }

        updateModelFieldBean(modelFieldBean, modelFieldAnnotation);
        model.addField(modelFieldBean);
    } else {
        if (modelType != null) {
            modelFieldBean = new ModelFieldBean(name, modelType);
            model.addField(modelFieldBean);
        }
    }

    ModelId modelIdAnnotation = accessibleObject.getAnnotation(ModelId.class);
    if (modelIdAnnotation != null) {
        model.setIdProperty(name);
    }

    ModelClientId modelClientId = accessibleObject.getAnnotation(ModelClientId.class);
    if (modelClientId != null) {
        model.setClientIdProperty(name);
        model.setClientIdPropertyAddToWriter(modelClientId.configureWriter());
    }

    ModelVersion modelVersion = accessibleObject.getAnnotation(ModelVersion.class);
    if (modelVersion != null) {
        model.setVersionProperty(name);
    }

    ModelAssociation modelAssociationAnnotation = accessibleObject.getAnnotation(ModelAssociation.class);
    if (modelAssociationAnnotation != null) {
        model.addAssociation(AbstractAssociation.createAssociation(modelAssociationAnnotation, model, javaType,
                declaringClass, name));
    }

    if (modelFieldBean != null && outputConfig.getIncludeValidation() != IncludeValidation.NONE) {

        Set<ModelValidation> modelValidationAnnotations = AnnotationUtils
                .getRepeatableAnnotation(accessibleObject, ModelValidations.class, ModelValidation.class);
        if (!modelValidationAnnotations.isEmpty()) {
            for (ModelValidation modelValidationAnnotation : modelValidationAnnotations) {
                AbstractValidation modelValidation = AbstractValidation.createValidation(name,
                        modelValidationAnnotation, outputConfig.getIncludeValidation());
                if (modelValidation != null) {
                    model.addValidation(modelValidation);
                }
            }
        } else {
            Annotation[] fieldAnnotations = accessibleObject.getAnnotations();

            for (Annotation fieldAnnotation : fieldAnnotations) {
                AbstractValidation.addValidationToModel(model, modelFieldBean, fieldAnnotation,
                        outputConfig.getIncludeValidation());
            }

            if (accessibleObject instanceof Field) {
                PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(declaringClass, name);
                if (pd != null && pd.getReadMethod() != null) {
                    for (Annotation readMethodAnnotation : pd.getReadMethod().getAnnotations()) {
                        AbstractValidation.addValidationToModel(model, modelFieldBean, readMethodAnnotation,
                                outputConfig.getIncludeValidation());
                    }
                }
            }
        }
    }

}

From source file:jease.cmf.service.Serializer.java

public void registerConverter(Field field) {
    xstream.registerLocalConverter(field.getDeclaringClass(), field.getName(),
            field.getType().isArray() ? nodeArrayConverter : nodeConverter);
}

From source file:com.github.javarch.support.log.LoggingAnnotationBeanPostProcessor.java

private void injectLogger(Object bean, Field field) {
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, bean, LoggerFactory.getLogger(field.getDeclaringClass()));
}