List of usage examples for java.lang.reflect Field getModifiers
public int getModifiers()
From source file:Main.java
/** * set attribute is accessible/*from w ww. j ava2s. c om*/ * * @param field {@link java.lang.reflect.Field} */ public static void makeAccessible(final Field field) { if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) { field.setAccessible(true); } }
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 www . ja va 2 s . c o m } } return constants; }
From source file:Main.java
/** * Returns the Jarvis Api Version or -1 if it is not supported * @return api version//from w ww . j a v a 2s . co m */ public static int getJarvisApiVersion() { Field[] declaredFields = Build.class.getDeclaredFields(); for (Field field : declaredFields) { if (Modifier.isStatic(field.getModifiers()) && field.getName().equals("JARVIS_VERSION")) { try { return field.getInt(null); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } } } return -1; }
From source file:Main.java
/** * Stores the classes 'static final' field values as a map. * //from w w w . j a v 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:Main.java
public static void printFields(Class cl) { Field[] fields = cl.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; Class type = f.getType(); String name = f.getName(); System.out.print(Modifier.toString(f.getModifiers())); System.out.println(" " + type.getName() + " " + name + ";"); }/*w w w . j ava2s. c o m*/ }
From source file:Main.java
private static Field getSingletonField(Class<?> hostClas) { Field f = null; try {//from www . ja v a 2 s . c o m f = hostClas.getDeclaredField("I"); if (!Modifier.isStatic(f.getModifiers())) throw new Exception("'I' field should be static." + f); f.setAccessible(true); } catch (Throwable ex) { } return f; }
From source file:Main.java
/** * Key to lowercase String, extracts the effective key that was pressed * (without shift, control, alt)// w ww . jav 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 Map<String, Object> optPublicFieldKeyValueMap(Object obj) { Map<String, Object> map = new HashMap<String, Object>(); if (obj != null) { Field[] fields = obj.getClass().getFields(); for (Field f : fields) { try { boolean isStatic = Modifier.isStatic(f.getModifiers()); if (!isStatic) { Object value = f.get(obj); if (value != null) map.put(f.getName(), value); }//from w w w . j av a 2s. c om } catch (Exception e) { } } } return map; }
From source file:com.greenline.hrs.admin.util.db.SchemaExport.java
public static String exportMySQL(Class type) { if (type == null) { return StringUtils.EMPTY; }/*from w w w .ja va 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
/** * get all fields for a class// www.ja va 2 s .c om * * @param type * @return all fields indexed by their names */ private static Map<String, Field> getAllFields(Class<?> type) { Map<String, Field> fields = new HashMap<String, Field>(); Class<?> currentType = type; while (!currentType.equals(Object.class)) { for (Field field : currentType.getDeclaredFields()) { int mod = field.getModifiers(); /* * by convention, our fields should not have any modifier */ if (mod == 0 || Modifier.isProtected(mod) && !Modifier.isStatic(mod)) { fields.put(field.getName(), field); } } currentType = currentType.getSuperclass(); } return fields; }