List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:com.playersun.jbf.common.utils.reflex.Reflections.java
public static Map<String, Object> bean2Map(Object... objects) { Map<String, Object> map = new HashMap<String, Object>(); for (Object object : objects) { if (object == null) continue; if (object instanceof Map) { map.putAll((Map) object); } else {//from www . ja v a2 s. com Validate.notNull(object, "object can't be null"); for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { for (Field field : superClass.getDeclaredFields()) { makeAccessible(field); if (field.get(object) != null) { map.put(field.getName(), field.get(object)); } } } catch (Exception e) {//NOSONAR // Field??,? continue;// new add } } } } return map; }
From source file:com.bellman.bible.service.common.CommonUtils.java
/** * By default popup menus do not show icons, but see the trick below * http://stackoverflow.com/questions/6805756/is-it-possible-to-display-icons-in-a-popupmenu *///from w ww. ja v a2s .c o m public static void forcePopupMenuToShowIcons(PopupMenu popup) { try { Field[] fields = popup.getClass().getDeclaredFields(); for (Field field : fields) { if ("mPopup".equals(field.getName())) { field.setAccessible(true); Object menuPopupHelper = field.get(popup); Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName()); Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class); setForceIcons.invoke(menuPopupHelper, true); break; } } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.github.rvesse.airline.Accessor.java
@SuppressWarnings("unchecked") private static Collection<Object> getOrCreateCollectionField(String name, Object object, Field field) { Collection<Object> collection; try {//from w w w . j a v a 2s . co m collection = (Collection<Object>) field.get(object); } catch (Exception e) { throw new ParseException(e, "Error getting collection field %s for argument %s", field.getName(), name); } if (collection == null) { collection = newCollection(field.getType()); try { field.set(object, collection); } catch (Exception e) { throw new ParseException(e, "Error setting collection field %s for argument %s", field.getName(), name); } } return collection; }
From source file:info.archinnov.achilles.internal.metadata.parsing.PropertyParser.java
public static String getIndexName(Field field) { log.debug("Check @Index annotation on field {} of class {}", field.getName(), field.getDeclaringClass().getCanonicalName()); String indexName = null;//from www.j av a 2s . c o m Index index = field.getAnnotation(Index.class); if (index != null) { indexName = index.name(); } return indexName; }
From source file:io.apiman.plugins.keycloak_oauth_policy.ClaimLookup.java
private static void getProperties(Class<?> klazz, String path, Deque<Field> fieldChain) { for (Field f : klazz.getDeclaredFields()) { f.setAccessible(true);/*from w w w. j ava2 s .c o m*/ JsonProperty jsonProperty = f.getAnnotation(JsonProperty.class); if (jsonProperty != null) { fieldChain.push(f); // If the inspected type has nested @JsonProperty annotations, we need to inspect it if (hasJsonPropertyAnnotation(f)) { getProperties(f.getType(), f.getName() + ".", fieldChain); // Add "." when traversing into new object. } else { // Otherwise, just assume it's simple as the best we can do is #toString List<Field> fieldList = new ArrayList<>(fieldChain); Collections.reverse(fieldList); STANDARD_CLAIMS_FIELD_MAP.put(path + jsonProperty.value(), fieldList); fieldChain.pop(); // Pop, as we have now reached end of this chain. } } } }
From source file:iing.uabc.edu.mx.persistencia.util.JSON.java
private static void stringifyObject(JsonGenerator generator, BeanManager manager) { String keyName;/* w w w. ja v a 2s . c o m*/ Field[] fields = manager.getFields(); //Read every field and transform it to a json property string for (Field field : fields) { Class fieldType = manager.getType(field.getName()); keyName = field.getName(); Object value = manager.getProperty(keyName); System.out.println("KeyName: " + keyName); System.out.println("Valor " + keyName + ": " + value); if (value == null) { //Set to null the property generator.writeNull(keyName); continue; } //Is a String if (fieldType == String.class) { generator.write(keyName, String.valueOf(value)); } //Is a Date else if (fieldType == Date.class) { String date = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(value); generator.write(keyName, date); } //Is a integer else if (fieldType == Integer.class || fieldType == Integer.TYPE) { generator.write(keyName, (int) value); } //Is a double else if (fieldType == Double.class || fieldType == Double.TYPE) { generator.write(keyName, (double) value); } //Is boolean else if (fieldType == Boolean.class || fieldType == Boolean.TYPE) { generator.write(keyName, (boolean) value); } //Is a collection else if (value instanceof Collection) { Class elementClass = manager.getCollectionElementType(keyName); System.out.println("Nueva Colleccion [] de clase: " + elementClass.getSimpleName()); generator.writeStartArray(keyName); //Create new collection manager with the given class CollectionManager collectionManager = new CollectionManager((Collection) value, elementClass); stringifyArray(generator, collectionManager); generator.writeEnd(); } else { //Is a object... probably BeanManager objectManager = new BeanManager(value); System.out.println("Nuevo Objecto {}: " + value.getClass().getSimpleName()); generator.writeStartObject(keyName); stringifyObject(generator, objectManager); generator.writeEnd(); } } }
From source file:net.ostis.sc.memory.SCKeynodesBase.java
private static boolean checkKeynode(SCSession session, SCSegment defaultSegment, Field field) throws IllegalArgumentException, IllegalAccessException { Keynode annotation = field.getAnnotation(Keynode.class); if (annotation != null) { String keynodeName = annotation.value(); if (StringUtils.isEmpty(keynodeName)) keynodeName = field.getName(); SCAddr keynode = session.findByIdtf(keynodeName, defaultSegment); Validate.notNull(keynode,// ww w. ja v a 2s .co m "Not found keynode with URI \"" + defaultSegment.getURI() + "/" + keynodeName + "\""); field.set(null, keynode); if (log.isDebugEnabled()) log.debug(defaultSegment.getURI() + "/" + keynodeName + " --> " + field.getName()); return true; } else { return false; } }
From source file:com.savor.ads.core.ApiRequestFactory.java
@NonNull private static JSONObject getJSONObject(Object object) { Field[] fields = object.getClass().getDeclaredFields(); JSONObject jObject = new JSONObject(); for (Field field : fields) { try {//from www . j av a 2 s. c o m field.setAccessible(true); Object fieldValue = field.get(object); if (fieldValue instanceof String || fieldValue instanceof Number || fieldValue == null) { jObject.put(field.getName(), fieldValue); } else if (fieldValue instanceof List) { jObject.put(field.getName(), getJSONArray((List<?>) fieldValue)); } else { jObject.put(field.getName(), getJSONObject(fieldValue)); } } catch (Exception e) { e.printStackTrace(); } } return jObject; }
From source file:edu.mit.csail.sdg.alloy4.Terminal.java
private static String getAvaliableSatSolvers() { StringBuilder sb = new StringBuilder(); for (Field f : A4Options.SatSolver.class.getFields()) { if (A4Options.SatSolver.class.isAssignableFrom(f.getType())) { sb.append(f.getName() + ", "); }/*from w w w.java 2 s . c o m*/ } if (sb.length() > 2) { sb.delete(sb.length() - 2, sb.length()); } return sb.toString(); }
From source file:com.alfresco.orm.ORMUtil.java
/** * @param alfrescoContent/*from www . j a v a 2 s.c om*/ * @param field * @return * @throws IllegalAccessException * @throws InvocationTargetException */ private static List<AlfrescoContent> getAsscoiationObject(final AlfrescoContent alfrescoContent, final Field field) throws IllegalAccessException, InvocationTargetException { List<AlfrescoContent> retVal = new ArrayList<AlfrescoContent>(); AlfrescoAssociation alfrescoAssociation = field.getAnnotation(AlfrescoAssociation.class); Method method = ReflectionUtil.getMethod(alfrescoContent.getClass(), field.getName()); if (alfrescoAssociation.many()) { Collection<? extends AlfrescoContent> temp = (Collection<? extends AlfrescoContent>) method .invoke(alfrescoContent); if (null != temp) { retVal.addAll(temp); } } else { AlfrescoContent temp = (AlfrescoContent) method.invoke(alfrescoContent); if (null != temp) { retVal.add(temp); } } return retVal; }