List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:com.erudika.para.core.ParaObjectUtils.java
/** * Returns a map of annotated fields of a domain object. Only annotated fields are returned. This method forms the * basis of an Object/Grid Mapper. It converts an object to a map of key/value pairs. That map can later be * persisted to a data store.//from ww w . j a va 2s .c o m * <br> * If {@code convertNestedToJsonString} is true all field values that are objects (i.e. not primitive types or * wrappers) are converted to a JSON string otherwise they are left as they are and will be serialized as regular * JSON objects later (structure is preserved). Null is considered a primitive type. Transient fields and * serialVersionUID are skipped. * * @param <P> the object type * @param pojo the object to convert to a map * @param filter a filter annotation. fields that have it will be skipped * @param convertNestedToJsonString true if you want to flatten the nested objects to a JSON string. * @return a map of fields and their values */ public static <P extends ParaObject> Map<String, Object> getAnnotatedFields(P pojo, Class<? extends Annotation> filter, boolean convertNestedToJsonString) { HashMap<String, Object> map = new HashMap<String, Object>(); if (pojo == null) { return map; } try { List<Field> fields = getAllDeclaredFields(pojo.getClass()); // filter transient fields and those without annotations for (Field field : fields) { boolean dontSkip = ((filter == null) ? true : !field.isAnnotationPresent(filter)); if (field.isAnnotationPresent(Stored.class) && dontSkip) { String name = field.getName(); Object value = PropertyUtils.getProperty(pojo, name); if (!Utils.isBasicType(field.getType()) && convertNestedToJsonString) { value = getJsonWriterNoIdent().writeValueAsString(value); } map.put(name, value); } } } catch (Exception ex) { logger.error(null, ex); } return map; }
From source file:ambroafb.general.AnnotiationUtils.java
private static boolean checkValidationForIsNotEmptyAnnotation(Field field, Object classObject) { boolean contentIsCorrect = false; Annotations.ContentNotEmpty annotation = field.getAnnotation(Annotations.ContentNotEmpty.class); Object[] typeAndContent = getNodesTypeAndContent(field, classObject); Object value = typeAndContent[1]; boolean predicateValue = getPredicateValue(annotation.predicate(), field.getName()); // case of comboBoxes value may contains tab or spaces so we need to trim the value. if (predicateValue && (value == null || value.toString().trim().isEmpty())) { changeNodeTitleLabelVisual((Node) typeAndContent[0], annotation.explain()); } else {//from w w w .jav a 2 s. c o m changeNodeTitleLabelVisual((Node) typeAndContent[0], ""); contentIsCorrect = true; } return contentIsCorrect; }
From source file:com.gargoylesoftware.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration.java
private static void process(final ClassConfiguration classConfiguration, final String hostClassName, final String expectedBrowserName, final float browserVersionNumeric) { final String simpleClassName = hostClassName.substring(hostClassName.lastIndexOf('.') + 1); CLASS_NAME_MAP_.put(hostClassName, simpleClassName); final Map<String, Method> allGetters = new HashMap<>(); final Map<String, Method> allSetters = new HashMap<>(); for (final Constructor<?> constructor : classConfiguration.getHostClass().getDeclaredConstructors()) { for (final Annotation annotation : constructor.getAnnotations()) { if (annotation instanceof JsxConstructor) { if (isSupported(((JsxConstructor) annotation).value(), expectedBrowserName, browserVersionNumeric)) { classConfiguration.setJSConstructor(constructor); }// w w w . j a va 2 s .com } } } for (final Method method : classConfiguration.getHostClass().getDeclaredMethods()) { for (final Annotation annotation : method.getAnnotations()) { if (annotation instanceof JsxGetter) { final JsxGetter jsxGetter = (JsxGetter) annotation; if (isSupported(jsxGetter.value(), expectedBrowserName, browserVersionNumeric)) { String property; if (jsxGetter.propertyName().isEmpty()) { final int prefix = method.getName().startsWith("is") ? 2 : 3; property = method.getName().substring(prefix); property = Character.toLowerCase(property.charAt(0)) + property.substring(1); } else { property = jsxGetter.propertyName(); } allGetters.put(property, method); } } else if (annotation instanceof JsxSetter) { final JsxSetter jsxSetter = (JsxSetter) annotation; if (isSupported(jsxSetter.value(), expectedBrowserName, browserVersionNumeric)) { String property; if (jsxSetter.propertyName().isEmpty()) { property = method.getName().substring(3); property = Character.toLowerCase(property.charAt(0)) + property.substring(1); } else { property = jsxSetter.propertyName(); } allSetters.put(property, method); } } else if (annotation instanceof JsxFunction) { if (isSupported(((JsxFunction) annotation).value(), expectedBrowserName, browserVersionNumeric)) { classConfiguration.addFunction(method); } } else if (annotation instanceof JsxStaticGetter) { final JsxStaticGetter jsxStaticGetter = (JsxStaticGetter) annotation; if (isSupported(jsxStaticGetter.value(), expectedBrowserName, browserVersionNumeric)) { final int prefix = method.getName().startsWith("is") ? 2 : 3; String property = method.getName().substring(prefix); property = Character.toLowerCase(property.charAt(0)) + property.substring(1); classConfiguration.addStaticProperty(property, method, null); } } else if (annotation instanceof JsxStaticFunction) { if (isSupported(((JsxStaticFunction) annotation).value(), expectedBrowserName, browserVersionNumeric)) { classConfiguration.addStaticFunction(method); } } else if (annotation instanceof JsxConstructor) { if (isSupported(((JsxConstructor) annotation).value(), expectedBrowserName, browserVersionNumeric)) { classConfiguration.setJSConstructor(method); } } } } for (final Field field : classConfiguration.getHostClass().getDeclaredFields()) { final JsxConstant jsxConstant = field.getAnnotation(JsxConstant.class); if (jsxConstant != null && isSupported(jsxConstant.value(), expectedBrowserName, browserVersionNumeric)) { classConfiguration.addConstant(field.getName()); } } for (final Entry<String, Method> getterEntry : allGetters.entrySet()) { final String property = getterEntry.getKey(); classConfiguration.addProperty(property, getterEntry.getValue(), allSetters.get(property)); } }
From source file:com.liferay.cli.support.util.ReflectionUtils.java
/** * Attempt to find a {@link Field field} on the supplied {@link Class} with * the supplied <code>name</code> and/or {@link Class type}. Searches all * superclasses up to {@link Object}./* w ww. ja va2s . c o m*/ * * @param clazz the class to introspect * @param name the name of the field (may be <code>null</code> if type is * specified) * @param type the type of the field (may be <code>null</code> if name is * specified) * @return the corresponding Field object, or <code>null</code> if not found */ public static Field findField(final Class<?> clazz, final String name, final Class<?> type) { Validate.notNull(clazz, "Class must not be null"); Validate.isTrue(name != null || type != null, "Either name or type of the field must be specified"); Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { final Field[] fields = searchType.getDeclaredFields(); for (final Field field : fields) { if ((name == null || name.equals(field.getName())) && (type == null || type.equals(field.getType()))) { return field; } } searchType = searchType.getSuperclass(); } return null; }
From source file:com.anteam.demo.codec.common.CoderUtil.java
public static Object encode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key) throws EncoderException { if (source == null || encryptAlgorithm == null) { return null; }// w ww . j ava2s.com Object result = source; if (source instanceof byte[]) { return CoderUtil.encode((byte[]) source, encryptAlgorithm, key); } else if (source instanceof String) { return CoderUtil.encode((String) source, encryptAlgorithm, key); } Field[] fields = source.getClass().getDeclaredFields(); for (Field field : fields) { Encrypted encrypted = field.getAnnotation(Encrypted.class); if (encrypted != null) { ReflectionUtils.makeAccessible(field); if (!Modifier.isStatic(field.getModifiers())) { try { field.set(source, CoderUtil.encode(field.get(source))); } catch (IllegalAccessException e) { LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e); } } } } return result; }
From source file:com.anteam.demo.codec.common.CoderUtil.java
public static Object decode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key) throws DecoderException { if (source == null || encryptAlgorithm == null) { return null; }// ww w . j a va2 s . co m Object result = source; if (source instanceof byte[]) { return CoderUtil.decode((byte[]) source, encryptAlgorithm, key); } else if (source instanceof String) { return CoderUtil.decode((String) source, encryptAlgorithm, key); } Field[] fields = source.getClass().getDeclaredFields(); for (Field field : fields) { Encrypted encrypted = field.getAnnotation(Encrypted.class); if (encrypted != null) { ReflectionUtils.makeAccessible(field); if (!Modifier.isStatic(field.getModifiers())) { try { field.set(source, CoderUtil.decode(field.get(source))); } catch (IllegalAccessException e) { LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e); } } } } return result; }
From source file:com.frand.easyandroid.db.sql.FFSqlBuilder.java
public static FFArrayList getFieldsAndValue(Object entity) throws FFDBException, IllegalArgumentException, IllegalAccessException { FFArrayList arrayList = new FFArrayList(); if (entity == null) { throw new FFDBException("?"); }/* w w w . j ava2s . c o m*/ Class<?> clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (!FFDBUtils.isTransient(field) && FFFieldUtil.isBaseDateType(field)) { FFPrimaryKey annotation = field.getAnnotation(FFPrimaryKey.class); if (annotation != null && annotation.autoIncrement()) { } else { String columnName = FFDBUtils.getColumnByField(field); field.setAccessible(true); if (columnName == null || columnName.equals("")) { columnName = field.getName(); } String value = ""; if (field.getType().equals(Date.class)) { value = field.get(entity) != null ? FFDBUtils.dateToString((Date) field.get(entity)) : ""; } else { value = field.get(entity) != null ? field.get(entity).toString() : ""; } arrayList.add(columnName, value); } } } return arrayList; }
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
public static Map<String, Object> extractVariablesWithEnumAsString(Object e) { Map<String, Object> map = new HashMap<String, Object>(); Class<?> clazz = e.getClass(); for (Field field : getAllFields(new ArrayList<Field>(), clazz)) { field.setAccessible(true);/*from ww w .j a v a2 s . co m*/ try { if (field.getClass().isEnum()) { Enum<?> enumVal = (Enum<?>) field.get(e); map.put(field.getName(), enumVal.name()); } else { map.put(field.getName(), field.get(e)); } } catch (IllegalArgumentException | IllegalAccessException e1) { e1.printStackTrace(); } } return map; }
From source file:me.neatmonster.spacebukkit.PanelListener.java
public static String dump(Object object) { Field[] fields = object.getClass().getDeclaredFields(); StringBuilder sb = new StringBuilder(); sb.append(object.getClass().getSimpleName()).append('{'); boolean firstRound = true; for (Field field : fields) { if (!firstRound) { sb.append(", "); }/*from w ww.j a va2 s .c o m*/ firstRound = false; field.setAccessible(true); try { final Object fieldObj = field.get(object); final String value; if (null == fieldObj) { value = "null"; } else { value = fieldObj.toString(); } sb.append(field.getName()).append('=').append('\'').append(value).append('\''); } catch (IllegalAccessException ignore) { //this should never happen } } sb.append('}'); return sb.toString(); }
From source file:com.google.code.siren4j.util.ReflectionUtils.java
/** * Retrieve the setter for the specified class/field if it exists. * * @param clazz/*from www .j av a 2s.c om*/ * @param f * @return */ public static Method getSetter(Class<?> clazz, Field f) { Method setter = null; for (Method m : clazz.getMethods()) { if (ReflectionUtils.isSetter(m) && m.getName().equals(SETTER_PREFIX + StringUtils.capitalize(f.getName()))) { setter = m; break; } } return setter; }