|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.apt; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import javax.annotation.processing.Messager; |
| 12 | +import javax.annotation.processing.ProcessingEnvironment; |
| 13 | +import javax.lang.model.element.*; |
| 14 | +import javax.tools.Diagnostic; |
| 15 | + |
| 16 | +import io.jooby.internal.apt.Annotations; |
| 17 | +import io.jooby.internal.apt.Opts; |
| 18 | + |
| 19 | +public class MvcContext { |
| 20 | + private final ProcessingEnvironment processingEnvironment; |
| 21 | + private final boolean debug; |
| 22 | + private final boolean incremental; |
| 23 | + private final boolean services; |
| 24 | + private int round; |
| 25 | + private final Messager messager; |
| 26 | + private final Map<Object, Object> attributes = new HashMap<>(); |
| 27 | + |
| 28 | + public MvcContext(ProcessingEnvironment processingEnvironment, Messager messager) { |
| 29 | + this.processingEnvironment = processingEnvironment; |
| 30 | + this.messager = messager; |
| 31 | + this.debug = Opts.boolOpt(processingEnvironment, Opts.OPT_DEBUG, false); |
| 32 | + this.incremental = Opts.boolOpt(processingEnvironment, Opts.OPT_INCREMENTAL, true); |
| 33 | + this.services = Opts.boolOpt(processingEnvironment, Opts.OPT_SERVICES, true); |
| 34 | + |
| 35 | + debug("Incremental annotation processing is turned %s.", incremental ? "ON" : "OFF"); |
| 36 | + debug("Generation of service provider configuration is turned %s.", services ? "ON" : "OFF"); |
| 37 | + } |
| 38 | + |
| 39 | + public ProcessingEnvironment getProcessingEnvironment() { |
| 40 | + return processingEnvironment; |
| 41 | + } |
| 42 | + |
| 43 | + public Map<Object, Object> getAttributes() { |
| 44 | + return attributes; |
| 45 | + } |
| 46 | + |
| 47 | + public boolean isHttpMethod(TypeElement annotated) { |
| 48 | + if (annotated == null) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + return Annotations.HTTP_METHODS.contains(annotated.toString()) |
| 52 | + || Annotations.HTTP_METHODS.contains(annotated.asType().toString()); |
| 53 | + } |
| 54 | + |
| 55 | + public List<String> path(TypeElement owner, ExecutableElement exec, TypeElement annotation) { |
| 56 | + var prefix = Collections.<String>emptyList(); |
| 57 | + // Look at parent @path annotation |
| 58 | + var superTypes = superTypes(owner); |
| 59 | + var i = 0; |
| 60 | + while (prefix.isEmpty() && i < superTypes.size()) { |
| 61 | + prefix = path(superTypes.get(i++)); |
| 62 | + } |
| 63 | + |
| 64 | + // Favor GET("/path") over Path("/path") at method level |
| 65 | + var path = path(annotation.getQualifiedName().toString(), annotation.getAnnotationMirrors()); |
| 66 | + if (path.isEmpty()) { |
| 67 | + path = path(annotation.getQualifiedName().toString(), exec.getAnnotationMirrors()); |
| 68 | + } |
| 69 | + var methodPath = path; |
| 70 | + if (prefix.isEmpty()) { |
| 71 | + return path.isEmpty() ? Collections.singletonList("/") : path; |
| 72 | + } |
| 73 | + if (path.isEmpty()) { |
| 74 | + return prefix; |
| 75 | + } |
| 76 | + return prefix.stream() |
| 77 | + .flatMap(root -> methodPath.stream().map(p -> root.equals("/") ? p : root + p)) |
| 78 | + .distinct() |
| 79 | + .toList(); |
| 80 | + } |
| 81 | + |
| 82 | + private List<String> path(Element element) { |
| 83 | + return path(null, element.getAnnotationMirrors()); |
| 84 | + } |
| 85 | + |
| 86 | + private List<String> path(String method, List<? extends AnnotationMirror> annotations) { |
| 87 | + return annotations.stream() |
| 88 | + .map(AnnotationMirror.class::cast) |
| 89 | + .flatMap( |
| 90 | + mirror -> { |
| 91 | + String type = mirror.getAnnotationType().toString(); |
| 92 | + if (Annotations.PATH.contains(type) || type.equals(method)) { |
| 93 | + return Stream.concat( |
| 94 | + Annotations.attribute(mirror, "path").stream(), |
| 95 | + Annotations.attribute(mirror, "value").stream()); |
| 96 | + } |
| 97 | + return Stream.empty(); |
| 98 | + }) |
| 99 | + .distinct() |
| 100 | + .toList(); |
| 101 | + } |
| 102 | + |
| 103 | + public List<TypeElement> superTypes(Element owner) { |
| 104 | + var typeUtils = processingEnvironment.getTypeUtils(); |
| 105 | + var supertypes = typeUtils.directSupertypes(owner.asType()); |
| 106 | + List<TypeElement> result = new ArrayList<>(); |
| 107 | + result.add((TypeElement) owner); |
| 108 | + if (supertypes == null || supertypes.isEmpty()) { |
| 109 | + return result; |
| 110 | + } |
| 111 | + var supertype = supertypes.get(0); |
| 112 | + var supertypeName = typeUtils.erasure(supertype).toString(); |
| 113 | + var supertypeElement = typeUtils.asElement(supertype); |
| 114 | + if (!Object.class.getName().equals(supertypeName) |
| 115 | + && supertypeElement.getKind() == ElementKind.CLASS) { |
| 116 | + result.addAll(superTypes(supertypeElement)); |
| 117 | + } |
| 118 | + return result; |
| 119 | + } |
| 120 | + |
| 121 | + public boolean generateServices() { |
| 122 | + return services; |
| 123 | + } |
| 124 | + |
| 125 | + public boolean isIncremental() { |
| 126 | + return incremental; |
| 127 | + } |
| 128 | + |
| 129 | + public boolean isServices() { |
| 130 | + return services; |
| 131 | + } |
| 132 | + |
| 133 | + public int nextRound() { |
| 134 | + return ++round; |
| 135 | + } |
| 136 | + |
| 137 | + public void debug(String message, Object... args) { |
| 138 | + if (debug) { |
| 139 | + print(Diagnostic.Kind.NOTE, message, args); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public void error(String message, Object... args) { |
| 144 | + print(Diagnostic.Kind.ERROR, message, args); |
| 145 | + } |
| 146 | + |
| 147 | + public void warning(String message, Object... args) { |
| 148 | + print(Diagnostic.Kind.WARNING, message, args); |
| 149 | + } |
| 150 | + |
| 151 | + public void info(String message, Object... args) { |
| 152 | + print(Diagnostic.Kind.NOTE, message, args); |
| 153 | + } |
| 154 | + |
| 155 | + private void print(Diagnostic.Kind kind, String message, Object... args) { |
| 156 | + var msg = message.formatted(args); |
| 157 | + Element element = |
| 158 | + args.length > 0 |
| 159 | + ? (args[args.length - 1] instanceof Element) ? (Element) args[args.length - 1] : null |
| 160 | + : null; |
| 161 | + messager.printMessage(kind, msg, element); |
| 162 | + } |
| 163 | +} |
0 commit comments