--- old/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java 2018-07-24 22:42:44.385000996 -0700 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java 2018-07-24 22:42:44.241000996 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2018, 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 @@ -170,6 +170,18 @@ e.accept(this, annotation); return annotatedElements; } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + public Set visitModule(ModuleElement e, TypeElement annotation) { + // Do not scan a module + return annotatedElements; + } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + public Set visitPackage(PackageElement e, TypeElement annotation) { + // Do not scan a package + return annotatedElements; + } } // Could be written as a local class inside getElementsAnnotatedWithAny @@ -193,6 +205,18 @@ e.accept(this, annotations); return annotatedElements; } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + public Set visitModule(ModuleElement e, Set annotations) { + // Do not scan a module + return annotatedElements; + } + + @Override @DefinedBy(Api.LANGUAGE_MODEL) + public Set visitPackage(PackageElement e, Set annotations) { + // Do not scan a package + return annotatedElements; + } } private static abstract class ElementScanningIncludingTypeParameters @@ -224,10 +248,12 @@ public Set getElementsAnnotatedWith(Class a) { throwIfNotAnnotation(a); String name = a.getCanonicalName(); + if (name == null) return Collections.emptySet(); else { - TypeElement annotationType = eltUtils.getTypeElement(name); + TypeElement annotationType = annotationToElement(a); + if (annotationType == null) return Collections.emptySet(); else @@ -244,12 +270,29 @@ String name = annotation.getCanonicalName(); if (name == null) continue; - annotationsAsElements.add(eltUtils.getTypeElement(name)); + annotationsAsElements.add(annotationToElement(annotation)); } return getElementsAnnotatedWithAny(annotationsAsElements.toArray(new TypeElement[0])); } + private TypeElement annotationToElement(Class annotation) { + // First, try an element lookup based on the annotation's + // canonical name. If that fails or is ambiguous, try a lookup + // using a particular module, perhaps an unnamed one. This + // offers more compatibility for compiling in single-module + // mode where the runtime module of an annotation type may + // differ from the single module being compiled. + String name = annotation.getCanonicalName(); + TypeElement annotationElement = eltUtils.getTypeElement(name); + if (annotationElement != null) + return annotationElement; + else { + String moduleName = Objects.requireNonNullElse(annotation.getModule().getName(), ""); + return eltUtils.getTypeElement(eltUtils.getModuleElement(moduleName), name); + } + } + private Element mirrorAsElement(AnnotationMirror annotationMirror) { return annotationMirror.getAnnotationType().asElement(); }