|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Tada AB and other contributors, as listed below. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the The BSD 3-Clause License |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://opensource.org/licenses/BSD-3-Clause |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Chapman Flack |
| 11 | + */ |
| 12 | +package org.postgresql.pljava.annotation; |
| 13 | + |
| 14 | +import java.lang.annotation.Documented; |
| 15 | +import java.lang.annotation.ElementType; |
| 16 | +import java.lang.annotation.Repeatable; |
| 17 | +import java.lang.annotation.Retention; |
| 18 | +import java.lang.annotation.RetentionPolicy; |
| 19 | +import java.lang.annotation.Target; |
| 20 | + |
| 21 | +/** |
| 22 | + * Declares a PostgreSQL {@code CAST}. |
| 23 | + *<p> |
| 24 | + * May annotate a Java method (which should also carry a |
| 25 | + * {@link Function @Function} annotation, making it a PostgreSQL function), |
| 26 | + * or a class or interface (just to have a place to put it when not directly |
| 27 | + * associated with a method). |
| 28 | + *<p> |
| 29 | + * If not applied to a method, must supply {@code path=} specifying |
| 30 | + * {@code BINARY} or {@code INOUT}. |
| 31 | + *<p> |
| 32 | + * The source and target types must be specified with {@code from} and |
| 33 | + * {@code to}, unless the annotation appears on a method, in which case these |
| 34 | + * default to the first parameter and return types of the function, |
| 35 | + * respectively. |
| 36 | + *<p> |
| 37 | + * The cast will, by default, have to be applied explicitly, unless |
| 38 | + * {@code application=ASSIGNMENT} or {@code application=IMPLICIT} is given. |
| 39 | + * |
| 40 | + * @author Chapman Flack |
| 41 | + */ |
| 42 | +@Documented |
| 43 | +@Target({ElementType.METHOD, ElementType.TYPE}) |
| 44 | +@Repeatable(Cast.Container.class) |
| 45 | +@Retention(RetentionPolicy.CLASS) |
| 46 | +public @interface Cast |
| 47 | +{ |
| 48 | + /** |
| 49 | + * When this cast can be applied: only in explicit form, when used in |
| 50 | + * assignment context, or implicitly whenever needed. |
| 51 | + */ |
| 52 | + enum Application { EXPLICIT, ASSIGNMENT, IMPLICIT }; |
| 53 | + |
| 54 | + /** |
| 55 | + * A known conversion path when a dedicated function is not supplied: |
| 56 | + * {@code BINARY} for two types that are known to have the same internal |
| 57 | + * representation, or {@code INOUT} to invoke the first type's text-output |
| 58 | + * function followed by the second type's text-input function. |
| 59 | + */ |
| 60 | + enum Path { BINARY, INOUT }; |
| 61 | + |
| 62 | + /** |
| 63 | + * The source type to be cast. Will default to the first parameter type of |
| 64 | + * the associated function, if known. |
| 65 | + *<p> |
| 66 | + * PostgreSQL will allow this type and the function's first parameter type |
| 67 | + * to differ, if there is an existing binary cast between them. That cannot |
| 68 | + * be checked at compile time, so a cast with a different type given here |
| 69 | + * might successfully compile but fail to deploy in PostgreSQL. |
| 70 | + */ |
| 71 | + String from() default ""; |
| 72 | + |
| 73 | + /** |
| 74 | + * The target type to cast to. Will default to the return type of |
| 75 | + * the associated function, if known. |
| 76 | + *<p> |
| 77 | + * PostgreSQL will allow this type and the function's return type |
| 78 | + * to differ, if there is an existing binary cast between them. That cannot |
| 79 | + * be checked at compile time, so a cast with a different type given here |
| 80 | + * might successfully compile but fail to deploy in PostgreSQL. |
| 81 | + */ |
| 82 | + String to() default ""; |
| 83 | + |
| 84 | + /** |
| 85 | + * A stock conversion path when a dedicated function is not supplied: |
| 86 | + * {@code BINARY} for two types that are known to have the same internal |
| 87 | + * representation, or {@code INOUT} to invoke the first type's text-output |
| 88 | + * function followed by the second type's text-input function. |
| 89 | + *<p> |
| 90 | + * To declare an {@code INOUT} cast, {@code with=INOUT} must appear |
| 91 | + * explicitly; the default value is treated as unspecified. |
| 92 | + */ |
| 93 | + Path path() default Path.INOUT; |
| 94 | + |
| 95 | + /** |
| 96 | + * When this cast can be applied: only in explicit form, when used in |
| 97 | + * assignment context, or implicitly whenever needed. |
| 98 | + */ |
| 99 | + Application application() default Application.EXPLICIT; |
| 100 | + |
| 101 | + /** |
| 102 | + * One or more arbitrary labels that will be considered 'provided' by the |
| 103 | + * object carrying this annotation. The deployment descriptor will be |
| 104 | + * generated in such an order that other objects that 'require' labels |
| 105 | + * 'provided' by this come later in the output for install actions, and |
| 106 | + * earlier for remove actions. |
| 107 | + */ |
| 108 | + String[] provides() default {}; |
| 109 | + |
| 110 | + /** |
| 111 | + * One or more arbitrary labels that will be considered 'required' by the |
| 112 | + * object carrying this annotation. The deployment descriptor will be |
| 113 | + * generated in such an order that other objects that 'provide' labels |
| 114 | + * 'required' by this come earlier in the output for install actions, and |
| 115 | + * later for remove actions. |
| 116 | + */ |
| 117 | + String[] requires() default {}; |
| 118 | + |
| 119 | + /** |
| 120 | + * The {@code <implementor name>} to be used around SQL code generated |
| 121 | + * for this cast. Defaults to {@code PostgreSQL}. Set explicitly to |
| 122 | + * {@code ""} to emit code not wrapped in an {@code <implementor block>}. |
| 123 | + */ |
| 124 | + String implementor() default ""; |
| 125 | + |
| 126 | + /** |
| 127 | + * A comment to be associated with the cast. If left to default, and the |
| 128 | + * annotated Java construct has a doc comment, its first sentence will be |
| 129 | + * used. If an empty string is explicitly given, no comment will be set. |
| 130 | + */ |
| 131 | + String comment() default ""; |
| 132 | + |
| 133 | + /** |
| 134 | + * @hidden container type allowing Cast to be repeatable. |
| 135 | + */ |
| 136 | + @Documented |
| 137 | + @Target({ElementType.METHOD, ElementType.TYPE}) |
| 138 | + @Retention(RetentionPolicy.CLASS) |
| 139 | + @interface Container |
| 140 | + { |
| 141 | + Cast[] value(); |
| 142 | + } |
| 143 | +} |
0 commit comments