Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8224922: Access JavaFileObject from Element(s) #5038

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
7054751
8224922: Access JavaFileObject from Element(s)
jddarcy Aug 6, 2021
e2acf39
Merge branch 'master' into JDK-8224922
jddarcy Aug 10, 2021
312804b
Merge branch 'master' into JDK-8224922
lahodaj Oct 22, 2021
4cf5038
Attempting to improve Elements.getFileObjectOf
lahodaj Oct 26, 2021
a9a4f36
Merge branch 'master' into JDK-8224922
jddarcy Oct 27, 2021
d5ae4bd
Fixing behavior for nested classes
lahodaj Oct 27, 2021
acecb8f
Merge remote-tracking branch 'JDK-8224922' into JDK-8224922
lahodaj Oct 27, 2021
3780012
Attempting to fix tests.
lahodaj Oct 27, 2021
487f477
Merge branch 'master' into JDK-8224922
jddarcy Oct 30, 2021
9cdb6c9
Update spec.
jddarcy Oct 30, 2021
d1b6fab
Merge branch 'master' into JDK-8224922
jddarcy Nov 1, 2021
fad7bb5
Merge remote-tracking branch 'darcy/JDK-8224922' into JDK-8224922
lahodaj Nov 2, 2021
f744fe7
Cleanup.
lahodaj Nov 2, 2021
766b9b9
Cleanup.
lahodaj Nov 2, 2021
b301d92
Test cleanup.
lahodaj Nov 2, 2021
1584c4b
Merge pull request #3 from lahodaj/JDK-8224922
jddarcy Nov 2, 2021
50ce61b
Appease jcheck.
jddarcy Nov 2, 2021
766ef36
Merge branch 'master' into JDK-8224922
jddarcy Nov 2, 2021
f9a99b8
Merge branch 'master' into JDK-8224922
jddarcy Nov 3, 2021
1599509
Ancillary improvements to the spec.
jddarcy Nov 3, 2021
4619a6a
Merge branch 'master' into JDK-8224922
jddarcy Nov 4, 2021
75e179e
Merge branch 'master' into JDK-8224922
jddarcy Nov 5, 2021
9f5dd46
Merge branch 'master' into JDK-8224922
jddarcy Nov 9, 2021
75b6264
Merge branch 'master' into JDK-8224922
jddarcy Nov 15, 2021
a18adfe
Merge branch 'master' into JDK-8224922
jddarcy Nov 16, 2021
1d2492d
Update spec for unnamed packages and modules.
jddarcy Nov 16, 2021
7ab409a
Adjusting to the specification requiring returning of the outtermost …
lahodaj Nov 16, 2021
9ca744b
Using a specific class file as the source.
lahodaj Nov 16, 2021
bc9b6b1
Refine spec.
jddarcy Nov 16, 2021
cfe2a8f
Add explicit spec coverage of auxillary types in package-info files.
jddarcy Nov 17, 2021
8cdbf52
Refine spec.
jddarcy Nov 17, 2021
1f5e548
Add spec for automatic modules.
jddarcy Nov 17, 2021
be16769
Merge pull request #5 from lahodaj/JDK-8224922-3
jddarcy Nov 17, 2021
f9c5888
Merge branch 'master' into JDK-8224922
jddarcy Nov 17, 2021
7f8c7f6
Merge branch 'JDK-8224922' of https://github.com/jddarcy/jdk into JDK…
jddarcy Nov 17, 2021
56953b4
Fix spec typo; update copyrights.
jddarcy Nov 17, 2021
04e8ea4
Reflow paragraphs.
jddarcy Nov 17, 2021
4e44aaa
Adding forgotten file/fixing test.
lahodaj Nov 18, 2021
a62ff19
Merge pull request #6 from lahodaj/JDK-8224922-4
jddarcy Nov 18, 2021
2b268a2
Attempting to fix a Windows test.
lahodaj Nov 18, 2021
cd9f6bc
Merge pull request #7 from lahodaj/JDK-8224922-5
jddarcy Nov 18, 2021
55759b2
Merge branch 'master' into JDK-8224922
jddarcy Nov 21, 2021
ff1badb
Respond to review feedback.
jddarcy Nov 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
Expand Down Expand Up @@ -69,7 +69,13 @@
* 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
*
Expand Down
@@ -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
Expand Down Expand Up @@ -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
Expand Down
@@ -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
Expand Down Expand Up @@ -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()
Expand Down
Expand Up @@ -761,4 +761,100 @@ default RecordComponentElement recordComponentFor(ExecutableElement accessor) {
}
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
Expand Down Expand Up @@ -376,7 +376,7 @@ public void visitTopLevel(JCCompilationUnit tree) {
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;
Expand Down Expand Up @@ -495,7 +495,7 @@ public void visitClassDef(JCClassDecl tree) {
// 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();

Expand Down
Expand Up @@ -329,7 +329,7 @@ private void enterModule(JCCompilationUnit toplevel, ClassSymbol c, Set<ModuleSy
}
}
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()) {
Expand Down
Expand Up @@ -727,6 +727,30 @@ public boolean isAutomaticModule(ModuleElement module) {
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.
Expand Down