private AnnotationBusinessKeyMetadata(final Class<?> type) { Assert.notNull(type, "Given Type must not be null"); businessKeyField = ReflectionUtils.findField(type, BUSINESSKEY_FILTER); }
private void setField(final Field keyField, final Object key) { if (keyField != null) { ReflectionUtils.setField(keyField, this.source, key); } } }
/** * Finds a constructor on the given type that matches the given constructor arguments. * * @param type must not be {@literal null}. * @param constructorArguments must not be {@literal null}. * @return a {@link Constructor} that is compatible with the given arguments. */ public static Optional<Constructor<?>> findConstructor(Class<?> type, Object... constructorArguments) { Assert.notNull(type, "Target type must not be null!"); Assert.notNull(constructorArguments, "Constructor arguments must not be null!"); return Arrays.stream(type.getDeclaredConstructors())// .filter(constructor -> argumentsMatch(constructor.getParameterTypes(), constructorArguments))// .findFirst(); }
private static Object getEntityInstantiator(MetaData metaData, @Nullable MappingContext<Neo4jPersistentEntity<?>, Neo4jPersistentProperty> mappingContext) { try { Class<?> queryResultInstantiatorClass = Class.forName( "org.springframework.data.neo4j.repository.query.QueryResultInstantiator", true, ClassUtils.getDefaultClassLoader()); Optional<Constructor<?>> queryResultInstantiatorConstructor = ReflectionUtils .findConstructor(queryResultInstantiatorClass, metaData, mappingContext); return queryResultInstantiatorConstructor.map(ctor -> BeanUtils.instantiateClass(ctor, metaData, mappingContext)) .get(); } catch (ClassNotFoundException e) { // It is there, we just don't want to reference it directly. return null; } } }
private static Path<?> reifyPath(EntityPathResolver resolver, PropertyPath path, Optional<Path<?>> base) { Optional<Path<?>> map = base.filter(it -> it instanceof CollectionPathBase).map(CollectionPathBase.class::cast)// .map(CollectionPathBase::any)// .map(Path.class::cast)// .map(it -> reifyPath(resolver, path, Optional.of(it))); return map.orElseGet(() -> { Path<?> entityPath = base.orElseGet(() -> resolver.createPath(path.getOwningType().getType())); Field field = org.springframework.data.util.ReflectionUtils.findRequiredField(entityPath.getClass(), path.getSegment()); Object value = ReflectionUtils.getField(field, entityPath); PropertyPath next = path.next(); if (next != null) { return reifyPath(resolver, next, Optional.of((Path<?>) value)); } return (Path<?>) value; }); } }
/** * Instantiates all Apache Geode/Pivotal GemFire objects which have been declared * via {@link System#getProperties() System properties}. * * @param <T> {@link Class type} of the {@link GemFireCache}. * @param gemfireCache reference to the {@link GemFireCache} instance. * @return the given {@link GemFireCache} instance. * @see org.apache.geode.cache.GemFireCache */ private static <T extends GemFireCache> T constructGemFireObjects(T gemfireCache) { Properties localGemfireProperties = gemfireProperties.get(); Arrays.stream(GEMFIRE_OBJECT_BASED_PROPERTIES) .map(localGemfireProperties::getProperty) .filter(StringUtils::hasText) .filter(className -> ClassUtils.isPresent(className, ClassUtils.getDefaultClassLoader())) .forEach(className -> cacheGemFireObject(ReflectionUtils.createInstanceIfPresent(className, null))); return gemfireCache; }
/** * Creates a repository of the repository base class defined in the given {@link RepositoryInformation} using * reflection. * * @param baseClass * @param constructorArguments * @return */ @SuppressWarnings("unchecked") protected final <R> R getTargetRepositoryViaReflection(Class<?> baseClass, Object... constructorArguments) { Optional<Constructor<?>> constructor = ReflectionUtils.findConstructor(baseClass, constructorArguments); return constructor.map(it -> (R) BeanUtils.instantiateClass(it, constructorArguments)) .orElseThrow(() -> new IllegalStateException(String.format( "No suitable constructor found on %s to match the given arguments: %s. Make sure you implement a constructor taking these", baseClass, Arrays.stream(constructorArguments).map(Object::getClass).collect(Collectors.toList())))); }
/** * Returns the target {@link TypeDescriptor} for the given {@link PathInformation} by either inspecting the field or * property (the latter preferred) to pick up annotations potentially defined for formatting purposes. * * @param path must not be {@literal null}. * @return */ private static TypeDescriptor getTargetTypeDescriptor(PathInformation path) { PropertyDescriptor descriptor = path.getLeafPropertyDescriptor(); Class<?> owningType = path.getLeafParentType(); String leafProperty = path.getLeafProperty(); TypeDescriptor result = descriptor == null // ? TypeDescriptor .nested(org.springframework.data.util.ReflectionUtils.findRequiredField(owningType, leafProperty), 0) : TypeDescriptor .nested(new Property(owningType, descriptor.getReadMethod(), descriptor.getWriteMethod(), leafProperty), 0); if (result == null) { throw new IllegalStateException(String.format("Could not obtain TypeDesciptor for PathInformation %s!", path)); } return result; }
/** * Finds the field matching the given {@link DescribedFieldFilter}. Will make sure there's only one field matching the * filter. * * @see #findField(Class, DescribedFieldFilter, boolean) * @param type must not be {@literal null}. * @param filter must not be {@literal null}. * @return the field matching the given {@link DescribedFieldFilter} or {@literal null} if none found. * @throws IllegalStateException in case more than one matching field is found */ @Nullable public static Field findField(Class<?> type, DescribedFieldFilter filter) { return findField(type, filter, true); }
private static Object constructInstance(Class<?> type, Object... constructorArguments) { return org.springframework.data.util.ReflectionUtils.findConstructor(type, constructorArguments) .map(constructor -> BeanUtils.instantiateClass(constructor, constructorArguments)) .orElseThrow(() -> newIllegalArgumentException( "No suitable constructor was found for type [%s] having parameters [%s]", type.getName(), stream(nullSafeArray(constructorArguments, Object.class)) .map(ObjectUtils::nullSafeClassName) .collect(Collectors.toList()))); }
/** * Sets the given field to the given value if present. * * @param field * @param value */ private <S> S setField(Optional<Field> field, S value) { field.ifPresent(it -> ReflectionUtils.setField(it, target, value)); return value; }
/** * Finds the first field on the given class matching the given {@link FieldFilter}. * * @param type must not be {@literal null}. * @param filter must not be {@literal null}. * @return the field matching the filter or {@literal null} in case no field could be found. */ @Nullable public static Field findField(Class<?> type, FieldFilter filter) { return findField(type, new DescribedFieldFilter() { public boolean matches(Field field) { return filter.matches(field); } public String getDescription() { return String.format("FieldFilter %s", filter.toString()); } }, false); }
private static Object constructInstance(Class<?> type, Object... constructorArguments) { return org.springframework.data.util.ReflectionUtils.findConstructor(type, constructorArguments) .map(constructor -> BeanUtils.instantiateClass(constructor, constructorArguments)) .orElseThrow(() -> newIllegalArgumentException( "No suitable constructor was found for type [%s] having parameters [%s]", type.getName(), stream(nullSafeArray(constructorArguments, Object.class)) .map(ObjectUtils::nullSafeClassName) .collect(Collectors.toList()))); }
/** * Sets the given field to the given value if present. * @param field * @param value */ private <S> S setField(Optional<Field> field, S value) { field.ifPresent(it -> ReflectionUtils.setField(it, target, value)); return value; }
/** * Creates a new {@link AnnotationAuditingMetadata} instance for the given type. * * @param type must not be {@literal null}. */ private AnnotationAuditingMetadata(Class<?> type) { Assert.notNull(type, "Given type must not be null!"); this.createdByField = Optional.ofNullable(ReflectionUtils.findField(type, CREATED_BY_FILTER)); this.createdDateField = Optional.ofNullable(ReflectionUtils.findField(type, CREATED_DATE_FILTER)); this.lastModifiedByField = Optional.ofNullable(ReflectionUtils.findField(type, LAST_MODIFIED_BY_FILTER)); this.lastModifiedDateField = Optional.ofNullable(ReflectionUtils.findField(type, LAST_MODIFIED_DATE_FILTER)); assertValidDateFieldType(createdDateField); assertValidDateFieldType(lastModifiedDateField); }
GraphRepositoryQuery(GraphQueryMethod graphQueryMethod, MetaData metaData, Session session, QueryMethodEvaluationContextProvider evaluationContextProvider) { super(graphQueryMethod, metaData, session); this.evaluationContextProvider = evaluationContextProvider; if (HAS_ENTITY_INSTANTIATOR_FEATURE) { this.entityInstantiator = getEntityInstantiator(metaData, queryMethod.getMappingContext()); this.singleUseEntityMapperUsingInstantiator = ReflectionUtils.findConstructor(SingleUseEntityMapper.class, metaData, entityInstantiator); } else { this.entityInstantiator = null; this.singleUseEntityMapperUsingInstantiator = Optional.empty(); } }
/** * Sets the given field to the given value if the field is not {@literal null}. * * @param field * @param value */ private TemporalAccessor setDateField(Optional<Field> field, TemporalAccessor value) { field.ifPresent(it -> ReflectionUtils.setField(it, target, getDateValueToSet(value, it.getType(), it))); return value; } }
/** * Creates a new {@link AnnotationAuditingMetadata} instance for the given type. * @param type must not be {@literal null}. */ private AnnotationAuditingMetadata(Class<?> type) { Assert.notNull(type, "Given type must not be null!"); this.createdByField = Optional .ofNullable(ReflectionUtils.findField(type, CREATED_BY_FILTER)); this.createdDateField = Optional .ofNullable(ReflectionUtils.findField(type, CREATED_DATE_FILTER)); this.lastModifiedByField = Optional .ofNullable(ReflectionUtils.findField(type, LAST_MODIFIED_BY_FILTER)); this.lastModifiedDateField = Optional .ofNullable(ReflectionUtils.findField(type, LAST_MODIFIED_DATE_FILTER)); assertValidDateFieldType(createdDateField); assertValidDateFieldType(lastModifiedDateField); }
/** * Sets the given field to the given value if the field is not {@literal null}. * @param field * @param value */ private TemporalAccessor setDateField(Optional<Field> field, TemporalAccessor value) { field.ifPresent(it -> ReflectionUtils.setField(it, target, getDateValueToSet(value, it.getType(), it))); return value; }
public void purperSequence(MongoTemplate template, String collectionName, Object entity) { Optional<Field> noField = Optional.ofNullable(ReflectionUtils.findField(entity.getClass(), IDNO_FILTER)); noField.ifPresent(f -> { try { String fieldName = noField.get().getName(); Long increment = generate(template, collectionName, fieldName, 1l); f.setAccessible(true); f.set(entity, increment); } catch (Exception e) { e.printStackTrace(); } }); }