2020
2121package com .apple .foundationdb .annotation ;
2222
23- import com .google .common .annotations .VisibleForTesting ;
2423import com .squareup .javapoet .AnnotationSpec ;
2524import com .squareup .javapoet .ClassName ;
2625import com .squareup .javapoet .CodeBlock ;
4443import javax .lang .model .element .Modifier ;
4544import javax .lang .model .element .PackageElement ;
4645import javax .lang .model .element .TypeElement ;
47- import javax .lang .model .type .DeclaredType ;
4846import javax .lang .model .type .TypeKind ;
4947import javax .lang .model .type .TypeMirror ;
5048import javax .lang .model .util .Types ;
5149import javax .tools .Diagnostic ;
5250import java .io .IOException ;
53- import java .util .Arrays ;
5451import java .util .List ;
5552import java .util .Locale ;
5653import java .util .Map ;
5754import java .util .Objects ;
5855import java .util .Set ;
5956import java .util .function .BiFunction ;
6057import java .util .stream .Collectors ;
61- import java .util .stream .Stream ;
6258
6359/**
6460 * A separate class to support (@link GenerateVisitorAnnotationProcessor) so that dependency on javapoet does not leak to anyone
@@ -106,8 +102,8 @@ static boolean process(final ProcessingEnvironment processingEnv, Set<? extends
106102 .getEnclosedElements ()
107103 .stream ()
108104 .flatMap (packageElement -> packageElement .getEnclosedElements ().stream ())
109- .flatMap (element -> element .getKind () == ElementKind .CLASS && element . getModifiers (). contains ( Modifier . ABSTRACT ) ? element . getEnclosedElements (). stream () : Stream . of ( element ) )
110- . filter ( element -> element . getKind () == ElementKind . CLASS && !element .getModifiers ().contains (Modifier .ABSTRACT ))
105+ .filter (element -> element .getKind () == ElementKind .CLASS &&
106+ !element .getModifiers ().contains (Modifier .ABSTRACT ))
111107 .map (Element ::asType )
112108 .filter (mirror -> mirror .getKind () == TypeKind .DECLARED )
113109 .filter (mirror -> typeUtils .isSubtype (mirror , rootTypeMirror ))
@@ -156,19 +152,18 @@ private static void generateInterface(@Nonnull final Types typeUtils,
156152 .addModifiers (Modifier .PUBLIC )
157153 .addTypeVariable (typeVariableName );
158154
159- final var packageName = packageElement .getQualifiedName ().toString ();
160155 final var jumpMapBuilder = FieldSpec .builder (ParameterizedTypeName .get (ClassName .get (Map .class ),
161156 ParameterizedTypeName .get (ClassName .get (Class .class ), WildcardTypeName .subtypeOf (Object .class )),
162157 ParameterizedTypeName .get (ClassName .get (BiFunction .class ),
163- ParameterizedTypeName .get (ClassName .get (packageName , interfaceName ), WildcardTypeName .subtypeOf (Object .class )),
158+ ParameterizedTypeName .get (ClassName .get (packageElement . getQualifiedName (). toString () , interfaceName ), WildcardTypeName .subtypeOf (Object .class )),
164159 TypeName .get (rootTypeMirror ),
165160 WildcardTypeName .subtypeOf (Object .class ))),
166161 "jumpMap" , Modifier .PUBLIC , Modifier .STATIC , Modifier .FINAL );
167162
168163 final var initializerStrings = subClassTypeMirrors .stream ()
169164 .map (typeMirror -> {
170165 final var typeElement = (TypeElement )typeUtils .asElement (typeMirror );
171- return "Map.entry(" + getRawTypeName ( typeMirror , packageName ) + ".class, (visitor, element) -> visitor." + methodNameOfVisitMethod (generateVisitor , typeElement ) + "((" + getWildcardTypeName ( typeMirror , packageName ) + ")element))" ;
166+ return "Map.entry(" + typeElement . getSimpleName ( ) + ".class, (visitor, element) -> visitor." + methodNameOfVisitMethod (generateVisitor , typeElement ) + "((" + typeElement . getSimpleName ( ) + ")element))" ;
172167 })
173168 .collect (Collectors .joining (", \n " ));
174169
@@ -177,7 +172,6 @@ private static void generateInterface(@Nonnull final Types typeUtils,
177172 .build ();
178173
179174 typeBuilder .addField (jumpMapBuilder
180- .addAnnotation (AnnotationSpec .builder (SuppressWarnings .class ).addMember ("value" , "$S" , "unchecked" ).build ())
181175 .initializer (initializerBlock )
182176 .build ());
183177
@@ -189,7 +183,7 @@ private static void generateInterface(@Nonnull final Types typeUtils,
189183 .methodBuilder (methodName )
190184 .addModifiers (Modifier .PUBLIC , Modifier .ABSTRACT )
191185 .addAnnotation (Nonnull .class )
192- .addParameter (ParameterSpec .builder (getWildcardTypeName (typeMirror , packageName ), parameterName ).addAnnotation (Nonnull .class ).build ())
186+ .addParameter (ParameterSpec .builder (TypeName . get (typeMirror ), parameterName ).addAnnotation (Nonnull .class ).build ())
193187 .returns (typeVariableName );
194188 typeBuilder .addMethod (specificVisitMethodBuilder .build ());
195189 }
@@ -199,7 +193,7 @@ private static void generateInterface(@Nonnull final Types typeUtils,
199193 .methodBuilder (defaultMethodName )
200194 .addModifiers (Modifier .PUBLIC , Modifier .ABSTRACT )
201195 .addAnnotation (Nonnull .class )
202- .addParameter (ParameterSpec .builder (getWildcardTypeName (rootTypeMirror , packageName ), parameterName ).addAnnotation (Nonnull .class ).build ())
196+ .addParameter (ParameterSpec .builder (TypeName . get (rootTypeMirror ), parameterName ).addAnnotation (Nonnull .class ).build ())
203197 .returns (typeVariableName );
204198 typeBuilder .addMethod (visitDefaultMethodBuilder .build ());
205199
@@ -222,105 +216,6 @@ private static void generateInterface(@Nonnull final Types typeUtils,
222216 .writeTo (Objects .requireNonNull (filer ));
223217 }
224218
225- /**
226- * Converts a type mirror to a raw TypeName without type parameters.
227- * <p>
228- * For generic types, this method returns the raw type without any type arguments
229- * (e.g., {@code List<String>} becomes {@code List}). For non-generic types, the type
230- * is returned as-is. If the type belongs to the same package as {@code currentPackage},
231- * the package prefix is omitted from the generated type name.
232- * <p>
233- * This is particularly useful when generating code that needs to reference the
234- * {@code .class} literal of a generic type, as class literals must use raw types.
235- *
236- * @param typeMirror the type mirror to convert
237- * @param currentPackage the current package name, used to determine whether to omit
238- * package prefixes for types in the same package
239- * @return a TypeName representing the raw type (without type parameters) if the type
240- * is generic, or the original type name if not generic
241- */
242- @ Nonnull
243- private static TypeName getRawTypeName (@ Nonnull TypeMirror typeMirror , @ Nonnull String currentPackage ) {
244- if (typeMirror .getKind () == TypeKind .DECLARED ) {
245- final var declaredType = (DeclaredType ) typeMirror ;
246- final var typeElement = (TypeElement ) declaredType .asElement ();
247- final boolean isGeneric = !typeElement .getTypeParameters ().isEmpty ();
248-
249- if (isGeneric ) {
250- final ClassName className = ClassName .get (typeElement );
251- return removePackagePrefix (className , currentPackage );
252- }
253- }
254-
255- // return as-is, remove the package if it is the same as the currentPackage.
256- final TypeName typeName = TypeName .get (typeMirror );
257- if (typeName instanceof ClassName ) {
258- return removePackagePrefix ((ClassName ) typeName , currentPackage );
259- }
260-
261- return typeName ;
262- }
263-
264- /**
265- * Converts a type mirror to a TypeName with wildcard type arguments for generic types.
266- * <p>
267- * For generic types, this method creates a parameterized type with wildcard bounds
268- * (e.g., {@code List<String>} becomes {@code List<?>}). For non-generic types, the type
269- * is returned as-is. If the type belongs to the same package as {@code currentPackage},
270- * the package prefix is omitted from the generated type name.
271- *
272- * @param typeMirror the type mirror to convert
273- * @param currentPackage the current package name, used to determine whether to omit
274- * package prefixes for types in the same package
275- * @return a TypeName representing the type with wildcard type arguments if the type
276- * is generic, or the original type name if not generic
277- */
278- @ Nonnull
279- private static TypeName getWildcardTypeName (@ Nonnull final TypeMirror typeMirror , @ Nonnull final String currentPackage ) {
280- if (typeMirror .getKind () == TypeKind .DECLARED ) {
281- final var declaredType = (DeclaredType ) typeMirror ;
282- final var typeElement = (TypeElement ) declaredType .asElement ();
283- final boolean isGeneric = !typeElement .getTypeParameters ().isEmpty ();
284-
285- if (isGeneric ) {
286- ClassName rawType = ClassName .get (typeElement );
287- rawType = removePackagePrefix (rawType , currentPackage );
288-
289- final WildcardTypeName [] wildcards = new WildcardTypeName [typeElement .getTypeParameters ().size ()];
290- Arrays .fill (wildcards , WildcardTypeName .subtypeOf (Object .class ));
291- return ParameterizedTypeName .get (rawType , wildcards );
292- }
293- }
294-
295- // return as-is, remove the package if it is the same as the currentPackage.
296- final TypeName typeName = TypeName .get (typeMirror );
297- if (typeName instanceof ClassName ) {
298- return removePackagePrefix ((ClassName ) typeName , currentPackage );
299- }
300-
301- return typeName ;
302- }
303-
304- /**
305- * Removes the package prefix from a ClassName if it belongs to the same package as currentPackage.
306- * <p>
307- * This is useful when generating code references to types that are in the same package,
308- * as the package prefix can be omitted for brevity.
309- *
310- * @param className the ClassName to potentially strip the package prefix from
311- * @param currentPackage the current package name to compare against
312- * @return a ClassName without the package prefix if it's in the same package,
313- * otherwise returns the original ClassName unchanged
314- */
315- @ Nonnull
316- private static ClassName removePackagePrefix (@ Nonnull final ClassName className , @ Nonnull final String currentPackage ) {
317- if (className .packageName ().equals (currentPackage )) {
318- return ClassName .get ("" , className .topLevelClassName ().simpleName (),
319- className .simpleNames ().subList (1 , className .simpleNames ().size ()).toArray (new String [0 ]));
320- }
321- return className ;
322- }
323-
324219 private static void generateImplementationWithDefaults (@ Nonnull final Types typeUtils ,
325220 @ Nonnull final Filer filer ,
326221 @ Nonnull final GenerateVisitor generateVisitor ,
@@ -345,7 +240,7 @@ private static void generateImplementationWithDefaults(@Nonnull final Types type
345240 .addModifiers (Modifier .PUBLIC , Modifier .DEFAULT )
346241 .addAnnotation (Nonnull .class )
347242 .addAnnotation (Override .class )
348- .addParameter (ParameterSpec .builder (getWildcardTypeName ( typeMirror , packageElement . getQualifiedName (). toString () ), parameterName ).addAnnotation (Nonnull .class ).build ())
243+ .addParameter (ParameterSpec .builder (TypeName . get ( typeMirror ), parameterName ).addAnnotation (Nonnull .class ).build ())
349244 .returns (typeVariableName )
350245 .addCode (CodeBlock .builder ()
351246 .addStatement ("return " + defaultMethodName + "(" + parameterName + ")" )
0 commit comments