List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
From source file:com.liferay.portal.configuration.ConfigurationImpl.java
public void removeProperties(Properties properties) { try {/*from w w w . ja v a 2 s . c om*/ ComponentProperties componentProperties = _componentConfiguration.getProperties(); AggregatedProperties aggregatedProperties = (AggregatedProperties) componentProperties .toConfiguration(); Class<?> clazz = aggregatedProperties.getClass(); Field field1 = clazz.getDeclaredField("baseConf"); field1.setAccessible(true); CompositeConfiguration compositeConfiguration = (CompositeConfiguration) field1 .get(aggregatedProperties); Field field2 = CompositeConfiguration.class.getDeclaredField("configList"); field2.setAccessible(true); List<Configuration> configurations = (List<Configuration>) field2.get(compositeConfiguration); Iterator<Configuration> itr = configurations.iterator(); while (itr.hasNext()) { Configuration configuration = itr.next(); if (!(configuration instanceof MapConfiguration)) { return; } MapConfiguration mapConfiguration = (MapConfiguration) configuration; if (mapConfiguration.getMap() == properties) { itr.remove(); aggregatedProperties.removeConfiguration(configuration); } } clearCache(); } catch (Exception e) { _log.error("The properties could not be removed", e); } }
From source file:jenkins.security.apitoken.ApiTokenStatsTest.java
private ApiTokenStats.SingleTokenStats createSingleTokenStatsByReflection(String uuid, String dateString, Integer counter) throws Exception { Class<ApiTokenStats.SingleTokenStats> clazz = ApiTokenStats.SingleTokenStats.class; Constructor<ApiTokenStats.SingleTokenStats> constructor = clazz.getDeclaredConstructor(String.class); constructor.setAccessible(true);// ww w . j a v a2 s .c om ApiTokenStats.SingleTokenStats result = constructor.newInstance(uuid); { Field field = clazz.getDeclaredField("useCounter"); field.setAccessible(true); field.set(result, counter); } if (dateString != null) { Field field = clazz.getDeclaredField("lastUseDate"); field.setAccessible(true); field.set(result, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(dateString)); } return result; }
From source file:Browser.java
/** * Called by a static initializer to load any classes, fields, and methods * required at runtime to locate the user's web browser. * @return <code>true</code> if all intialization succeeded * <code>false</code> if any portion of the initialization failed *//*from ww w. ja va 2 s . co m*/ private static boolean loadClasses() { switch (jvm) { case MRJ_2_0: try { Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget"); Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils"); Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent"); Class aeClass = Class.forName("com.apple.MacOS.ae"); aeDescClass = Class.forName("com.apple.MacOS.AEDesc"); aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class[] { int.class }); appleEventConstructor = appleEventClass.getDeclaredConstructor( new Class[] { int.class, int.class, aeTargetClass, int.class, int.class }); aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class }); makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class[] { String.class }); putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass }); sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] {}); Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject"); keyDirectObject = (Integer) keyDirectObjectField.get(null); Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID"); kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null); Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID"); kAnyTransactionID = (Integer) anyTransactionIDField.get(null); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_2_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType"); Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType"); kSystemFolderType = systemFolderField.get(null); findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass }); getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class }); getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (SecurityException se) { errorMessage = se.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_0: try { Class linker = Class.forName("com.apple.mrj.jdirect.Linker"); Constructor constructor = linker.getConstructor(new Class[] { Class.class }); linkage = constructor.newInstance(new Object[] { Browser.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (InvocationTargetException ite) { errorMessage = ite.getMessage(); return false; } catch (InstantiationException ie) { errorMessage = ie.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } break; default: break; } return true; }
From source file:fxts.stations.util.BrowserLauncher.java
/** * Called by a static initializer to load any classes, fields, and methods required at runtime * to locate the user's web browser.//ww w. ja v a 2 s . com * * @return <code>true</code> if all intialization succeeded * <code>false</code> if any portion of the initialization failed */ private static boolean loadClasses() { switch (jvm) { case MRJ_2_0: try { Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget"); Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils"); Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent"); Class aeClass = Class.forName("com.apple.MacOS.ae"); aeDescClass = Class.forName("com.apple.MacOS.AEDesc"); aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class[] { int.class }); appleEventConstructor = appleEventClass.getDeclaredConstructor( new Class[] { int.class, int.class, aeTargetClass, int.class, int.class }); aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class }); makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class[] { String.class }); putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass }); sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] {}); Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject"); keyDirectObject = (Integer) keyDirectObjectField.get(null); Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID"); kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null); Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID"); kAnyTransactionID = (Integer) anyTransactionIDField.get(null); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_2_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType"); Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType"); kSystemFolderType = systemFolderField.get(null); findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass }); getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class }); getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchFieldException nsfe) { errorMessage = nsfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (SecurityException se) { errorMessage = se.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_0: try { Class linker = Class.forName("com.apple.mrj.jdirect.Linker"); Constructor constructor = linker.getConstructor(new Class[] { Class.class }); linkage = constructor.newInstance(new Object[] { BrowserLauncher.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } catch (InvocationTargetException ite) { errorMessage = ite.getMessage(); return false; } catch (InstantiationException ie) { errorMessage = ie.getMessage(); return false; } catch (IllegalAccessException iae) { errorMessage = iae.getMessage(); return false; } break; case MRJ_3_1: try { mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils"); openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class }); } catch (ClassNotFoundException cnfe) { errorMessage = cnfe.getMessage(); return false; } catch (NoSuchMethodException nsme) { errorMessage = nsme.getMessage(); return false; } break; default: break; } return true; }
From source file:com.taobao.adfs.util.Utilities.java
public static Field getField(Class<?> clazz, String fieldName) throws IOException { if (clazz == null) throw new IOException("clazz is null"); if (fieldName == null) throw new IOException("fieldName is null"); if (fieldName.isEmpty()) throw new IOException("fieldName is empty"); Field field = null;/*w ww. j ava2s . c om*/ try { field = clazz.getDeclaredField(fieldName); } catch (Throwable t0) { try { field = clazz.getField(fieldName); } catch (Throwable t1) { if (clazz.getSuperclass() != null) { field = getField(clazz.getSuperclass(), fieldName); } else throw new IOException("fail to get field for " + clazz.getName() + "." + fieldName, t1); } } field.setAccessible(true); return field; }
From source file:com.db2eshop.gui.component.table.api.GenericTable.java
/** * <p>//from w w w . j av a 2s .c o m * asTableData. * </p> * * @param entity * a T object. * @return an array of {@link java.lang.Object} objects. */ protected Object[] asTableData(T entity) { if (entity == null) { throw new RuntimeException("Entity cannot be null"); } if (columnNames == null) { throw new RuntimeException("Entity extraction fields cannot be null"); } Object[] data = new Object[columnNames.length]; int index = 0; Class<?> entityClass = entity.getClass(); for (String field : columnNames) { try { Object object = ClassUtil.getValueOf(field, entity, entityClass, entityClass.getDeclaredField(field).getType()); if (object != null && AbstractModel.class.isAssignableFrom(object.getClass())) { if (object instanceof AbstractModel) { object = ((AbstractModel<?>) object).getId(); } else if (object instanceof String) { log.error("Target was entity but found String " + object); } } else if (object != null && object instanceof Date) { object = DateUtil.asString((Date) object); } data[index] = object; } catch (Exception e) { log.error("Field " + field + " could not been found", e); data[index] = e.getMessage(); } index++; } return data; }
From source file:com.glaf.core.util.ReflectUtils.java
public static void setFieldValue(Object target, String name, Class<?> type, Object value) { if (target == null || StringUtils.isEmpty(name) || (value != null && !type.isAssignableFrom(value.getClass()))) { return;/*from w w w . ja v a 2s . c om*/ } Class<?> clazz = target.getClass(); try { Method method = clazz .getDeclaredMethod("set" + Character.toUpperCase(name.charAt(0)) + name.substring(1), type); if (!Modifier.isPublic(method.getModifiers())) { method.setAccessible(true); } method.invoke(target, value); } catch (Exception ex) { if (LogUtils.isDebug()) { logger.debug(ex); } try { Field field = clazz.getDeclaredField(name); if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } field.set(target, value); } catch (Exception e) { if (LogUtils.isDebug()) { logger.debug(e); } } } }
From source file:com.thesett.util.validation.core.JsonSchemaConstraintMapping.java
/** * Creates constraints against a field, matching those found in a json schema. * * @param propertyContext The context of the field to create constraints for. * @param javaPropertyName The name of the property that constraints are being added to. * @param jsonPropertyName The name of the property in the json-schema. * @param value The json schema definition for the field, to take additional constraints from. * @param type The type of the class that property constraints are being added to. *///from w w w . ja v a 2 s. c o m private void addConstraints(PropertyConstraintMappingContext propertyContext, String javaPropertyName, String jsonPropertyName, JsonSchema value, Class type) { convertMinimum(propertyContext, value); convertMaximum(propertyContext, value); convertMaxLength(propertyContext, value); convertMinLength(propertyContext, value); convertPattern(propertyContext, value); convertTitle(propertyContext, value); convertDescription(propertyContext, value); if (SchemaType.OBJECT.equals(value.getType())) { Class<?> fieldClass = null; try { fieldClass = type.getDeclaredField(javaPropertyName).getType(); } catch (NoSuchFieldException e) { throw new ValidationException("No matching field found with name: " + javaPropertyName, e); } propertyContext.valid(); addSchema(fieldClass, value); } }
From source file:com.google.api.ads.adwords.awreporting.model.persistence.sql.SqlReportEntitiesPersister.java
private <T> Field getField(Class<T> classT, String fieldName) throws NoSuchFieldException { try {/* w w w. j a va 2 s . com*/ return classT.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { Class<? super T> superClass = classT.getSuperclass(); if (superClass == null) { throw e; } else { return getField(superClass, fieldName); } } }
From source file:me.piebridge.bible.Bible.java
public static Object getField(Object object, final Class<?> clazz, final String fieldName) { try {/*ww w .ja v a 2s.c o m*/ Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return field.get(object); } catch (Exception e) { Log.e(TAG, "no such filed " + object.getClass().getName() + "." + fieldName); } return null; }