List of usage examples for java.lang Class getFields
@CallerSensitive public Field[] getFields() throws SecurityException
From source file:Capabilities.java
/** * Return an array of capability bits for the object * * If no capabilities are set then an array of length 0 is returned * * @param obj The object for which to extract the capability bits *//*from w w w . ja va2s . c o m*/ public static int[] getCapabilities(javax.media.j3d.SceneGraphObject obj) { ArrayList bits = new ArrayList(); int value; String str; Class cl = obj.getClass(); java.lang.reflect.Field[] fields = cl.getFields(); try { for (int i = 0; i < fields.length; i++) { str = fields[i].getName(); value = fields[i].getInt(fields[i]); if (str.indexOf("ALLOW_") != -1 || str.indexOf("ENABLE_") != -1) { if (obj.getCapability(value)) bits.add(new Integer(value)); } } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Internal Error"); } int[] ret = new int[bits.size()]; for (int i = 0; i < ret.length; i++) ret[i] = ((Integer) bits.get(i)).intValue(); return ret; }
From source file:Main.java
public static Object toSerializableObject(Object obj) { // Case for objects have no specified method to JSON string. if (obj.getClass().isArray()) { JSONArray result = new JSONArray(); Object[] arr = (Object[]) obj; for (int i = 0; i < arr.length; ++i) { try { result.put(i, toSerializableObject(arr[i])); } catch (Exception e) { e.printStackTrace();/*from ww w .j a va2 s . c o m*/ } } return result; } // The original serializable object. if (isSerializable(obj)) return obj; // Customized serializable object. // // If the object is not directly serializable, we check if it is customised // serializable. That means the developer implemented the public serialization // method "toJSONString". try { Method m = obj.getClass().getMethod("toJSONString", new Class<?>[0]); String jsonStr = (String) (m.invoke(obj, new Object[0])); if (jsonStr.trim().charAt(0) == '[') { return new JSONArray(jsonStr); } else { return new JSONObject(jsonStr); } } catch (Exception e) { Log.w(TAG, "No serialization method: \"toJSONString\", or errors happened."); } /* * For ordinary objects, we will just serialize the accessible fields. */ try { Class<?> c = obj.getClass(); JSONObject json = new JSONObject(); Field[] fields = c.getFields(); for (Field f : fields) { json.put(f.getName(), f.get(obj)); } return json; } catch (Exception e) { Log.e(TAG, "Field to serialize object to JSON."); e.printStackTrace(); return null; } }
From source file:com.antsdb.saltedfish.util.CursorUtil.java
public static Record toRecord(Object obj) throws IllegalArgumentException, IllegalAccessException { HashMapRecord rec = new HashMapRecord(); Class<?> klass = obj.getClass(); for (Field i : klass.getFields()) { Object value = i.get(obj); rec.set(rec.size(), value);//from ww w .ja v a2s . com } return rec; }
From source file:Main.java
/** * Stores the classes 'static final' field values as a map. * /*from w w w . jav a 2 s .c o m*/ * @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:org.springframework.test.context.junit4.rules.SpringClassRule.java
private static Optional<Field> findSpringMethodRuleField(Class<?> testClass) { return Arrays.stream(testClass.getFields()).filter(field -> !Modifier.isStatic(field.getModifiers())) .filter(field -> Modifier.isPublic(field.getModifiers())) .filter(field -> SpringMethodRule.class.isAssignableFrom(field.getType())).findFirst(); }
From source file:com.antsdb.saltedfish.util.CursorUtil.java
public static CursorMeta toMeta(Class<?> klass) { CursorMeta meta = new CursorMeta(); for (Field i : klass.getFields()) { FieldMeta column = new FieldMeta(); column.setName(i.getName());//from w w w . java 2 s .c o m if (i.getType() == String.class) { column.setType(DataType.varchar()); } else if (i.getType() == int.class) { column.setType(DataType.integer()); } else if (i.getType() == Integer.class) { column.setType(DataType.integer()); } else if (i.getType() == long.class) { column.setType(DataType.longtype()); } else if (i.getType() == Long.class) { column.setType(DataType.longtype()); } else if (i.getType() == Timestamp.class) { column.setType(DataType.timestamp()); } else if (i.getType() == byte[].class) { column.setType(DataType.blob()); } else { throw new NotImplementedException(); } meta.addColumn(column); } return meta; }
From source file:org.blockartistry.mod.DynSurround.util.ConfigProcessor.java
public static void process(final Configuration config, final Class<?> clazz, final Object parameters) { for (final Field field : clazz.getFields()) { final Parameter annotation = field.getAnnotation(Parameter.class); if (annotation != null) { final String category = annotation.category(); final String property = annotation.property(); final String comment = field.getAnnotation(Comment.class) != null ? field.getAnnotation(Comment.class).value() : "NEEDS COMMENT"; try { final Object defaultValue = field.get(parameters); if (defaultValue instanceof Boolean) { field.set(parameters, config.getBoolean(property, category, Boolean.valueOf(annotation.defaultValue()), comment)); } else if (defaultValue instanceof Integer) { int minInt = Integer.MIN_VALUE; int maxInt = Integer.MAX_VALUE; final MinMaxInt mmi = field.getAnnotation(MinMaxInt.class); if (mmi != null) { minInt = mmi.min(); maxInt = mmi.max(); }//from w w w.j a v a 2 s . c om field.set(parameters, config.getInt(property, category, Integer.valueOf(annotation.defaultValue()), minInt, maxInt, comment)); } else if (defaultValue instanceof Float) { float minFloat = Float.MIN_VALUE; float maxFloat = Float.MAX_VALUE; final MinMaxFloat mmf = field.getAnnotation(MinMaxFloat.class); if (mmf != null) { minFloat = mmf.min(); maxFloat = mmf.max(); } field.set(parameters, config.getFloat(property, category, Float.valueOf(annotation.defaultValue()), minFloat, maxFloat, comment)); } else if (defaultValue instanceof String) { field.set(parameters, config.getString(property, category, annotation.defaultValue(), comment)); } else if (defaultValue instanceof String[]) { field.set(parameters, config.getStringList(property, category, StringUtils.split(annotation.defaultValue(), ','), comment)); } // Configure restart settings final Property prop = config.getCategory(category).get(property); if (field.getAnnotation(RestartRequired.class) != null) { final RestartRequired restart = field.getAnnotation(RestartRequired.class); prop.setRequiresMcRestart(restart.server()); prop.setRequiresWorldRestart(restart.world()); } else { prop.setRequiresMcRestart(false); prop.setRequiresWorldRestart(false); } prop.setShowInGui(field.getAnnotation(Hidden.class) == null); } catch (final Throwable t) { ModLog.error("Unable to parse configuration", t); } } } }
From source file:org.springframework.test.context.junit4.rules.SpringMethodRule.java
private static Optional<Field> findSpringClassRuleField(Class<?> testClass) { return Arrays.stream(testClass.getFields()).filter(ReflectionUtils::isPublicStaticFinal) .filter(field -> SpringClassRule.class.isAssignableFrom(field.getType())).findFirst(); }
From source file:it.nicola_amatucci.util.Json.java
public static <T> JSONObject json_from_object(Object o, Class<T> objClass) throws Exception { JSONObject json = new JSONObject(); Field[] fields = objClass.getFields(); try {/*from w w w.j av a 2 s . c om*/ for (Field field : fields) { //valore del campo try { String typeName = field.getType().getSimpleName(); //in base al tipo richiamo l'opportuna funzione if (typeName.equals("String") || typeName.equals("boolean") || typeName.equals("int") || typeName.equals("long") || typeName.equals("double")) { json.put(field.getName(), objClass.getField(field.getName()).get(o)); } else if (typeName.equals("Date")) { json.put(field.getName(), new SimpleDateFormat(Json.DATA_FORMAT) .format((java.util.Date) objClass.getField(field.getName()).get(o))); } else if (field.getType().isArray()) { try { JSONArray array = new JSONArray(); Class c = Class.forName(field.getType().getName()).getComponentType(); Object[] objArray = (Object[]) objClass.getField(field.getName()).get(o); for (int i = 0; i < objArray.length; i++) { array.put(json_from_object(objArray[i], c)); } json.put(field.getName(), array); } catch (Exception e) { throw e; } } else { JSONObject jsonObj = json_from_object(objClass.getField(field.getName()).get(o), field.getType()); json.put(field.getName(), jsonObj); } } catch (Exception e) { throw e; } } } catch (Exception e) { throw e; } return json; }
From source file:Main.java
static public boolean printAllInform(Class clsShow) { try {//from ww w . j a v a 2 s .c o m Method[] methods = clsShow.getMethods(); int i = 0; for (; i < methods.length; i++) { Log.e(TAG, methods[i].getName() + ";and the i is:" + i); } Field[] allFields = clsShow.getFields(); for (i = 0; i < allFields.length; i++) { Log.e(TAG, allFields[i].getName()); } } catch (Exception e) { // TODO: handle exception return false; } return true; }