use of com.github.havardh.javaflow.ast.Field in project javaflow by havardh.
the class MemberFieldsPresentVerifier method verify.
/**
* Verifies that there are no missing types referenced
* in the list of {@code Type}.
*
* A missing type is found when a type is referenced which is not
* in the given set of types of in the {@code TypeMap} of the verifier
* If a any missing types are discovered a {@code MissingTypeException}
* is thrown containing the complete list of missing types.
*
* @param types list of {@code Type} to verify
* @throws MissingTypeException when a missing type is found
*/
@Override
public void verify(List<Type> types) {
Set<CanonicalName> nameSet = types.stream().map(Type::getCanonicalName).collect(toSet());
Map<Type, List<Field>> missingTypes = new HashMap<>();
for (Type type : types.stream().filter(t -> t instanceof Class).collect(toList())) {
((Class) type).getFields().stream().filter(field -> !nameSet.contains(field.getType().getCanonicalName())).filter(field -> !isObject(field.getType().toString())).filter(field -> !isPrimitive(field.getType().toString())).filter(field -> !customTypes.containsKey(field.getType().toString())).forEach(field -> missingTypes.compute(type, (ignored, fields) -> fields == null ? singletonList(field) : concat(fields, singletonList(field))));
}
if (!missingTypes.isEmpty()) {
throw new MissingTypeException(missingTypes);
}
}
use of com.github.havardh.javaflow.ast.Field in project javaflow by havardh.
the class ClassWriter method writeFields.
private void writeFields(Class aClass, java.io.Writer writer) throws IOException {
if (aClass.getFields().size() > 0) {
writer.write("\n");
}
for (Field field : aClass.getFields()) {
writer.write(" ");
fieldWriter.write(field, writer);
writer.write(",\n");
}
}
use of com.github.havardh.javaflow.ast.Field in project javaflow by havardh.
the class ClassGetterNamingVerifier method validate.
private List<Exception> validate(Class classToValidate) {
List<Method> getters = classToValidate.getGetters();
List<Field> fields = classToValidate.getFields();
if (getters.size() != fields.size()) {
return singletonList(new FieldGettersMismatchException(classToValidate.getCanonicalName(), format("Number of getters and fields is not the same.\n" + "Fields in model: %s\n" + "Getters in model: %s", fields, getters)));
}
List<Exception> exceptions = new ArrayList<>();
for (Method getter : getters) {
try {
Field correspondingField = findFieldByGetter(classToValidate, fields, getter);
if (!correspondingField.getType().equals(getter.getType())) {
throw new FieldGettersMismatchException(classToValidate.getCanonicalName(), format("Type of getter %s (%s) does not correspond to field %s (%s)", getter.getName(), getter.getType(), correspondingField.getName(), correspondingField.getType()));
}
} catch (FieldGettersMismatchException e) {
exceptions.add(e);
}
}
return exceptions;
}
use of com.github.havardh.javaflow.ast.Field in project javaflow by havardh.
the class MemberFieldsPresentVerifierTest method shouldFailTypeWithMissingMember.
@Test
public void shouldFailTypeWithMissingMember() {
Field field = fieldBuilder().withName("name").withType(TypeBuilder.type().withName(CanonicalName.fromString("com.github.havardh.Type")).build()).build();
Class aClass = ClassBuilder.classBuilder().withName("Test1").withField(field).build();
MissingTypeException exception = assertThrows(MissingTypeException.class, () -> verifier.verify(singletonList(aClass)));
Map<Type, List<Field>> missing = exception.getTypes();
assertThat(missing.get(aClass), is(singletonList(field)));
}
Aggregations