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:info.archinnov.achilles.helper.EntityIntrospector.java

protected String deriveSetterName(Field field) {
    log.debug("Derive setter name for field {} from class {}", field.getName(),
            field.getDeclaringClass().getCanonicalName());

    String fieldName = field.getName();
    String setter = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    log.trace("Derived setter : {}", setter);
    return setter;
}

From source file:org.artifactory.webapp.wicket.util.DescriptionExtractor.java

/**
 * @param descriptor   The descriptor/* ww  w  .jav a2  s .com*/
 * @param propertyName The property name.
 * @return The description of the given property. If no description for the input property empty string will be
 *         returned.
 * @throws IllegalArgumentException if the property not found.
 */
public String getDescription(Descriptor descriptor, String propertyName) {
    if (descriptor == null) {
        throw new IllegalArgumentException("Descriptor must not be null");
    }

    if (propertyName == null || "".equals(propertyName)) {
        throw new IllegalArgumentException("Property name must not be null or empty");
    }

    Field field = getField(descriptor, propertyName);

    String elementName = getElementName(field);
    String complexTypeName = getComplexTypeName(field.getDeclaringClass());

    String query = buildXPathQuery(complexTypeName, elementName);
    log.debug("Executing xpath query: {}", query);
    String description = executeQuery(query);
    return description;
}

From source file:info.archinnov.achilles.helper.EntityIntrospector.java

protected String[] deriveGetterName(Field field) {
    log.debug("Derive getter name for field {} from class {}", field.getName(),
            field.getDeclaringClass().getCanonicalName());

    String camelCase = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);

    String[] getters;//from   ww w.  j  a  v a 2s .  c  o  m

    if (StringUtils.equals(field.getType().toString(), "boolean")) {
        getters = new String[] { "is" + camelCase, "get" + camelCase };
    } else {
        getters = new String[] { "get" + camelCase };
    }
    if (log.isTraceEnabled()) {
        log.trace("Derived getters : {}", StringUtils.join(getters, ","));
    }
    return getters;
}

From source file:com.cognifide.slice.mapper.GenericSlingMapper.java

private <T> void mapResourceToField(final Resource resource, final T object, ValueMap valueMap, Field field)
        throws IllegalAccessException {
    MapperStrategy mapperStrategy = mapperStrategyFactory.getMapperStrategy(field.getDeclaringClass());
    if (shouldFieldBeMapped(field, mapperStrategy)) {
        Object value = getValueForField(resource, valueMap, field);
        if (value == null && field.getType().isPrimitive()) {
            // don't write null values to primitive
            return;
        }//ww w .j a  va 2s  .co  m
        FieldUtils.writeField(field, object, value, ReflectionHelper.FORCE_ACCESS);
    }
}

From source file:org.jgentleframework.utils.ReflectUtils.java

/**
 * Make the given field accessible, explicitly setting it accessible if
 * necessary. The <code>setAccessible(true)</code> method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active)./*  w w w .ja  va  2 s  . c  om*/
 * 
 * @param field
 *            the field to make accessible
 * @see java.lang.reflect.Field#setAccessible(boolean)
 */
public static void makeAccessible(Field field) {

    if (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
        field.setAccessible(true);
    }
}

From source file:it.unibas.spicy.persistence.object.operators.AnalyzeFields.java

private boolean isGetAndSetAvailable(Field field) {
    String fieldName = field.getName();
    fieldName = ClassUtility.convertFirstCapitalLetter(fieldName);
    Class currentClass = field.getDeclaringClass();
    String getMethod = "get" + fieldName;
    String setMethod = "set" + fieldName;
    if (findPublicMethodInClass(currentClass, getMethod) && findPublicMethodInClass(currentClass, setMethod)) {
        return true;
    }// ww w  .  j  a  v a2s. c om
    return false;
}

From source file:org.apache.struts2.oval.interceptor.OValValidationInterceptor.java

/**
 * Get field name and message, used to add the validation error to fieldErrors
 *//*  ww w .  j  a  va2s .co  m*/
protected ValidationError buildValidationError(ConstraintViolation violation, String message) {
    OValContext context = violation.getContext();
    if (context instanceof FieldContext) {
        Field field = ((FieldContext) context).getField();
        String className = field.getDeclaringClass().getName();

        //the default OVal message shows the field name as ActionClass.fieldName
        String finalMessage = StringUtils.removeStart(message, className + ".");

        return new ValidationError(field.getName(), finalMessage);
    } else if (context instanceof MethodReturnValueContext) {
        Method method = ((MethodReturnValueContext) context).getMethod();
        String className = method.getDeclaringClass().getName();
        String methodName = method.getName();

        //the default OVal message shows the field name as ActionClass.fieldName
        String finalMessage = StringUtils.removeStart(message, className + ".");

        String fieldName = null;
        if (methodName.startsWith("get")) {
            fieldName = StringUtils.uncapitalize(StringUtils.removeStart(methodName, "get"));
        } else if (methodName.startsWith("is")) {
            fieldName = StringUtils.uncapitalize(StringUtils.removeStart(methodName, "is"));
        }

        //the result will have the full method name, like "getName()", replace it by "name" (obnly if it is a field)
        if (fieldName != null)
            finalMessage = finalMessage.replaceAll(methodName + "\\(.*?\\)", fieldName);

        return new ValidationError(StringUtils.defaultString(fieldName, methodName), finalMessage);
    }

    return new ValidationError(violation.getCheckName(), message);
}

From source file:org.apache.streams.plugins.StreamsScalaSourceGenerator.java

private boolean override(Field field) {
    try {/*  w  ww  .j a v a 2s.  c o  m*/
        return field.getDeclaringClass().getSuperclass().getField(field.getName()) != null;
    } catch (Exception ex) {
        return false;
    }
}

From source file:annis.model.DataObject.java

@Override
public boolean equals(final Object obj) {
    // enforce equals contract (turn on debugging for this class to see the bug)
    if (obj == null)
        return false;

    if (this.getClass() != obj.getClass())
        return false;

    final EqualsBuilder equalsBuilder = new EqualsBuilder();

    final Object _this = this;
    forEachFieldDo(new FieldCallBack() {

        public void doForField(Field field) throws IllegalAccessException {
            Object thisValue = field.get(_this);
            Object otherValue = field.get(obj);
            if (log.isDebugEnabled()) {
                String fieldName = field.getDeclaringClass().getSimpleName() + "." + field.getName();
                try {
                    boolean equal = thisValue != null && thisValue.equals(otherValue)
                            || thisValue == null && otherValue == null;
                    log.debug(fieldName + ": " + thisValue + " " + (equal ? "=" : "!=") + " " + otherValue);
                } catch (RuntimeException e) {
                    log.error("Exception while comparing " + fieldName + "(" + thisValue + ", " + otherValue
                            + ")");
                    throw e;
                }/*from  w  ww  .j a  v  a  2s . co m*/
            }
            equalsBuilder.append(thisValue, otherValue);
        }

    });

    return equalsBuilder.isEquals();

}

From source file:info.archinnov.achilles.entity.parsing.PropertyParser.java

private <K, V> Pair<Class<K>, Class<V>> determineMapGenericTypes(Field field) {
    log.trace("Determine generic types for field Map<K,V> {} of entity class {}", field.getName(),
            field.getDeclaringClass().getCanonicalName());

    Type genericType = field.getGenericType();
    ParameterizedType pt = (ParameterizedType) genericType;
    Type[] actualTypeArguments = pt.getActualTypeArguments();

    Class<K> keyClass = propertyHelper.getClassFromType(actualTypeArguments[0]);
    Class<V> valueClass = propertyHelper.getClassFromType(actualTypeArguments[1]);

    return Pair.create(keyClass, valueClass);
}