• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.doclava.javadoc;
18 
19 import com.sun.javadoc.Doc;
20 import com.sun.source.doctree.DocTree;
21 import com.sun.source.doctree.ParamTree;
22 import com.sun.source.doctree.SeeTree;
23 import com.sun.source.doctree.SerialFieldTree;
24 import com.sun.source.doctree.ThrowsTree;
25 import java.util.HashMap;
26 import java.util.Map;
27 import javax.lang.model.element.AnnotationValue;
28 import javax.lang.model.element.Element;
29 import javax.lang.model.element.ExecutableElement;
30 import javax.lang.model.element.PackageElement;
31 import javax.lang.model.element.TypeElement;
32 import javax.lang.model.element.VariableElement;
33 import javax.lang.model.type.ArrayType;
34 import javax.lang.model.type.DeclaredType;
35 import javax.lang.model.type.TypeMirror;
36 import javax.lang.model.type.TypeVariable;
37 import javax.lang.model.type.WildcardType;
38 import javax.lang.model.util.SimpleElementVisitor14;
39 import jdk.javadoc.doclet.DocletEnvironment;
40 
41 /**
42  * Holds temporary objects required to construct {@link RootDocImpl} from a {@link
43  * jdk.javadoc.doclet.DocletEnvironment}.
44  */
45 class Context {
46 
47     public final DocletEnvironment environment;
48     public final Caches caches = new Caches();
49     public final DocletElementUtils docletElementUtils;
50 
obtain(Element element)51     public Doc obtain(Element element) {
52         return OBTAIN_VISITOR.visit(element, this);
53     }
54 
55     public static class Caches {
56 
57         public final Map<TypeElement, ClassDocImpl> classes = new HashMap<>();
58         public final Map<TypeElement, AnnotationTypeDocImpl> annotations = new HashMap<>();
59         public final Map<PackageElement, PackageDocImpl> packages = new HashMap<>();
60         public final Map<ExecutableElement, AnnotationMethodDocImpl> annotationMethods =
61                 new HashMap<>();
62         public final Map<AnnotationValue, AnnotationValueImpl> annotationValues = new HashMap<>();
63         public final Map<ExecutableElement, ConstructorDocImpl> constructors = new HashMap<>();
64         public final Map<ExecutableElement, MethodDocImpl> methods = new HashMap<>();
65         public final Map<VariableElement, FieldDocImpl> fields = new HashMap<>();
66         public final Map<VariableElement, ParameterImpl> parameters = new HashMap<>();
67 
68         public final Tags tags = new Tags();
69         public final Types types = new Types();
70 
71         public static class Tags {
72 
73             public final Map<Element, Map<DocTree, TagImpl>> generic = new HashMap<>();
74             public final Map<Element, Map<SeeTree, SeeTagImpl>> see = new HashMap<>();
75             public final Map<Element, Map<SerialFieldTree, SerialFieldTagImpl>> serialField =
76                     new HashMap<>();
77             public final Map<Element, Map<ParamTree, ParamTagImpl>> param = new HashMap<>();
78             public final Map<Element, Map<ThrowsTree, ThrowsTagImpl>> throwz = new HashMap<>();
79         }
80 
81         public static class Types {
82 
83             public final Map<TypeMirror, TypeImpl> common = new HashMap<>();
84             public final Map<DeclaredType, AnnotatedTypeImpl> annotated = new HashMap<DeclaredType, AnnotatedTypeImpl>();
85             public final Map<WildcardType, WildcardTypeImpl> wildcard = new HashMap<>();
86             public final Map<DeclaredType, ParameterizedTypeImpl> parameterized = new HashMap<>();
87             public final Map<TypeVariable, TypeVariableImpl> typevar = new HashMap<>();
88             public final Map<ArrayType, ArrayTypeImpl> array = new HashMap<>();
89         }
90     }
91 
Context(DocletEnvironment environment)92     public Context(DocletEnvironment environment) {
93         this.environment = environment;
94         this.docletElementUtils = new DocletElementUtils(environment);
95     }
96 
97     private static final SimpleElementVisitor14<Doc, Context> OBTAIN_VISITOR =
98             new SimpleElementVisitor14<>() {
99 
100                 @Override
101                 public Doc visitPackage(PackageElement e, Context context) {
102                     return PackageDocImpl.create(e, context);
103                 }
104 
105                 @Override
106                 public Doc visitType(TypeElement e, Context context) {
107                     return switch (e.getKind()) {
108                         case CLASS, ENUM, INTERFACE -> ClassDocImpl.create(e, context);
109                         case ANNOTATION_TYPE -> AnnotationTypeDocImpl.create(e, context);
110                         case RECORD -> throw new UnsupportedOperationException(
111                                 "Records not yet supported");
112                         default -> throw new IllegalArgumentException(
113                                 "Expected ANNOTATION_TYPE, CLASS, "
114                                         + "ENUM, INTERFACE, or RECORD; but got " + e.getKind());
115                     };
116                 }
117             };
118 }
119