List<NameSpaceMapping> convertToNameSpaceMappings(AnnotationValue nameSpaceMappingAnnotationValue, Element element) { if (nameSpaceMappingAnnotationValue == null) return null; return new SimpleAnnotationValueVisitor8<List<NameSpaceMapping>, Void>() { @Override public List<NameSpaceMapping> visitArray(List<? extends AnnotationValue> vals, Void aVoid) { return vals.stream() .map(x -> { String value = (String) x.getValue(); Optional<NameSpaceMapping> result = splitIntoTwo(value).flatMap(y -> Optional.of(NameSpaceMappingBuilder.of(y.getFirst(), y.getSecond()))); if (!result.isPresent()) env.getMessager().printMessage(Diagnostic.Kind.ERROR, "param not valid. Expecting a name space mapping. Got:" + x, element); return result; }) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); } }.visit(nameSpaceMappingAnnotationValue); }
List<Pattern> convertExclusion(AnnotationValue exclAnnotationValue, Element element) { if (exclAnnotationValue == null) return null; return new SimpleAnnotationValueVisitor8<List<Pattern>, Void>() { @Override public List<Pattern> visitArray(List<? extends AnnotationValue> vals, Void aVoid) { return vals.stream() .map(x -> { String value = (String) x.getValue(); Optional<Pattern> result = compileToPattern(value); if (!result.isPresent()) env.getMessager().printMessage(Diagnostic.Kind.ERROR, "param not valid. Expecting a regular Expression. Got:" + x, element); return result; }) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); } }.visit(exclAnnotationValue); }
Map<String, TSTargetType> convertTypeMapping(Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry) { AnnotationValue customMappingValue = entry.getValue(); Element element = entry.getKey(); if (customMappingValue == null) return new HashMap<>(); List<TSTargetType> targetTypes = new SimpleAnnotationValueVisitor8<List<TSTargetType>, Void>() { @Override public List<TSTargetType> visitArray(List<? extends AnnotationValue> vals, Void aVoid) { return vals.stream() .map(x -> { String value = (String) x.getValue(); Either<String,TSTargetType> result = TSTargetFactory.createTSTargetByDSL(value); if (result.isLeft()) env.getMessager().printMessage(Diagnostic.Kind.ERROR, "param not valid:" + x + "error: " + result.leftValue(), element); return result.toOptional(); }) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); } }.visit(customMappingValue); return targetTypes.stream().filter(Objects::nonNull).collect(Collectors.toMap(TSTargetType::getJavaType, Function.identity())); }
/** * At compile time classes can not be obtained from annotation directly. * This method extracts class information from annotation mirrors. * * @param element element annotated with {@link SqlSource} annotation * @param isReq true for request, false for result parameters * @return list of class names from {@link SqlSource} annotation */ private List<String> getTypesFromAnnotation(Element element, boolean isReq) { TypeMirror sqlSourceTypeMirror = processingEnv.getElementUtils().getTypeElement(SqlSource.class.getName()).asType(); //method names from SqlSource annotation String methodName = isReq ? "reqImpl" : "resImpl"; List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); List<String> typeNames = new ArrayList<>(); for (AnnotationMirror am : annotationMirrors) { if (!am.getAnnotationType().equals(sqlSourceTypeMirror)) { continue; } for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am.getElementValues().entrySet()) { if (!methodName.equals(entry.getKey().getSimpleName().toString())) { continue; } AnnotationValue impResVal = entry.getValue(); impResVal.accept(new SimpleAnnotationValueVisitor8<Void, Void>() { @Override public Void visitArray(List<? extends AnnotationValue> list, Void s) { for (AnnotationValue val : list) { TypeMirror typeMirror = (TypeMirror) val.getValue(); typeNames.add(typeMirror.toString()); } return null; } }, null); break; } } return typeNames; }
public static Boolean toBoolean(AnnotationValue value) { if (value == null) { return null; } return value.accept(new SimpleAnnotationValueVisitor8<Boolean, Void>() { @Override public Boolean visitBoolean(boolean b, Void p) { return b; } }, null); }
public static Integer toInteger(AnnotationValue value) { if (value == null) { return null; } return value.accept(new SimpleAnnotationValueVisitor8<Integer, Void>() { @Override public Integer visitInt(int i, Void p) { return i; } }, null); }
public static Long toLong(AnnotationValue value) { if (value == null) { return null; } return value.accept(new SimpleAnnotationValueVisitor8<Long, Void>() { @Override public Long visitLong(long l, Void p) { return l; } }, null); }
public static String toString(AnnotationValue value) { if (value == null) { return null; } return value.accept(new SimpleAnnotationValueVisitor8<String, Void>() { @Override public String visitString(String s, Void p) { return s; } }, null); }
public static TypeMirror toType(AnnotationValue value) { if (value == null) { return null; } return value.accept( new SimpleAnnotationValueVisitor8<TypeMirror, Void>() { @Override public TypeMirror visitType(TypeMirror t, Void p) { return t; } }, null); }
public static AnnotationMirror toAnnotationMirror(AnnotationValue value) { if (value == null) { return null; } return value.accept( new SimpleAnnotationValueVisitor8<AnnotationMirror, Void>() { @Override public AnnotationMirror visitAnnotation(AnnotationMirror a, Void p) { return a; } }, null); }
public static VariableElement toEnumConstant(AnnotationValue value) { if (value == null) { return null; } return value.accept( new SimpleAnnotationValueVisitor8<VariableElement, Void>() { @Override public VariableElement visitEnumConstant(VariableElement c, Void p) { return c; } }, null); }