Example usage for java.lang.reflect Field getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the name of the field represented by this Field object.

Usage

From source file:com.hortonworks.minicluster.util.ReflectionUtils.java

/**
 *
 * @param clazz/*ww w  .  j  a v a2  s  .  c  om*/
 * @param fieldName
 * @return
 */
public static Field getFieldAndMakeAccessible(Class<?> clazz, String fieldName) {
    ObjectAssertUtils.assertNotNull(clazz);
    StringAssertUtils.assertNotEmptyAndNoSpaces(fieldName);
    Class<?> searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        Field[] fields = searchType.getDeclaredFields();
        for (Field field : fields) {
            if (fieldName == null || fieldName.equals(field.getName())) {
                field.setAccessible(true);
                return field;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:dk.dma.ais.lib.FileConvert.java

/**
 * getOutputSinks//from  w  ww. ja va 2s .co  m
 *
 * @return
 */
static final List<String> getOutputSinks() {
    LinkedList<String> methods = new LinkedList<String>();
    Field[] declaredMethods = AisPacketOutputSinks.class.getDeclaredFields();
    for (Field m : declaredMethods) {
        if (m.getName().startsWith("OUTPUT")) {
            methods.add(m.getName());
        }
    }
    return methods;
}

From source file:com.gcrm.util.security.UserUtil.java

public static void setAccessValue(Role role, User user) throws Exception {

    Field[] fields = role.getClass().getDeclaredFields();
    for (Field field : fields) {
        String fieldName = field.getName();
        if (fieldName.startsWith("scope_") || fieldName.startsWith("view_") || fieldName.startsWith("create_")
                || fieldName.startsWith("update_") || fieldName.startsWith("delete_")) {
            Integer valueOfRole = (Integer) BeanUtil.getFieldValue(role, fieldName);
            if (valueOfRole == null) {
                continue;
            }/*from   w ww. j  av a  2  s.  c  o  m*/
            Integer valueOfUser = (Integer) BeanUtil.getFieldValue(user, fieldName);
            switch (valueOfRole) {
            case Role.NOT_SET:
                if (valueOfUser == null) {
                    BeanUtil.setFieldValue(user, fieldName, Role.NOT_SET);
                }
                break;
            case Role.ALL_OR_ENABLED:
                if (valueOfUser == null || valueOfUser != Role.OWNER_OR_DISABLED) {
                    BeanUtil.setFieldValue(user, fieldName, Role.ALL_OR_ENABLED);
                }
                break;
            case Role.OWNER_OR_DISABLED:
                BeanUtil.setFieldValue(user, fieldName, Role.OWNER_OR_DISABLED);
                break;
            }
        }
    }

}

From source file:Main.java

public static boolean isFiledWithName(Field field, String fieldName) {
    if (field == null || TextUtils.isEmpty(fieldName)) {
        throw new IllegalArgumentException("params is illegal");
    }// w w w. j  av  a 2 s . c  om
    if (fieldName.equals(field.getName())) {
        return true;
    }
    return false;
}

From source file:org.LexGrid.LexBIG.caCore.utils.LexEVSCaCoreUtils.java

public static Field getField(Class clazz, String fieldName) throws NoSuchFieldException {
    ArrayList<Field> allFields = getAllFields(clazz);
    for (Field field : allFields) {
        if (field.getName().equals(fieldName)) {
            return field;
        }/* w  ww.java  2  s .c  o m*/
    }
    throw new NoSuchFieldException("The Field: " + fieldName + " was not found.");
}

From source file:org.openmrs.module.webservices.rest.web.DelegatingCrudResourceTest.java

/**
 * Convenience method that checks if the specified property is included among the allowed
 * missing properties of the given resource class via reflection
 * //from  w ww. ja  v  a 2  s  .c  o  m
 * @param clazz
 * @param fieldName
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
@SuppressWarnings("rawtypes")
private static boolean isallowedMissingProperty(Class resourceClass, String propName)
        throws IllegalArgumentException, IllegalAccessException, InstantiationException {
    List<Field> fields = Reflect.getAllFields(resourceClass);
    if (CollectionUtils.isNotEmpty(fields)) {
        for (Field field : fields) {
            if (field.getName().equals("allowedMissingProperties"))
                return ((Set) field.get(resourceClass.newInstance())).contains(propName);
        }
    }
    return false;
}

From source file:com.prepaird.objectgraphdb.utils.Utils.java

public static Method getGetterMethod(Class cl, Field f) {
    String getterName = "get" + WordUtils.capitalize(f.getName()),
            isName = "is" + WordUtils.capitalize(f.getName());
    //find the getter method and call it
    Method getterMethod = getMethod(cl, getterName, null), isMethod = getMethod(cl, isName, null);
    return isMethod != null ? isMethod : getterMethod;
}

From source file:Main.java

@SuppressLint("UseSparseArrays")
public static ArrayList<String> extractor(Notification notification) {
    ArrayList<String> notifText = new ArrayList<String>();
    RemoteViews views = notification.contentView;
    @SuppressWarnings("rawtypes")
    Class secretClass = views.getClass();

    try {//from   w  w  w .  ja v a  2s.  c  om

        Field outerFields[] = secretClass.getDeclaredFields();
        for (int i = 0; i < outerFields.length; i++) {

            if (!outerFields[i].getName().equals("mActions"))
                continue;

            outerFields[i].setAccessible(true);

            @SuppressWarnings("unchecked")
            ArrayList<Object> actions = (ArrayList<Object>) outerFields[i].get(views);
            for (Object action : actions) {

                Field innerFields[] = action.getClass().getDeclaredFields();

                Object value = null;
                Integer type = null;
                @SuppressWarnings("unused")
                Integer viewId = null;
                for (Field field : innerFields) {

                    field.setAccessible(true);
                    if (field.getName().equals("value")) {
                        value = field.get(action);
                    } else if (field.getName().equals("type")) {
                        type = field.getInt(action);
                    } else if (field.getName().equals("viewId")) {
                        viewId = field.getInt(action);
                    }
                }

                if (type != null && (type == 9 || type == 10) && value != null) {
                    // System.out.println("Type: " + Integer.toString(type)
                    // + " Value: " + value.toString());
                    if (!notifText.contains(value.toString()))
                        notifText.add(value.toString());
                }

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return notifText;
}

From source file:com.greenline.hrs.admin.util.db.SchemaExport.java

public static String exportMySQL(Class type) {
    if (type == null) {
        return StringUtils.EMPTY;
    }//from www .  j a v  a 2s . c  o m
    String tableName = type.getName().substring(type.getName().lastIndexOf('.') + 1);
    tableName = StringUtil.underscoreName(tableName);
    StringBuilder sb = new StringBuilder();
    sb.append("DROP TABLE IF EXISTS `" + tableName + "`;" + LINUX_LINE_DELIMITER);
    sb.append("CREATE TABLE `");
    sb.append(tableName);
    sb.append("` (" + LINUX_LINE_DELIMITER + LINUX_LINE_DELIMITER);
    sb.append("`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '',");
    Field[] fields = type.getDeclaredFields();
    for (Field field : fields) {
        if (!Modifier.isStatic(field.getModifiers())) {
            sb.append("`" + StringUtil.underscoreName(field.getName()) + "` "
                    + JdbcColumnUtil.getColumeTypeDesc(field.getType()) + " NOT NULL COMMENT '',"
                    + LINUX_LINE_DELIMITER);
        }
    }
    sb.append("PRIMARY KEY (`id`)" + LINUX_LINE_DELIMITER);
    sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
    return sb.toString();
}

From source file:Main.java

public static boolean creteEntity(Object entity, String fileName) {
    boolean flag = false;
    try {/* w  w  w.  j  a va  2  s.  c  o m*/
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(fileName);

        Class clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Node EntityElement = document.getElementsByTagName(clazz.getSimpleName() + "s").item(0);

        Element newEntity = document.createElement(clazz.getSimpleName());
        EntityElement.appendChild(newEntity);

        for (Field field : fields) {
            field.setAccessible(true);
            Element element = document.createElement(field.getName());
            element.appendChild(document.createTextNode(field.get(entity).toString()));
            newEntity.appendChild(element);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(new File(fileName));
        transformer.transform(domSource, streamResult);
        flag = true;
    } catch (ParserConfigurationException pce) {
        System.out.println(pce.getLocalizedMessage());
        pce.printStackTrace();
    } catch (TransformerException te) {
        System.out.println(te.getLocalizedMessage());
        te.printStackTrace();
    } catch (IOException ioe) {
        System.out.println(ioe.getLocalizedMessage());
        ioe.printStackTrace();
    } catch (SAXException sae) {
        System.out.println(sae.getLocalizedMessage());
        sae.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return flag;
}