1 /* 2 * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.google.doclava.javadoc; 27 28 import com.google.doclava.annotation.Used; 29 import com.sun.javadoc.AnnotationValue; 30 import java.util.List; 31 import javax.annotation.Nonnull; 32 import javax.annotation.Nullable; 33 import javax.lang.model.element.AnnotationMirror; 34 import javax.lang.model.element.TypeElement; 35 import javax.lang.model.element.VariableElement; 36 import javax.lang.model.type.TypeMirror; 37 import javax.lang.model.util.SimpleAnnotationValueVisitor14; 38 39 class AnnotationValueImpl implements AnnotationValue { 40 41 protected final javax.lang.model.element.AnnotationValue annotationValue; 42 protected final Context context; 43 AnnotationValueImpl(@onnull javax.lang.model.element.AnnotationValue av, Context context)44 protected AnnotationValueImpl(@Nonnull javax.lang.model.element.AnnotationValue av, 45 Context context) { 46 this.context = context; 47 annotationValue = av; 48 } 49 50 public static @Nullable create(@ullable javax.lang.model.element.AnnotationValue av, Context context)51 AnnotationValueImpl create(@Nullable javax.lang.model.element.AnnotationValue av, 52 Context context) { 53 // av could be null if it's a result of ExecutableElement#getDefaultValue() 54 if (av == null) { 55 return null; 56 } 57 return context.caches.annotationValues.computeIfAbsent(av, 58 el -> new AnnotationValueImpl(el, context)); 59 } 60 61 private Object value; 62 63 @Override 64 @Used(implemented = true) value()65 public Object value() { 66 if (value == null) { 67 value = valueVisitor.visit(annotationValue, context); 68 } 69 return value; 70 } 71 72 private static final SimpleAnnotationValueVisitor14<Object, Context> valueVisitor = 73 new SimpleAnnotationValueVisitor14<>() { 74 @Override 75 public Object visitBoolean(boolean b, Context ctx) { 76 return b; 77 } 78 79 @Override 80 public Object visitByte(byte b, Context ctx) { 81 return b; 82 } 83 84 @Override 85 public Object visitChar(char c, Context ctx) { 86 return c; 87 } 88 89 @Override 90 public Object visitDouble(double d, Context ctx) { 91 return d; 92 } 93 94 @Override 95 public Object visitFloat(float f, Context ctx) { 96 return f; 97 } 98 99 @Override 100 public Object visitInt(int i, Context ctx) { 101 return i; 102 } 103 104 @Override 105 public Object visitLong(long l, Context ctx) { 106 return l; 107 } 108 109 @Override 110 public Object visitShort(short s, Context ctx) { 111 return s; 112 } 113 114 @Override 115 public Object visitString(String s, Context ctx) { 116 return s; 117 } 118 119 @Override 120 public Object visitType(TypeMirror m, Context ctx) { 121 var e = ctx.environment.getTypeUtils().asElement(m); 122 return switch (e.getKind()) { 123 case CLASS, INTERFACE, ENUM -> ClassDocImpl.create((TypeElement) e, ctx); 124 case ANNOTATION_TYPE -> AnnotationTypeDocImpl.create((TypeElement) e, ctx); 125 default -> throw new UnsupportedOperationException( 126 e.getKind() + " is not not yet implemented"); 127 }; 128 } 129 130 @Override 131 public Object visitEnumConstant(VariableElement c, Context context) { 132 return null; 133 } 134 135 @Override 136 public Object visitAnnotation(AnnotationMirror m, Context ctx) { 137 return null; 138 } 139 140 @Override 141 public Object visitArray( 142 List<? extends javax.lang.model.element.AnnotationValue> vals, 143 Context ctx) { 144 AnnotationValueImpl[] ret = new AnnotationValueImpl[vals.size()]; 145 for (int i = 0; i < vals.size(); i++) { 146 ret[i] = AnnotationValueImpl.create(vals.get(i), ctx); 147 } 148 return ret; 149 } 150 151 @Override 152 protected Object defaultAction(Object o, Context context) { 153 throw new UnsupportedOperationException("Unexpected annotation value: " + o); 154 } 155 }; 156 } 157