Wed, 23 Jan 2013 20:11:07 -0800
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
Reviewed-by: jjg
duke@1 | 1 | /* |
darcy@1522 | 2 | * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package javax.lang.model.util; |
duke@1 | 27 | |
duke@1 | 28 | |
duke@1 | 29 | import java.util.List; |
duke@1 | 30 | import javax.lang.model.element.*; |
duke@1 | 31 | |
duke@1 | 32 | import javax.lang.model.type.TypeMirror; |
duke@1 | 33 | import static javax.lang.model.SourceVersion.*; |
duke@1 | 34 | import javax.lang.model.SourceVersion; |
duke@1 | 35 | import javax.annotation.processing.SupportedSourceVersion; |
duke@1 | 36 | |
duke@1 | 37 | /** |
duke@1 | 38 | * A simple visitor for annotation values with default behavior |
duke@1 | 39 | * appropriate for the {@link SourceVersion#RELEASE_6 RELEASE_6} |
duke@1 | 40 | * source version. Visit methods call {@link |
duke@1 | 41 | * #defaultAction} passing their arguments to {@code defaultAction}'s |
duke@1 | 42 | * corresponding parameters. |
duke@1 | 43 | * |
duke@1 | 44 | * <p> Methods in this class may be overridden subject to their |
duke@1 | 45 | * general contract. Note that annotating methods in concrete |
duke@1 | 46 | * subclasses with {@link java.lang.Override @Override} will help |
duke@1 | 47 | * ensure that methods are overridden as intended. |
duke@1 | 48 | * |
duke@1 | 49 | * <p> <b>WARNING:</b> The {@code AnnotationValueVisitor} interface |
duke@1 | 50 | * implemented by this class may have methods added to it in the |
duke@1 | 51 | * future to accommodate new, currently unknown, language structures |
duke@1 | 52 | * added to future versions of the Java™ programming language. |
duke@1 | 53 | * Therefore, methods whose names begin with {@code "visit"} may be |
duke@1 | 54 | * added to this class in the future; to avoid incompatibilities, |
duke@1 | 55 | * classes which extend this class should not declare any instance |
duke@1 | 56 | * methods with names beginning with {@code "visit"}. |
duke@1 | 57 | * |
duke@1 | 58 | * <p>When such a new visit method is added, the default |
duke@1 | 59 | * implementation in this class will be to call the {@link |
duke@1 | 60 | * #visitUnknown visitUnknown} method. A new simple annotation |
duke@1 | 61 | * value visitor class will also be introduced to correspond to the |
duke@1 | 62 | * new language level; this visitor will have different default |
duke@1 | 63 | * behavior for the visit method in question. When the new visitor is |
duke@1 | 64 | * introduced, all or portions of this visitor may be deprecated. |
duke@1 | 65 | * |
darcy@1522 | 66 | * <p>Note that adding a default implementation of a new visit method |
darcy@1522 | 67 | * in a visitor class will occur instead of adding a <em>default |
darcy@1522 | 68 | * method</em> directly in the visitor interface since a Java SE 8 |
darcy@1522 | 69 | * language feature cannot be used to this version of the API since |
darcy@1522 | 70 | * this version is required to be runnable on Java SE 7 |
darcy@1522 | 71 | * implementations. Future versions of the API that are only required |
darcy@1522 | 72 | * to run on Java SE 8 and later may take advantage of default methods |
darcy@1522 | 73 | * in this situation. |
darcy@1522 | 74 | * |
duke@1 | 75 | * @param <R> the return type of this visitor's methods |
duke@1 | 76 | * @param <P> the type of the additional parameter to this visitor's methods. |
duke@1 | 77 | * |
duke@1 | 78 | * @author Joseph D. Darcy |
duke@1 | 79 | * @author Scott Seligman |
duke@1 | 80 | * @author Peter von der Ahé |
darcy@575 | 81 | * |
darcy@575 | 82 | * @see SimpleAnnotationValueVisitor7 |
darcy@1054 | 83 | * @see SimpleAnnotationValueVisitor8 |
duke@1 | 84 | * @since 1.6 |
duke@1 | 85 | */ |
duke@1 | 86 | @SupportedSourceVersion(RELEASE_6) |
duke@1 | 87 | public class SimpleAnnotationValueVisitor6<R, P> |
duke@1 | 88 | extends AbstractAnnotationValueVisitor6<R, P> { |
duke@1 | 89 | |
duke@1 | 90 | /** |
duke@1 | 91 | * Default value to be returned; {@link #defaultAction |
duke@1 | 92 | * defaultAction} returns this value unless the method is |
duke@1 | 93 | * overridden. |
duke@1 | 94 | */ |
duke@1 | 95 | protected final R DEFAULT_VALUE; |
duke@1 | 96 | |
duke@1 | 97 | /** |
duke@1 | 98 | * Constructor for concrete subclasses; uses {@code null} for the |
duke@1 | 99 | * default value. |
duke@1 | 100 | */ |
duke@1 | 101 | protected SimpleAnnotationValueVisitor6() { |
duke@1 | 102 | super(); |
duke@1 | 103 | DEFAULT_VALUE = null; |
duke@1 | 104 | } |
duke@1 | 105 | |
duke@1 | 106 | /** |
duke@1 | 107 | * Constructor for concrete subclasses; uses the argument for the |
duke@1 | 108 | * default value. |
duke@1 | 109 | * |
duke@1 | 110 | * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} |
duke@1 | 111 | */ |
duke@1 | 112 | protected SimpleAnnotationValueVisitor6(R defaultValue) { |
duke@1 | 113 | super(); |
duke@1 | 114 | DEFAULT_VALUE = defaultValue; |
duke@1 | 115 | } |
duke@1 | 116 | |
duke@1 | 117 | /** |
duke@1 | 118 | * The default action for visit methods. The implementation in |
duke@1 | 119 | * this class just returns {@link #DEFAULT_VALUE}; subclasses will |
duke@1 | 120 | * commonly override this method. |
duke@1 | 121 | * |
duke@1 | 122 | * @param o the value of the annotation |
duke@1 | 123 | * @param p a visitor-specified parameter |
duke@1 | 124 | * @return {@code DEFAULT_VALUE} unless overridden |
duke@1 | 125 | */ |
duke@1 | 126 | protected R defaultAction(Object o, P p) { |
duke@1 | 127 | return DEFAULT_VALUE; |
duke@1 | 128 | } |
duke@1 | 129 | |
duke@1 | 130 | /** |
duke@1 | 131 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 132 | * |
duke@1 | 133 | * @param b {@inheritDoc} |
duke@1 | 134 | * @param p {@inheritDoc} |
duke@1 | 135 | * @return the result of {@code defaultAction} |
duke@1 | 136 | */ |
duke@1 | 137 | public R visitBoolean(boolean b, P p) { |
duke@1 | 138 | return defaultAction(b, p); |
duke@1 | 139 | } |
duke@1 | 140 | |
duke@1 | 141 | /** |
duke@1 | 142 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 143 | * |
duke@1 | 144 | * @param b {@inheritDoc} |
duke@1 | 145 | * @param p {@inheritDoc} |
duke@1 | 146 | * @return the result of {@code defaultAction} |
duke@1 | 147 | */ |
duke@1 | 148 | public R visitByte(byte b, P p) { |
duke@1 | 149 | return defaultAction(b, p); |
duke@1 | 150 | } |
duke@1 | 151 | |
duke@1 | 152 | /** |
duke@1 | 153 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 154 | * |
duke@1 | 155 | * @param c {@inheritDoc} |
duke@1 | 156 | * @param p {@inheritDoc} |
duke@1 | 157 | * @return the result of {@code defaultAction} |
duke@1 | 158 | */ |
duke@1 | 159 | public R visitChar(char c, P p) { |
duke@1 | 160 | return defaultAction(c, p); |
duke@1 | 161 | } |
duke@1 | 162 | |
duke@1 | 163 | /** |
duke@1 | 164 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 165 | * |
duke@1 | 166 | * @param d {@inheritDoc} |
duke@1 | 167 | * @param p {@inheritDoc} |
duke@1 | 168 | * @return the result of {@code defaultAction} |
duke@1 | 169 | */ |
duke@1 | 170 | public R visitDouble(double d, P p) { |
duke@1 | 171 | return defaultAction(d, p); |
duke@1 | 172 | } |
duke@1 | 173 | |
duke@1 | 174 | /** |
duke@1 | 175 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 176 | * |
duke@1 | 177 | * @param f {@inheritDoc} |
duke@1 | 178 | * @param p {@inheritDoc} |
duke@1 | 179 | * @return the result of {@code defaultAction} |
duke@1 | 180 | */ |
duke@1 | 181 | public R visitFloat(float f, P p) { |
duke@1 | 182 | return defaultAction(f, p); |
duke@1 | 183 | } |
duke@1 | 184 | |
duke@1 | 185 | /** |
duke@1 | 186 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 187 | * |
duke@1 | 188 | * @param i {@inheritDoc} |
duke@1 | 189 | * @param p {@inheritDoc} |
duke@1 | 190 | * @return the result of {@code defaultAction} |
duke@1 | 191 | */ |
duke@1 | 192 | public R visitInt(int i, P p) { |
duke@1 | 193 | return defaultAction(i, p); |
duke@1 | 194 | } |
duke@1 | 195 | |
duke@1 | 196 | /** |
duke@1 | 197 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 198 | * |
duke@1 | 199 | * @param i {@inheritDoc} |
duke@1 | 200 | * @param p {@inheritDoc} |
duke@1 | 201 | * @return the result of {@code defaultAction} |
duke@1 | 202 | */ |
duke@1 | 203 | public R visitLong(long i, P p) { |
duke@1 | 204 | return defaultAction(i, p); |
duke@1 | 205 | } |
duke@1 | 206 | |
duke@1 | 207 | /** |
duke@1 | 208 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 209 | * |
duke@1 | 210 | * @param s {@inheritDoc} |
duke@1 | 211 | * @param p {@inheritDoc} |
duke@1 | 212 | * @return the result of {@code defaultAction} |
duke@1 | 213 | */ |
duke@1 | 214 | public R visitShort(short s, P p) { |
duke@1 | 215 | return defaultAction(s, p); |
duke@1 | 216 | } |
duke@1 | 217 | |
duke@1 | 218 | /** |
duke@1 | 219 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 220 | * |
duke@1 | 221 | * @param s {@inheritDoc} |
duke@1 | 222 | * @param p {@inheritDoc} |
duke@1 | 223 | * @return the result of {@code defaultAction} |
duke@1 | 224 | */ |
duke@1 | 225 | public R visitString(String s, P p) { |
duke@1 | 226 | return defaultAction(s, p); |
duke@1 | 227 | } |
duke@1 | 228 | |
duke@1 | 229 | /** |
duke@1 | 230 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 231 | * |
duke@1 | 232 | * @param t {@inheritDoc} |
duke@1 | 233 | * @param p {@inheritDoc} |
duke@1 | 234 | * @return the result of {@code defaultAction} |
duke@1 | 235 | */ |
duke@1 | 236 | public R visitType(TypeMirror t, P p) { |
duke@1 | 237 | return defaultAction(t, p); |
duke@1 | 238 | } |
duke@1 | 239 | |
duke@1 | 240 | /** |
duke@1 | 241 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 242 | * |
duke@1 | 243 | * @param c {@inheritDoc} |
duke@1 | 244 | * @param p {@inheritDoc} |
duke@1 | 245 | * @return the result of {@code defaultAction} |
duke@1 | 246 | */ |
duke@1 | 247 | public R visitEnumConstant(VariableElement c, P p) { |
duke@1 | 248 | return defaultAction(c, p); |
duke@1 | 249 | } |
duke@1 | 250 | |
duke@1 | 251 | /** |
duke@1 | 252 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 253 | * |
duke@1 | 254 | * @param a {@inheritDoc} |
duke@1 | 255 | * @param p {@inheritDoc} |
duke@1 | 256 | * @return the result of {@code defaultAction} |
duke@1 | 257 | */ |
duke@1 | 258 | public R visitAnnotation(AnnotationMirror a, P p) { |
duke@1 | 259 | return defaultAction(a, p); |
duke@1 | 260 | } |
duke@1 | 261 | |
duke@1 | 262 | /** |
duke@1 | 263 | * {@inheritDoc} This implementation calls {@code defaultAction}. |
duke@1 | 264 | * |
duke@1 | 265 | * @param vals {@inheritDoc} |
duke@1 | 266 | * @param p {@inheritDoc} |
duke@1 | 267 | * @return the result of {@code defaultAction} |
duke@1 | 268 | */ |
duke@1 | 269 | public R visitArray(List<? extends AnnotationValue> vals, P p) { |
duke@1 | 270 | return defaultAction(vals, p); |
duke@1 | 271 | } |
duke@1 | 272 | } |