Search in sources :

Example 1 with ClassModel

use of javaslang.match.model.ClassModel in project javaslang by javaslang.

the class PatternsProcessor method generate.

// Expands all @Patterns classes
private void generate(Set<ClassModel> classModels) {
    final Filer filer = processingEnv.getFiler();
    for (ClassModel classModel : classModels) {
        final String derivedClassName = deriveClassName(classModel);
        final String code = Generator.generate(derivedClassName, classModel);
        final String fqn = (classModel.hasDefaultPackage() ? "" : classModel.getPackageName() + ".") + derivedClassName;
        try (final Writer writer = filer.createSourceFile(fqn, classModel.typeElement()).openWriter()) {
            writer.write(code);
        } catch (IOException x) {
            throw new Error("Error writing " + fqn, x);
        }
    }
}
Also used : ClassModel(javaslang.match.model.ClassModel) IOException(java.io.IOException) Filer(javax.annotation.processing.Filer) Writer(java.io.Writer)

Example 2 with ClassModel

use of javaslang.match.model.ClassModel in project javaslang by javaslang.

the class PatternsProcessor method transform.

// Verify correct usage of annotations @Patterns and @Unapply
private Set<ClassModel> transform(Set<TypeElement> typeElements) {
    final Set<ClassModel> classModels = new HashSet<>();
    final javax.lang.model.util.Elements elementUtils = processingEnv.getElementUtils();
    final Messager messager = processingEnv.getMessager();
    for (TypeElement typeElement : typeElements) {
        final ClassModel classModel = ClassModel.of(elementUtils, typeElement);
        final List<MethodModel> methodModels = classModel.getMethods().stream().filter(method -> method.isAnnotatedWith(Unapply.class)).collect(toList());
        if (methodModels.isEmpty()) {
            messager.printMessage(Diagnostic.Kind.WARNING, "No @Unapply methods found.", classModel.typeElement());
        } else {
            final boolean methodsValid = methodModels.stream().reduce(true, (bool, method) -> bool && UnapplyChecker.isValid(method.getExecutableElement(), messager), (b1, b2) -> b1 && b2);
            if (methodsValid) {
                classModels.add(classModel);
            }
        }
    }
    return classModels;
}
Also used : AbstractProcessor(javax.annotation.processing.AbstractProcessor) Patterns(javaslang.match.annotation.Patterns) Set(java.util.Set) TypeElement(javax.lang.model.element.TypeElement) IOException(java.io.IOException) Unapply(javaslang.match.annotation.Unapply) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) SourceVersion(javax.lang.model.SourceVersion) MethodModel(javaslang.match.model.MethodModel) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RoundEnvironment(javax.annotation.processing.RoundEnvironment) Diagnostic(javax.tools.Diagnostic) Filer(javax.annotation.processing.Filer) Writer(java.io.Writer) Collections(java.util.Collections) Generator(javaslang.match.generator.Generator) Messager(javax.annotation.processing.Messager) ClassModel(javaslang.match.model.ClassModel) MethodModel(javaslang.match.model.MethodModel) Messager(javax.annotation.processing.Messager) ClassModel(javaslang.match.model.ClassModel) TypeElement(javax.lang.model.element.TypeElement) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)2 Writer (java.io.Writer)2 ClassModel (javaslang.match.model.ClassModel)2 Filer (javax.annotation.processing.Filer)2 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Patterns (javaslang.match.annotation.Patterns)1 Unapply (javaslang.match.annotation.Unapply)1 Generator (javaslang.match.generator.Generator)1 MethodModel (javaslang.match.model.MethodModel)1 AbstractProcessor (javax.annotation.processing.AbstractProcessor)1 Messager (javax.annotation.processing.Messager)1 RoundEnvironment (javax.annotation.processing.RoundEnvironment)1 SourceVersion (javax.lang.model.SourceVersion)1 TypeElement (javax.lang.model.element.TypeElement)1 Diagnostic (javax.tools.Diagnostic)1