Search in sources :

Example 1 with MissingTypeException

use of com.github.havardh.javaflow.exceptions.MissingTypeException 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);
    }
}
Also used : Objects.isObject(com.github.havardh.javaflow.phases.writer.flow.converter.definitions.Objects.isObject) Lists.concat(com.github.havardh.javaflow.util.Lists.concat) CanonicalName(com.github.havardh.javaflow.model.CanonicalName) Set(java.util.Set) HashMap(java.util.HashMap) Field(com.github.havardh.javaflow.ast.Field) Collections.singletonList(java.util.Collections.singletonList) MissingTypeException(com.github.havardh.javaflow.exceptions.MissingTypeException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Class(com.github.havardh.javaflow.ast.Class) Map(java.util.Map) Primitives.isPrimitive(com.github.havardh.javaflow.phases.writer.flow.converter.definitions.Primitives.isPrimitive) Type(com.github.havardh.javaflow.ast.Type) TypeMap(com.github.havardh.javaflow.model.TypeMap) Collectors.toSet(java.util.stream.Collectors.toSet) Type(com.github.havardh.javaflow.ast.Type) MissingTypeException(com.github.havardh.javaflow.exceptions.MissingTypeException) HashMap(java.util.HashMap) Collections.singletonList(java.util.Collections.singletonList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Class(com.github.havardh.javaflow.ast.Class) CanonicalName(com.github.havardh.javaflow.model.CanonicalName)

Example 2 with MissingTypeException

use of com.github.havardh.javaflow.exceptions.MissingTypeException 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)));
}
Also used : Field(com.github.havardh.javaflow.ast.Field) MissingTypeException(com.github.havardh.javaflow.exceptions.MissingTypeException) Type(com.github.havardh.javaflow.ast.Type) Class(com.github.havardh.javaflow.ast.Class) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

Class (com.github.havardh.javaflow.ast.Class)2 Field (com.github.havardh.javaflow.ast.Field)2 Type (com.github.havardh.javaflow.ast.Type)2 MissingTypeException (com.github.havardh.javaflow.exceptions.MissingTypeException)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 CanonicalName (com.github.havardh.javaflow.model.CanonicalName)1 TypeMap (com.github.havardh.javaflow.model.TypeMap)1 Objects.isObject (com.github.havardh.javaflow.phases.writer.flow.converter.definitions.Objects.isObject)1 Primitives.isPrimitive (com.github.havardh.javaflow.phases.writer.flow.converter.definitions.Primitives.isPrimitive)1 Lists.concat (com.github.havardh.javaflow.util.Lists.concat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Test (org.junit.jupiter.api.Test)1