ModuleElement.java revision 4150:fff0714129d8
12061Sjkh/*
229938Smckay * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
32061Sjkh * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
42061Sjkh *
515603Smarkm * This code is free software; you can redistribute it and/or modify it
629930Smarkm * under the terms of the GNU General Public License version 2 only, as
720710Sasami * published by the Free Software Foundation.  Oracle designates this
820710Sasami * particular file as subject to the "Classpath" exception as provided
93197Scsgr * by Oracle in the LICENSE file that accompanied this code.
102061Sjkh *
1112483Speter * This code is distributed in the hope that it will be useful, but WITHOUT
122160Scsgr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
132834Swollman * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
142061Sjkh * version 2 for more details (a copy is included in the LICENSE file that
152061Sjkh * accompanied this code).
162160Scsgr *
1717308Speter * You should have received a copy of the GNU General Public License version
1819320Sadam * 2 along with this work; if not, write to the Free Software Foundation,
1927788Sasami * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2027788Sasami *
2125980Sasami * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
221594Srgrimes * or visit www.oracle.com if you need additional information or have any
2317308Speter * questions.
2417308Speter */
2527910Sasami
2627910Sasamipackage javax.lang.model.element;
2727910Sasami
2817308Speterimport java.util.List;
2917308Speter
3017308Speter/**
3119175Sbde * Represents a module program element.  Provides access to
3219175Sbde * information about the module, its directives, and its members.
3319175Sbde *
3419175Sbde * @see javax.lang.model.util.Elements#getModuleOf
3517308Speter * @since 9
3627910Sasami * @jls 7.7 Module Declarations
3725647Sbde * @spec JPMS
3827910Sasami */
3917308Speterpublic interface ModuleElement extends Element, QualifiedNameable {
402061Sjkh
412061Sjkh    /**
421594Srgrimes     * Returns the fully qualified name of this module.  For an
4328531Sbde     * {@linkplain #isUnnamed() unnamed module}, an empty name is returned.
4428531Sbde     *
4528531Sbde     * @return the fully qualified name of this module, or an
4628531Sbde     * empty name if this is an unnamed module
4728531Sbde     */
4828531Sbde    @Override
4928531Sbde    Name getQualifiedName();
5028531Sbde
517407Srgrimes    /**
527108Sphk     * Returns the simple name of this module.  For an {@linkplain
537108Sphk     * #isUnnamed() unnamed module}, an empty name is returned.
547108Sphk     *
557407Srgrimes     * @return the simple name of this module or an empty name if
567407Srgrimes     * this is an unnamed module
577407Srgrimes     */
587108Sphk    @Override
592061Sjkh    Name getSimpleName();
602061Sjkh
612061Sjkh    /**
6217308Speter     * Returns the packages within this module.
632061Sjkh     * @return the packages within this module
642061Sjkh     */
652061Sjkh    @Override
662061Sjkh    List<? extends Element> getEnclosedElements();
672061Sjkh
6829930Smarkm    /**
6929930Smarkm     * Returns {@code true} if this is an open module and {@code
702626Scsgr     * false} otherwise.
712061Sjkh     *
722061Sjkh     * @return {@code true} if this is an open module and {@code
732061Sjkh     * false} otherwise
742061Sjkh     */ // TODO: add @jls to unnamed module section
752061Sjkh    boolean isOpen();
762061Sjkh
7719320Sadam    /**
782061Sjkh     * Returns {@code true} if this is an unnamed module and {@code
792061Sjkh     * false} otherwise.
802061Sjkh     *
812061Sjkh     * @return {@code true} if this is an unnamed module and {@code
822061Sjkh     * false} otherwise
832061Sjkh     */ // TODO: add @jls to unnamed module section
842061Sjkh    boolean isUnnamed();
852061Sjkh
862061Sjkh    /**
872061Sjkh     * Returns {@code null} since a module is not enclosed by another
882061Sjkh     * element.
892834Swollman     *
902834Swollman     * @return {@code null}
912834Swollman     */
922834Swollman    @Override
932834Swollman    Element getEnclosingElement();
942834Swollman
951594Srgrimes    /**
964486Sphk     * Returns the directives contained in the declaration of this module.
974486Sphk     * @return  the directives in the declaration of this module
984486Sphk     */
994486Sphk    List<? extends Directive> getDirectives();
1004486Sphk
1012061Sjkh    /**
1022061Sjkh     * The {@code kind} of a directive.
10325979Sjkh     *
10425979Sjkh     * <p>Note that it is possible additional directive kinds will be added
10525979Sjkh     * to accommodate new, currently unknown, language structures added to
10625979Sjkh     * future versions of the Java&trade; programming language.
1072061Sjkh     *
10825979Sjkh     * @since 9
1092061Sjkh     * @spec JPMS
1102061Sjkh     */
11117308Speter    enum DirectiveKind {
1122061Sjkh        /** A "requires (static|transitive)* module-name" directive. */
1132061Sjkh        REQUIRES,
1142061Sjkh        /** An "exports package-name [to module-name-list]" directive. */
1152061Sjkh        EXPORTS,
1162061Sjkh        /** An "opens package-name [to module-name-list]" directive. */
11712483Speter        OPENS,
11812483Speter        /** A "uses service-name" directive. */
11912483Speter        USES,
12012483Speter        /** A "provides service-name with implementation-name" directive. */
1212061Sjkh        PROVIDES
1222061Sjkh    };
1238854Srgrimes
1242061Sjkh    /**
1252061Sjkh     * Represents a directive within the declaration of this
12612483Speter     * module. The directives of a module declaration configure the
1272061Sjkh     * module in the Java Platform Module System.
12827910Sasami     *
12927910Sasami     * @since 9
13018714Sache     * @spec JPMS
13117308Speter     */
13217308Speter    interface Directive {
13317308Speter        /**
13417308Speter         * Returns the {@code kind} of this directive.
13521536Sjmacd         *
13615603Smarkm         * @return the kind of this directive
13717308Speter         */
13817308Speter        DirectiveKind getKind();
13917308Speter
14017308Speter        /**
14117308Speter         * Applies a visitor to this directive.
14217308Speter         *
14317308Speter         * @param <R> the return type of the visitor's methods
14417308Speter         * @param <P> the type of the additional parameter to the visitor's methods
14517308Speter         * @param v   the visitor operating on this directive
14618362Sjkh         * @param p   additional parameter to the visitor
14719966Sache         * @return a visitor-specified result
14818362Sjkh         */
14917308Speter        <R, P> R accept(DirectiveVisitor<R, P> v, P p);
15027910Sasami    }
15117308Speter
15217308Speter    /**
15317308Speter     * A visitor of module directives, in the style of the visitor design
15428803Speter     * pattern.  Classes implementing this interface are used to operate
15527910Sasami     * on a directive when the kind of directive is unknown at compile time.
15628803Speter     * When a visitor is passed to a directive's {@link Directive#accept
15728803Speter     * accept} method, the <code>visit<i>Xyz</i></code> method applicable
15827910Sasami     * to that directive is invoked.
15917308Speter     *
1602061Sjkh     * <p> Classes implementing this interface may or may not throw a
16127910Sasami     * {@code NullPointerException} if the additional parameter {@code p}
1622061Sjkh     * is {@code null}; see documentation of the implementing class for
16328803Speter     * details.
16427910Sasami     *
1652061Sjkh     * <p> <b>WARNING:</b> It is possible that methods will be added to
16617308Speter     * this interface to accommodate new, currently unknown, language
16727910Sasami     * structures added to future versions of the Java&trade; programming
16817308Speter     * language. Methods to accommodate new language constructs will
16927910Sasami     * be added in a source <em>compatible</em> way using
17027910Sasami     * <em>default methods</em>.
17127910Sasami     *
17217308Speter     * @param <R> the return type of this visitor's methods.  Use {@link
17327910Sasami     *            Void} for visitors that do not need to return results.
17417308Speter     * @param <P> the type of the additional parameter to this visitor's
17527910Sasami     *            methods.  Use {@code Void} for visitors that do not need an
17627910Sasami     *            additional parameter.
17727910Sasami     *
17827910Sasami     * @since 9
17927910Sasami     * @spec JPMS
18027910Sasami     */
18127910Sasami    interface DirectiveVisitor<R, P> {
18227910Sasami        /**
18327910Sasami         * Visits any directive as if by passing itself to that
18427910Sasami         * directive's {@link Directive#accept accept} method and passing
18527910Sasami         * {@code null} for the additional parameter.
18627910Sasami         * The invocation {@code v.visit(d)} is equivalent to
18727910Sasami         * {@code d.accept(v, null)}.
18827910Sasami         * @param d  the directive to visit
18927910Sasami         * @return a visitor-specified result
19027910Sasami         * @implSpec This implementation is {@code visit(d, null)}
19127910Sasami         */
19227910Sasami        default R visit(Directive d) {
19327910Sasami            return d.accept(this, null);
19427910Sasami        }
19527910Sasami
19627910Sasami        /**
19727910Sasami         * Visits any directive as if by passing itself to that
19827910Sasami         * directive's {@link Directive#accept accept} method.
19927910Sasami         * The invocation {@code v.visit(d, p)} is equivalent to
20027910Sasami         * {@code d.accept(v, p)}.
20127910Sasami         * @param d  the directive to visit
20227910Sasami         * @param p  a visitor-specified parameter
20327910Sasami         * @return a visitor-specified result
20427910Sasami         */
20527910Sasami        default R visit(Directive d, P p) {
20627910Sasami            return d.accept(this, p);
20727910Sasami        }
20827910Sasami
20927910Sasami        /**
21027910Sasami         * Visits a {@code requires} directive.
21127910Sasami         * @param d  the directive to visit
21217308Speter         * @param p  a visitor-specified parameter
21317308Speter         * @return a visitor-specified result
21427910Sasami         */
21517308Speter        R visitRequires(RequiresDirective d, P p);
21627910Sasami
21727910Sasami        /**
21827910Sasami         * Visits an {@code exports} directive.
21927910Sasami         * @param d  the directive to visit
22017466Speter         * @param p  a visitor-specified parameter
22117308Speter         * @return a visitor-specified result
22227910Sasami         */
22317308Speter        R visitExports(ExportsDirective d, P p);
22427910Sasami
22527910Sasami        /**
22629130Speter         * Visits an {@code opens} directive.
22729872Sjkh         * @param d  the directive to visit
22829130Speter         * @param p  a visitor-specified parameter
22928531Sbde         * @return a visitor-specified result
23017308Speter         */
23117308Speter        R visitOpens(OpensDirective d, P p);
23227910Sasami
23317308Speter        /**
23427910Sasami         * Visits a {@code uses} directive.
23527910Sasami         * @param d  the directive to visit
23617308Speter         * @param p  a visitor-specified parameter
23717308Speter         * @return a visitor-specified result
23827910Sasami         */
23917308Speter        R visitUses(UsesDirective d, P p);
24027910Sasami
24127910Sasami        /**
24227910Sasami         * Visits a {@code provides} directive.
24317308Speter         * @param d  the directive to visit
24417308Speter         * @param p  a visitor-specified parameter
24527910Sasami         * @return a visitor-specified result
24617308Speter         */
24727910Sasami        R visitProvides(ProvidesDirective d, P p);
24827910Sasami
24917308Speter        /**
25017308Speter         * Visits an unknown directive.
25127910Sasami         * This can occur if the language evolves and new kinds of directive are added.
25217308Speter         * @param d  the directive to visit
25327910Sasami         * @param p  a visitor-specified parameter
25417308Speter         * @return a visitor-specified result
25517308Speter         * @throws UnknownDirectiveException a visitor implementation may optionally throw this exception
25627910Sasami         * @implSpec This implementation throws {@code new UnknownDirectiveException(d, p)}.
25717308Speter         */
25827910Sasami        default R visitUnknown(Directive d, P p) {
25917308Speter            throw new UnknownDirectiveException(d, p);
26017308Speter        }
26127910Sasami    }
26217308Speter
26329503Sbde    /**
26417962Speter     * A dependency of a module.
26517962Speter     * @since 9
26627910Sasami     * @spec JPMS
26717962Speter     */
26827910Sasami    interface RequiresDirective extends Directive {
26917962Speter        /**
27017962Speter         * Returns whether or not this is a static dependency.
27127910Sasami         * @return whether or not this is a static dependency
27217962Speter         */
27327910Sasami        boolean isStatic();
27417962Speter
27517962Speter        /**
27627910Sasami         * Returns whether or not this is a transitive dependency.
27717962Speter         * @return whether or not this is a transitive dependency
27827910Sasami         */
27917308Speter        boolean isTransitive();
28017308Speter
28127910Sasami        /**
28217308Speter         * Returns the module that is required
28327910Sasami         * @return the module that is required
28417308Speter         */
28517308Speter        ModuleElement getDependency();
28627910Sasami    }
28717962Speter
28827910Sasami    /**
2892061Sjkh     * An exported package of a module.
29017308Speter     * @since 9
29127910Sasami     * @spec JPMS
29227910Sasami     */
29327910Sasami    interface ExportsDirective extends Directive {
29427910Sasami
29527910Sasami        /**
29627910Sasami         * Returns the package being exported.
29727910Sasami         * @return the package being exported
29827910Sasami         */
29917308Speter        PackageElement getPackage();
30017308Speter
30117308Speter        /**
30217308Speter         * Returns the specific modules to which the package is being exported,
30317308Speter         * or null, if the package is exported to all modules which
30417308Speter         * have readability to this module.
30517308Speter         * @return the specific modules to which the package is being exported
30612483Speter         */
30717308Speter        List<? extends ModuleElement> getTargetModules();
30812483Speter    }
30917308Speter
31012483Speter    /**
3112061Sjkh     * An opened package of a module.
31217962Speter     * @since 9
31317962Speter     * @spec JPMS
31417308Speter     */
31517962Speter    interface OpensDirective extends Directive {
31617962Speter
31717962Speter        /**
31817962Speter         * Returns the package being opened.
31917308Speter         * @return the package being opened
3202061Sjkh         */
32117308Speter        PackageElement getPackage();
32217308Speter
32317308Speter        /**
32417308Speter         * Returns the specific modules to which the package is being open
32517308Speter         * or null, if the package is open all modules which
32617308Speter         * have readability to this module.
3272302Spaul         * @return the specific modules to which the package is being opened
3282302Spaul         */
3292302Spaul        List<? extends ModuleElement> getTargetModules();
3302302Spaul    }
3312302Spaul
33218714Sache    /**
33310760Sache     * An implementation of a service provided by a module.
33418714Sache     * @since 9
3352302Spaul     * @spec JPMS
33610760Sache     */
33718714Sache    interface ProvidesDirective extends Directive {
33810760Sache        /**
33910760Sache         * Returns the service being provided.
3402302Spaul         * @return the service being provided
3412302Spaul         */
3422302Spaul        TypeElement getService();
3432302Spaul
34427790Sasami        /**
3452302Spaul         * Returns the implementations of the service being provided.
3462302Spaul         * @return the implementations of the service being provided
34717308Speter         */
34817308Speter        List<? extends TypeElement> getImplementations();
34917308Speter    }
35017308Speter
35117308Speter    /**
35217308Speter     * A reference to a service used by a module.
3532061Sjkh     * @since 9
35417308Speter     * @spec JPMS
3552061Sjkh     */
35628803Speter    interface UsesDirective extends Directive {
35728803Speter        /**
35828803Speter         * Returns the service that is used.
35928803Speter         * @return the service that is used
36028803Speter         */
36128803Speter        TypeElement getService();
36228803Speter    }
36328803Speter}
36429930Smarkm