public PureJavaReflectionProvider() { this(new FieldDictionary(new ImmutableFieldKeySorter())); }
/** * Returns an iterator for all fields for some class * * @param cls the class you are interested on * @return an iterator for its fields */ public Iterator fieldsFor(final Class cls) { return buildMap(cls, true).values().iterator(); }
public Field getField(Class definedIn, String fieldName) { return fieldDictionary.field(definedIn, fieldName, null); }
private Map buildMap(final Class type, final boolean tupleKeyed) { Class cls = type; DictionaryEntry lastDictionaryEntry = null; final LinkedList superClasses = new LinkedList(); while (lastDictionaryEntry == null) { if (Object.class.equals(cls) || cls == null) { lastDictionaryEntry = OBJECT_DICTIONARY_ENTRY; } else { lastDictionaryEntry = getDictionaryEntry(cls); } if (lastDictionaryEntry == null) { superClasses.addFirst(cls); cls = cls.getSuperclass(); } } for (final Iterator iter = superClasses.iterator(); iter.hasNext();) { cls = (Class)iter.next(); DictionaryEntry newDictionaryEntry = buildDictionaryEntryForClass(cls, lastDictionaryEntry); synchronized (this) { final DictionaryEntry concurrentEntry = getDictionaryEntry(cls); if (concurrentEntry == null) { dictionaryEntries.put(cls, newDictionaryEntry); } else { newDictionaryEntry = concurrentEntry; } } lastDictionaryEntry = newDictionaryEntry; } return tupleKeyed ? lastDictionaryEntry.getKeyedByFieldKey() : lastDictionaryEntry.getKeyedByFieldName(); }
private Object readResolve() { fieldDictionary = new FieldDictionary(); attributeMap = new HashMap(); for (final Iterator iterator = fieldDictionary.fieldsFor(type); iterator .hasNext();) { final Field field = (Field)iterator.next(); if (field.getType() == type && Modifier.isStatic(field.getModifiers())) { try { final Object attribute = field.get(null); attributeMap.put(toString(attribute), attribute); } catch (IllegalAccessException e) { throw new ObjectAccessException("Cannot get object of " + field, e); } } } return this; }
public FieldDictionary(final FieldKeySorter sorter) { this.sorter = sorter; init(); }
/** * Returns an iterator for all fields for some class * * @param cls the class you are interested on * @return an iterator for its fields * @deprecated As of 1.3, use {@link #fieldsFor(Class)} instead */ public Iterator serializableFieldsFor(final Class cls) { return fieldsFor(cls); }
public Field getFieldOrNull(Class definedIn, String fieldName) { return fieldDictionary.fieldOrNull(definedIn, fieldName, null); }
private DictionaryEntry buildCache(final Class<?> type) { Class<?> cls = type; DictionaryEntry lastDictionaryEntry = null; final LinkedList<Class<?>> superClasses = new LinkedList<>(); while (lastDictionaryEntry == null) { if (Object.class.equals(cls) || cls == null) { lastDictionaryEntry = OBJECT_DICTIONARY_ENTRY; } else { lastDictionaryEntry = dictionaryEntries.get(cls); } if (lastDictionaryEntry == null) { superClasses.addFirst(cls); cls = cls.getSuperclass(); } } for (final Class<?> element : superClasses) { cls = element; DictionaryEntry currentDictionaryEntry = dictionaryEntries.get(cls); if (currentDictionaryEntry == null) { currentDictionaryEntry = buildDictionaryEntryForClass(cls, lastDictionaryEntry); final DictionaryEntry existingValue = dictionaryEntries.putIfAbsent(cls, currentDictionaryEntry); if (existingValue != null) { currentDictionaryEntry = existingValue; } } lastDictionaryEntry = currentDictionaryEntry; } return lastDictionaryEntry; }
private Object readResolve() { fieldDictionary = new FieldDictionary(); attributeMap = new HashMap(); for (final Iterator iterator = fieldDictionary.fieldsFor(type); iterator .hasNext();) { final Field field = (Field)iterator.next(); if (field.getType() == type && Modifier.isStatic(field.getModifiers())) { try { final Object attribute = field.get(null); attributeMap.put(toString(attribute), attribute); } catch (IllegalAccessException e) { throw new ObjectAccessException("Cannot get object of " + field, e); } } } return this; }
protected Object readResolve() { init(); return this; }
public void visitSerializableFields(Object object, ReflectionProvider.Visitor visitor) { for (Iterator iterator = fieldDictionary.fieldsFor(object.getClass()); iterator.hasNext();) { Field field = (Field) iterator.next(); if (!fieldModifiersSupported(field)) { continue; } validateFieldAccess(field); Object value = Fields.read(field, object); visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value); } }
/** * @deprecated As of 1.4.5, use {@link #getFieldOrNull(Class, String)} instead */ public boolean fieldDefinedInClass(String fieldName, Class type) { Field field = fieldDictionary.fieldOrNull(type, fieldName, null); return field != null && fieldModifiersSupported(field); }
private Map buildMap(final Class type, final boolean tupleKeyed) { Class cls = type; DictionaryEntry lastDictionaryEntry = null; final LinkedList superClasses = new LinkedList(); while (lastDictionaryEntry == null) { if (Object.class.equals(cls) || cls == null) { lastDictionaryEntry = OBJECT_DICTIONARY_ENTRY; } else { lastDictionaryEntry = getDictionaryEntry(cls); } if (lastDictionaryEntry == null) { superClasses.addFirst(cls); cls = cls.getSuperclass(); } } for (final Iterator iter = superClasses.iterator(); iter.hasNext();) { cls = (Class)iter.next(); DictionaryEntry newDictionaryEntry = buildDictionaryEntryForClass(cls, lastDictionaryEntry); synchronized (this) { final DictionaryEntry concurrentEntry = getDictionaryEntry(cls); if (concurrentEntry == null) { dictionaryEntries.put(cls, newDictionaryEntry); } else { newDictionaryEntry = concurrentEntry; } } lastDictionaryEntry = newDictionaryEntry; } return tupleKeyed ? lastDictionaryEntry.getKeyedByFieldKey() : lastDictionaryEntry.getKeyedByFieldName(); }
/** Constructs the persister and underlying xstream specifying the stream driver explicitly. */ protected XStreamPersister(HierarchicalStreamDriver streamDriver) { // control the order in which fields are sorted SortableFieldKeySorter sorter = new SortableFieldKeySorter(); // sorter.registerFieldOrder( DefaultCatalogDAO.class, new String[]{ "workspaces", // "namespaces", "stores", "styles", /* these we actually omit, but the sorter needs them specified */ // "layerGroups", "resources", "maps", "defaultStores", "listeners", "layers", // "resourcePool", "resourceLoader", "LOGGER" } ); ReflectionProvider reflectionProvider = new CustomReflectionProvider(new FieldDictionary(sorter)); // new Sun14ReflectionProvider( new FieldDictionary( sorter ) ); if (streamDriver != null) { xs = new SecureXStream(reflectionProvider, streamDriver); } else { xs = new SecureXStream(reflectionProvider); } xs.setMode(XStream.NO_REFERENCES); init(xs); }
private Object readResolve() { fieldDictionary = new FieldDictionary(); attributeMap = new HashMap(); for (final Iterator iterator = fieldDictionary.fieldsFor(type); iterator .hasNext();) { final Field field = (Field)iterator.next(); if (field.getType() == type && Modifier.isStatic(field.getModifiers())) { try { final Object attribute = field.get(null); attributeMap.put(toString(attribute), attribute); } catch (IllegalAccessException e) { throw new ObjectAccessException("Cannot get object of " + field, e); } } } return this; }
public Class getFieldType(Object object, String fieldName, Class definedIn) { return fieldDictionary.field(object.getClass(), fieldName, definedIn).getType(); }
/** * Returns an specific field of some class. If definedIn is null, it searches for the field named 'name' inside the * class cls. If definedIn is different than null, tries to find the specified field name in the specified class cls * which should be defined in class definedIn (either equals cls or a one of it's superclasses) * * @param cls the class where the field is to be searched * @param name the field name * @param definedIn the superclass (or the class itself) of cls where the field was defined * @return the field itself or <code>null</code> * @since 1.4 */ public Field fieldOrNull(final Class cls, final String name, final Class definedIn) { final Map fields = buildMap(cls, definedIn != null); final Field field = (Field)fields.get(definedIn != null ? (Object)new FieldKey(name, definedIn, -1) : (Object)name); return field; }
protected Object readResolve() { init(); return this; } }
/** * Returns an iterator for all fields for some class * * @param cls the class you are interested on * @return an iterator for its fields * @deprecated As of 1.3, use {@link #fieldsFor(Class)} instead */ public Iterator serializableFieldsFor(Class cls) { return fieldsFor(cls); }