Skip to content

Commit 0c3bc71

Browse files
committed
8281169: Expand discussion of elements and types
Reviewed-by: mcimadamore, prappo
1 parent f143380 commit 0c3bc71

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

src/java.compiler/share/classes/javax/lang/model/element/package-info.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -110,6 +110,7 @@
110110
* a {@code NullPointerException} if given a {@code null} argument.
111111
*
112112
* @see javax.lang.model.util.Elements
113+
* @see javax.lang.model##elementsAndTypes Elements and Types
113114
* @see <a href="https://jcp.org/en/jsr/detail?id=269">
114115
* JSR 269: Pluggable Annotation Processing API</a>
115116
* @jls 6.1 Declarations

src/java.compiler/share/classes/javax/lang/model/package-info.java

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
2525

2626
/**
2727
* Types and hierarchies of packages comprising a {@index "Java language
28-
* model"}, a model of the declarations and types of the Java
28+
* model"}, a reflective API that models the declarations and types of the Java
2929
* programming language.
3030
*
3131
* The members of this package and its subpackages are for use in
@@ -52,6 +52,89 @@
5252
* <p>Unless otherwise specified, methods in this package will throw
5353
* a {@code NullPointerException} if given a {@code null} argument.
5454
*
55+
* <h2><a id=elementsAndTypes>Elements and Types</a></h2>
56+
*
57+
* <h3><a id=DefUse>Definitions and Uses</a></h3>
58+
*
59+
* In broad terms the {@link javax.lang.model.element element} package
60+
* models the declarations, that is the <em>definitions</em>, of elements while
61+
* the {@link javax.lang.model.type type} package models <em>uses</em>
62+
* of types. In general, distinct uses can have individualized
63+
* information separate from the information associated with the
64+
* definition. In some sense, the information in the definition is
65+
* shared by all the uses.
66+
67+
* <p>For example, consider the uses of {@code
68+
* java.lang.String} in the string processing method {@code
69+
* identityOrEmpty} below:
70+
*
71+
* {@snippet lang=java :
72+
* // Return the argument if it is non-null and the empty string otherwise.
73+
* public static @DefinitelyNotNull String identityOrEmpty(@MightBeNull String argument) {
74+
* ...
75+
* }
76+
* }
77+
*
78+
* The return type of the method is a {@code String} annotated with
79+
* a {@code @DefinitelyNotNull} type annotation while the type of
80+
* the parameter is a {@code String} annotated with a {@code
81+
* @MightBeNull} type annotation. In a reflective API, since the set
82+
* of annotations is different for the two <em>uses</em> of {@code
83+
* String} as a type, the return type and argument type would need to
84+
* be represented by different objects to distinguish between these two
85+
* cases. The <em>definition</em> of {@code java.lang.String} itself
86+
* is annotated with neither of the type annotations in question.
87+
*
88+
* <p>Another example, consider the declaration of the generic
89+
* interface (JLS {@jls 9.1.2}) {@code java.util.Set} which has one
90+
* type parameter. This declaration captures commonality between the
91+
* many parameterized types (JLS {@jls 4.5}) derived from that
92+
* declaration such as {@code java.util.Set<String>}, {@code
93+
* java.util.Set<E>}, {@code java.util.Set<?>}, and also the raw type
94+
* (JLS {@jls 4.8}) {@code java.util.Set}.
95+
*
96+
* <h3><a id=elementTypeMapping>Mapping between Elements and Types</a></h3>
97+
*
98+
* While distinct concepts, there are bidirectional (partial) mappings
99+
* between elements and types, between definitions and uses. For
100+
* example, roughly speaking, information that would be invariant for
101+
* all uses of a type can be retrieved from the element defining a
102+
* type. For example, consider a {@link
103+
* javax.lang.model.type.DeclaredType DeclaredType} type mirror
104+
* modeling a use of {@code java.lang.String}. Calling {@link
105+
* javax.lang.model.type.DeclaredType#asElement()} would return the
106+
* {@link javax.lang.model.element.TypeElement} for {@code
107+
* java.lang.String}. From the {@code TypeElement}, common information
108+
* such as {@linkplain
109+
* javax.lang.model.element.TypeElement#getSimpleName() name} and
110+
* {@linkplain javax.lang.model.element.TypeElement#getModifiers()
111+
* modifiers} can be retrieved.
112+
*
113+
* <p>All elements can be {@linkplain
114+
* javax.lang.model.element.Element#asType() mapped to} some type.
115+
* The elements for classes and interfaces get {@linkplain
116+
* javax.lang.model.element.TypeElement#asType() mapped to} a
117+
* prototypical type. (If a class or interface is generic, its
118+
* prototypical type mirror is parameterized with type arguments
119+
* matching the type variables of the declaration, all
120+
* unannotated. Otherwise, for a non-generic class or interface, the
121+
* prototypical type mirror corresponds to an unannotated use of the
122+
* type.) Conversely, in general, many types can map to the same
123+
* {@linkplain javax.lang.model.element.TypeElement type element}. For
124+
* example, the type mirror for the raw type {@code java.util.Set},
125+
* the prototypical type {@code java.util.Set<E>}, and the type {@code
126+
* java.util.Set<String>} would all {@linkplain
127+
* javax.lang.model.type.DeclaredType#asElement() map to} the type
128+
* element for {@code java.util.Set}. Several kinds of types can be
129+
* mapped to elements, but other kinds of types do <em>not</em> have
130+
* an {@linkplain javax.lang.model.util.Types#asElement(TypeMirror)
131+
* element mapping}. For example, the type mirror of an {@linkplain
132+
* javax.lang.model.type.ExecutableType executable type} does
133+
* <em>not</em> have an element mapping while a {@linkplain
134+
* javax.lang.model.type.DeclaredType declared type} would map to a
135+
* {@linkplain javax.lang.model.element.TypeElement type element}, as
136+
* discussed above.
137+
*
55138
* @since 1.6
56139
*
57140
* @see <a href="https://jcp.org/en/jsr/detail?id=269">

src/java.compiler/share/classes/javax/lang/model/type/package-info.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,7 @@
3434
* a {@code NullPointerException} if given a {@code null} argument.
3535
*
3636
* @see javax.lang.model.util.Types
37+
* @see javax.lang.model##elementsAndTypes Elements and Types
3738
* @see <a href="https://jcp.org/en/jsr/detail?id=269">
3839
* JSR 269: Pluggable Annotation Processing API</a>
3940
* @jls 4.1 The Kinds of Types and Values

0 commit comments

Comments
 (0)