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:com.uphyca.testing.junit3.support.v4.FragmentTestCase.java

/**
 * This function is called by various TestCase implementations, at
 * tearDown() time, in order to scrub out any class variables. This protects
 * against memory leaks in the case where a test case creates a non-static
 * inner class (thus referencing the test case) and gives it to someone else
 * to hold onto./*from  w  ww  .j  av  a 2 s  . c om*/
 * 
 * @param testCaseClass
 *            The class of the derived TestCase implementation.
 * 
 * @throws IllegalAccessException
 */
protected void scrubClass(final Class<?> testCaseClass) throws IllegalAccessException {
    final Field[] fields = getClass().getDeclaredFields();
    for (Field field : fields) {
        final Class<?> fieldClass = field.getDeclaringClass();
        if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()
                && (field.getModifiers() & Modifier.FINAL) == 0) {
            try {
                field.setAccessible(true);
                field.set(this, null);
            } catch (Exception e) {
                android.util.Log.d("TestCase", "Error: Could not nullify field!");
            }

            if (field.get(this) != null) {
                android.util.Log.d("TestCase", "Error: Could not nullify field!");
            }
        }
    }
}

From source file:info.archinnov.achilles.internal.metadata.parsing.context.PropertyParsingContext.java

public PropertyParsingContext duplicateForField(Field field) {
    return new PropertyParsingContext(context.duplicateForClass(field.getDeclaringClass()), field);
}

From source file:com.google.gwt.dev.shell.mac.WebKitDispatchAdapter.java

@SuppressWarnings("unchecked")
public String[] getFields() {
    try {//www.j a  v  a 2  s  . c o m
        DispatchClassInfo classInfo = classLoader.getClassInfoFromClassName(getTarget().getClass().getName());
        Collection<Field> selected = CollectionUtils.select(classInfo.getMembers(), new Predicate() {
            public boolean evaluate(Object object) {
                return object instanceof Field;
            }
        });
        String[] result = new String[selected.size()];
        int index = 0;
        for (Field field : selected) {
            String jsFieldName = "@" + field.getDeclaringClass().getName() + "::" + field.getName();
            result[index++] = Jsni.WBP_MEMBER + classLoader.getDispId(jsFieldName);
        }
        return result;
    } catch (Throwable e) {
        return new String[0];
    }
}

From source file:org.gradle.api.internal.tasks.options.FieldOptionElement.java

public FieldOptionElement(Field field, String optionName, Option option, Class<?> optionType,
        ValueAwareNotationParser<?> notationParser) {
    super(optionName, option, optionType, field.getDeclaringClass(), notationParser);
    this.field = field;
    getSetter();/*  w w  w.  j av a2s  .  co  m*/
}

From source file:com.haulmont.cuba.core.sys.CubaAnnotationsLoader.java

private boolean propertyBelongsTo(Field field, MetaProperty metaProperty, List<Class> systemInterfaces) {
    String getterName = "get" + StringUtils.capitalize(metaProperty.getName());

    Class<?> aClass = field.getDeclaringClass();
    //noinspection unchecked
    List<Class> allInterfaces = ClassUtils.getAllInterfaces(aClass);
    for (Class intf : allInterfaces) {
        Method[] methods = intf.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals(getterName) && method.getParameterTypes().length == 0) {
                if (systemInterfaces.contains(intf))
                    return true;
            }/*from   w  w w .j  a va  2 s  .c om*/
        }
    }
    return false;
}

From source file:com.rosenvold.spring.SpringContextAnalyzer.java

List<FieldProblem> getFieldProblemsForSingletonBean(Object singletonBean, BeanDefinition beanDefinition) {
    List<FieldProblem> fieldProblems = new ArrayList<FieldProblem>();
    Field[] fields = singletonBean.getClass().getDeclaredFields();
    for (Field field : fields) {
        Class clazz = field.getDeclaringClass();
        if (!isInLegalRuntimeState(singletonBean, field)) { // This is the best analysis. Simply checks that all declared fields have object values when spring is done.
            fieldProblems.add(new FieldProblem(field, FieldProblemType.NotInitializedAtRuntime));
        }/*from   www .  java  2s .co  m*/

        if (violatesProxyRequirementsForSingletonClient(singletonBean, field)) {
            fieldProblems.add(new FieldProblem(field, FieldProblemType.RequiresScopeProxy));

        }
        /*else if (!isLegalForSingletonBean(field, beanDefinition)) {
        fieldProblems.add( new FieldProblem(field, FieldProblemType.NotVaildForSingleton));
        } */
    }
    return fieldProblems;
}

From source file:net.cpollet.jixture.hibernate3.helper.Hibernate3MappingDefinitionHolder.java

private BeanInfo getBeanInfo(Field field) {
    try {/*from  w  ww .  j a  v  a2  s. co  m*/
        return Introspector.getBeanInfo(field.getDeclaringClass());
    } catch (IntrospectionException e) {
        throw ExceptionUtils.wrapInRuntimeException(e);
    }
}

From source file:ch.ralscha.extdirectspring.generator.association.AbstractAssociation.java

public static AbstractAssociation createAssociation(ModelAssociation associationAnnotation, ModelBean model,
        Field field) {
    ModelAssociationType type = associationAnnotation.value();

    Class<?> associationClass = associationAnnotation.model();
    if (associationClass == Object.class) {
        associationClass = field.getType();
    }//from w w w.  j  a  v a  2 s. c o  m

    AbstractAssociation association;

    if (type == ModelAssociationType.HAS_MANY) {
        association = new HasManyAssociation(associationClass);
    } else if (type == ModelAssociationType.BELONGS_TO) {
        association = new BelongsToAssociation(associationClass);
    } else {
        association = new HasOneAssociation(associationClass);
    }

    association.setAssociationKey(field.getName());

    if (StringUtils.hasText(associationAnnotation.foreignKey())) {
        association.setForeignKey(associationAnnotation.foreignKey());
    } else if (type == ModelAssociationType.HAS_MANY) {
        association.setForeignKey(StringUtils.uncapitalize(field.getDeclaringClass().getSimpleName()) + "_id");
    } else if (type == ModelAssociationType.BELONGS_TO || type == ModelAssociationType.HAS_ONE) {
        association.setForeignKey(StringUtils.uncapitalize(associationClass.getSimpleName()) + "_id");
    }

    if (StringUtils.hasText(associationAnnotation.primaryKey())) {
        association.setPrimaryKey(associationAnnotation.primaryKey());
    } else if (type == ModelAssociationType.HAS_MANY && StringUtils.hasText(model.getIdProperty())
            && !model.getIdProperty().equals("id")) {
        association.setPrimaryKey(model.getIdProperty());
    } else if (type == ModelAssociationType.BELONGS_TO || type == ModelAssociationType.HAS_ONE) {
        Model associationModelAnnotation = associationClass.getAnnotation(Model.class);
        if (associationModelAnnotation != null && StringUtils.hasText(associationModelAnnotation.idProperty())
                && !associationModelAnnotation.idProperty().equals("id")) {
            association.setPrimaryKey(associationModelAnnotation.idProperty());
        }
    }

    if (type == ModelAssociationType.HAS_MANY) {
        HasManyAssociation hasManyAssociation = (HasManyAssociation) association;

        if (StringUtils.hasText(associationAnnotation.setterName())) {
            LogFactory.getLog(ModelGenerator.class)
                    .warn(getWarningText(field, association.getType(), "setterName"));
        }

        if (StringUtils.hasText(associationAnnotation.getterName())) {
            LogFactory.getLog(ModelGenerator.class)
                    .warn(getWarningText(field, association.getType(), "getterName"));
        }

        if (associationAnnotation.autoLoad()) {
            hasManyAssociation.setAutoLoad(true);
        }
        if (StringUtils.hasText(associationAnnotation.name())) {
            hasManyAssociation.setName(associationAnnotation.name());
        } else {
            hasManyAssociation.setName(field.getName());
        }

    } else if (type == ModelAssociationType.BELONGS_TO) {
        BelongsToAssociation belongsToAssociation = (BelongsToAssociation) association;

        if (StringUtils.hasText(associationAnnotation.setterName())) {
            belongsToAssociation.setSetterName(associationAnnotation.setterName());
        } else {
            belongsToAssociation.setSetterName("set" + StringUtils.capitalize(field.getName()));
        }

        if (StringUtils.hasText(associationAnnotation.getterName())) {
            belongsToAssociation.setGetterName(associationAnnotation.getterName());
        } else {
            belongsToAssociation.setGetterName("get" + StringUtils.capitalize(field.getName()));
        }

        if (associationAnnotation.autoLoad()) {
            LogFactory.getLog(ModelGenerator.class)
                    .warn(getWarningText(field, association.getType(), "autoLoad"));
        }
        if (StringUtils.hasText(associationAnnotation.name())) {
            LogFactory.getLog(ModelGenerator.class).warn(getWarningText(field, association.getType(), "name"));
        }
    } else {
        HasOneAssociation hasOneAssociation = (HasOneAssociation) association;

        if (StringUtils.hasText(associationAnnotation.setterName())) {
            hasOneAssociation.setSetterName(associationAnnotation.setterName());
        } else {
            hasOneAssociation.setSetterName("set" + StringUtils.capitalize(field.getName()));
        }

        if (StringUtils.hasText(associationAnnotation.getterName())) {
            hasOneAssociation.setGetterName(associationAnnotation.getterName());
        } else {
            hasOneAssociation.setGetterName("get" + StringUtils.capitalize(field.getName()));
        }

        if (associationAnnotation.autoLoad()) {
            LogFactory.getLog(ModelGenerator.class)
                    .warn(getWarningText(field, association.getType(), "autoLoad"));
        }
        if (StringUtils.hasText(associationAnnotation.name())) {
            LogFactory.getLog(ModelGenerator.class).warn(getWarningText(field, association.getType(), "name"));
        }
    }

    return association;
}

From source file:org.jnosql.artemis.reflection.Reflections.java

/**
 * Make the given field accessible, explicitly setting it accessible
 * if necessary. The setAccessible(true) method is only
 * called when actually necessary, to avoid unnecessary
 * conflicts with a JVM SecurityManager (if active).
 *
 * @param field field the field to make accessible
 *///from   w  w w  .j a  v  a 2 s.c o  m
public void makeAccessible(Field field) {
    if ((!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) && !field.isAccessible()) {
        field.setAccessible(true);
    }
}

From source file:org.polymap.model2.engine.InstanceBuilder.java

protected String propName(Field field) {
    return field.getDeclaringClass().getSimpleName() + "#" + field.getName();
}