8261625: Add Elements.isAutomaticModule(ModuleElement)

Reviewed-by: vromero, jlahoda
This commit is contained in:
Joe Darcy 2021-04-08 21:25:17 +00:00
parent 8a2358074f
commit ccefa5e378
5 changed files with 194 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, 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
@ -656,6 +656,22 @@ public interface Elements {
*/
boolean isFunctionalInterface(TypeElement type);
/**
* {@return {@code true} if the module element is an automatic
* module, {@code false} otherwise}
*
* @implSpec
* The default implementation of this method returns {@code
* false}.
*
* @param module the module element being examined
* @jls 7.7.1 Dependences
* @since 17
*/
default boolean isAutomaticModule(ModuleElement module) {
return false;
}
/**
* Returns the record component for the given accessor. Returns null if the
* given method is not a record component accessor.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, 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
@ -715,6 +715,12 @@ public class JavacElements implements Elements {
}
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public boolean isAutomaticModule(ModuleElement module) {
ModuleSymbol msym = (ModuleSymbol) module;
return (msym.flags() & Flags.AUTOMATIC_MODULE) != 0;
}
/**
* Returns the tree node and compilation unit corresponding to this
* element, or null if they can't be found.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, 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
@ -23,7 +23,7 @@
/**
* @test
* @bug 8155026 8178011 8220702
* @bug 8155026 8178011 8220702 8261625
* @summary Test automatic modules
* @library /tools/lib
* @modules
@ -39,8 +39,13 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import toolbox.JarTask;
import toolbox.JavacTask;
@ -665,9 +670,31 @@ public class AutomaticModules extends ModuleTestBase {
"-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
.processors(new AbstractProcessor() {
// Processor verifies api.Api is enclosed by an automatic module.
@Override
public Set<String> getSupportedAnnotationTypes() {
return Set.of("*");
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
var elts = processingEnv.getElementUtils();
if (!elts.isAutomaticModule(elts.getModuleOf(elts.getTypeElement("api.Api")))) {
throw new RuntimeException("module of class api.Api is not automatic");
}
}
return true;
}
})
.run(Task.Expect.SUCCESS);
tb.writeJavaFiles(src,
"module m { requires automatic; }");

View File

@ -0,0 +1,124 @@
/*
* Copyright (c) 2006, 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8261625
* @summary Test Elements.isAutomaticModule
* @library /tools/javac/lib
* @build JavacTestingAbstractProcessor TestIsAutomaticMod
* @compile -processor TestIsAutomaticMod -proc:only TestIsAutomaticMod.java
*/
import java.io.Writer;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.lang.model.util.*;
/**
* Test basic workings of Elements.isAutomaticModule
*/
public class TestIsAutomaticMod extends JavacTestingAbstractProcessor {
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
// Named module java.base
checkMod(eltUtils.getModuleElement("java.base"), false);
// Unnamed module for TestIsAutomaticMod
for (Element e : roundEnv.getRootElements() ) {
ModuleElement enclosing = elements.getModuleOf(e);
checkMod(enclosing, false);
}
if ((new TestElements()).isAutomaticModule(null) != false) {
throw new RuntimeException("Bad behavior from default isAutomaticModule method");
}
}
return true;
}
private void checkMod(ModuleElement mod, boolean expectedIsAuto) {
boolean actualIsAuto = elements.isAutomaticModule(mod);
if (actualIsAuto != expectedIsAuto) {
throw new RuntimeException(String.format("Unexpected isAutomatic ``%s''' for %s, expected ``%s''%n",
actualIsAuto,
mod,
expectedIsAuto));
}
}
// Use default methods of javax.lang.model.util.Elements; define
// vacuous methods to override the abstract methods.
private static class TestElements implements Elements {
public TestElements() {}
@Override
public PackageElement getPackageElement(CharSequence name) {return null;}
@Override
public TypeElement getTypeElement(CharSequence name) {return null;}
@Override
public Map<? extends ExecutableElement, ? extends AnnotationValue>
getElementValuesWithDefaults(AnnotationMirror a) {return null;}
@Override
public String getDocComment(Element e) {return null;}
@Override
public boolean isDeprecated(Element e) {return false;}
@Override
public Name getBinaryName(TypeElement type) {return null;}
@Override
public PackageElement getPackageOf(Element e) {return null;}
@Override
public List<? extends Element> getAllMembers(TypeElement type) {return null;}
@Override
public List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e) {return null;}
@Override
public boolean hides(Element hider, Element hidden) {return false;}
@Override
public boolean overrides(ExecutableElement overrider,
ExecutableElement overridden,
TypeElement type) {return false;}
@Override
public String getConstantExpression(Object value) {return null;}
@Override
public void printElements(Writer w, Element... elements) {}
@Override
public Name getName(CharSequence cs) {return null;}
@Override
public boolean isFunctionalInterface(TypeElement type) {return false;}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, 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
@ -37,6 +37,7 @@ import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.processing.Processor;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
@ -60,6 +61,7 @@ public class JavacTask extends AbstractTask<JavacTask> {
private List<JavaFileObject> fileObjects;
private JavaFileManager fileManager;
private Consumer<com.sun.source.util.JavacTask> callback;
private List<Processor> procs;
private JavaCompiler compiler;
private StandardJavaFileManager internalFileManager;
@ -255,6 +257,14 @@ public class JavacTask extends AbstractTask<JavacTask> {
return this;
}
/**
* Sets the the annotation processors to be used.
*/
public JavacTask processors(Processor... procs) {
this.procs = List.of(procs);
return this;
}
/**
* Sets the file manager to be used by this task.
* @param fileManager the file manager
@ -358,6 +368,9 @@ public class JavacTask extends AbstractTask<JavacTask> {
allOpts,
classes,
allFiles);
if (procs != null) {
task.setProcessors(procs);
}
JavacTaskImpl taskImpl = (JavacTaskImpl) task;
if (callback != null) {
callback.accept(taskImpl);