Search in sources :

Example 1 with Type

use of com.github.havardh.javaflow.ast.Type in project javaflow by havardh.

the class JavaFlowTest method parseAll.

private static Map<String, Type> parseAll(String... modelNames) {
    FileReader adapter = new FileReader();
    Parser parser = new JavaParser();
    Transformer transformer = new InheritanceTransformer();
    List<Type> types = stream(modelNames).map(name -> BASE_PATH + name + ".java").map(adapter::read).map(Optional::get).map(parser::parse).map(Optional::get).collect(toList());
    transformer.transform(types);
    return types.stream().collect(toMap(type -> type.getCanonicalName().getName(), identity()));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Nested(org.junit.jupiter.api.Nested) Field(com.github.havardh.javaflow.ast.Field) InheritanceTransformer(com.github.havardh.javaflow.phases.transform.InheritanceTransformer) Parent(com.github.havardh.javaflow.ast.Parent) Collectors.toMap(java.util.stream.Collectors.toMap) Parser(com.github.havardh.javaflow.phases.parser.Parser) Class(com.github.havardh.javaflow.ast.Class) Map(java.util.Map) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Type(com.github.havardh.javaflow.ast.Type) Enum(com.github.havardh.javaflow.ast.Enum) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) CanonicalName(com.github.havardh.javaflow.model.CanonicalName) Transformer(com.github.havardh.javaflow.phases.transform.Transformer) Test(org.junit.jupiter.api.Test) JavaParser(com.github.havardh.javaflow.phases.parser.java.JavaParser) JavaFlowConverter(com.github.havardh.javaflow.phases.writer.flow.converter.JavaFlowConverter) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) FileReader(com.github.havardh.javaflow.phases.reader.FileReader) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Arrays.stream(java.util.Arrays.stream) JavaParser(com.github.havardh.javaflow.phases.parser.java.JavaParser) Type(com.github.havardh.javaflow.ast.Type) InheritanceTransformer(com.github.havardh.javaflow.phases.transform.InheritanceTransformer) Transformer(com.github.havardh.javaflow.phases.transform.Transformer) InheritanceTransformer(com.github.havardh.javaflow.phases.transform.InheritanceTransformer) FileReader(com.github.havardh.javaflow.phases.reader.FileReader) Parser(com.github.havardh.javaflow.phases.parser.Parser) JavaParser(com.github.havardh.javaflow.phases.parser.java.JavaParser)

Example 2 with Type

use of com.github.havardh.javaflow.ast.Type 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 3 with Type

use of com.github.havardh.javaflow.ast.Type 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)3 Field (com.github.havardh.javaflow.ast.Field)3 Type (com.github.havardh.javaflow.ast.Type)3 List (java.util.List)3 MissingTypeException (com.github.havardh.javaflow.exceptions.MissingTypeException)2 CanonicalName (com.github.havardh.javaflow.model.CanonicalName)2 Collections.singletonList (java.util.Collections.singletonList)2 Map (java.util.Map)2 Collectors.toList (java.util.stream.Collectors.toList)2 Test (org.junit.jupiter.api.Test)2 Enum (com.github.havardh.javaflow.ast.Enum)1 Parent (com.github.havardh.javaflow.ast.Parent)1 TypeMap (com.github.havardh.javaflow.model.TypeMap)1 Parser (com.github.havardh.javaflow.phases.parser.Parser)1 JavaParser (com.github.havardh.javaflow.phases.parser.java.JavaParser)1 FileReader (com.github.havardh.javaflow.phases.reader.FileReader)1 InheritanceTransformer (com.github.havardh.javaflow.phases.transform.InheritanceTransformer)1 Transformer (com.github.havardh.javaflow.phases.transform.Transformer)1 JavaFlowConverter (com.github.havardh.javaflow.phases.writer.flow.converter.JavaFlowConverter)1 Objects.isObject (com.github.havardh.javaflow.phases.writer.flow.converter.definitions.Objects.isObject)1