List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
From source file:com.twinsoft.convertigo.engine.util.CarUtils.java
private static Document exportProject(Project project, final List<TestCase> selectedTestCases) throws EngineException { try {//from w w w. ja v a2s. co m final Document document = XMLUtils.getDefaultDocumentBuilder().newDocument(); // ProcessingInstruction pi = document.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""); // document.appendChild(pi); final Element rootElement = document.createElement("convertigo"); rootElement.setAttribute("version", com.twinsoft.convertigo.engine.Version.fullProductVersion); rootElement.setAttribute("engine", com.twinsoft.convertigo.engine.Version.version); rootElement.setAttribute("beans", com.twinsoft.convertigo.beans.Version.version); String studioVersion = ""; try { Class<?> c = Class.forName("com.twinsoft.convertigo.eclipse.Version"); studioVersion = (String) c.getDeclaredField("version").get(null); } catch (Exception e) { } catch (Throwable th) { } rootElement.setAttribute("studio", studioVersion); document.appendChild(rootElement); new WalkHelper() { protected Element parentElement = rootElement; @Override protected void walk(DatabaseObject databaseObject) throws Exception { Element parentElement = this.parentElement; Element element = parentElement; element = databaseObject.toXml(document); String name = " : " + databaseObject.getName(); try { name = CachedIntrospector.getBeanInfo(databaseObject.getClass()).getBeanDescriptor() .getDisplayName() + name; } catch (IntrospectionException e) { name = databaseObject.getClass().getSimpleName() + name; } Integer depth = (Integer) document.getUserData("depth"); if (depth == null) { depth = 0; } String openpad = StringUtils.repeat(" ", depth); String closepad = StringUtils.repeat(" ", depth); parentElement.appendChild(document.createTextNode("\n")); parentElement.appendChild( document.createComment(StringUtils.rightPad(openpad + "<" + name + ">", 150))); if (databaseObject instanceof TestCase && selectedTestCases.size() > 0) { if (selectedTestCases.contains((TestCase) databaseObject)) { parentElement.appendChild(element); } } else { parentElement.appendChild(element); } document.setUserData("depth", depth + 1, null); this.parentElement = element; super.walk(databaseObject); element.appendChild(document.createTextNode("\n")); element.appendChild( document.createComment(StringUtils.rightPad(closepad + "</" + name + ">", 150))); document.setUserData("depth", depth, null); databaseObject.hasChanged = false; databaseObject.bNew = false; this.parentElement = parentElement; } }.init(project); return document; } catch (Exception e) { throw new EngineException("Unable to export the project \"" + project.getName() + "\".", e); } }
From source file:com.expressui.core.util.ReflectionUtil.java
/** * Finds field on a given type./*from w w w.ja va2s . c o m*/ * * @param type class containing the field * @param fieldName name of field to search for * @return found field */ public static Field getField(Class type, String fieldName) { Field field = null; Class currentType = type; while (field == null && !currentType.equals(Object.class)) { try { field = currentType.getDeclaredField(fieldName); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchFieldException e) { currentType = currentType.getSuperclass(); } } return field; }
From source file:IntrospectionUtil.java
protected static Field findInheritedField(Package pack, Class clazz, String fieldName, Class fieldType, boolean strictType) throws NoSuchFieldException { if (clazz == null) throw new NoSuchFieldException("No class"); if (fieldName == null) throw new NoSuchFieldException("No field name"); try {/*from w w w .j a v a2 s. com*/ Field field = clazz.getDeclaredField(fieldName); if (isInheritable(pack, field) && isTypeCompatible(fieldType, field.getType(), strictType)) return field; else return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), fieldName, fieldType, strictType); } catch (NoSuchFieldException e) { return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), fieldName, fieldType, strictType); } }
From source file:com.oltpbenchmark.util.ClassUtil.java
/** * Convert a Enum array to a Field array * This assumes that the name of each Enum element corresponds to a data member in the clas * @param <E>//www .j av a 2s .co m * @param clazz * @param members * @return * @throws NoSuchFieldException */ public static <E extends Enum<?>> Field[] getFieldsFromMembersEnum(Class<?> clazz, E members[]) throws NoSuchFieldException { Field fields[] = new Field[members.length]; for (int i = 0; i < members.length; i++) { fields[i] = clazz.getDeclaredField(members[i].name().toLowerCase()); } // FOR return (fields); }
From source file:com.facebook.presto.hive.TestPrestoS3FileSystem.java
@SuppressWarnings("unchecked") private static <T> T getFieldValue(Object instance, Class<?> clazz, String name, Class<T> type) { try {// w w w .ja v a 2 s. c o m Field field = clazz.getDeclaredField(name); checkArgument(field.getType() == type, "expected %s but found %s", type, field.getType()); field.setAccessible(true); return (T) field.get(instance); } catch (ReflectiveOperationException e) { throw Throwables.propagate(e); } }
From source file:de.beyondjava.angularFaces.core.ELTools.java
private static Field getField(String p_expression) { synchronized (fields) { if (fields.containsKey(p_expression)) { return fields.get(p_expression); }//www . j a v a2 s .c o m } if (p_expression.startsWith("#{") && p_expression.endsWith("}")) { int delimiterPos = p_expression.lastIndexOf('.'); if (delimiterPos < 0) { LOGGER.log(Level.WARNING, "There's no field to access: #{" + p_expression + "}"); return null; } String beanExp = p_expression.substring(0, delimiterPos) + "}"; String fieldName = p_expression.substring(delimiterPos + 1, p_expression.length() - 1); Object container = evalAsObject(beanExp); if (null == container) { LOGGER.severe("Can't read the bean '" + beanExp + "'. Thus JSR 303 annotations can't be read, let alone used by the AngularDart client."); return null; } Class<? extends Object> c = container.getClass(); while (c != null) { Field declaredField; try { declaredField = c.getDeclaredField(fieldName); synchronized (fields) { fields.put(p_expression, declaredField); } return declaredField; } catch (NoSuchFieldException e) { // let\"s try with the super class c = c.getSuperclass(); } catch (SecurityException e) { LOGGER.log(Level.SEVERE, "Unable to access a field", e); e.printStackTrace(); return null; } } } return null; }
From source file:org.openmrs.module.distrotools.test.TestUtils.java
/** * Modifies a constant in a constants class. If the constant is final and not based on a runtime expression, then * it's value will have been inlined by the compiler and this method will have no effect. * @param constantsClass the class of constants * @param fieldName the field name of the constant * @param newValue the new value for the constant * @throws Exception if field not found or couldn't be modified *//*from w ww.j a v a2 s .c o m*/ public static void modifyConstant(Class<?> constantsClass, String fieldName, Object newValue) throws Exception { Field field = constantsClass.getDeclaredField(fieldName); field.setAccessible(true); int existingModifiers = field.getModifiers(); // Remove final modifier from field Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, existingModifiers & ~Modifier.FINAL); field.set(null, newValue); // Reset previous modifiers modifiersField.setInt(field, existingModifiers); }
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
public static void setField(Object obj, String fieldName, Object value) throws NoSuchFieldException, IllegalArgumentException { Class<?> clazz = obj.getClass(); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true);/*from ww w.j a v a2s .co m*/ try { field.set(obj, value); } catch (IllegalAccessException e) { e.printStackTrace(); } }
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
public static Object getField(Object obj, String fieldName) throws NoSuchFieldException, IllegalArgumentException { Class<?> clazz = obj.getClass(); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true);//www . j a v a 2 s .c o m try { return field.get(obj); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; }
From source file:hu.javaforum.commons.ReflectionHelper.java
/** * Gets the the field.//from w w w . j ava 2 s . com * * @param objectClass The object * @param fieldName The field name of the object * @return The class * @exception NoSuchFieldException Throws, when the field isn't exists */ public static Field getField(final Class objectClass, final String fieldName) throws NoSuchFieldException { Field field; if ("return".equals(fieldName)) { field = objectClass.getDeclaredField("_" + fieldName); } else { field = objectClass.getDeclaredField(fieldName); } return field; }