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:Main.java

/**
 * Key to lowercase String, extracts the effective key that was pressed
 * (without shift, control, alt)//  w  w  w.j  a v a 2  s .c om
 */
public static String getKeyText(KeyEvent e) {
    // special cases
    if (e.getKeyCode() == KeyEvent.VK_DELETE) {
        return "delete";
    }
    // prio 1: get text of unresolved code (shift-1 --> '1')
    String s = "" + e.getKeyChar();
    if (e.getKeyCode() > 0) {
        int flags = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
        for (Field f : KeyEvent.class.getFields()) {
            if ((f.getModifiers() & flags) == flags) {
                try {
                    if (f.getName().startsWith("VK_") && ((Integer) f.get(null)) == e.getKeyCode()) {
                        s = f.getName().substring(3).toLowerCase();
                        break;
                    }
                } catch (Throwable t) {
                    // nop
                }
            }
        }
    }
    if (s.length() != 1) {
        // prio 2: check if the resolved char is valid (shift-1 --> '+')
        if (e.getKeyChar() >= 32 && e.getKeyChar() < 128) {
            s = "" + e.getKeyChar();
        }
    }
    return s.toLowerCase();
}

From source file:Main.java

public static String getAttributeName(Field field, XmlAttribute xmlAttribute) {
    String name = xmlAttribute.name();
    if ("##default".equals(name)) {
        name = null;//from   ww w  .  ja  v a 2 s  .c o  m
    }

    if (name == null) {
        name = field.getName();
    }

    return name;
}

From source file:com.spectralogic.ds3contractcomparator.print.utils.HtmlRowGeneratorUtils.java

/**
 * Retrieves the indentation that should be used with the specified field. If the
 * field is the unique property of an object, then the indent remains the same,
 * else, if it is non-unique, then the indent is increased by one.
 *//*  w  w w  .  j av  a 2s.  c o m*/
public static <T> int toFieldIndent(final int curIndent, final T object, final Field field) {
    final String uniqueProperty = getUniqueProperty(object);
    return field.getName().equals(uniqueProperty) ? curIndent : curIndent + 1;
}

From source file:edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.EditConfigurationConstants.java

public static Map<String, String> exportConstants() {
    Map<String, String> constants = new HashMap<String, String>();
    java.lang.reflect.Field[] fields = EditConfigurationConstants.class.getDeclaredFields();
    for (java.lang.reflect.Field f : fields) {
        if (Modifier.isStatic(f.getModifiers()) && Modifier.isPublic(f.getModifiers())) {
            try {
                constants.put(f.getName(), f.get(null).toString());
            } catch (Exception ex) {
                log.error("An exception occurred in trying to retrieve this field ", ex);
            }//from   w w  w  . j av  a  2  s  .c  o m
        }
    }
    return constants;
}

From source file:com.amazonaws.services.iot.client.shadow.AwsIotJsonDeserializer.java

private static void updateDeviceProperty(ObjectMapper jsonObjectMapper, JsonNode node,
        AbstractAwsIotDevice device, Field field) throws IOException {
    Object value = jsonObjectMapper.treeToValue(node, field.getType());
    invokeSetterMethod(device, field.getName(), field.getType(), value);
}

From source file:Main.java

/**
 * @param context//from  ww w  . j av a  2 s.  c o  m
 * @param type
 * @param field
 * @param errResId
 * @param methodPrefix
 * @param methodParameters
 */
public static void checkIfMethodExists(Context context, Class<?> type, Field field, int errResId,
        String methodPrefix, Class<?>... methodParameters) {
    try {
        Method m = type.getDeclaredMethod(methodPrefix + getFirstLetterUppercased(field.getName()),
                methodParameters);

        if (!Modifier.isPublic(m.getModifiers()) || Modifier.isStatic(m.getModifiers()))
            throw new RuntimeException(context.getString(errResId, field.getName()));
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(context.getString(errResId, field.getName()));
    }
}

From source file:Main.java

/**
 * Stores the classes 'static final' field values as a map.
 * /*from  www  .  java2 s  . c  om*/
 * @param clazz
 *            The class containing static field values.
 * @return A map keyed by static field name to value.
 */
public static Map<String, Object> constantsAsMap(Class<?> clazz) {
    try {
        final Map<String, Object> constants = new HashMap<String, Object>();
        final int staticFinalMods = Modifier.STATIC | Modifier.FINAL;
        for (Field field : clazz.getFields()) {
            if (staticFinalMods == (field.getModifiers() & staticFinalMods)) {
                // this is a constant!
                constants.put(field.getName(), field.get(null));
            }
        }

        return constants;
    } catch (Exception e) {
        // wrap in general error
        throw new IllegalStateException("Unable to initialize class constants for: " + clazz);
    }
}

From source file:com.robertsmieja.test.utils.junit.GettersAndSettersUtils.java

static String accessorMethodNameForField(String accessorPrefix, Field field) {
    return accessorPrefix + StringUtils.capitalize(field.getName());
}

From source file:se.berazy.api.examples.App.java

/**
 * Writes out all public fields.//  w  ww. jav a2s. c  o m
 * @param T response
 */
static <T> void outPutResponse(T response) {
    String retval = "\nResponse: \n";
    Class<?> clazz = response.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!field.getName().equalsIgnoreCase("ExtensionData")) {
            try {
                retval += String.format("%s: %s\n", field.getName(), field.get(response));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.println(retval);
}

From source file:hu.petabyte.redflags.engine.util.MappingUtils.java

public static List<String> listAllProperties(Class<?> clazz, String rootName) {
    List<String> p = new ArrayList<String>();
    for (Field f : clazz.getDeclaredFields()) {
        String n = f.getName();
        Class<?> c = f.getType();
        if (null != rootName && !rootName.isEmpty()) {
            n = String.format("%s.%s", rootName, n);
        }/*w w w.  j a  v  a2 s . c o m*/
        p.add(n);
        if (!c.getName().startsWith("java.")) {
            p.addAll(listAllProperties(c, n));
        }
    }
    return p;
}