List of usage examples for java.lang.reflect Modifier isTransient
public static boolean isTransient(int mod)
From source file:org.apache.axis.utils.BeanUtils.java
public static BeanPropertyDescriptor[] processPropertyDescriptors(PropertyDescriptor[] rawPd, Class cls, TypeDesc typeDesc) {// w w w. ja va 2 s . c o m // Create a copy of the rawPd called myPd BeanPropertyDescriptor[] myPd = new BeanPropertyDescriptor[rawPd.length]; ArrayList pd = new ArrayList(); try { for (int i = 0; i < rawPd.length; i++) { // Skip the special "any" field if (rawPd[i].getName().equals(Constants.ANYCONTENT)) continue; pd.add(new BeanPropertyDescriptor(rawPd[i])); } // Now look for public fields Field fields[] = cls.getFields(); if (fields != null && fields.length > 0) { // See if the field is in the list of properties // add it if not. for (int i = 0; i < fields.length; i++) { Field f = fields[i]; // skip if field come from a java.* or javax.* package // WARNING: Is this going to make bad things happen for // users JavaBeans? Do they WANT these fields serialzed? String clsName = f.getDeclaringClass().getName(); if (clsName.startsWith("java.") || clsName.startsWith("javax.")) { continue; } // skip field if it is final, transient, or static if (!(Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers()) || Modifier.isTransient(f.getModifiers()))) { String fName = f.getName(); boolean found = false; for (int j = 0; j < rawPd.length && !found; j++) { String pName = ((BeanPropertyDescriptor) pd.get(j)).getName(); if (pName.length() == fName.length() && pName.substring(0, 1).equalsIgnoreCase(fName.substring(0, 1))) { found = pName.length() == 1 || pName.substring(1).equals(fName.substring(1)); } } if (!found) { pd.add(new FieldPropertyDescriptor(f.getName(), f)); } } } } // If typeDesc meta data exists, re-order according to the fields if (typeDesc != null && typeDesc.getFields(true) != null) { ArrayList ordered = new ArrayList(); // Add the TypeDesc elements first FieldDesc[] fds = typeDesc.getFields(true); for (int i = 0; i < fds.length; i++) { FieldDesc field = fds[i]; if (field.isElement()) { boolean found = false; for (int j = 0; j < pd.size() && !found; j++) { if (field.getFieldName().equals(((BeanPropertyDescriptor) pd.get(j)).getName())) { ordered.add(pd.remove(j)); found = true; } } } } // Add the remaining elements while (pd.size() > 0) { ordered.add(pd.remove(0)); } // Use the ordered list pd = ordered; } myPd = new BeanPropertyDescriptor[pd.size()]; for (int i = 0; i < pd.size(); i++) { myPd[i] = (BeanPropertyDescriptor) pd.get(i); } } catch (Exception e) { log.error(Messages.getMessage("badPropertyDesc00", cls.getName()), e); throw new InternalException(e); } return myPd; }
From source file:org.eclipse.epp.internal.logging.aeri.ui.v2.ServerConfigurationTest.java
@Test public void testCompatibility() throws ClientProtocolException, IOException { // validates, that the client and the server share exactly the same fields in configuration // additional fields on the client have to be transient or must be excluded from the test String asString = AeriServer.request(URI.create(SERVER_URL), Executor.newInstance()).returnContent() .asString();/* w ww . j a v a 2 s . co m*/ @SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) new GsonBuilder().create().fromJson(asString, Object.class); Set<String> serverFields = map.keySet(); map.remove("id"); List<Field> classFieldsAll = Lists.newArrayList(ServerConfiguration.class.getDeclaredFields()); Set<String> classFields = Sets.newHashSet(); for (int i = classFieldsAll.size() - 1; i >= 0; i--) { Field field = classFieldsAll.get(i); if (Modifier.isTransient(field.getModifiers())) { // local helper fields continue; } if (field.getName().equals("problemsZipLastDownloadTimestamp") || field.getName().equals("timestamp")) { // not transient, but will be created and persisted on the client continue; } classFields.add(field.getName()); } for (String s : serverFields) { assertThat("Missing value in client ServerConfiguration", classFields, hasItem(s)); } for (String s : classFields) { assertThat("Missing value on remote server", serverFields, hasItem(s)); } }
From source file:org.vulpe.model.dao.impl.db4o.AbstractVulpeBaseDAODB4O.java
/** * Verified if entity property is empty and set to null; * * @param object/*from ww w . j a v a 2 s . c om*/ */ public void emptyToNull(final Object object) { final List<Field> fields = VulpeReflectUtil.getFields(object.getClass()); for (final Field field : fields) { try { if (field.isAnnotationPresent(SkipEmpty.class)) { continue; } if ((Modifier.isTransient(field.getModifiers()) || field.isAnnotationPresent(Transient.class)) && !field.isAnnotationPresent(QueryParameter.class) && !field.getType().isPrimitive()) { PropertyUtils.setProperty(object, field.getName(), null); } else { final Object value = PropertyUtils.getProperty(object, field.getName()); if (value != null) { if (String.class.isAssignableFrom(field.getType())) { if (StringUtils.isEmpty(value.toString()) || "obj.id".equals(value) || "null".equals(value) /* || "%".equals(value) */) { PropertyUtils.setProperty(object, field.getName(), null); } } else if (VulpeEntity.class.isAssignableFrom(value.getClass()) && !value.getClass().isAnnotationPresent(CreateIfNotExist.class)) { emptyToNull(value); } } } } catch (NoSuchMethodException e) { LOG.debug("Method not found.", e); } catch (Exception e) { throw new VulpeSystemException(e); } } }
From source file:org.objectpocket.viewer.Viewer.java
private void updateTable() { String selectedType = ((DefaultMutableTreeNode) classTree.getSelectionPath().getLastPathComponent()) .toString();/*from w w w. ja v a 2s. com*/ String selectedObjectPocket = ((DefaultMutableTreeNode) classTree.getSelectionPath() .getPathComponent(classTree.getSelectionPath().getPathCount() - 2)).toString(); ObjectPocketImpl objectPocket = null; for (ObjectPocketImpl objectPocketImpl : objectPocketList) { if (objectPocketImpl.getSource().equals(selectedObjectPocket)) { objectPocket = objectPocketImpl; } } if (objectPocket != null) { Map<String, Object> objectMap = objectPocket.getMapForType(selectedType); try { Class<?> clazz = Class.forName(selectedType); List<Field> allFields = FieldUtils.getAllFieldsList(clazz); List<Field> fields = new ArrayList<>(); // filter fields for (Field field : allFields) { if (!Modifier.isTransient(field.getModifiers()) && !field.getName().equals("id")) { fields.add(field); } } String[] columnNames = new String[fields.size() + 2]; columnNames[0] = ""; columnNames[1] = "id"; int index = 2; for (Field f : fields) { columnNames[index] = f.getName() + "(" + f.getType().getSimpleName() + ")"; index++; } // filter objects Map<String, Object> filteredObjects = filterObjects(objectMap, fields); String[][] rowData = new String[filteredObjects.size()][columnNames.length]; index = 0; statusLabel.setText("object count: " + filteredObjects.size()); for (String key : filteredObjects.keySet()) { rowData[index][0] = "" + (index + 1); rowData[index][1] = key; for (int i = 0; i < fields.size(); i++) { Field f = fields.get(i); f.setAccessible(true); try { Object object = f.get(filteredObjects.get(key)); if (object != null) { rowData[index][2 + i] = object.toString(); } else { rowData[index][2 + i] = "null"; } } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } index++; } DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames); objectTable.setModel(tableModel); viewerFrame.revalidate(); } catch (ClassNotFoundException e) { Logger.getAnonymousLogger().warning("Could not load class for type. " + selectedType); e.printStackTrace(); // --- fallback String[] columnNames = { "", "id", "data" }; String[][] rowData = new String[objectMap.size()][3]; int index = 0; for (String key : objectMap.keySet()) { rowData[index][0] = "" + (index + 1); rowData[index][1] = key; rowData[index][2] = objectMap.get(key).toString(); index++; } DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames); objectTable.setModel(tableModel); viewerFrame.revalidate(); } } for (int i = 0; i < objectTable.getColumnModel().getColumnCount(); i++) { if (i == 0) { objectTable.getColumnModel().getColumn(i).setPreferredWidth(50); } else { objectTable.getColumnModel().getColumn(i).setPreferredWidth(200); } } }
From source file:org.vulpe.commons.util.VulpeReflectUtil.java
/** * Copy transient attributes from <code>origin</code> to * <code>destination</code>. * * @param destination/*w w w. jav a 2 s .c om*/ * @param origin */ public static void copyOnlyTransient(final Object destination, final Object origin) { final List<Field> fields = getFields(origin.getClass()); for (final Field field : fields) { if (!Modifier.isTransient(field.getModifiers())) { continue; } try { final Object value = PropertyUtils.getProperty(origin, field.getName()); if (Collection.class.isAssignableFrom(field.getType())) { final Collection valueDes = (Collection) PropertyUtils.getProperty(destination, field.getName()); if (value == null) { if (valueDes != null) { valueDes.clear(); } } else { if (valueDes == null) { PropertyUtils.setProperty(destination, field.getName(), value); } else { valueDes.clear(); valueDes.addAll((Collection) value); } } } else { PropertyUtils.setProperty(destination, field.getName(), value); } } catch (NoSuchMethodException e) { LOG.debug("Method not found.", e); } catch (Exception e) { throw new VulpeSystemException(e); } } }
From source file:edu.cmu.tetrad.util.TetradSerializableUtils.java
/** * Checks all of the classes in the serialization scope that implement * TetradSerializable to make sure all of their fields are either themselves * (a) primitive, (b) TetradSerializable, or (c) assignable from types * designated as safely serializable by virtue of being included in the * safelySerializableTypes array (see), or are arrays whose lowest order * component types satisfy either (a), (b), or (c). Safely serializable * classes in the Java API currently include collections classes, plus * String and Class. Collections classes are included, since their types * will be syntactically checkable in JDK 1.5. String and Class are members * of a broader type of Class whose safely can by checked by making sure * there is no way to pass into them via constructor or method argument any * object that is not TetradSerializable or safely serializable. But it's * easy enough now to just make a list.//from w ww . j a v a2 s . co m * * @see #safelySerializableTypes */ public void checkNestingOfFields() { List classes = getAssignableClasses(new File(getSerializableScope()), TetradSerializable.class); boolean foundUnsafeField = false; for (Object aClass : classes) { Class clazz = (Class) aClass; if (TetradSerializableExcluded.class.isAssignableFrom(clazz)) { continue; } Field[] fields = clazz.getDeclaredFields(); FIELDS: for (Field field : fields) { // System.out.println(field); if (Modifier.isTransient(field.getModifiers())) { continue; } if (Modifier.isStatic(field.getModifiers())) { continue; } Class type = field.getType(); while (type.isArray()) { type = type.getComponentType(); } if (type.isPrimitive()) { continue; } if (type.isEnum()) { continue; } // // Printing out Collections fields temporarily. // if (Collection.class.isAssignableFrom(type)) { // System.out.println("COLLECTION FIELD: " + field); // } // // if (Map.class.isAssignableFrom(type)) { // System.out.println("MAP FIELD: " + field); // } if (TetradSerializable.class.isAssignableFrom(type) && !TetradSerializableExcluded.class.isAssignableFrom(clazz)) { continue; } for (Class safelySerializableClass : safelySerializableTypes) { if (safelySerializableClass.isAssignableFrom(type)) { continue FIELDS; } } // A reference in an inner class to the outer class. if (field.getName().equals("this$0")) { continue; } System.out.println("UNSAFE FIELD:" + field); foundUnsafeField = true; } } if (foundUnsafeField) { throw new RuntimeException("Unsafe serializable fields found. Please " + "fix immediately."); } }
From source file:de.micromata.genome.util.strings.ReducedReflectionToStringBuilder.java
@Override protected boolean accept(Field field) { // if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) { // // Reject field from inner class. // return false; // }//from ww w .j av a 2 s. c o m if (field.getAnnotation(NoStringifyAnnotation.class) != null) { return false; } if (Modifier.isTransient(field.getModifiers()) && !this.isAppendTransients()) { // transients. return false; } if (Modifier.isStatic(field.getModifiers()) && !this.isAppendStatics()) { // transients. return false; } return true; }
From source file:de.codesourcery.eve.skills.util.XMLMapper.java
private BeanDescription createBeanDescription(Class<?> clasz) { BeanDescription result = new BeanDescription(); for (java.lang.reflect.Field f : clasz.getDeclaredFields()) { final int modifiers = f.getModifiers(); if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) { continue; }/*from w ww .j av a 2 s.co m*/ if (!f.isAccessible()) { f.setAccessible(true); } result.addField(f); } return result; }
From source file:org.castor.jaxb.reflection.ClassInfoBuilder.java
/** * Checks if the Method is describeable. * /*from w ww. j a v a 2 s. c om*/ * @param type * the Class of the Method * @param method * the Method to check * @return true if the method is describeable */ private boolean isDescribeable(final Class<?> type, final Method method) { boolean isDescribeable = true; Class<?> declaringClass = method.getDeclaringClass(); if ((declaringClass != null) && !type.equals(declaringClass) && (!declaringClass.isInterface())) { isDescribeable = false; } if (method.isSynthetic()) { isDescribeable &= false; } if (Modifier.isStatic(method.getModifiers())) { isDescribeable &= false; } if (Modifier.isTransient(method.getModifiers())) { isDescribeable &= false; } if (!(javaNaming.isAddMethod(method) || javaNaming.isCreateMethod(method) || javaNaming.isGetMethod(method) || javaNaming.isIsMethod(method) || javaNaming.isSetMethod(method))) { isDescribeable = false; } return isDescribeable; }
From source file:com.threewks.thundr.bigmetrics.service.BigMetricsServiceImpl.java
protected Map<Field, FieldProcessor<?>> findFieldProcessors(Class<?> eventClass) { List<Field> fields = Arrays.asList(ReflectUtil.getSupportedFields(eventClass, Object.class)); Map<Field, FieldProcessor<?>> processors = new LinkedHashMap<>(); for (Field field : fields) { if (!field.isSynthetic() && !Modifier.isTransient(field.getModifiers()) && !field.isAnnotationPresent(Ignore.class)) { field.setAccessible(true);// w w w . j av a 2s .c o m FieldProcessor<?> processor = determineProcessor(field); processors.put(field, processor); } } return processors; }