8224630: ElementScannerN, N > 9 should scan type parameters

Reviewed-by: mcimadamore, erikj
This commit is contained in:
Joe Darcy 2019-12-05 14:14:06 -08:00
parent 565770872a
commit df308ac680
3 changed files with 52 additions and 31 deletions

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
@ -345,7 +345,7 @@ AC_DEFUN_ONCE([BOOTJDK_SETUP_BOOT_JDK],
# When compiling code to be executed by the Boot JDK, force compatibility with the
# oldest supported bootjdk.
BOOT_JDK_SOURCETARGET="-source 9 -target 9"
BOOT_JDK_SOURCETARGET="-source 13 -target 13"
AC_SUBST(BOOT_JDK_SOURCETARGET)
AC_SUBST(JAVAC_FLAGS)

View File

@ -25,6 +25,8 @@
package javax.lang.model.util;
import java.util.List;
import java.util.ArrayList;
import javax.lang.model.element.*;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
@ -120,6 +122,48 @@ public class ElementScanner14<R, P> extends ElementScanner9<R, P> {
super(defaultValue);
}
/**
* {@inheritDoc}
*
* @implSpec This implementation scans the type parameters, if
* any, and then the enclosed elements.
*
*
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of scanning
*/
@Override
public R visitType(TypeElement e, P p) {
return scan(createScanningList(e, e.getEnclosedElements()), p);
}
/**
* {@inheritDoc}
*
* @implSpec This implementation first scans the type parameters, if any, and then
* the parameters.
*
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of scanning
*/
public R visitExecutable(ExecutableElement e, P p) {
return scan(createScanningList(e, e.getParameters()), p);
}
private List<? extends Element> createScanningList(Parameterizable element,
List<? extends Element> toBeScanned) {
var typeParameters = element.getTypeParameters();
if (typeParameters.isEmpty()) {
return toBeScanned;
} else {
List<Element> scanningList = new ArrayList<>(typeParameters);
scanningList.addAll(toBeScanned);
return scanningList;
}
}
/**
* {@inheritDoc}
*

View File

@ -124,8 +124,7 @@ public class JavacRoundEnvironment implements RoundEnvironment {
Set<Element> result = Collections.emptySet();
@SuppressWarnings("preview")
ElementScanner14<Set<Element>, TypeElement> scanner =
new AnnotationSetScanner(result);
var scanner = new AnnotationSetScanner(result);
for (Element element : rootElements)
result = scanner.scan(element, a);
@ -146,8 +145,7 @@ public class JavacRoundEnvironment implements RoundEnvironment {
Set<Element> result = Collections.emptySet();
@SuppressWarnings("preview")
ElementScanner14<Set<Element>, Set<TypeElement>> scanner =
new AnnotationSetMultiScanner(result);
var scanner = new AnnotationSetMultiScanner(result);
for (Element element : rootElements)
result = scanner.scan(element, annotationSet);
@ -156,8 +154,9 @@ public class JavacRoundEnvironment implements RoundEnvironment {
}
// Could be written as a local class inside getElementsAnnotatedWith
@SuppressWarnings("preview")
private class AnnotationSetScanner extends
ElementScanningIncludingTypeParameters<Set<Element>, TypeElement> {
ElementScanner14<Set<Element>, TypeElement> {
// Insertion-order preserving set
private Set<Element> annotatedElements = new LinkedHashSet<>();
@ -191,8 +190,9 @@ public class JavacRoundEnvironment implements RoundEnvironment {
}
// Could be written as a local class inside getElementsAnnotatedWithAny
@SuppressWarnings("preview")
private class AnnotationSetMultiScanner extends
ElementScanningIncludingTypeParameters<Set<Element>, Set<TypeElement>> {
ElementScanner14<Set<Element>, Set<TypeElement>> {
// Insertion-order preserving set
private Set<Element> annotatedElements = new LinkedHashSet<>();
@ -225,29 +225,6 @@ public class JavacRoundEnvironment implements RoundEnvironment {
}
}
@SuppressWarnings("preview")
private static abstract class ElementScanningIncludingTypeParameters<R, P>
extends ElementScanner14<R, P> {
protected ElementScanningIncludingTypeParameters(R defaultValue) {
super(defaultValue);
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public R visitType(TypeElement e, P p) {
// Type parameters are not considered to be enclosed by a type
scan(e.getTypeParameters(), p);
return super.visitType(e, p);
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public R visitExecutable(ExecutableElement e, P p) {
// Type parameters are not considered to be enclosed by an executable
scan(e.getTypeParameters(), p);
return super.visitExecutable(e, p);
}
}
/**
* {@inheritDoc}
*/