private Collection<Field> findOptions(Matcher<String> optionNameMatcher) { final Collection<Field> matchFields = new ArrayList<Field>(); for (final Field optionField : FieldUtils.getAllFields(GlobalOptions.class)) { if (!optionField.isAnnotationPresent(Option.class)) { continue; } final Option optionAnnotation = optionField.getAnnotation(Option.class); if (optionAnnotation != null && !optionNameMatcher.matching(optionAnnotation.name())) { continue; } matchFields.add(optionField); } return matchFields; }
private void appendFields(final Class<?> clazz) { for (final Field field : FieldUtils.getAllFields(clazz)) { if (accept(field)) { try { diffBuilder.append(field.getName(), readField(field, left, true), readField(field, right, true)); } catch (final IllegalAccessException ex) { //this can't happen. Would get a Security exception instead //throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } } }
@Test public void testGetAllFields() { assertArrayEquals(new Field[0], FieldUtils.getAllFields(Object.class)); final Field[] fieldsNumber = Number.class.getDeclaredFields(); assertArrayEquals(fieldsNumber, FieldUtils.getAllFields(Number.class)); final Field[] fieldsInteger = Integer.class.getDeclaredFields(); assertArrayEquals(ArrayUtils.addAll(fieldsInteger, fieldsNumber), FieldUtils.getAllFields(Integer.class)); final Field[] allFields = FieldUtils.getAllFields(PublicChild.class); // Under Jacoco,0.8.1 and Java 10, the field count is 7. int expected = 5; for (Field field : allFields) { if (field.getName().equals(JACOCO_DATA_FIELD_NAME)) { expected++; } } assertEquals(Arrays.toString(allFields), expected, allFields.length); }
@Override public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException { super.setColumnMapping(new String[FieldUtils.getAllFields(bean.getClass()).length]); final int numColumns = findMaxFieldIndex(); if (!isAnnotationDriven() || numColumns == -1) { return super.generateHeader(bean); } String[] header = new String[numColumns + 1]; BeanField<T> beanField; for (int i = 0; i <= numColumns; i++) { beanField = findField(i); String columnHeaderName = extractHeaderName(beanField); header[i] = columnHeaderName; } return header; }
@Override public Model resolve(JavaType type, ModelConverterContext context, Iterator<ModelConverter> chain) { Model model = super.resolve(type, context, chain); // If there are no @ApiModelPropety exclusions configured, return the untouched model if (apiModelPropertyAccessExclusions == null || apiModelPropertyAccessExclusions.isEmpty()) { return model; } Class<?> cls = type.getRawClass(); for (Method method : cls.getDeclaredMethods()) { ApiModelProperty apiModelPropertyAnnotation = AnnotationUtils.findAnnotation(method, ApiModelProperty.class); processProperty(apiModelPropertyAnnotation, model); } for (Field field : FieldUtils.getAllFields(cls)) { ApiModelProperty apiModelPropertyAnnotation = AnnotationUtils.getAnnotation(field, ApiModelProperty.class); processProperty(apiModelPropertyAnnotation, model); } return model; }
private List<Field> loadFields(Class<? extends T> cls) { List<Field> fields = new LinkedList<>(); for (Field field : FieldUtils.getAllFields(cls)) { if (field.isAnnotationPresent(CsvBindByPosition.class) || field.isAnnotationPresent(CsvCustomBindByPosition.class) || field.isAnnotationPresent(CsvBindAndJoinByPosition.class) || field.isAnnotationPresent(CsvBindAndSplitByPosition.class)) { fields.add(field); } } setAnnotationDriven(!fields.isEmpty()); return fields; }
private List<Field> loadFields(Class<? extends T> cls) { List<Field> fields = new LinkedList<>(); for (Field field : FieldUtils.getAllFields(cls)) { if (field.isAnnotationPresent(CsvBindByName.class) || field.isAnnotationPresent(CsvCustomBindByName.class) || field.isAnnotationPresent(CsvBindAndSplitByName.class) || field.isAnnotationPresent(CsvBindAndJoinByName.class)) { fields.add(field); } } setAnnotationDriven(!fields.isEmpty()); return fields; }
@Override @SuppressWarnings("unchecked") public Object resolve(final InjectionPoint injectionPoint) { final Type type = injectionPoint.getType(); final Set<Field> candidates = stream(getAllFields(testInstance.getClass())) .filter(field -> field.getAnnotation(ResourceDef.class) != null) .filter(field -> ((Class)type).isAssignableFrom(field.getType())) .collect(toSet()); if (candidates.isEmpty()) { return null; } if (candidates.size() > 1) { throw new TestEEfiException("Ambiguous fields for injection point " + injectionPoint + " found: " + candidates); } final Field field = candidates.iterator().next(); field.setAccessible(true); try { return field.get(testInstance); } catch (final IllegalAccessException e) { throw new TestEEfiException("Failed to retrieve @ResourceDef field value from " + field, e); } }
private void appendFields(final Class<?> clazz) { for (final Field field : FieldUtils.getAllFields(clazz)) { if (accept(field)) { try { diffBuilder.append(field.getName(), readField(field, left, true), readField(field, right, true)); } catch (final IllegalAccessException ex) { //this can't happen. Would get a Security exception instead //throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } } }
private void appendFields(final Class<?> clazz) { for (final Field field : FieldUtils.getAllFields(clazz)) { if (accept(field)) { try { diffBuilder.append(field.getName(), readField(field, left, true), readField(field, right, true)); } catch (final IllegalAccessException ex) { //this can't happen. Would get a Security exception instead //throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } } }
private void appendFields(final Class<?> clazz) { for (final Field field : FieldUtils.getAllFields(clazz)) { if (accept(field)) { try { diffBuilder.append(field.getName(), readField(field, left, true), readField(field, right, true)); } catch (final IllegalAccessException ex) { //this can't happen. Would get a Security exception instead //throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } } }
private Iterable<? extends Field> getEligibleFields(Class<? extends SqoopOptions> clazz) { Field[] allFields = FieldUtils.getAllFields(clazz); List<Field> eligibleFields = new ArrayList<>(); for(Field field : allFields) { if(!field.getType().isPrimitive() && !excludedClassesFromClone.contains(field.getType()) && !excludedFieldsFromClone.contains(field.getName()) && !Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers())) { // final and static fields are expected to be the same field.setAccessible(true); eligibleFields.add(field); } } return eligibleFields; }
@Override public void write(@NonNull Object domain, @NonNull MappingGremlinConverter converter, @NonNull GremlinSource source) { if (!(source instanceof GremlinSourceGraph)) { throw new GremlinUnexpectedSourceTypeException("should be the instance of GremlinSourceEdge"); } final GremlinSourceGraph sourceGraph = (GremlinSourceGraph) source; final GremlinPersistentEntity<?> persistentEntity = converter.getPersistentEntity(domain.getClass()); final ConvertingPropertyAccessor accessor = converter.getPropertyAccessor(domain); for (final Field field : FieldUtils.getAllFields(domain.getClass())) { final PersistentProperty property = persistentEntity.getPersistentProperty(field.getName()); Assert.notNull(property, "persistence property should not be null"); if (field.getName().equals(Constants.PROPERTY_ID) || field.getAnnotation(Id.class) != null) { continue; } @SuppressWarnings("unchecked") final List<Object> objects = (List<Object>) accessor.getProperty(property); if (field.getAnnotation(VertexSet.class) != null || field.getAnnotation(EdgeSet.class) != null) { this.writeGraphSet(objects, converter, sourceGraph); } } } }
@Override public void write(@NonNull Object domain, @NonNull MappingGremlinConverter converter, @NonNull GremlinSource source) { if (!(source instanceof GremlinSourceVertex)) { throw new GremlinUnexpectedSourceTypeException("should be the instance of GremlinSourceVertex"); } source.setId(converter.getIdFieldValue(domain)); final GremlinPersistentEntity<?> persistentEntity = converter.getPersistentEntity(domain.getClass()); final ConvertingPropertyAccessor accessor = converter.getPropertyAccessor(domain); for (final Field field : FieldUtils.getAllFields(domain.getClass())) { final PersistentProperty property = persistentEntity.getPersistentProperty(field.getName()); Assert.notNull(property, "persistence property should not be null"); if (field.getName().equals(PROPERTY_ID) || field.getAnnotation(Id.class) != null) { continue; } else if (field.getName().equals(GREMLIN_PROPERTY_CLASSNAME)) { throw new GremlinEntityInformationException("Domain Cannot use pre-defined field name: " + GREMLIN_PROPERTY_CLASSNAME); } source.setProperty(field.getName(), accessor.getProperty(property)); } } }
@Override public void write(@NonNull Object domain, @NonNull MappingGremlinConverter converter, @NonNull GremlinSource source) { if (!(source instanceof GremlinSourceVertex)) { throw new GremlinUnexpectedSourceTypeException("should be the instance of GremlinSourceVertex"); } source.setId(converter.getIdFieldValue(domain)); final GremlinPersistentEntity<?> persistentEntity = converter.getPersistentEntity(domain.getClass()); final ConvertingPropertyAccessor accessor = converter.getPropertyAccessor(domain); for (final Field field : FieldUtils.getAllFields(domain.getClass())) { final PersistentProperty property = persistentEntity.getPersistentProperty(field.getName()); Assert.notNull(property, "persistence property should not be null"); if (field.getName().equals(PROPERTY_ID) || field.getAnnotation(Id.class) != null) { continue; } else if (field.getName().equals(GREMLIN_PROPERTY_CLASSNAME)) { throw new GremlinEntityInformationException("Domain Cannot use pre-defined field name: " + GREMLIN_PROPERTY_CLASSNAME); } source.setProperty(field.getName(), accessor.getProperty(property)); } } }
@Override public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException { super.setColumnMapping(new String[FieldUtils.getAllFields(bean.getClass()).length]); final int numColumns = findMaxFieldIndex(); if (!isAnnotationDriven() || numColumns == -1) { return super.generateHeader(bean); } String[] header = new String[numColumns + 1]; BeanField<T> beanField; for (int i = 0; i <= numColumns; i++) { beanField = findField(i); String columnHeaderName = extractHeaderName(beanField); header[i] = columnHeaderName; } return header; }
@Override public <T extends Object> T read(@NonNull Class<T> domainClass, @NonNull MappingGremlinConverter converter, @NonNull GremlinSource<T> source) { if (!(source instanceof GremlinSourceVertex)) { throw new GremlinUnexpectedSourceTypeException("should be instance of GremlinSourceVertex"); } final T domain = GremlinUtils.createInstance(domainClass); final ConvertingPropertyAccessor accessor = converter.getPropertyAccessor(domain); final GremlinPersistentEntity persistentEntity = converter.getPersistentEntity(domainClass); for (final Field field : FieldUtils.getAllFields(domainClass)) { final PersistentProperty property = persistentEntity.getPersistentProperty(field.getName()); Assert.notNull(property, "persistence property should not be null"); if (field.getName().equals(Constants.PROPERTY_ID) || field.getAnnotation(Id.class) != null) { accessor.setProperty(property, super.getGremlinSourceId(source)); } else { final Object value = super.readProperty(property, source.getProperties().get(field.getName())); accessor.setProperty(property, value); } } return domain; } }
@Override public <T extends Object> T read(@NonNull Class<T> domainClass, @NonNull MappingGremlinConverter converter, @NonNull GremlinSource<T> source) { if (!(source instanceof GremlinSourceVertex)) { throw new GremlinUnexpectedSourceTypeException("should be instance of GremlinSourceVertex"); } final T domain = GremlinUtils.createInstance(domainClass); final ConvertingPropertyAccessor accessor = converter.getPropertyAccessor(domain); final GremlinPersistentEntity persistentEntity = converter.getPersistentEntity(domainClass); for (final Field field : FieldUtils.getAllFields(domainClass)) { final PersistentProperty property = persistentEntity.getPersistentProperty(field.getName()); Assert.notNull(property, "persistence property should not be null"); if (field.getName().equals(Constants.PROPERTY_ID) || field.getAnnotation(Id.class) != null) { accessor.setProperty(property, super.getGremlinSourceId(source)); } else { final Object value = super.readProperty(property, source.getProperties().get(field.getName())); accessor.setProperty(property, value); } } return domain; } }
@Override public <T> T read(@NonNull Class<T> type, @NonNull MappingGremlinConverter converter, @NonNull GremlinSource<T> source) { if (!(source instanceof GremlinSourceGraph)) { throw new GremlinUnexpectedSourceTypeException("Should be instance of GremlinSourceGraph"); } final GremlinSourceGraph<T> graphSource = (GremlinSourceGraph<T>) source; final T entity = GremlinUtils.createInstance(type); final ConvertingPropertyAccessor accessor = converter.getPropertyAccessor(entity); final GremlinPersistentEntity persistentEntity = converter.getPersistentEntity(type); for (final Field field : FieldUtils.getAllFields(type)) { final PersistentProperty property = persistentEntity.getPersistentProperty(field.getName()); Assert.notNull(property, "persistence property should not be null"); if ((field.getName().equals(PROPERTY_ID) || field.getAnnotation(Id.class) != null)) { accessor.setProperty(property, super.getGremlinSourceId(graphSource)); } else if (field.isAnnotationPresent(VertexSet.class)) { accessor.setProperty(property, readEntitySet(graphSource.getVertexSet(), converter)); } else if (field.isAnnotationPresent(EdgeSet.class)) { accessor.setProperty(property, readEntitySet(graphSource.getEdgeSet(), converter)); } } return entity; }
private SqoopOptions createSqoopOptionsFilledWithRandomData() throws Exception { SqoopOptions options; options = new SqoopOptions(); options.setMapColumnJava(COLUMN_MAPPING); options.getColumnNames(); // initializes the mapReplacedColumnJava field, which is cloned but is otherwise inaccessible Field[] allFields = FieldUtils.getAllFields(options.getClass()); for (Field field: allFields) { setFieldToValueIfApplicable(options, field); } return options; }