8224922: Access JavaFileObject from Element(s)
Co-authored-by: Jan Lahoda <jlahoda@openjdk.org> Reviewed-by: jjg
This commit is contained in:
parent
0a9e76c4f9
commit
4ff43010bb
src
java.compiler/share/classes/javax
annotation/processing
lang/model
jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac
@ -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
|
||||
@ -69,7 +69,13 @@ import java.io.IOException;
|
||||
* originating elements are the classes or interfaces or packages
|
||||
* (representing {@code package-info} files) or modules (representing
|
||||
* {@code module-info} files) which caused an annotation processor to
|
||||
* attempt to create a new file. For example, if an annotation
|
||||
* attempt to create a new file.
|
||||
* In other words, the originating elements are intended to have the
|
||||
* granularity of <em>compilation units</em> (JLS section {@jls 7.3}),
|
||||
* essentially file-level granularity, rather than finer-scale
|
||||
* granularity of, say, a method or field declaration.
|
||||
*
|
||||
* <p>For example, if an annotation
|
||||
* processor tries to create a source file, {@code
|
||||
* GeneratedFromUserSource}, in response to processing
|
||||
*
|
||||
|
@ -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
|
||||
@ -131,6 +131,12 @@ public interface ExecutableElement extends Element, Parameterizable {
|
||||
*/
|
||||
AnnotationValue getDefaultValue();
|
||||
|
||||
/**
|
||||
* {@return the class or interface defining the executable}
|
||||
*/
|
||||
@Override
|
||||
Element getEnclosingElement();
|
||||
|
||||
/**
|
||||
* {@return the simple name of a constructor, method, or
|
||||
* initializer} For a constructor, the name {@code "<init>"} is
|
||||
|
@ -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
|
||||
@ -36,19 +36,20 @@
|
||||
* appearing inside method bodies, such as local variables and
|
||||
* anonymous classes.
|
||||
*
|
||||
* <p>When used in the context of annotation processing, an accurate
|
||||
* model of the element being represented must be returned. As this
|
||||
* is a language model, the source code provides the fiducial
|
||||
* (reference) representation of the construct in question rather than
|
||||
* a representation in an executable output like a class file.
|
||||
* Executable output may serve as the basis for creating a modeling
|
||||
* element. However, the process of translating source code to
|
||||
* executable output may not permit recovering some aspects of the
|
||||
* source code representation. For example, annotations with
|
||||
* {@linkplain java.lang.annotation.RetentionPolicy#SOURCE source}
|
||||
* {@linkplain java.lang.annotation.Retention retention} cannot be
|
||||
* recovered from class files and class files might not be able to
|
||||
* provide source position information.
|
||||
* <p id="accurate_model">When used in the context of annotation
|
||||
* processing, an accurate model of the element being represented must
|
||||
* be returned. As this is a language model, the source code provides
|
||||
* the fiducial (reference) representation of the construct in
|
||||
* question rather than a representation in an executable output like
|
||||
* a class file. Executable output may serve as the basis for
|
||||
* creating a modeling element. However, the process of translating
|
||||
* source code to executable output may not permit recovering some
|
||||
* aspects of the source code representation. For example,
|
||||
* annotations with {@linkplain
|
||||
* java.lang.annotation.RetentionPolicy#SOURCE source} {@linkplain
|
||||
* java.lang.annotation.Retention retention} cannot be recovered from
|
||||
* class files and class files might not be able to provide source
|
||||
* position information.
|
||||
*
|
||||
* Names of {@linkplain
|
||||
* javax.lang.model.element.ExecutableElement#getParameters()
|
||||
|
@ -761,4 +761,100 @@ public interface Elements {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the file object for this element or {@code null} if
|
||||
* there is no such file object}
|
||||
*
|
||||
* <p>The returned file object is for the <a
|
||||
* href="../element/package-summary.html#accurate_model">reference
|
||||
* representation</a> of the information used to construct the
|
||||
* element. For example, if during compilation or annotation
|
||||
* processing, a source file for class {@code Foo} is compiled
|
||||
* into a class file, the file object returned for the element
|
||||
* representing {@code Foo} would be for the source file and
|
||||
* <em>not</em> for the class file.
|
||||
*
|
||||
* <p>An implementation may choose to not support the
|
||||
* functionality of this method, in which case {@link
|
||||
* UnsupportedOperationException} is thrown.
|
||||
*
|
||||
* <p>In the context of annotation processing, a non-{@code null}
|
||||
* value is returned if the element was included as part of the
|
||||
* initial inputs or the containing file was created during the
|
||||
* run of the annotation processing tool. Otherwise, a {@code
|
||||
* null} may be returned. In annotation processing, if a
|
||||
* {@linkplain javax.annotation.processing.Filer#createClassFile
|
||||
* class file is created}, that class file can serve as the
|
||||
* reference representation for elements.
|
||||
*
|
||||
* <p>If it has a file object, the file object for a package will
|
||||
* be a {@code package-info} file. A package may exist and not
|
||||
* have any {@code package-info} file even if the package is
|
||||
* (implicitly) created during an annotation processing run from
|
||||
* the creation of source or class files in that package. An
|
||||
* {@linkplain PackageElement#isUnnamed unnamed package} will have
|
||||
* a {@code null} file since it cannot be declared in a
|
||||
* compilation unit.
|
||||
*
|
||||
* <p>If it has a file object, the file object for a module will
|
||||
* be a {@code module-info} file. An {@linkplain
|
||||
* ModuleElement#isUnnamed unnamed module} will have a {@code
|
||||
* null} file since it cannot be declared in a compilation unit.
|
||||
* An {@linkplain #isAutomaticModule automatic module} will have a
|
||||
* {@code null} file since it is implicitly declared.
|
||||
*
|
||||
* <p>If it has a file object, the file object for a top-level
|
||||
* {@code public} class or interface will be a source or class
|
||||
* file corresponding to that class or interface. In this case,
|
||||
* typically the leading portion of the name of the file will
|
||||
* match the name of the class or interface. A single compilation
|
||||
* unit can define multiple top-level classes and interfaces, such
|
||||
* as a primary {@code public} class or interfaces whose name
|
||||
* corresponds to the file name and one or more <em>auxiliary</em>
|
||||
* classes or interfaces whose names do not correspond to the file
|
||||
* name. If a source file is providing the reference
|
||||
* representation of an auxiliary class or interface, the file for
|
||||
* the primary class is returned. (An auxiliary class or interface
|
||||
* can also be defined in a {@code package-info} source file, in
|
||||
* which case the file for the {@code package-info} file is
|
||||
* returned.) If a class file is providing the reference
|
||||
* representation of an auxiliary class or interface, the separate
|
||||
* class file for the auxiliary class is returned.
|
||||
*
|
||||
* <p>For a nested class or interface, if it has a file object:
|
||||
*
|
||||
* <ul>
|
||||
*
|
||||
* <li>if a source file is providing the reference representation,
|
||||
* the file object will be that of the {@linkplain
|
||||
* #getOutermostTypeElement(Element) outermost enclosing} class or
|
||||
* interface
|
||||
*
|
||||
* <li>if a class file is providing the reference representation,
|
||||
* the file object will be that of the nested class or interface
|
||||
* itself
|
||||
*
|
||||
* </ul>
|
||||
*
|
||||
* <p>For other lexically enclosed elements, such as {@linkplain
|
||||
* VariableElement#getEnclosingElement() variables}, {@linkplain
|
||||
* ExecutableElement#getEnclosingElement() methods, and
|
||||
* constructors}, if they have a file object, the file object will
|
||||
* be the object associated with the {@linkplain
|
||||
* Element#getEnclosingElement() enclosing element} of the
|
||||
* lexically enclosed element.
|
||||
*
|
||||
* @implSpec The default implementation unconditionally throws
|
||||
* {@link UnsupportedOperationException}.
|
||||
*
|
||||
* @throws UnsupportedOperationException if this functionality is
|
||||
* not supported
|
||||
*
|
||||
* @param e the element to find a file object for
|
||||
* @since 18
|
||||
*/
|
||||
default javax.tools.JavaFileObject getFileObjectOf(Element e) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 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
|
||||
@ -376,7 +376,7 @@ public class Enter extends JCTree.Visitor {
|
||||
Name name = names.package_info;
|
||||
ClassSymbol c = syms.enterClass(tree.modle, name, tree.packge);
|
||||
c.flatname = names.fromString(tree.packge + "." + name);
|
||||
c.sourcefile = tree.sourcefile;
|
||||
c.classfile = c.sourcefile = tree.sourcefile;
|
||||
c.completer = Completer.NULL_COMPLETER;
|
||||
c.members_field = WriteableScope.create(c);
|
||||
tree.packge.package_info = c;
|
||||
@ -495,7 +495,7 @@ public class Enter extends JCTree.Visitor {
|
||||
// Fill out class fields.
|
||||
c.completer = Completer.NULL_COMPLETER; // do not allow the initial completer linger on.
|
||||
c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree);
|
||||
c.sourcefile = env.toplevel.sourcefile;
|
||||
c.classfile = c.sourcefile = env.toplevel.sourcefile;
|
||||
c.members_field = WriteableScope.create(c);
|
||||
c.clearAnnotationMetadata();
|
||||
|
||||
|
@ -329,7 +329,7 @@ public class Modules extends JCTree.Visitor {
|
||||
}
|
||||
}
|
||||
sym.completer = getSourceCompleter(toplevel);
|
||||
sym.module_info.sourcefile = toplevel.sourcefile;
|
||||
sym.module_info.classfile = sym.module_info.sourcefile = toplevel.sourcefile;
|
||||
decl.sym = sym;
|
||||
|
||||
if (multiModuleMode || modules.isEmpty()) {
|
||||
|
@ -727,6 +727,30 @@ public class JavacElements implements Elements {
|
||||
return (msym.flags() & Flags.AUTOMATIC_MODULE) != 0;
|
||||
}
|
||||
|
||||
@Override @DefinedBy(Api.LANGUAGE_MODEL)
|
||||
public JavaFileObject getFileObjectOf(Element e) {
|
||||
Symbol sym = (Symbol) e;
|
||||
return switch(sym.kind) {
|
||||
case PCK -> {
|
||||
PackageSymbol psym = (PackageSymbol) sym;
|
||||
if (psym.package_info == null) {
|
||||
yield null;
|
||||
}
|
||||
yield psym.package_info.classfile;
|
||||
}
|
||||
|
||||
case MDL -> {
|
||||
ModuleSymbol msym = (ModuleSymbol) sym;
|
||||
if (msym.module_info == null) {
|
||||
yield null;
|
||||
}
|
||||
yield msym.module_info.classfile;
|
||||
}
|
||||
case TYP -> ((ClassSymbol) sym).classfile;
|
||||
default -> sym.enclClass().classfile;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tree node and compilation unit corresponding to this
|
||||
* element, or null if they can't be found.
|
||||
|
@ -0,0 +1,596 @@
|
||||
/*
|
||||
* Copyright (c) 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 8224922
|
||||
* @summary Verify the behavior of the Elements.getFileObjectOf
|
||||
* @library /tools/lib
|
||||
* @modules jdk.compiler/com.sun.tools.javac.api
|
||||
* jdk.compiler/com.sun.tools.javac.main
|
||||
* jdk.jdeps/com.sun.tools.classfile
|
||||
* @build toolbox.ToolBox toolbox.JavacTask toolbox.TestRunner
|
||||
* @build TestFileObjectOf
|
||||
* @run main TestFileObjectOf
|
||||
*/
|
||||
|
||||
import com.sun.source.tree.VariableTree;
|
||||
import com.sun.source.util.TreePath;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.processing.*;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.util.Elements;
|
||||
|
||||
import com.sun.source.util.TreePathScanner;
|
||||
import com.sun.source.util.Trees;
|
||||
import javax.tools.JavaFileObject;
|
||||
import toolbox.JarTask;
|
||||
import toolbox.JavacTask;
|
||||
import toolbox.Task;
|
||||
import toolbox.TestRunner;
|
||||
import toolbox.ToolBox;
|
||||
|
||||
public class TestFileObjectOf extends TestRunner {
|
||||
|
||||
private final ToolBox tb;
|
||||
|
||||
TestFileObjectOf() {
|
||||
super(System.err);
|
||||
tb = new ToolBox();
|
||||
}
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
new TestFileObjectOf().runTests(m -> new Object[] { Paths.get(m.getName()) });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSourceFiles(Path base) throws Exception {
|
||||
Path src = base.resolve("src");
|
||||
tb.writeJavaFiles(src,
|
||||
"""
|
||||
module m {}
|
||||
""",
|
||||
"""
|
||||
package test;
|
||||
""",
|
||||
"""
|
||||
package test;
|
||||
public class TestClass {
|
||||
int fieldTestClass;
|
||||
TestClass() { }
|
||||
void methodTestClass(int parameterTestClass) {
|
||||
int localTestClass;
|
||||
}
|
||||
public static class InnerClass {
|
||||
int fieldInnerClass;
|
||||
InnerClass() {}
|
||||
void methodInnerClass(int parameterInnerClass) {
|
||||
int localInnerClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
""",
|
||||
"""
|
||||
package test;
|
||||
public enum TestEnum {
|
||||
CONSTANT;
|
||||
}
|
||||
""",
|
||||
"""
|
||||
package test2;
|
||||
public class TestClass2 {}
|
||||
""");
|
||||
Path classes = base.resolve("classes").resolve("m");
|
||||
tb.createDirectories(classes);
|
||||
|
||||
//from source, implicit:
|
||||
{
|
||||
String moduleInfoSource = src.resolve("module-info.java").toUri().toString();
|
||||
String packageInfoSource = src.resolve("test").resolve("package-info.java").toUri().toString();
|
||||
String testClassSource = src.resolve("test").resolve("TestClass.java").toUri().toString();
|
||||
String testEnumSource = src.resolve("test").resolve("TestEnum.java").toUri().toString();
|
||||
String testClass2Source = src.resolve("test2").resolve("TestClass2.java").toUri().toString();
|
||||
|
||||
List<String> log;
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("-Xpkginfo:always",
|
||||
"-processorpath", System.getProperty("test.classes"),
|
||||
"-processor", PrintFiles.class.getName(),
|
||||
"-sourcepath", src.toString())
|
||||
.outdir(classes)
|
||||
.classes("java.lang.Object")
|
||||
.run()
|
||||
.writeAll()
|
||||
.getOutputLines(Task.OutputKind.STDOUT);
|
||||
|
||||
List<String> expected = List.of(
|
||||
"m: " + moduleInfoSource,
|
||||
"test: " + packageInfoSource,
|
||||
"test2: " + "<null>",
|
||||
"TestClass: " + testClassSource,
|
||||
"TestEnum: " + testEnumSource,
|
||||
"TestClass2: " + testClass2Source,
|
||||
"<init>: " + testClassSource,
|
||||
"InnerClass: " + testClassSource,
|
||||
"fieldTestClass: " + testClassSource,
|
||||
"methodTestClass: " + testClassSource,
|
||||
"parameterTestClass: " + testClassSource,
|
||||
"localTestClass: " + testClassSource,
|
||||
"<init>: " + testEnumSource,
|
||||
"CONSTANT: " + testEnumSource,
|
||||
"valueOf: " + testEnumSource,
|
||||
"values: " + testEnumSource,
|
||||
"<init>: " + testClass2Source,
|
||||
"<init>: " + testClassSource,
|
||||
"fieldInnerClass: " + testClassSource,
|
||||
"methodInnerClass: " + testClassSource,
|
||||
"parameterInnerClass: " + testClassSource,
|
||||
"localInnerClass: " + testClassSource
|
||||
);
|
||||
|
||||
if (!expected.equals(log))
|
||||
throw new AssertionError("expected output not found: " + log);
|
||||
}
|
||||
|
||||
tb.cleanDirectory(classes);
|
||||
|
||||
//from source, explicit:
|
||||
{
|
||||
String moduleInfoSource = src.resolve("module-info.java").toUri().toString();
|
||||
String packageInfoSource = src.resolve("test").resolve("package-info.java").toUri().toString();
|
||||
String testClassSource = src.resolve("test").resolve("TestClass.java").toUri().toString();
|
||||
String testEnumSource = src.resolve("test").resolve("TestEnum.java").toUri().toString();
|
||||
String testClass2Source = src.resolve("test2").resolve("TestClass2.java").toUri().toString();
|
||||
|
||||
List<String> log;
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("-Xpkginfo:always",
|
||||
"-processorpath", System.getProperty("test.classes"),
|
||||
"-processor", PrintFiles.class.getName())
|
||||
.outdir(classes)
|
||||
.files(tb.findJavaFiles(src))
|
||||
.run()
|
||||
.writeAll()
|
||||
.getOutputLines(Task.OutputKind.STDOUT);
|
||||
|
||||
List<String> expected = List.of(
|
||||
"m: " + moduleInfoSource,
|
||||
"test: " + packageInfoSource,
|
||||
"test2: " + "<null>",
|
||||
"TestClass: " + testClassSource,
|
||||
"TestEnum: " + testEnumSource,
|
||||
"TestClass2: " + testClass2Source,
|
||||
"<init>: " + testClassSource,
|
||||
"InnerClass: " + testClassSource,
|
||||
"fieldTestClass: " + testClassSource,
|
||||
"methodTestClass: " + testClassSource,
|
||||
"parameterTestClass: " + testClassSource,
|
||||
"localTestClass: " + testClassSource,
|
||||
"<init>: " + testEnumSource,
|
||||
"CONSTANT: " + testEnumSource,
|
||||
"valueOf: " + testEnumSource,
|
||||
"values: " + testEnumSource,
|
||||
"<init>: " + testClass2Source,
|
||||
"<init>: " + testClassSource,
|
||||
"fieldInnerClass: " + testClassSource,
|
||||
"methodInnerClass: " + testClassSource,
|
||||
"parameterInnerClass: " + testClassSource,
|
||||
"localInnerClass: " + testClassSource
|
||||
);
|
||||
|
||||
if (!expected.equals(log))
|
||||
throw new AssertionError("expected output not found: " + log);
|
||||
}
|
||||
|
||||
//from class:
|
||||
{
|
||||
String moduleInfoSource = classes.resolve("module-info.class").toUri().toString();
|
||||
String packageInfoSource = classes.resolve("test").resolve("package-info.class").toUri().toString();
|
||||
String testClassSource = classes.resolve("test").resolve("TestClass.class").toUri().toString();
|
||||
String testInnerClassSource = classes.resolve("test").resolve("TestClass$InnerClass.class").toUri().toString();
|
||||
String testEnumSource = classes.resolve("test").resolve("TestEnum.class").toUri().toString();
|
||||
String testClass2Source = classes.resolve("test2").resolve("TestClass2.class").toUri().toString();
|
||||
|
||||
List<String> log;
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("-processorpath", System.getProperty("test.classes"),
|
||||
"-processor", PrintFiles.class.getName(),
|
||||
"--module-path", classes.toString(),
|
||||
"--add-modules", "m")
|
||||
.outdir(classes)
|
||||
.classes("java.lang.Object")
|
||||
.run()
|
||||
.writeAll()
|
||||
.getOutputLines(Task.OutputKind.STDOUT);
|
||||
|
||||
List<String> expected = List.of(
|
||||
"m: " + moduleInfoSource,
|
||||
"test: " + packageInfoSource,
|
||||
"test2: " + "<null>",
|
||||
"TestClass: " + testClassSource,
|
||||
"TestEnum: " + testEnumSource,
|
||||
"TestClass2: " + testClass2Source,
|
||||
"<init>: " + testClassSource,
|
||||
"InnerClass: " + testInnerClassSource,
|
||||
"fieldTestClass: " + testClassSource,
|
||||
"methodTestClass: " + testClassSource,
|
||||
"<clinit>: " + testEnumSource,
|
||||
"<init>: " + testEnumSource,
|
||||
"CONSTANT: " + testEnumSource,
|
||||
"valueOf: " + testEnumSource,
|
||||
"values: " + testEnumSource,
|
||||
"<init>: " + testClass2Source,
|
||||
"<init>: " + testInnerClassSource,
|
||||
"fieldInnerClass: " + testInnerClassSource,
|
||||
"methodInnerClass: " + testInnerClassSource
|
||||
);
|
||||
|
||||
if (!expected.equals(log))
|
||||
throw new AssertionError("expected output not found: " + log);
|
||||
}
|
||||
}
|
||||
|
||||
@SupportedAnnotationTypes("*")
|
||||
@SupportedOptions("fromClass")
|
||||
public static final class PrintFiles extends AbstractProcessor {
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
if (!roundEnv.processingOver())
|
||||
return false;
|
||||
|
||||
Elements elements = processingEnv.getElementUtils();
|
||||
Trees trees = Trees.instance(processingEnv);
|
||||
Queue<Element> q = new ArrayDeque<>();
|
||||
q.add(elements.getModuleElement("m"));
|
||||
|
||||
while (!q.isEmpty()) {
|
||||
Element currentElement = q.remove();
|
||||
|
||||
handleDeclaration(currentElement);
|
||||
|
||||
switch (currentElement.getKind()) {
|
||||
case METHOD -> {
|
||||
ExecutableElement method = (ExecutableElement) currentElement;
|
||||
TreePath tp = trees.getPath(method);
|
||||
if (tp != null) {
|
||||
new TreePathScanner<>() {
|
||||
@Override
|
||||
public Object visitVariable(VariableTree node, Object p) {
|
||||
Element el = trees.getElement(getCurrentPath());
|
||||
handleDeclaration(el);
|
||||
return super.visitVariable(node, p);
|
||||
}
|
||||
}.scan(tp, null);
|
||||
}
|
||||
}
|
||||
case MODULE -> {
|
||||
q.add(elements.getPackageElement("test"));
|
||||
q.add(elements.getPackageElement("test2"));
|
||||
}
|
||||
default ->
|
||||
currentElement.getEnclosedElements()
|
||||
.stream()
|
||||
.sorted((e1, e2) -> e1.getSimpleName().toString().compareTo(e2.getSimpleName().toString()))
|
||||
.forEach(q::add);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void handleDeclaration(Element el) {
|
||||
Elements elements = processingEnv.getElementUtils();
|
||||
JavaFileObject fileObjects = elements.getFileObjectOf(el);
|
||||
System.out.println(el.getSimpleName() + ": " + (fileObjects != null ? fileObjects.toUri().toString() : "<null>"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
return SourceVersion.latestSupported();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnnamed(Path base) throws Exception {
|
||||
Path src = base.resolve("src");
|
||||
tb.writeJavaFiles(src,
|
||||
"""
|
||||
public class TestClass {
|
||||
}
|
||||
""");
|
||||
Path classes = base.resolve("classes");
|
||||
tb.createDirectories(classes);
|
||||
|
||||
//from source, implicit:
|
||||
{
|
||||
String testClassSource = src.resolve("TestClass.java").toUri().toString();
|
||||
|
||||
List<String> log;
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("-Xpkginfo:always",
|
||||
"-classpath", "",
|
||||
"-processorpath", System.getProperty("test.classes"),
|
||||
"-processor", UnnamedPrintFiles.class.getName(),
|
||||
"-sourcepath", src.toString())
|
||||
.outdir(classes)
|
||||
.classes("java.lang.Object")
|
||||
.run()
|
||||
.writeAll()
|
||||
.getOutputLines(Task.OutputKind.STDOUT);
|
||||
|
||||
List<String> expected = List.of(
|
||||
": " + "<null>",
|
||||
": " + "<null>",
|
||||
"TestClass: " + testClassSource,
|
||||
"<init>: " + testClassSource
|
||||
);
|
||||
|
||||
if (!expected.equals(log))
|
||||
throw new AssertionError("expected output not found: " + log);
|
||||
}
|
||||
|
||||
tb.cleanDirectory(classes);
|
||||
|
||||
//from source, explicit:
|
||||
{
|
||||
String testClassSource = src.resolve("TestClass.java").toUri().toString();
|
||||
|
||||
List<String> log;
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("-Xpkginfo:always",
|
||||
"-classpath", "",
|
||||
"-processorpath", System.getProperty("test.classes"),
|
||||
"-processor", UnnamedPrintFiles.class.getName())
|
||||
.outdir(classes)
|
||||
.files(tb.findJavaFiles(src))
|
||||
.run()
|
||||
.writeAll()
|
||||
.getOutputLines(Task.OutputKind.STDOUT);
|
||||
|
||||
List<String> expected = List.of(
|
||||
": " + "<null>",
|
||||
": " + "<null>",
|
||||
"TestClass: " + testClassSource,
|
||||
"<init>: " + testClassSource
|
||||
);
|
||||
|
||||
if (!expected.equals(log))
|
||||
throw new AssertionError("expected output not found: " + log);
|
||||
}
|
||||
|
||||
//from class:
|
||||
{
|
||||
String testClassSource = classes.resolve("TestClass.class").toUri().toString();
|
||||
|
||||
List<String> log;
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("-processorpath", System.getProperty("test.classes"),
|
||||
"-processor", UnnamedPrintFiles.class.getName(),
|
||||
"-classpath", classes.toString())
|
||||
.outdir(classes)
|
||||
.classes("java.lang.Object")
|
||||
.run()
|
||||
.writeAll()
|
||||
.getOutputLines(Task.OutputKind.STDOUT);
|
||||
|
||||
List<String> expected = List.of(
|
||||
": " + "<null>",
|
||||
": " + "<null>",
|
||||
"TestClass: " + testClassSource,
|
||||
"<init>: " + testClassSource
|
||||
);
|
||||
|
||||
if (!expected.equals(log))
|
||||
throw new AssertionError("expected output not found: " + log);
|
||||
}
|
||||
}
|
||||
|
||||
@SupportedAnnotationTypes("*")
|
||||
@SupportedOptions("fromClass")
|
||||
public static final class UnnamedPrintFiles extends AbstractProcessor {
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
if (!roundEnv.processingOver())
|
||||
return false;
|
||||
|
||||
Elements elements = processingEnv.getElementUtils();
|
||||
Trees trees = Trees.instance(processingEnv);
|
||||
Queue<Element> q = new ArrayDeque<>();
|
||||
q.add(elements.getModuleElement(""));
|
||||
|
||||
while (!q.isEmpty()) {
|
||||
Element currentElement = q.remove();
|
||||
|
||||
handleDeclaration(currentElement);
|
||||
|
||||
switch (currentElement.getKind()) {
|
||||
case METHOD -> {
|
||||
ExecutableElement method = (ExecutableElement) currentElement;
|
||||
TreePath tp = trees.getPath(method);
|
||||
if (tp != null) {
|
||||
new TreePathScanner<>() {
|
||||
@Override
|
||||
public Object visitVariable(VariableTree node, Object p) {
|
||||
Element el = trees.getElement(getCurrentPath());
|
||||
handleDeclaration(el);
|
||||
return super.visitVariable(node, p);
|
||||
}
|
||||
}.scan(tp, null);
|
||||
}
|
||||
}
|
||||
case MODULE -> {
|
||||
q.add(elements.getPackageElement(""));
|
||||
}
|
||||
default ->
|
||||
currentElement.getEnclosedElements()
|
||||
.stream()
|
||||
.sorted((e1, e2) -> e1.getSimpleName().toString().compareTo(e2.getSimpleName().toString()))
|
||||
.forEach(q::add);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void handleDeclaration(Element el) {
|
||||
Elements elements = processingEnv.getElementUtils();
|
||||
JavaFileObject fileObjects = elements.getFileObjectOf(el);
|
||||
System.out.println(el.getSimpleName() + ": " + (fileObjects != null ? fileObjects.toUri().toString() : "<null>"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
return SourceVersion.latestSupported();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutomaticModule(Path base) throws Exception {
|
||||
Path src = base.resolve("src");
|
||||
tb.writeJavaFiles(src,
|
||||
"""
|
||||
package test;
|
||||
public class TestClass {
|
||||
}
|
||||
""");
|
||||
Path classes = base.resolve("classes");
|
||||
tb.createDirectories(classes);
|
||||
|
||||
Path module = base.resolve("m.jar");
|
||||
|
||||
new JavacTask(tb)
|
||||
.options("-classpath", "")
|
||||
.outdir(classes)
|
||||
.files(tb.findJavaFiles(src))
|
||||
.run()
|
||||
.writeAll();
|
||||
new JarTask(tb, module)
|
||||
.baseDir(classes)
|
||||
.files(".")
|
||||
.run();
|
||||
|
||||
String testClassSource = "jar:" + module.toUri().toString() + "!/test/TestClass.class";
|
||||
|
||||
List<String> log;
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("-processorpath", System.getProperty("test.classes"),
|
||||
"-processor", AutomaticModulePrintFiles.class.getName(),
|
||||
"--module-path", module.toString(),
|
||||
"--add-modules", "m")
|
||||
.outdir(classes)
|
||||
.classes("java.lang.Object")
|
||||
.run()
|
||||
.writeAll()
|
||||
.getOutputLines(Task.OutputKind.STDOUT);
|
||||
|
||||
List<String> expected = List.of(
|
||||
"m: " + "<null>",
|
||||
"test: " + "<null>",
|
||||
"TestClass: " + testClassSource,
|
||||
"<init>: " + testClassSource
|
||||
);
|
||||
|
||||
if (!expected.equals(log))
|
||||
throw new AssertionError("expected output not found: " + log);
|
||||
}
|
||||
|
||||
@SupportedAnnotationTypes("*")
|
||||
@SupportedOptions("fromClass")
|
||||
public static final class AutomaticModulePrintFiles extends AbstractProcessor {
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
if (!roundEnv.processingOver())
|
||||
return false;
|
||||
|
||||
Elements elements = processingEnv.getElementUtils();
|
||||
Trees trees = Trees.instance(processingEnv);
|
||||
Queue<Element> q = new ArrayDeque<>();
|
||||
q.add(elements.getModuleElement("m"));
|
||||
|
||||
while (!q.isEmpty()) {
|
||||
Element currentElement = q.remove();
|
||||
|
||||
handleDeclaration(currentElement);
|
||||
|
||||
switch (currentElement.getKind()) {
|
||||
case METHOD -> {
|
||||
ExecutableElement method = (ExecutableElement) currentElement;
|
||||
TreePath tp = trees.getPath(method);
|
||||
if (tp != null) {
|
||||
new TreePathScanner<>() {
|
||||
@Override
|
||||
public Object visitVariable(VariableTree node, Object p) {
|
||||
Element el = trees.getElement(getCurrentPath());
|
||||
handleDeclaration(el);
|
||||
return super.visitVariable(node, p);
|
||||
}
|
||||
}.scan(tp, null);
|
||||
}
|
||||
}
|
||||
case MODULE -> {
|
||||
q.add(elements.getPackageElement("test"));
|
||||
}
|
||||
default ->
|
||||
currentElement.getEnclosedElements()
|
||||
.stream()
|
||||
.sorted((e1, e2) -> e1.getSimpleName().toString().compareTo(e2.getSimpleName().toString()))
|
||||
.forEach(q::add);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void handleDeclaration(Element el) {
|
||||
Elements elements = processingEnv.getElementUtils();
|
||||
JavaFileObject fileObjects = elements.getFileObjectOf(el);
|
||||
System.out.println(el.getSimpleName() + ": " + (fileObjects != null ? fileObjects.toUri().toString() : "<null>"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
return SourceVersion.latestSupported();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -89,7 +89,8 @@ public class NoPrivateTypesExported extends JavacTestingAbstractProcessor {
|
||||
"java.text.",
|
||||
"java.util.",
|
||||
"javax.lang.model.",
|
||||
"javax.annotation.processing.SupportedSourceVersion"
|
||||
"javax.annotation.processing.SupportedSourceVersion",
|
||||
"javax.tools.JavaFileObject"
|
||||
));
|
||||
Set<String> javaxToolsProcessingAcceptableTemp = new HashSet<>();
|
||||
javaxToolsProcessingAcceptableTemp.addAll(javaxLangModelAcceptable);
|
||||
|
Loading…
x
Reference in New Issue
Block a user