Example usage for java.lang.reflect Field getClass

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

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:jef.database.DbUtils.java

/**
 * /*w  w w  .  j a  v a 2 s  .c  o  m*/
 * 
 * @param field
 *            
 * @return ??
 */
public static ColumnMapping toColumnMapping(Field field) {
    if (field instanceof ColumnMapping) {
        return (ColumnMapping) field;
    } else if (field instanceof MetadataContainer) {
        return ((MetadataContainer) field).getMeta().getColumnDef(field);
    } else if (field instanceof Enum) {
        Class<?> c = field.getClass().getDeclaringClass();
        Assert.isTrue(IQueryableEntity.class.isAssignableFrom(c),
                field + " is not a defined in a IQueryableEntity's meta-model.");
        ITableMetadata meta = MetaHolder.getMeta(c);
        return meta.getColumnDef(field);
    }
    throw new IllegalArgumentException(
            "method 'getTableMeta' doesn't support field type of " + field.getClass());
}

From source file:jef.database.DbUtils.java

/**
 * class//from   w ww  . ja  v a  2 s.  c o m
 * 
 * @param field
 * @return
 */
public static AbstractMetadata getTableMeta(Field field) {
    Assert.notNull(field);
    if (field instanceof MetadataContainer) {
        return (AbstractMetadata) ((MetadataContainer) field).getMeta();
    }
    if (field instanceof Enum) {
        // FIXME ??
        Class<?> c = field.getClass().getDeclaringClass();
        Assert.isTrue(IQueryableEntity.class.isAssignableFrom(c),
                field + " is not a defined in a IQueryableEntity's meta-model.");
        return MetaHolder.getMeta(c.asSubclass(IQueryableEntity.class));
    } else {
        throw new IllegalArgumentException(
                "method 'getTableMeta' doesn't support field type of " + field.getClass());
    }
}

From source file:org.apache.cassandra.db.ScrubTest.java

/** The SecondaryIndex class is used for custom indexes so to avoid
 * making a public final field into a private field with getters
 * and setters, we resort to this hack in order to test it properly
 * since it can have two values which influence the scrubbing behavior.
 * @param comparator - the key comparator we want to test
 *//*from   w w  w.  j ava2  s .com*/
private void setKeyComparator(AbstractType<?> comparator) {
    try {
        Field keyComparator = SecondaryIndex.class.getDeclaredField("keyComparator");
        keyComparator.setAccessible(true);
        int modifiers = keyComparator.getModifiers();
        Field modifierField = keyComparator.getClass().getDeclaredField("modifiers");
        modifiers = modifiers & ~Modifier.FINAL;
        modifierField.setAccessible(true);
        modifierField.setInt(keyComparator, modifiers);

        keyComparator.set(null, comparator);
    } catch (Exception ex) {
        fail("Failed to change key comparator in secondary index : " + ex.getMessage());
        ex.printStackTrace();
    }
}

From source file:org.projectforge.framework.configuration.ConfigXml.java

/**
 * Copies only not null values of the configuration.
 *///www .j a  v  a2s  . c o  m
private static void copyDeclaredFields(final String prefix, final Class<?> srcClazz, final Object src,
        final Object dest, final String... ignoreFields) {
    final Field[] fields = srcClazz.getDeclaredFields();
    AccessibleObject.setAccessible(fields, true);
    for (final Field field : fields) {
        if (ignoreFields != null && ArrayUtils.contains(ignoreFields, field.getName()) == false
                && accept(field)) {
            try {
                final Object srcFieldValue = field.get(src);
                if (srcFieldValue == null) {
                    // Do nothing
                } else if (srcFieldValue instanceof ConfigurationData) {
                    final Object destFieldValue = field.get(dest);
                    Validate.notNull(destFieldValue);
                    final StringBuffer buf = new StringBuffer();
                    if (prefix != null) {
                        buf.append(prefix);
                    }
                    String alias = null;
                    if (field.isAnnotationPresent(XmlField.class)) {
                        final XmlField xmlFieldAnn = field.getAnnotation(XmlField.class);
                        if (xmlFieldAnn != null) {
                            alias = xmlFieldAnn.alias();
                        }
                    }
                    if (alias != null) {
                        buf.append(alias);
                    } else {
                        buf.append(field.getClass().getName());
                    }
                    buf.append(".");
                    copyDeclaredFields(buf.toString(), srcFieldValue.getClass(), srcFieldValue, destFieldValue,
                            ignoreFields);
                } else if (PLUGIN_CONFIGS_FIELD_NAME.equals(field.getName()) == true) {
                    // Do nothing.
                } else {
                    field.set(dest, srcFieldValue);
                    if (field.isAnnotationPresent(ConfigXmlSecretField.class) == true) {
                        log.info(StringUtils.defaultString(prefix) + field.getName() + " = "
                                + SECRET_PROPERTY_STRING);
                    } else {
                        log.info(StringUtils.defaultString(prefix) + field.getName() + " = " + srcFieldValue);
                    }
                }
            } catch (final IllegalAccessException ex) {
                throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage());
            }
        }
    }
    final Class<?> superClazz = srcClazz.getSuperclass();
    if (superClazz != null) {
        copyDeclaredFields(prefix, superClazz, src, dest, ignoreFields);
    }
}

From source file:com.m4rc310.cb.builders.ComponentBuilder.java

private void loadAllFields(Object object, Class<?> type) {
    for (Field field : type.getDeclaredFields()) {
        if (!field.isAnnotationPresent(Acomponent.class)) {
            continue;
        }/*from www .j a v  a 2 s.com*/

        field.setAccessible(true);

        Acomponent ac = field.getDeclaredAnnotation(Acomponent.class);

        if (field.getClass().getDeclaredFields().length > 0) {
            Object value;

            try {
                value = field.get(object);
                if (ac.type().equals(EnumComponentType.PANEL)) {
                    addTargets(value);
                    loadAllFields(value, field.getType());
                }

                mudarReferencia(field);
                addField(field);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                infoError(e);
                LogServer.getInstance().error(null,
                        "O <field> [{0}] est nulo!\nDialog foi iniciado de forma incompleta com falhas!",
                        field);
            }
        }
    }
}

From source file:com.impetus.client.rdbms.HibernateClient.java

/**
 * Gets the data type./*from w  ww .  jav  a 2  s  .c o  m*/
 * 
 * @param entityMetadata
 *            the entity metadata
 * @param arg1
 *            the arg1
 * @return the data type
 * @throws PropertyAccessException
 *             the property access exception
 */
private Object[] getDataType(EntityMetadata entityMetadata, Object... arg1) throws PropertyAccessException {
    Field idField = (Field) entityMetadata.getIdAttribute().getJavaMember();
    PropertyAccessor<?> accessor = PropertyAccessorFactory.getPropertyAccessor(idField);

    Object[] pKeys = new Object[arg1.length];
    int cnt = 0;
    for (Object r : arg1) {
        pKeys[cnt++] = accessor.fromString(idField.getClass(), r.toString());
    }
    return pKeys;
}

From source file:com.github.reinert.jjschema.JsonSchemaGenerator.java

private void processPropertyCollection(Method method, Field field, ObjectNode schema) throws TypeException {
    schema.put(TAG_TYPE, TAG_ARRAY);/*from w  w w .  jav  a2s.co m*/
    Class<?> genericClass = null;
    if (method != null) {
        Type methodType = method.getGenericReturnType();
        if (!ParameterizedType.class.isAssignableFrom(methodType.getClass())) {
            throw new TypeException("Collection property must be parameterized: " + method.getName());
        }
        ParameterizedType genericType = (ParameterizedType) methodType;
        genericClass = (Class<?>) genericType.getActualTypeArguments()[0];
    } else {
        genericClass = field.getClass();
    }
    schema.put("items", generateSchema(genericClass));
}

From source file:org.projectforge.core.ConfigXml.java

/**
 * Copies only not null values of the configuration.
 *///from   w w  w  . ja v  a 2 s.c  o m
private static void copyDeclaredFields(final String prefix, final Class<?> srcClazz, final Object src,
        final Object dest, final String... ignoreFields) {
    final Field[] fields = srcClazz.getDeclaredFields();
    AccessibleObject.setAccessible(fields, true);
    for (final Field field : fields) {
        if (ignoreFields != null && ArrayUtils.contains(ignoreFields, field.getName()) == false
                && accept(field)) {
            try {
                final Object srcFieldValue = field.get(src);
                if (srcFieldValue == null) {
                    // Do nothing
                } else if (srcFieldValue instanceof ConfigurationData) {
                    final Object destFieldValue = field.get(dest);
                    Validate.notNull(destFieldValue);
                    final StringBuffer buf = new StringBuffer();
                    if (prefix != null) {
                        buf.append(prefix);
                    }
                    String alias = null;
                    if (field.isAnnotationPresent(XmlField.class)) {
                        final XmlField xmlFieldAnn = field.getAnnotation(XmlField.class);
                        if (xmlFieldAnn != null) {
                            alias = xmlFieldAnn.alias();
                        }
                    }
                    if (alias != null) {
                        buf.append(alias);
                    } else {
                        buf.append(field.getClass().getName());
                    }
                    buf.append(".");
                    copyDeclaredFields(buf.toString(), srcFieldValue.getClass(), srcFieldValue, destFieldValue,
                            ignoreFields);
                } else if (PLUGIN_CONFIGS_FIELD_NAME.equals(field.getName()) == true) {
                    // Do nothing.
                } else {
                    field.set(dest, srcFieldValue);
                    if (StringHelper.isIn(field.getName(), "receiveSmsKey", "phoneLookupKey") == true) {
                        log.info(StringUtils.defaultString(prefix) + field.getName() + " = ****");
                    } else {
                        log.info(StringUtils.defaultString(prefix) + field.getName() + " = " + srcFieldValue);
                    }
                }
            } catch (final IllegalAccessException ex) {
                throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage());
            }
        }
    }
    final Class<?> superClazz = srcClazz.getSuperclass();
    if (superClazz != null) {
        copyDeclaredFields(prefix, superClazz, src, dest, ignoreFields);
    }
}

From source file:com.qspin.qtaste.testsuite.impl.JythonTestScript.java

private DebugVariable dumpJavaObject(Object javaObject, DebugVariable debugVar) {
    if (javaObject.getClass().getName().startsWith("java.lang.")) {
        return debugVar;
    }//from  www. j  a va  2 s.co m
    if (javaObject.getClass().equals(ArrayList.class)) {
        ArrayList<?> arrayList = (ArrayList<?>) javaObject;
        Iterator<?> arrayListIt = arrayList.iterator();
        int index = 0;
        while (arrayListIt.hasNext()) {
            Object javaObjectInArray = arrayListIt.next();
            DebugVariable fieldVar = new DebugVariable("[" + index + "]",
                    javaObjectInArray.getClass().toString(), javaObjectInArray.toString());
            fieldVar = dumpJavaObject(javaObjectInArray, fieldVar);
            debugVar.addField(fieldVar);
            index++;
        }
    }
    Field[] fields = javaObject.getClass().getFields();
    for (Field field : fields) {
        String fieldName = field.getName();
        try {
            Object fieldObject = field.get(javaObject);

            String fieldValue = fieldObject.toString();
            DebugVariable fieldVar = new DebugVariable(fieldName, field.getClass().toString(), fieldValue);
            fieldVar = dumpJavaObject(fieldValue, fieldVar);
            debugVar.addField(fieldVar);
        } catch (IllegalArgumentException e) {
            debugVar.addField(new DebugVariable(fieldName, field.getClass().toString(), "Illegal argument"));
        } catch (IllegalAccessException e) {
            debugVar.addField(
                    new DebugVariable(fieldName, field.getClass().toString(), "Illegal Access Exception"));
        } catch (NullPointerException e) {
            debugVar.addField(
                    new DebugVariable(fieldName, field.getClass().toString(), "Null Pointer Exception"));
        }
    }
    Method[] methods = javaObject.getClass().getMethods();
    for (Method method : methods) {
        if ((method.getName().startsWith("get")) && (!(method.getName().equals("getClass")))
                && (!(method.getName().equals("getAccessorKeys")))) {
            try {
                Object returnValue = method.invoke(javaObject);
                DebugVariable fieldVar = new DebugVariable(method.getName(), javaObject.getClass().toString(),
                        returnValue.toString());
                fieldVar = dumpJavaObject(returnValue, fieldVar);
                debugVar.addField(fieldVar);
            } catch (Exception e) {
            }

        }
    }

    return debugVar;
}

From source file:com.github.reinert.jjschema.JsonSchemaGenerator.java

protected <T> ObjectNode generatePropertySchema(Class<T> type, Method method, Field field)
        throws TypeException {
    Class<?> returnType = method != null ? method.getReturnType() : field.getType();

    AccessibleObject propertyReflection = field != null ? field : method;

    SchemaIgnore ignoreAnn = propertyReflection.getAnnotation(SchemaIgnore.class);
    if (ignoreAnn != null)
        return null;

    ObjectNode schema = createInstance();

    JsonManagedReference refAnn = propertyReflection.getAnnotation(JsonManagedReference.class);
    if (refAnn != null) {
        ManagedReference fowardReference;
        Class<?> genericClass;
        Class<?> collectionClass;
        if (Collection.class.isAssignableFrom(returnType)) {
            if (method != null) {
                ParameterizedType genericType = (ParameterizedType) method.getGenericReturnType();
                genericClass = (Class<?>) genericType.getActualTypeArguments()[0];
            } else {
                genericClass = field.getClass();
            }//from   ww w .j  a v  a 2  s .  com
            collectionClass = returnType;
        } else {
            genericClass = returnType;
        }
        fowardReference = new ManagedReference(type, refAnn.value(), genericClass);

        if (!isFowardReferencePiled(fowardReference)) {
            pushFowardReference(fowardReference);
        } else
        //           if (isBackwardReferencePiled(fowardReference)) 
        {
            boolean a = pullFowardReference(fowardReference);
            boolean b = pullBackwardReference(fowardReference);
            //return null;
            return createRefSchema("#");
        }
    }

    JsonBackReference backRefAnn = propertyReflection.getAnnotation(JsonBackReference.class);
    if (backRefAnn != null) {
        ManagedReference backReference;
        Class<?> genericClass;
        Class<?> collectionClass;
        if (Collection.class.isAssignableFrom(returnType)) {
            ParameterizedType genericType = (ParameterizedType) method.getGenericReturnType();
            genericClass = (Class<?>) genericType.getActualTypeArguments()[0];
            collectionClass = returnType;
        } else {
            genericClass = returnType;
        }
        backReference = new ManagedReference(genericClass, backRefAnn.value(), type);

        if (isFowardReferencePiled(backReference) && !isBackwardReferencePiled(backReference)) {
            pushBackwardReference(backReference);
        } else {
            //              pullFowardReference(backReference);
            //              pullBackwardReference(backReference);
            return null;
        }
    }

    if (Collection.class.isAssignableFrom(returnType)) {
        processPropertyCollection(method, field, schema);
    } else {
        schema = generateSchema(returnType);
    }

    // Check the field annotations, if the get method references a field, or the
    // method annotations on the other hand, and processSchemaProperty them to
    // the JsonSchema object
    Attributes attrs = propertyReflection.getAnnotation(Attributes.class);
    if (attrs != null) {
        processSchemaProperty(schema, attrs);
        // The declaration of $schema is only necessary at the root object
        schema.remove("$schema");
    }

    // Check if the Nullable annotation is present, and if so, add 'null' to type attr
    Nullable nullable = propertyReflection.getAnnotation(Nullable.class);
    if (nullable != null) {
        if (returnType.isEnum()) {
            ((ArrayNode) schema.get("enum")).add("null");
        } else {
            String oldType = schema.get(TAG_TYPE).asText();
            ArrayNode typeArray = schema.putArray(TAG_TYPE);
            typeArray.add(oldType);
            typeArray.add("null");
        }
    }

    return schema;
}