Directive.java revision 3294:9adfb22ff08f
140516Swpaul/*
2119868Swpaul * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
340516Swpaul * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
440516Swpaul *
540516Swpaul * This code is free software; you can redistribute it and/or modify it
640516Swpaul * under the terms of the GNU General Public License version 2 only, as
740516Swpaul * published by the Free Software Foundation.  Oracle designates this
840516Swpaul * particular file as subject to the "Classpath" exception as provided
940516Swpaul * by Oracle in the LICENSE file that accompanied this code.
1040516Swpaul *
1140516Swpaul * This code is distributed in the hope that it will be useful, but WITHOUT
1240516Swpaul * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1340516Swpaul * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1440516Swpaul * version 2 for more details (a copy is included in the LICENSE file that
1540516Swpaul * accompanied this code).
1640516Swpaul *
1740516Swpaul * You should have received a copy of the GNU General Public License version
1840516Swpaul * 2 along with this work; if not, write to the Free Software Foundation,
1940516Swpaul * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2040516Swpaul *
2140516Swpaul * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2240516Swpaul * or visit www.oracle.com if you need additional information or have any
2340516Swpaul * questions.
2440516Swpaul */
2540516Swpaul
2640516Swpaulpackage com.sun.tools.javac.code;
2740516Swpaul
2840516Swpaulimport java.util.Collections;
2940516Swpaulimport java.util.EnumSet;
3040516Swpaulimport java.util.Set;
3140516Swpaul
3240516Swpaulimport javax.lang.model.element.ModuleElement;
33122678Sobrienimport javax.lang.model.element.PackageElement;
34122678Sobrienimport javax.lang.model.element.TypeElement;
35122678Sobrien
3640516Swpaulimport com.sun.tools.javac.code.Symbol.ClassSymbol;
37119868Swpaulimport com.sun.tools.javac.code.Symbol.ModuleSymbol;
3840516Swpaulimport com.sun.tools.javac.code.Symbol.PackageSymbol;
39119868Swpaulimport com.sun.tools.javac.util.DefinedBy;
40119868Swpaulimport com.sun.tools.javac.util.DefinedBy.Api;
4140516Swpaulimport com.sun.tools.javac.util.List;
4240516Swpaul
43119868Swpaul
44119868Swpaul/**
45119868Swpaul *  Root class for the directives that may appear in module compilation units.
4640516Swpaul *
4740516Swpaul *  <p><b>This is NOT part of any supported API.
4840516Swpaul *  If you write code that depends on this, you do so at your own risk.
4940516Swpaul *  This code and its internal interfaces are subject to change or
5040516Swpaul *  deletion without notice.</b>
5140516Swpaul */
5240516Swpaulpublic abstract class Directive implements ModuleElement.Directive {
5340516Swpaul
5440516Swpaul    /** Flags for RequiresDirective. */
5540516Swpaul    public enum RequiresFlag {
5641569Swpaul        PUBLIC(0x0020),
5740516Swpaul        SYNTHETIC(0x1000),
5840516Swpaul        MANDATED(0x8000),
5940516Swpaul        EXTRA(0x10000);
6040516Swpaul
6140516Swpaul        // overkill? move to ClassWriter?
6240516Swpaul        public static int value(Set<RequiresFlag> s) {
6340516Swpaul            int v = 0;
6440516Swpaul            for (RequiresFlag f: s)
6540516Swpaul                v |= f.value;
6640516Swpaul            return v;
6740516Swpaul        }
6840516Swpaul
6940516Swpaul        RequiresFlag(int value) {
7040516Swpaul            this.value = value;
7140516Swpaul        }
7240516Swpaul
7340516Swpaul        public final int value;
7440516Swpaul    }
7540516Swpaul
7640516Swpaul    /**
7740516Swpaul     * 'exports' Package ';'
7840516Swpaul     */
7940516Swpaul    public static class ExportsDirective extends Directive
8040516Swpaul            implements ModuleElement.ExportsDirective {
8140516Swpaul        public final PackageSymbol packge;
8240516Swpaul        public final List<ModuleSymbol> modules;
8340516Swpaul
8440516Swpaul        public ExportsDirective(PackageSymbol packge, List<ModuleSymbol> modules) {
8540516Swpaul            this.packge = packge;
8640516Swpaul            this.modules = modules;
87108729Sjake        }
8840516Swpaul
8940516Swpaul        @Override @DefinedBy(Api.LANGUAGE_MODEL)
9040516Swpaul        public ModuleElement.DirectiveKind getKind() {
9140516Swpaul            return ModuleElement.DirectiveKind.EXPORTS;
9240516Swpaul        }
9340516Swpaul
9440516Swpaul        @Override @DefinedBy(Api.LANGUAGE_MODEL)
9540516Swpaul        public PackageElement getPackage() {
9640516Swpaul            return packge;
9740516Swpaul        }
9840516Swpaul
9940516Swpaul        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10040516Swpaul        public java.util.List<? extends ModuleElement> getTargetModules() {
10140516Swpaul            return Collections.unmodifiableList(modules);
10240516Swpaul        }
10341569Swpaul
10441569Swpaul        @Override
10541569Swpaul        public String toString() {
10650703Swpaul            if (modules == null)
10750703Swpaul                return "Exports[" + packge + "]";
10850703Swpaul            else
10940516Swpaul                return "Exports[" + packge + ":" + modules + "]";
11050703Swpaul        }
11150703Swpaul    }
11250703Swpaul
113119871Swpaul    /**
114119871Swpaul     * 'provides' ServiceName 'with' QualifiedIdentifer ';'
11540516Swpaul     */
116113506Smdodd    public static class ProvidesDirective extends Directive
117113506Smdodd            implements ModuleElement.ProvidesDirective {
11859758Speter        public final ClassSymbol service;
11959758Speter        public final ClassSymbol impl;
12051089Speter
12150703Swpaul        public ProvidesDirective(ClassSymbol service, ClassSymbol impl) {
12250703Swpaul            this.service = service;
12340516Swpaul            this.impl = impl;
12440516Swpaul        }
12540516Swpaul
12640516Swpaul        @Override @DefinedBy(Api.LANGUAGE_MODEL)
12740516Swpaul        public ModuleElement.DirectiveKind getKind() {
12840516Swpaul            return ModuleElement.DirectiveKind.PROVIDES;
12940516Swpaul        }
13040516Swpaul
13140516Swpaul        @Override @DefinedBy(Api.LANGUAGE_MODEL)
13240516Swpaul        public TypeElement getService() {
13340516Swpaul            return service;
13440516Swpaul        }
13540516Swpaul
13640516Swpaul        @Override @DefinedBy(Api.LANGUAGE_MODEL)
13740516Swpaul        public TypeElement getImplementation() {
13840516Swpaul            return impl;
139117388Swpaul        }
14040516Swpaul
141117388Swpaul        @Override
14240516Swpaul        public String toString() {
143117388Swpaul            return "Provides[" + service + "," + impl + "]";
14467771Swpaul        }
145118978Swpaul
146118978Swpaul        @Override
147117388Swpaul        public boolean equals(Object obj) {
14841243Swpaul            if (!(obj instanceof ProvidesDirective)) {
149117388Swpaul                return false;
15044238Swpaul            }
151117388Swpaul            ProvidesDirective other = (ProvidesDirective)obj;
15244238Swpaul            return service == other.service && impl == other.impl;
153117388Swpaul        }
15472813Swpaul
155117388Swpaul        @Override
15696112Sjhb        public int hashCode() {
157117388Swpaul            return service.hashCode() * 31 + impl.hashCode() * 37;
15894400Swpaul        }
159117388Swpaul    }
160103020Siwasaki
161117388Swpaul    /**
162109095Ssanpei     * 'requires' ['public'] ModuleName ';'
163117388Swpaul     */
164111381Sdan    public static class RequiresDirective extends Directive
165117388Swpaul            implements ModuleElement.RequiresDirective {
166112379Ssanpei        public final ModuleSymbol module;
167117388Swpaul        public final Set<RequiresFlag> flags;
168117388Swpaul
169117388Swpaul        public RequiresDirective(ModuleSymbol module) {
170117388Swpaul            this(module, EnumSet.noneOf(RequiresFlag.class));
171117388Swpaul        }
172117388Swpaul
173123740Speter        public RequiresDirective(ModuleSymbol module, Set<RequiresFlag> flags) {
17440516Swpaul            this.module = module;
17540516Swpaul            this.flags = flags;
17692739Salfred        }
17792739Salfred
17892739Salfred        @Override @DefinedBy(Api.LANGUAGE_MODEL)
17940516Swpaul        public ModuleElement.DirectiveKind getKind() {
180119868Swpaul            return ModuleElement.DirectiveKind.REQUIRES;
18140516Swpaul        }
18292739Salfred
18392739Salfred        @Override @DefinedBy(Api.LANGUAGE_MODEL)
18492739Salfred        public boolean isPublic() {
18592739Salfred            return flags.contains(RequiresFlag.PUBLIC);
18692739Salfred        }
18792739Salfred
18892739Salfred        @Override @DefinedBy(Api.LANGUAGE_MODEL)
18992739Salfred        public ModuleElement getDependency() {
19092739Salfred            return module;
19192739Salfred        }
19292739Salfred
19392739Salfred        @Override
19492739Salfred        public String toString() {
19592739Salfred            return "Requires[" + flags + "," + module + "]";
19640516Swpaul        }
19792739Salfred    }
19892739Salfred
19992739Salfred    /**
20092739Salfred     * 'uses' ServiceName ';'
20192739Salfred     */
20292739Salfred    public static class UsesDirective extends Directive
20392739Salfred            implements ModuleElement.UsesDirective {
20440516Swpaul        public final ClassSymbol service;
20592739Salfred
20692739Salfred        public UsesDirective(ClassSymbol service) {
20792739Salfred            this.service = service;
20840516Swpaul        }
209123289Sobrien
21092739Salfred        @Override @DefinedBy(Api.LANGUAGE_MODEL)
21192739Salfred        public ModuleElement.DirectiveKind getKind() {
21292739Salfred            return ModuleElement.DirectiveKind.USES;
21340516Swpaul        }
21492739Salfred
21592739Salfred        @Override @DefinedBy(Api.LANGUAGE_MODEL)
21681713Swpaul        public TypeElement getService() {
21750703Swpaul            return service;
21850703Swpaul        }
21950703Swpaul
22050703Swpaul        @Override
22150703Swpaul        public String toString() {
22250703Swpaul            return "Uses[" + service + "]";
22350703Swpaul        }
22450703Swpaul
22550703Swpaul        @Override
22650703Swpaul        public boolean equals(Object obj) {
22750703Swpaul            if (!(obj instanceof UsesDirective)) {
22850703Swpaul                return false;
22950703Swpaul            }
23086822Siwasaki            UsesDirective other = (UsesDirective)obj;
23186822Siwasaki            return service == other.service;
23250703Swpaul        }
23350703Swpaul
23450703Swpaul        @Override
23550703Swpaul        public int hashCode() {
23650703Swpaul            return service.hashCode() * 31;
23750703Swpaul        }
23850703Swpaul    }
23950703Swpaul}
24050703Swpaul