8175118: Revisit modeling of module directives
Reviewed-by: darcy, jlahoda
This commit is contained in:
parent
54adf4b246
commit
e1d9daf27f
@ -134,6 +134,128 @@ public interface ModuleElement extends Element, QualifiedNameable {
|
||||
* @return the kind of this directive
|
||||
*/
|
||||
DirectiveKind getKind();
|
||||
|
||||
/**
|
||||
* Applies a visitor to this directive.
|
||||
*
|
||||
* @param <R> the return type of the visitor's methods
|
||||
* @param <P> the type of the additional parameter to the visitor's methods
|
||||
* @param v the visitor operating on this directive
|
||||
* @param p additional parameter to the visitor
|
||||
* @return a visitor-specified result
|
||||
*/
|
||||
<R, P> R accept(DirectiveVisitor<R, P> v, P p);
|
||||
}
|
||||
|
||||
/**
|
||||
* A visitor of module directives, in the style of the visitor design
|
||||
* pattern. Classes implementing this interface are used to operate
|
||||
* on a directive when the kind of directive is unknown at compile time.
|
||||
* When a visitor is passed to a directive's {@link Directive#accept
|
||||
* accept} method, the <tt>visit<i>Xyz</i></tt> method applicable
|
||||
* to that directive is invoked.
|
||||
*
|
||||
* <p> Classes implementing this interface may or may not throw a
|
||||
* {@code NullPointerException} if the additional parameter {@code p}
|
||||
* is {@code null}; see documentation of the implementing class for
|
||||
* details.
|
||||
*
|
||||
* <p> <b>WARNING:</b> It is possible that methods will be added to
|
||||
* this interface to accommodate new, currently unknown, language
|
||||
* structures added to future versions of the Java™ programming
|
||||
* language. Methods to accommodate new language constructs will
|
||||
* be added in a source <em>compatible</em> way using
|
||||
* <em>default methods</em>.
|
||||
*
|
||||
* @param <R> the return type of this visitor's methods. Use {@link
|
||||
* Void} for visitors that do not need to return results.
|
||||
* @param <P> the type of the additional parameter to this visitor's
|
||||
* methods. Use {@code Void} for visitors that do not need an
|
||||
* additional parameter.
|
||||
*
|
||||
* @since 9
|
||||
* @spec JPMS
|
||||
*/
|
||||
interface DirectiveVisitor<R, P> {
|
||||
/**
|
||||
* Visits any directive as if by passing itself to that
|
||||
* directive's {@link Directive#accept accept} method and passing
|
||||
* {@code null} for the additional parameter.
|
||||
* The invocation {@code v.visit(d)} is equivalent to
|
||||
* {@code d.accept(v, null)}.
|
||||
* @param d the directive to visit
|
||||
* @return a visitor-specified result
|
||||
* @implSpec This implementation is {@code visit(d, null)}
|
||||
*/
|
||||
default R visit(Directive d) {
|
||||
return d.accept(this, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits any directive as if by passing itself to that
|
||||
* directive's {@link Directive#accept accept} method.
|
||||
* The invocation {@code v.visit(d, p)} is equivalent to
|
||||
* {@code d.accept(v, p)}.
|
||||
* @param d the directive to visit
|
||||
* @param p a visitor-specified parameter
|
||||
* @return a visitor-specified result
|
||||
*/
|
||||
default R visit(Directive d, P p) {
|
||||
return d.accept(this, p);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits a {@code requires} directive.
|
||||
* @param d the directive to visit
|
||||
* @param p a visitor-specified parameter
|
||||
* @return a visitor-specified result
|
||||
*/
|
||||
R visitRequires(RequiresDirective d, P p);
|
||||
|
||||
/**
|
||||
* Visits an {@code exports} directive.
|
||||
* @param d the directive to visit
|
||||
* @param p a visitor-specified parameter
|
||||
* @return a visitor-specified result
|
||||
*/
|
||||
R visitExports(ExportsDirective d, P p);
|
||||
|
||||
/**
|
||||
* Visits an {@code opens} directive.
|
||||
* @param d the directive to visit
|
||||
* @param p a visitor-specified parameter
|
||||
* @return a visitor-specified result
|
||||
*/
|
||||
R visitOpens(OpensDirective d, P p);
|
||||
|
||||
/**
|
||||
* Visits a {@code uses} directive.
|
||||
* @param d the directive to visit
|
||||
* @param p a visitor-specified parameter
|
||||
* @return a visitor-specified result
|
||||
*/
|
||||
R visitUses(UsesDirective d, P p);
|
||||
|
||||
/**
|
||||
* Visits a {@code provides} directive.
|
||||
* @param d the directive to visit
|
||||
* @param p a visitor-specified parameter
|
||||
* @return a visitor-specified result
|
||||
*/
|
||||
R visitProvides(ProvidesDirective d, P p);
|
||||
|
||||
/**
|
||||
* Visits an unknown directive.
|
||||
* This can occur if the language evolves and new kinds of directive are added.
|
||||
* @param d the directive to visit
|
||||
* @param p a visitor-specified parameter
|
||||
* @return a visitor-specified result
|
||||
* @throws UnknownDirectiveException a visitor implementation may optionally throw this exception
|
||||
* @implSpec This implementation throws {@code UnknownDirectiveException}.
|
||||
*/
|
||||
default R visitUnknown(Directive d, P p) {
|
||||
throw new UnknownDirectiveException(d, p);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2017, 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. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package javax.lang.model.element;
|
||||
|
||||
import javax.lang.model.UnknownEntityException;
|
||||
|
||||
/**
|
||||
* Indicates that an unknown kind of module directive was encountered.
|
||||
* This can occur if the language evolves and new kinds of directives are
|
||||
* added to the {@code Directive} hierarchy. May be thrown by a
|
||||
* {@linkplain ModuleElement.DirectiveVisitor directive visitor} to
|
||||
* indicate that the visitor was created for a prior version of the language.
|
||||
*
|
||||
* @author Joseph D. Darcy
|
||||
* @author Scott Seligman
|
||||
* @author Peter von der Ahé
|
||||
* @see ModuleElement.DirectiveVisitor#visitUnknown
|
||||
* @since 9
|
||||
* @spec JPMS
|
||||
*/
|
||||
public class UnknownDirectiveException extends UnknownEntityException {
|
||||
|
||||
private static final long serialVersionUID = 269L;
|
||||
|
||||
private final transient ModuleElement.Directive directive;
|
||||
private final transient Object parameter;
|
||||
|
||||
/**
|
||||
* Creates a new {@code UnknownElementException}. The {@code p}
|
||||
* parameter may be used to pass in an additional argument with
|
||||
* information about the context in which the unknown directive was
|
||||
* encountered; for example, the visit methods of {@link
|
||||
* ModuleElement.DirectiveVisitor DirectiveVisitor} may pass in
|
||||
* their additional parameter.
|
||||
*
|
||||
* @param d the unknown directive, may be {@code null}
|
||||
* @param p an additional parameter, may be {@code null}
|
||||
*/
|
||||
public UnknownDirectiveException(ModuleElement.Directive d, Object p) {
|
||||
super("Unknown directive: " + d);
|
||||
directive = d;
|
||||
parameter = p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unknown directive.
|
||||
* The value may be unavailable if this exception has been
|
||||
* serialized and then read back in.
|
||||
*
|
||||
* @return the unknown directive, or {@code null} if unavailable
|
||||
*/
|
||||
public ModuleElement.Directive getUnknownDirective() {
|
||||
return directive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the additional argument.
|
||||
*
|
||||
* @return the additional argument, or {@code null} if unavailable
|
||||
*/
|
||||
public Object getArgument() {
|
||||
return parameter;
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2009, 2017, 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
|
||||
@ -30,8 +30,7 @@ import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.lang.model.element.ModuleElement;
|
||||
import javax.lang.model.element.PackageElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.ModuleElement.DirectiveVisitor;
|
||||
|
||||
import com.sun.tools.javac.code.Symbol.ClassSymbol;
|
||||
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
|
||||
@ -138,6 +137,11 @@ public abstract class Directive implements ModuleElement.Directive {
|
||||
else
|
||||
return "Exports[" + packge + ":" + modules + "]";
|
||||
}
|
||||
|
||||
@Override @DefinedBy(Api.LANGUAGE_MODEL)
|
||||
public <R, P> R accept(DirectiveVisitor<R, P> v, P p) {
|
||||
return v.visitExports(this, p);
|
||||
}
|
||||
}
|
||||
|
||||
/** Flags for OpensDirective. */
|
||||
@ -204,6 +208,11 @@ public abstract class Directive implements ModuleElement.Directive {
|
||||
else
|
||||
return "Opens[" + packge + ":" + modules + "]";
|
||||
}
|
||||
|
||||
@Override @DefinedBy(Api.LANGUAGE_MODEL)
|
||||
public <R, P> R accept(DirectiveVisitor<R, P> v, P p) {
|
||||
return v.visitOpens(this, p);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -239,6 +248,11 @@ public abstract class Directive implements ModuleElement.Directive {
|
||||
return "Provides[" + service + "," + impls + "]";
|
||||
}
|
||||
|
||||
@Override @DefinedBy(Api.LANGUAGE_MODEL)
|
||||
public <R, P> R accept(DirectiveVisitor<R, P> v, P p) {
|
||||
return v.visitProvides(this, p);
|
||||
}
|
||||
|
||||
// TODO: delete?
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
@ -297,6 +311,11 @@ public abstract class Directive implements ModuleElement.Directive {
|
||||
public String toString() {
|
||||
return "Requires[" + flags + "," + module + "]";
|
||||
}
|
||||
|
||||
@Override @DefinedBy(Api.LANGUAGE_MODEL)
|
||||
public <R, P> R accept(DirectiveVisitor<R, P> v, P p) {
|
||||
return v.visitRequires(this, p);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -325,6 +344,11 @@ public abstract class Directive implements ModuleElement.Directive {
|
||||
return "Uses[" + service + "]";
|
||||
}
|
||||
|
||||
@Override @DefinedBy(Api.LANGUAGE_MODEL)
|
||||
public <R, P> R accept(DirectiveVisitor<R, P> v, P p) {
|
||||
return v.visitUses(this, p);
|
||||
}
|
||||
|
||||
// TODO: delete?
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
146
langtools/test/tools/javac/modules/DirectiveVisitorTest.java
Normal file
146
langtools/test/tools/javac/modules/DirectiveVisitorTest.java
Normal file
@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Copyright (c) 2017, 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.
|
||||
*/
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Path;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.lang.model.element.ModuleElement;
|
||||
import javax.lang.model.element.ModuleElement.Directive;
|
||||
import javax.lang.model.element.ModuleElement.DirectiveKind;
|
||||
import javax.lang.model.element.ModuleElement.DirectiveVisitor;
|
||||
import javax.lang.model.element.ModuleElement.ExportsDirective;
|
||||
import javax.lang.model.element.ModuleElement.OpensDirective;
|
||||
import javax.lang.model.element.ModuleElement.ProvidesDirective;
|
||||
import javax.lang.model.element.ModuleElement.RequiresDirective;
|
||||
import javax.lang.model.element.ModuleElement.UsesDirective;
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.JavaFileObject;
|
||||
import javax.tools.StandardJavaFileManager;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
import com.sun.source.util.JavacTask;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8175118
|
||||
* @summary Add ModuleElement.DirectiveVisitor
|
||||
* @library /tools/lib
|
||||
* @modules jdk.compiler/com.sun.tools.javac.api
|
||||
* jdk.compiler/com.sun.tools.javac.main
|
||||
* jdk.compiler/com.sun.tools.javac.util
|
||||
* @build toolbox.ToolBox ModuleTestBase
|
||||
* @run main DirectiveVisitorTest
|
||||
*/
|
||||
|
||||
public class DirectiveVisitorTest extends ModuleTestBase {
|
||||
public static void main(String... args) throws Exception {
|
||||
new DirectiveVisitorTest().runTests();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVisitor(Path base) throws Exception {
|
||||
Path src = base.resolve("src");
|
||||
Path src_m1 = src.resolve("m1x");
|
||||
tb.writeJavaFiles(src_m1,
|
||||
"module m1x { "
|
||||
+ " requires m2x;"
|
||||
+ " exports p1;"
|
||||
+ " opens p2;"
|
||||
+ " uses p1.Service;"
|
||||
+ " provides p1.Service with p2.Impl;"
|
||||
+ "}",
|
||||
"package p1; public interface Service { }",
|
||||
"package p2; public class Impl implements p1.Service { }");
|
||||
Path src_m2 = src.resolve("m2x");
|
||||
tb.writeJavaFiles(src_m2,
|
||||
"module m2x { }");
|
||||
|
||||
Path modules = base.resolve("modules");
|
||||
tb.createDirectories(modules);
|
||||
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
|
||||
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(src));
|
||||
List<String> options = List.of(
|
||||
"--module-source-path", src.toString(),
|
||||
"-d", modules.toString()
|
||||
);
|
||||
JavacTask t = (JavacTask) javac.getTask(null, fm, null, options, null, files);
|
||||
t.analyze();
|
||||
ModuleElement e = t.getElements().getModuleElement("m1x");
|
||||
Set<DirectiveKind> kinds = EnumSet.<DirectiveKind>allOf(DirectiveKind.class);
|
||||
Visitor v = new Visitor();
|
||||
v.visit(e, kinds);
|
||||
if (!kinds.equals(EnumSet.<DirectiveKind>noneOf(DirectiveKind.class))) {
|
||||
error("Some kinds not found: " + kinds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class Visitor implements DirectiveVisitor<Void,Set<DirectiveKind>> {
|
||||
private final PrintStream out = System.err;
|
||||
|
||||
public void visit(ModuleElement e, Set<DirectiveKind> kinds) {
|
||||
e.getDirectives().stream().forEach(d -> visit(d, kinds));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitRequires(RequiresDirective d, Set<DirectiveKind> kinds) {
|
||||
visitAny(d, kinds);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitExports(ExportsDirective d, Set<DirectiveKind> kinds) {
|
||||
visitAny(d, kinds);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitOpens(OpensDirective d, Set<DirectiveKind> kinds) {
|
||||
visitAny(d, kinds);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitUses(UsesDirective d, Set<DirectiveKind> kinds) {
|
||||
visitAny(d, kinds);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitProvides(ProvidesDirective d, Set<DirectiveKind> kinds) {
|
||||
visitAny(d, kinds);
|
||||
return null;
|
||||
}
|
||||
|
||||
private void visitAny(Directive d, Set<DirectiveKind> kinds) {
|
||||
out.println("visit: " + d);
|
||||
kinds.remove(d.getKind());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user